Example #1
0
 function syncdb()
 {
     # note: same as build without write
     # - separate build to build+build-write ?
     $b = new SalamaBuild();
     $classes = $b->getModelnames();
     foreach ($classes as $c) {
         $b->genTable($c);
     }
     $b->processConstraints();
     $b->setTableNames();
     $r = $b->createTable();
     $sql = implode("\n", $r);
     # databases in use
     # @TODO cleanup. duplicate code from SalamaSuite.php
     $xml = simplexml_load_string(file_get_contents(Salama::$_settings['config'] . '/databases.xml'));
     $env = Salama::$_settings['env'];
     $res = $xml->xpath("//configuration[@environment='{$env}']/database");
     $dbInUse = array();
     foreach ($res as $row) {
         $dsn = trim($row->dsn);
         $parts = explode('/', $dsn);
         $db = array_pop($parts);
         $dbInUse[] = $db;
     }
     # [/END-NON-DRY]
     Model::raw($sql)->goraw();
 }
Example #2
0
 public static function getMap($current_class, $target_table, $relation = null, $join = false)
 {
     if (isset(SalamaData::$c[$relation])) {
         # TABLE: (possible) belongsTo (foreignKey) relation via Table; FK JOIN
         $target_table = $current_class;
         $map = self::$belongsTo;
         if (SalamaBuild::getRelation($relation, $target_table, 'ref')) {
             list($fk, $pk) = SalamaBuild::getRelation($relation, $target_table, 'both');
         }
         # inverse belongsTo
         if (empty($fk)) {
             $map = self::$hasOne;
             list($pk, $fk) = SalamaBuild::getRelation($target_table, $relation, 'both');
         }
         # check for hasMany under JOIN conditions
         if ($join) {
             if (isset(SalamaData::$rel[self::$hasMany][$target_table][$relation])) {
                 $relation = SalamaData::$rel[self::$hasMany][$target_table][$relation][0];
                 $map = self::$hasMany;
             }
         }
     } else {
         # hasOne/hasMany relation (fieldname)
         if (isset(SalamaData::$c[$target_table][$relation])) {
             $field = SalamaData::$c[$target_table][$relation];
             # Table.column_name (key=>val settings)
             if (isset($field[self::$belongsTo])) {
                 $map = self::$belongsTo;
             } elseif (isset($field[self::$hasOne])) {
                 $map = self::$hasOne;
             } elseif (isset($field[self::$hasMany])) {
                 $map = self::$hasMany;
             }
             if (isset($field['foreignkey'])) {
                 $fk = $field['foreignkey'];
             } elseif (isset($field[self::$through])) {
                 # through (m:m)?
                 list($pk, $fk) = SalamaBuild::getRelation($field[self::$through], $target_table, 'both');
             } else {
                 list($pk, $fk) = SalamaBuild::getRelation($current_class, $target_table, 'both');
             }
         } else {
             # FK taken from Table we are obtaining data from; _rel[reference][target]
             list($pk, $fk) = SalamaBuild::getRelation($current_class, $target_table, 'both');
         }
     }
     return array('map' => isset($map) ? $map : null, 'foreignkey' => isset($fk) ? $fk : null, 'localkey' => isset($pk) ? $pk : null);
 }
Example #3
0
 function setupJoin($args, $query)
 {
     if (count($args) == 1) {
         $from = $query->model;
     } else {
         $from = $args[1];
     }
     $from_alias = $this->builder->getAlias($from, $query);
     $type = 'LEFT';
     foreach ($args as $key => $model) {
         $model_alias = $this->builder->getAlias($model, $query);
         $map = SalamaRelation::getMap($model, $from);
         $model_fk = $map['foreignkey'];
         $from_pk = SalamaBuild::getPk($from);
         $result[] = "{$type} JOIN {$model} {$model_alias} ON {$from_alias}.{$from_pk}={$model_alias}.{$model_fk}";
         $query->_involved_tables[] = array('table' => $model, 'alias' => $model_alias);
     }
     return $result;
 }
Example #4
0
 public function save($mode = 'insert')
 {
     $query = $this->getQuery();
     $query->_query_type = $mode;
     $pk = SalamaBuild::getPk($query->model);
     if ($mode == SalamaQuery::$INSERT) {
         $rows = $this->_row;
         foreach ($rows as $row) {
             $query = $row->getQuery();
             $query->_query_type = $mode;
             $this->call($query);
         }
         // initial insert
         if (empty($rows)) {
             $this->call($query);
         }
     } else {
         $this->call($query);
     }
     // @TODO boolean of all queries
     return $query->_code;
 }
Example #5
0
 public function from($args, $query)
 {
     $this->sql = $this->builder->getRealTableName($query->model) . " AS " . SalamaBuild::getAlias($query->model);
 }
Example #6
0
 public function warpspeed($query, $instance)
 {
     $table = $query->model;
     $start = microtime(true);
     $pk = SalamaBuild::getPk($query->model);
     if ($query->_query_type != SalamaQuery::$DELETE) {
         if ($query->getItem($pk)) {
             $query->_query_type = SalamaQuery::$UPDATE;
         }
     }
     $query->builder->prepare($query, $instance);
     $items = $query->database->execute($query, $instance);
     $query->clearDirty();
     $query->executed = true;
     if ($query->_query_type == SalamaQuery::$INSERT && $query->model) {
         $pk_name = SalamaBuild::getPk($query->model);
         $query->setItem($pk_name, $query->_last_insert_id, false);
     }
     if (count($query->_involved_tables) == 1) {
         # single table query
         // SELECT
         if ($items) {
             $res = $query->hydrateResult($items, $query->builder->getAlias($table, $query), false);
             $items = $res[$table];
         }
         $count = count($items);
         for ($i = 0; $i < $count; $i++) {
             $model = $query->model;
             $instance->_row[$i] = clone $instance;
             $instance->_row[$i]->_row = array();
             $instance->_row[$i]->_query = array();
             if ($i == 0) {
                 $instance->_row[$i]->_query = $query;
             } else {
                 $instance->_row[$i]->_query = clone $query;
             }
             $instance->_row[$i]->_query->setItems($items[$i]);
         }
     } elseif ($query->_query_type == SalamaQuery::$INSERT || isset($data['raw']) && !$items) {
         $query->setItems($items);
     } elseif (isset($data['raw'])) {
         $last_query = $query->builder->sql;
         foreach (SalamaData::$c as $model_ => $data_) {
             $table = $query->builder->getRealTableName($model_);
             if (strpos($last_query, $table)) {
                 $query->_involved_tables[] = $table;
             }
         }
         $query->setItems($items);
     } else {
         // JOIN: setup instances for every table with their respective data
         foreach ($query->_involved_tables as $field => $tbl) {
             $res = $query->hydrateResult($items, $tbl['alias'], true);
             if ($tbl['table'] == $query->getTable()) {
                 $pk = SalamaBuild::getPk($tbl['table']);
                 $unique = array_intersect_key($res, array_unique($res));
             }
             $query->setItems($res, true);
         }
     }
     if ($query->_query_type == SalamaQuery::$INSERT) {
         $query->setItem($pk, $query->_last_insert_id, false);
     }
     $job_class = $query->jobs;
     if ($query->_query_type == SalamaQuery::$INSERT) {
         $job = $job_class->getJob(SalamaQuery::$INSERT);
         # update all query params to be available in resultset items
         foreach ($job->params[0] as $k => $v) {
             $query->setItem($k, $v, false);
         }
     }
     if ($query->_query_type == SalamaQuery::$UPDATE) {
         if ($job = $job_class->getJob('set')) {
             # update all query params to be available in resultset items
             foreach ($job->objs as $k => $q) {
                 $query->setItem($q->name, $q->arguments[0], false);
             }
         }
     }
     # housekeeping...
     $query->jobs = null;
     $query->jobs();
     return $query;
 }
Example #7
0
 public function prepare($query)
 {
     $this->getInvolvedTables($query);
     if ($query->_query_type == 'select') {
         if ($this->recursive_array_search('select', $query->methods) === false) {
             $query->methods[] = array('select', array());
         }
         if ($this->recursive_array_search('from', $query->methods) === false) {
             $query->methods[] = array('from', array());
         }
     }
     $job_class = $query->jobs;
     # setup query, executing respective jobs for each valid method
     foreach ($query->methods as $k => $array) {
         list($method, $args) = $array;
         if ($this->isValidMethod($method)) {
             if ($job = $job_class->getJob($method)) {
             } else {
                 $job = $job_class->createJob($method, $query);
             }
             $job->{$method}($args, $query);
         }
     }
     $jobs = get_class_methods(get_class($job_class));
     $target_table = null;
     switch ($query->_query_type) {
         case 'select':
             $qs = array();
             foreach (self::$sql_select as $name => $data) {
                 if ($job = $job_class->getJob($name)) {
                     $sql_funk = sprintf('%sSql', $job->name);
                     if (in_array($sql_funk, $jobs)) {
                         $sql = $job->{$sql_funk}($job, $query);
                     } else {
                         $sql = $job->sql ?: self::$sql_select[$job->name]['default'];
                     }
                     $qs[] = sprintf(self::$sql_select[$job->name]['name'], $sql);
                 }
             }
             $qs = implode(' ', $qs);
             break;
         case 'insert':
             $job = $job_class->createJob('insert', $query);
             $pass_items = array();
             foreach ($query->getItems() as $i => $j) {
                 if ($i == SalamaBuild::getPk($query->model)) {
                     continue;
                 }
                 $pass_items[$i] = $j;
             }
             $job->insert(array($pass_items), $query);
             // QUERYSTRING: all items
             $cols = array();
             $items = $query->getItems();
             foreach ($items as $k => $v) {
                 if ($k != SalamaBuild::getPk($query->model)) {
                     $cols[$k] = $v;
                 }
             }
             list($fieldString, $in) = array(implode(', ', array_keys($cols)), substr(str_repeat('?, ', count($cols)), 0, -2));
             $qs = sprintf("INSERT INTO %s (%s) VALUES (%s)", $this->getRealTableName($query->model), $fieldString, $in);
             break;
         case 'update':
             /* instead of a single update() we do a series of set()'s */
             if ($job_set = $job_class->getJob('set')) {
             } else {
                 $job_set = $job_class->createJob('set', $query);
             }
             $pkName = SalamaBuild::getPk($query->model);
             foreach ($query->getAllDirty() as $name) {
                 if ($name != $pkName) {
                     $job_set->set(array(q::$name($query->getItem($name))), $query, false);
                 }
             }
             if ($job_where = $job_class->getJob('where')) {
             } else {
                 $job_where = $job_class->createJob('where', $query);
                 $job_where->where(array(q::$pkName($query->getItem($pkName))), $query);
             }
             // QUERYSTRING: set()job + modified values
             $cols = array();
             foreach ($job_set->objs as $k => $q) {
                 $cols[$q->name] = $q->arguments[0];
             }
             foreach ($query->getAllDirty() as $name) {
                 if ($name != $pkName) {
                     $cols[$name] = $query->getItem($name);
                 }
             }
             $fieldString = implode('=?, ', array_keys($cols)) . '=?';
             $target_table = $query->model;
             $tableAlias = $this->getAlias($target_table, $query);
             $qs = sprintf("UPDATE %s SET %s WHERE %s", $this->getRealTableName($target_table), $fieldString, $job_where->sql);
             break;
         case 'delete':
             $pkName = SalamaBuild::getPk($query->model);
             if ($job_where = $job_class->getJob('where')) {
             } else {
                 $job_where = $job_class->createJob('where', $query);
                 $job_where->where(array(q::$pkName($query->getItem($pkName))), $query);
             }
             $qs = sprintf("DELETE FROM %s WHERE %s", $this->getRealTableName($query->model), $job_where->sql);
             break;
     }
     # raw sql override
     if ($job = $job_class->getJob('raw')) {
         $qs = $job->sql;
     }
     $this->sql = $qs;
 }