public function actionDefault() { $confirm = $this->getParam('confirm'); $execute = $confirm ? true : false; PerfORMController::sqlset($execute); $this->template->confirm = $execute; }
public function actionDefault() { $confirm = $this->getParam('confirm'); $execute = $confirm ? true : false; $sql = PerfORMController::sqlclear($execute); $this->template->sql = is_null($sql) ? false : $sql; $this->template->confirm = $execute; }
/** * Getter for datasource * @return DibiDataSource */ public function getDataSource() { if (!$this->dataSource) { # build query $query = array(); $query[] = "\nSELECT"; $this->addFields($this->model); $query[] = implode(",\n", $this->fields); $query[] = sprintf("FROM \"%s\"", $this->model->getTableName()); $this->addJoins($this->model); $query[] = implode("\n", $this->joins); $this->dataSource = new DibiDataSource(implode("\n", $query), PerfORMController::getConnection()); } return $this->dataSource; }
/** * Getter and cleaner for all sql queries in buffer * @return array|boolean */ public static function getSqlBufferAndClear() { $buffer = self::$sqlBuffer; self::$sqlBuffer = array(); return (is_array($buffer) and count($buffer) > 0) ? $buffer : false; }
/** * Save (update) model to database * * Only modified fields will be updated * Triggers NOTICE when no need to update * * @return mixed model's primary key value */ public function update() { $update = array(); foreach ($this->fields as $key => $field) { $finalColumn = $field->getRealName() . '%' . $field->getType(); if ($field->isPrimaryKey()) { $primaryKey = $field->getRealName(); $primaryKeyValue = $field->getDbValue(false); $primaryKeyType = $field->getType(); } elseif (!is_null($dbValue = $field->getDbValue(false)) && $field->isModified()) { $update[$finalColumn] = $dbValue; } elseif ($field->getNullCallback()) { $update[$finalColumn] = $dbValue; } } if (count($update) > 0) { #Debug::barDump($update, 'update array'); PerfORMController::queryAndLog('update %n set', $this->getTableName(), $update, "where %n = %{$primaryKeyType}", $primaryKey, $primaryKeyValue); $this->setUnmodified(); return $primaryKeyValue; } else { trigger_error("The model '" . get_class($this) . "' has no unmodified data to update", E_USER_NOTICE); } }
protected function profiler() { if ($this->profiler) { PerfORMController::addSql(dibi::$sql); $this->profiler = false; } }
/** * Storage processing * @return string */ public function process() { # renamed fields, remove adds and drops foreach ($this->renamedFields as $key => $array) { if ($array->counter == 2 and isset($array->from) and isset($array->to) and isset($array->modelName)) { $field = $this->queue[PerfORMStorage::FIELD_ADD][$array->to . '|' . $array->modelName]->values['field']; unset($this->queue[PerfORMStorage::FIELD_ADD][$array->to . '|' . $array->modelName]); unset($this->queue[PerfORMStorage::FIELD_DROP][$array->from . '|' . $array->modelName]); $this->query('update [fields] set [name] = %s where [name] = %s and [table] = %s', $array->to, $array->from, $array->modelName); $this->query('update [tables] set [hash] = %s where [name] = %s', $field->getModel()->getHash(), $array->modelName); PerfORMController::getBuilder()->renameField($field, $array->from); } } # renamed tables, remove adds and drops foreach ($this->renamedTables as $key => $array) { if ($array->counter == 2 and isset($array->from) and isset($array->to)) { $model = $this->queue[PerfORMStorage::TABLE_ADD][$array->to]->values['model']; unset($this->queue[PerfORMStorage::TABLE_ADD][$array->to]); unset($this->queue[PerfORMStorage::TABLE_DROP][$array->from]); $this->query('update [tables] set [name] = %s where [name] = %s', $array->to, $array->from); $this->query('update [fields] set [table] = %s where [table] = %s', $array->to, $array->from); PerfORMController::getBuilder()->renameTable($model, $array->from); $this->query('delete from [indexes] where [table] = %s', $array->from); foreach ($model->getIndexes() as $index) { $this->query('insert into [indexes] values( null, %s, %s, %s, %s)', $index->getName(), $index->getModel()->getTableName(), $index->getHash(), $index->isUnique()); PerfORMController::getBuilder()->renameIndex($index, $array->from); } } } # renamed views, remove adds and drops foreach ($this->renamedViews as $key => $array) { if ($array->counter == 2 and isset($array->from) and isset($array->to)) { $model = $this->queue[PerfORMStorage::VIEW_ADD][$array->to]->values['model']; unset($this->queue[PerfORMStorage::VIEW_ADD][$array->to]); unset($this->queue[PerfORMStorage::VIEW_DROP][$array->from]); $this->query('update [views] set [name] = %s where [name] = %s', $array->to, $array->from); $this->query('update [views] set [table] = %s where [table] = %s', $array->to, $array->from); PerfORMController::getBuilder()->renameView($model, $array->from); } } foreach ($this->queue as $operation => $actions) { foreach ($actions as $key => $action) { //list($field, $table)= explode('|', $key); switch ($operation) { case PerfORMStorage::FIELD_ADD: $field = $action->values['field']; $builder = PerfORMController::getBuilder('fieldadd'); $template = $builder->getTemplate('field-add'); $fieldInfo = $builder->getField($field); $fieldInfo->nullable = true; $template->field = $fieldInfo; PerfORMController::getBuilder()->addToBuffer($builder->renderTemplate($template), 'alters'); //PerfORMController::getConnection()->nativeQuery($sql); $result = PerfORMController::getConnection()->query('select * from %n', $field->getModel()->getTableName()); $pk = $field->getModel()->getPrimaryKey(); foreach ($result as $row) { if (!is_null($default = $field->getDefault($row))) { PerfORMController::getBuilder()->addToBuffer(PerfORMController::getConnection()->sql('update %n set %n = %' . $field->getType() . ' where %n = %i;', $field->getModel()->getTableName(), $field->getName(), $default, $pk, $row->{$pk}), 'alters'); } elseif (!$field->isNullable()) { throw new Exception("Unable to find default value for field '" . $field->getName() . "' (id=" . $row->{$pk} . ")"); } } if (!$field->isNullable()) { $fieldInfo->nullable = false; $template = $builder->getTemplate('field-change-nullable'); $template->field = $fieldInfo; PerfORMController::getBuilder()->addToBuffer($builder->renderTemplate($template), 'alters'); //PerfORMController::getConnection()->nativeQuery($sql); } //PerfORMController::getBuilder()->addField(); $this->updateModelSync($action->values['field']->getModel()); break; case PerfORMStorage::FIELD_DROP: PerfORMController::getBuilder()->dropField($action->values['fieldName'], $action->values['model']); $this->updateModelSync($action->values['model']); break; case PerfORMStorage::TABLE_ADD: PerfORMController::getBuilder()->createTable($action->values['model']); foreach ($action->values['model']->getIndexes() as $index) { $this->addIndexToModel($index); } break; case PerfORMStorage::TABLE_DROP: PerfORMController::getBuilder()->dropTable($action->values['model']); break; case PerfORMStorage::VIEW_ADD: PerfORMController::getBuilder()->createView($action->values['model']); break; case PerfORMStorage::VIEW_DROP: PerfORMController::getBuilder()->dropView($action->values['model']); break; } if (is_array($action->sql)) { array_walk($action->sql, array($this, 'query')); } else { $this->query($action->sql); } } } return PerfORMController::getBuilder()->getSql(); }
public function actionDefault() { $sql = PerfORMController::sqlall(); $this->template->sql = is_null($sql) ? false : $sql; }