Esempio n. 1
0
 public function __set($key, $value)
 {
     $exists = isset($this->{$key});
     if ($value === NULL) {
         unset($this[$key]);
     } elseif (is_string($value)) {
         if (!$exists) {
             throw new \Exception("Table '{$key}' does not exists");
         } elseif (strlen(trim($value)) === 0) {
             throw new \Exception("Cannot rename the table '{$key}' to '{$value}'");
         } elseif (isset($this->{$value})) {
             throw new \Exception("Cannot rename the table '{$key}' to '{$value}' (already exists)");
         }
         $this->rename($key, $value);
     } elseif (is_array($value)) {
         if ($exists) {
             throw new \Exception("Table '{$key}' already exists");
         } elseif (sizeof($value) === 0) {
             throw new \Exception("Empty fields for '{$key}'");
         } elseif (!\Grocery\Helpers::is_assoc($value)) {
             throw new \Exception("Invalid fields for '{$key}'");
         }
         $this->create($key, $value);
     } else {
         throw new \Exception("Nothing to do with '{$value}' on '{$key}'");
     }
 }
Esempio n. 2
0
 public static function hydrate($table, array $columns, array $indexes = array())
 {
     $old = $table->columns();
     foreach ($columns as $key => $val) {
         is_array($val) or $val = array($val);
         if (!\Grocery\Helpers::is_assoc($val)) {
             @(list($type, $length, $default, $not_null) = $val);
             $val = compact('type', 'length', 'default', 'not_null');
         }
         if (isset($old[$key])) {
             $type = isset($val['type']) ? $val['type'] : $old[$key]['type'];
             $length = isset($val['length']) ? $val['length'] : $old[$key]['length'];
             $default = isset($val['default']) ? $val['default'] : $old[$key]['default'];
             $not_null = isset($val['not_null']) ? $val['not_null'] : $old[$key]['not_null'];
             $tmp = $old[$key]['type'];
             $left = isset(static::$mask[$tmp]) ? static::$mask[$tmp] : array_search($tmp, static::$mask);
             $right = isset(static::$mask[$type]) ? static::$mask[$type] : array_search($type, static::$mask);
             if ($left === $right) {
                 continue;
             }
             $tmp = compact('type', 'length', 'default', 'not_null');
             if ($tmp != $old[$key]) {
                 $table[$key] = $tmp;
             }
         } else {
             $table[$key] = $val;
         }
     }
     if (sizeof($old) != sizeof($columns)) {
         foreach (array_keys(array_diff_key($old, $columns)) as $one) {
             unset($table[$one]);
         }
     }
     $tmp = $out = array();
     $idx = $table->indexes();
     foreach ($idx as $name => $set) {
         foreach ($set['column'] as $one) {
             $tmp[$one] = $set['unique'];
         }
     }
     foreach ($indexes as $key => $val) {
         $on = is_numeric($key) ? FALSE : (bool) $val;
         $key = is_numeric($key) ? $val : $key;
         if (isset($tmp[$key])) {
             if ($on !== $tmp[$key]) {
                 $table[$key]->unindex('_');
                 $table[$key]->index('_', $on);
             }
         } else {
             $table[$key]->index('_', $on);
         }
         $out[] = $key;
     }
     foreach (array_diff(array_keys($tmp), $out) as $old) {
         $table[$old]->unindex('_');
     }
 }
Esempio n. 3
0
 public function prepare($sql, array $vars = array())
 {
     if (\Grocery\Helpers::is_assoc($vars)) {
         $sql = strtr($sql, $this->fixate_value($vars, TRUE));
     } else {
         $args = $this->fixate_value($vars, TRUE);
         $sql = preg_replace_callback('/((?<!\\\\)\\?)/', function () use($args) {
             return array_shift($args);
         }, $sql);
     }
     return $sql;
 }
Esempio n. 4
0
 protected function build_where($test, $operator = 'AND', $supertable = FALSE)
 {
     $sql = array();
     $operator = strtoupper($operator);
     $sub_prefix = $supertable ? $this->protect_names($supertable) . '.' : '';
     foreach ($test as $key => $val) {
         if (is_numeric($key)) {
             if (!\Grocery\Helpers::is_assoc($val)) {
                 $raw = array_shift($val);
                 if ($val && strpos($raw, '?')) {
                     $sql[] = $this->prepare($raw, $val);
                 } else {
                     array_unshift($val, $raw) && ($sql[] = join("\n", $val));
                 }
             } else {
                 $sql[] = is_array($val) ? $this->build_where($val, $operator, $supertable) : $val;
             }
         } elseif (\Grocery\Helpers::is_keyword($key)) {
             $sql[] = '(' . trim($this->build_where($val, strtoupper($key), $supertable)) . ')';
         } elseif (preg_match('/_(?:and|or)_/i', $key, $match)) {
             $sub = array();
             foreach (explode($match[0], $key) as $one) {
                 $sub[$one] = $val;
             }
             $sql[] = '(' . trim($this->build_where($sub, strtoupper(trim($match[0], '_')), $supertable)) . ')';
         } elseif (preg_match('/^(.+?)(?:\\s+(!=?|[<>]=?|<>|NOT|R?LIKE)\\s*)?$/', $key, $match)) {
             $sub = '';
             $key = $this->protect_names($match[1]);
             if ($val === NULL) {
                 $sub = 'IS NULL';
             } else {
                 $val = $this->fixate_value($val, TRUE);
                 $sub = !empty($match[2]) ? $match[2] == '!' ? '!=' : $match[2] : '=';
             }
             if (is_array($val) && sizeof($val) > 1) {
                 $key .= in_array($sub, array('!=', '<>')) ? ' NOT' : '';
                 $sql[] = " {$sub_prefix}{$key} IN(" . join(', ', $val) . ")";
             } else {
                 $val = is_array($val) ? array_shift($val) : $val;
                 $sql[] = " {$sub_prefix}{$key} {$sub} {$val}";
             }
         }
     }
     return join("\n{$operator}\n", $sql);
 }