diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Components/FieldDefinition.php | 33 | ||||
-rw-r--r-- | src/Components/OptionsArray.php | 46 | ||||
-rw-r--r-- | src/Utils/Table.php | 2 |
3 files changed, 54 insertions, 27 deletions
diff --git a/src/Components/FieldDefinition.php b/src/Components/FieldDefinition.php index 8c568a1..717cc4f 100644 --- a/src/Components/FieldDefinition.php +++ b/src/Components/FieldDefinition.php @@ -36,6 +36,11 @@ class FieldDefinition extends Component * @var array */ public static $FIELD_OPTIONS = array( + + // Tells the `OptionsArray` to not sort the options. + // See the note below. + '_UNSORTED' => true, + 'NOT NULL' => 1, 'NULL' => 1, 'DEFAULT' => array(2, 'var'), @@ -48,14 +53,26 @@ class FieldDefinition extends Component 'COLUMN_FORMAT' => array(6, 'var'), 'ON UPDATE' => array(7, 'var'), - // generated columns options. - 'GENERATED ALWAYS' => 1, - 'AS' => array(2, 'expr', array('bracketsDelimited' => true)), - 'VIRTUAL' => 3, - 'PERSISTENT' => 3, - 'STORED' => 3, - // 'UNIQUE' => 4, // common - // 'UNIQUE KEY' => 4, // common + // Generated columns options. + 'GENERATED ALWAYS' => 8, + 'AS' => array(9, 'expr', array('bracketsDelimited' => true)), + 'VIRTUAL' => 10, + 'PERSISTENT' => 11, + 'STORED' => 11, + // Common entries. + // + // NOTE: Some of the common options are not in the same order which + // causes troubles when checking if the options are in the right order. + // I should find a way to define multiple sets of options and make the + // parser select the right set. + // + // 'UNIQUE' => 4, + // 'UNIQUE KEY' => 4, + // 'COMMENT' => array(5, 'var'), + // 'NOT NULL' => 1, + // 'NULL' => 1, + // 'PRIMARY' => 4, + // 'PRIMARY KEY' => 4, ); /** diff --git a/src/Components/OptionsArray.php b/src/Components/OptionsArray.php index aabb807..513355d 100644 --- a/src/Components/OptionsArray.php +++ b/src/Components/OptionsArray.php @@ -36,7 +36,8 @@ class OptionsArray extends Component * Constructor. * * @param array $options The array of options. Options that have a value - * must be an array with two keys 'name' and 'value'. + * must be an array with at least two keys `name` and + * `expr` or `value`. */ public function __construct(array $options = array()) { @@ -162,11 +163,11 @@ class OptionsArray extends Component 'name' => $token->value, // @var bool Whether it contains an equal sign. // This is used by the builder to rebuild it. - 'equal' => $lastOption[1] === 'var=', + 'equals' => $lastOption[1] === 'var=', // @var string Raw value. - 'value' => '', + 'expr' => '', // @var string Processed value. - 'value_' => '', + 'value' => '', ); $state = 1; } elseif ($lastOption[1] === 'expr') { @@ -179,14 +180,14 @@ class OptionsArray extends Component // @var string The name of the option. 'name' => $token->value, // @var Expression The parsed expression. - 'value' => null, + 'expr' => null, ); $state = 1; } } elseif ($state === 1) { $state = 2; if ($token->value === '=') { - $ret->options[$lastOptionId]['equal'] = true; + $ret->options[$lastOptionId]['equals'] = true; continue; } } @@ -195,11 +196,13 @@ class OptionsArray extends Component // change this iteration. if ($state === 2) { if ($lastOption[1] === 'expr') { - $ret->options[$lastOptionId]['value'] = Expression::parse( + $ret->options[$lastOptionId]['expr'] = Expression::parse( $parser, $list, empty($lastOption[2]) ? array() : $lastOption[2] ); + $ret->options[$lastOptionId]['value'] = + $ret->options[$lastOptionId]['expr']->expr; $lastOption = null; $state = 0; } else { @@ -209,11 +212,14 @@ class OptionsArray extends Component --$brackets; } - // Raw value. - $ret->options[$lastOptionId]['value'] .= $token->token; + $ret->options[$lastOptionId]['expr'] .= $token->token; - // Processed value. - $ret->options[$lastOptionId]['value_'] .= $token->value; + if (!((($token->token === '(') && ($brackets === 1)) + || (($token->token === ')') && ($brackets === 0))) + ) { + // First pair of brackets is being skipped. + $ret->options[$lastOptionId]['value'] .= $token->value; + } // Checking if we finished parsing. if ($brackets === 0) { @@ -223,7 +229,9 @@ class OptionsArray extends Component } } - ksort($ret->options); + if (empty($options['_UNSORTED'])) { + ksort($ret->options); + } --$list->idx; return $ret; @@ -245,9 +253,9 @@ class OptionsArray extends Component $options[] = $option; } else { $options[] = $option['name'] - . (!empty($option['equal']) ? '=' : ' ') - . ((string) $option['value']); - // If `$option['value']` happens to be a component, the magic + . (!empty($option['equals']) ? '=' : ' ') + . (!empty($option['expr']) ? ((string) $option['expr']) : $option['value']); + // If `$option['expr']` happens to be a component, the magic // method will build it automatically. } } @@ -257,17 +265,19 @@ class OptionsArray extends Component /** * Checks if it has the specified option and returns it value or true. * - * @param string $key The key to be checked. + * @param string $key The key to be checked. + * @param string $getExpr Gets the expression instead of the value. + * The value is the processed form of the expression. * * @return mixed */ - public function has($key) + public function has($key, $getExpr = false) { foreach ($this->options as $option) { if ($key === $option) { return true; } elseif ((is_array($option)) && ($key === $option['name'])) { - return $option['value']; + return $getExpr ? $option['expr'] : $option['value']; } } return false; diff --git a/src/Utils/Table.php b/src/Utils/Table.php index 6b17457..c5ce2fe 100644 --- a/src/Utils/Table.php +++ b/src/Utils/Table.php @@ -125,7 +125,7 @@ class Table if (($option = $field->options->has('AS'))) { $ret[$field->name]['generated'] = true; - $ret[$field->name]['expr'] = $option->expr; + $ret[$field->name]['expr'] = $option; } } |