function initialize()
 {
     $this->available_migrations = array_reverse(array_walk_copy(array_filter($this->scan_migrations_dir(), function ($file) {
         return ends_with($file, '.sql');
     }), function (&$file, $key) {
         $file = substr($file, 0, -4);
     }));
     $schema_migrations = array();
     $migration = SchemaMigration::find();
     while ($migration->next()) {
         $schema_migrations[] = $migration->version;
     }
     $this->schema_migrations = $schema_migrations;
 }
Example #2
0
 public function save($returning = '', $set_where = false)
 {
     if (array_value($this->query, 'command', 'SELECT') == 'SELECT') {
         if (count(array_value($this->query, 'where', array()))) {
             $this->query['command'] = 'UPDATE';
         } else {
             $this->query['command'] = 'INSERT';
         }
     }
     if (blank($returning)) {
         $returning = array();
     } else {
         $returning = preg_split('/[\\s"]*,[\\s"]*/', trim($returning, " \t\n\r\v\""));
     }
     $returning_special = array();
     if (in_array('*', $returning)) {
         $this->query['returning'] = '*';
     } else {
         $update_special = array_keys(array_value($this->query, 'update_special', array()));
         foreach ($update_special as $name) {
             if (!in_array($name, $returning)) {
                 $returning_special[] = $name;
             }
         }
         $this->query['returning'] = implode(', ', array_walk_copy(array_merge($returning, $returning_special), function (&$value, $key) {
             if (!preg_match('/[\\s\\(\\)]/', $value)) {
                 $value = "\"{$value}\"";
             }
         }));
     }
     $result = $this->execute();
     $this->query['returning'] = '';
     if ($result && (count($returning) || count($returning_special))) {
         $returned = pg_fetch_assoc($this->result);
         if (is_array($returned)) {
             $this->data = array_merge($this->data ?: [], $returned);
             if ($set_where) {
                 foreach ($returned as $field => $value) {
                     if (in_array($field, $returning)) {
                         $this->where("\"{$field}\" = ?", $value);
                     }
                 }
             }
         }
     }
     if (count(array_value($this->query, 'where', array()))) {
         $this->query['command'] = 'SELECT';
     }
     return $result;
 }