mysql - SQL RegEx Create Split: Include SET -


i'm writing regex pattern split mysql create statements column definition arrays. far, works great aside set columns. here's process:

$lines = explode("\n", $create_sql);  foreach ($lines $line) {     $line = str_replace('not null', 'not_null', $line);      $pattern = '/`(.*)`[\s]*([^\s]*)[\s]*(not_null|null)[\s]*(.*),/';      $search = preg_match($pattern, $line, $matches);      if ($search !== false && count($matches) === 5) {         $columns[$matches[1]] = array(             'type' => $matches[2],             'null' => $matches[3] === 'null',             'extra' => $matches[4],         );     } } 

this process works great on column definition this:

`id`          int(11)               not null         auto_increment, 

...but fails on set columns. how can best accommodate quotes , parentheses in line?

`platform`    set('ios', 'android') null             default null, 

to summarize, need integrate pattern match set('one', 'two', n) in string.

first let modify pattern expression, first readability (\s* instead of [\s]*), ensure working lowercase statements :

$pattern = '/`(.*)`\s*([^\s]*)\s*(not_null|null)\s*(.*),/i'; 

then, answer question, prepare pattern depending on set present or not in line:

$sub_pattern = stripos($line, ' set(') ?   'set\([^\)]*\)\s*[^\s]*'    : '[^\s]*'; $pattern = '/`(.*)`\s*(' . $sub_pattern . ')\s*(not_null|null)\s*(.*),/i'; 

note that, however, code not secured against syntax errors in source lines.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -