function checkModuleInstalled() { $tableName = $this->getTableName(); // check if table exists $tableExists = new CDbCommand(Yii::app()->getDb(), "\n show tables like '{$tableName}'\n "); try { $exists = $tableExists->queryColumn(); } catch (Exception $e) { $exists = false; } return $exists ? true : false; }
/** * @return array|CDbDataReader|mixed */ protected function getExaminationEvent() { if (!$this->examinationEvent) { $this->examinationEvent = $this->command->select('id')->from('event_type')->where('`name` = "Examination"')->queryRow(); $this->command->reset(); } return $this->examinationEvent; }
/** * Binds parameter values for an SQL command. * @param CDbCommand $command database command * @param array $values values for binding (integer-indexed array for question mark placeholders, string-indexed array for named placeholders) */ public function bindValues($command, $values) { if (($n = count($values)) === 0) { return; } // by default yii checks value type and if it is float, yii sends value to pdo as string // mysql process this floats as string normal, but sphinx doesn't if (isset($values[0])) { for ($i = 0; $i < $n; ++$i) { $type = is_float($values[$i]) ? PDO::PARAM_INT : null; $command->bindValue($i + 1, $values[$i], $type); } } else { foreach ($values as $name => $value) { if ($name[0] !== ':') { $name = ':' . $name; } $type = is_float($value) ? PDO::PARAM_INT : null; $command->bindValue($name, $value, $type); } } }
/** * Executes the given sql statement(s). * * @param mixed sql statement(s) * @return mixed sql statement(s) (imploded) or false */ private function executeSql($sql) { try { $sql = (array) $sql; foreach ($sql as $sql1) { $cmd = new CDbCommand(self::$db, $sql1); $cmd->prepare(); $cmd->execute(); $this->afterSave(); $this->refresh(); } return implode("\n", $sql); } catch (CDbException $ex) { $this->afterSave(); if ($this->throwExceptions) { throw new DbException($cmd); } else { $errorInfo = $cmd->getPdoStatement()->errorInfo(); $this->addError(null, Yii::t('core', 'sqlErrorOccured', array('{errno}' => $errorInfo[1], '{errmsg}' => $errorInfo[2]))); return false; } } }
/** * Биндинг значений в SQL запросе. * @param array $values Параметры построения запроса. * @return DbCommand Для цепочечных вызовов. */ public function bindValues($values) { $values = $this->simplifyParams($values); return parent::bindValues($values); }
public function bindValues($values) { $this->prepare(); if ($this->toExplain) { foreach ($values as $name => $value) { $this->_statement_explain->bindValue($name, $value, $this->getP("_connection")->getPdoType(gettype($value))); } } parent::bindValues($values); return $this; }
public function actionReset_count() { $db_command = new CDbCommand(Yii::app()->db, 'truncate table `adtest`'); $db_command->execute(); $this->redirect(array('default/index', '#' => 'banner-id-54449')); }
/** * Binds parameter values for an SQL command. * @param CDbCommand $command database command * @param array $values values for binding (integer-indexed array for question mark placeholders, string-indexed array for named placeholders) */ public function bindValues($command, $values) { if (($n = count($values)) === 0) { return; } if (isset($values[0])) { for ($i = 0; $i < $n; ++$i) { $command->bindValue($i + 1, $values[$i]); } } else { foreach ($values as $name => $value) { if ($name[0] !== ':') { $name = ':' . $name; } $command->bindValue($name, $value); } } }
/** * Function to activate a survey * @param int $iSurveyID The Survey ID * @param bool $simulate * @return string */ function activateSurvey($iSurveyID, $simulate = false) { $createsurvey = ''; $activateoutput = ''; $createsurveytimings = ''; $fieldstiming = array(); $createsurveydirectory = false; //Check for any additional fields for this survey and create necessary fields (token and datestamp) $prow = Survey::model()->findByAttributes(array('sid' => $iSurveyID)); //Get list of questions for the base language $fieldmap = createFieldMap($iSurveyID, 'full', true, false, getBaseLanguageFromSurveyID($iSurveyID)); $createsurvey = array(); foreach ($fieldmap as $j => $arow) { switch ($arow['type']) { case 'startlanguage': $createsurvey[$arow['fieldname']] = "VARCHAR(20) NOT NULL"; break; case 'id': $createsurvey[$arow['fieldname']] = "pk"; break; case "startdate": case "datestamp": $createsurvey[$arow['fieldname']] = "datetime NOT NULL"; break; case "submitdate": $createsurvey[$arow['fieldname']] = "datetime"; break; case "lastpage": $createsurvey[$arow['fieldname']] = "integer"; break; case "N": //NUMERICAL $createsurvey[$arow['fieldname']] = "decimal (30,10)"; break; case "S": //SHORT TEXT if (Yii::app()->db->driverName == 'mysql' || Yii::app()->db->driverName == 'mysqli') { $createsurvey[$arow['fieldname']] = "text"; } else { $createsurvey[$arow['fieldname']] = "string"; } break; case "L": //LIST (RADIO) //LIST (RADIO) case "!": //LIST (DROPDOWN) //LIST (DROPDOWN) case "M": //Multiple choice //Multiple choice case "P": //Multiple choice with comment //Multiple choice with comment case "O": //DROPDOWN LIST WITH COMMENT if ($arow['aid'] != 'other' && strpos($arow['aid'], 'comment') === false && strpos($arow['aid'], 'othercomment') === false) { $createsurvey[$arow['fieldname']] = "VARCHAR(5)"; } else { $createsurvey[$arow['fieldname']] = "text"; } break; case "K": // Multiple Numerical $createsurvey[$arow['fieldname']] = "float"; break; case "U": //Huge text //Huge text case "Q": //Multiple short text //Multiple short text case "T": //LONG TEXT //LONG TEXT case ";": //Multi Flexi //Multi Flexi case ":": //Multi Flexi $createsurvey[$arow['fieldname']] = "text"; break; case "D": //DATE $createsurvey[$arow['fieldname']] = "datetime"; break; case "5": //5 Point Choice //5 Point Choice case "G": //Gender //Gender case "Y": //YesNo //YesNo case "X": //Boilerplate $createsurvey[$arow['fieldname']] = "VARCHAR(1)"; break; case "I": //Language switch $createsurvey[$arow['fieldname']] = "VARCHAR(20)"; break; case "|": $createsurveydirectory = true; if (strpos($arow['fieldname'], "_")) { $createsurvey[$arow['fieldname']] = "integer"; } else { $createsurvey[$arow['fieldname']] = "text"; } break; case "ipaddress": if ($prow->ipaddr == "Y") { $createsurvey[$arow['fieldname']] = "text"; } break; case "url": if ($prow->refurl == "Y") { $createsurvey[$arow['fieldname']] = "text"; } break; case "token": if ($prow->anonymized == "N") { $createsurvey[$arow['fieldname']] = "VARCHAR(36)"; } break; case '*': // Equation $createsurvey[$arow['fieldname']] = "text"; break; default: $createsurvey[$arow['fieldname']] = "VARCHAR(5)"; } if ($simulate) { $tempTrim = trim($createsurvey); $brackets = strpos($tempTrim, "("); if ($brackets === false) { $type = substr($tempTrim, 0, 2); } else { $type = substr($tempTrim, 0, 2); } $arrSim[] = array($type); } } if ($simulate) { return array('dbengine' => $CI->db->databasetabletype, 'dbtype' => Yii::app()->db->driverName, 'fields' => $arrSim); } // If last question is of type MCABCEFHP^QKJR let's get rid of the ending coma in createsurvey //$createsurvey = rtrim($createsurvey, ",\n")."\n"; // Does nothing if not ending with a comma $tabname = "{{survey_{$iSurveyID}}}"; $command = new CDbCommand(Yii::app()->db); try { $execresult = $command->createTable($tabname, $createsurvey); } catch (CDbException $e) { return array('error' => 'surveytablecreation'); } $anquery = "SELECT autonumber_start FROM {{surveys}} WHERE sid={$iSurveyID}"; if ($anresult = Yii::app()->db->createCommand($anquery)->query()->readAll()) { //if there is an autonumber_start field, start auto numbering here foreach ($anresult as $row) { if ($row['autonumber_start'] > 0) { if (Yii::app()->db->driverName == 'mssql' || Yii::app()->db->driverName == 'sqlsrv') { mssql_drop_primary_index('survey_' . $iSurveyID); mssql_drop_constraint('id', 'survey_' . $iSurveyID); $autonumberquery = "alter table {{survey_{$iSurveyID}}} drop column id "; Yii::app()->db->createCommand($autonumberquery)->execute(); $autonumberquery = "alter table {{survey_{$iSurveyID}}} add [id] int identity({$row['autonumber_start']},1)"; Yii::app()->db->createCommand($autonumberquery)->execute(); } elseif (Yii::app()->db->driverName == 'pgsql') { } else { $autonumberquery = "ALTER TABLE {{survey_{$iSurveyID}}} AUTO_INCREMENT = " . $row['autonumber_start']; $result = @Yii::app()->db->createCommand($autonumberquery)->execute(); } } } } if ($prow->savetimings == "Y") { $timingsfieldmap = createTimingsFieldMap($iSurveyID, "full", false, false, getBaseLanguageFromSurveyID($iSurveyID)); $column = array(); $column['id'] = $createsurvey['id']; foreach ($timingsfieldmap as $field => $fielddata) { $column[$field] = 'FLOAT'; } $command = new CDbCommand(Yii::app()->db); $tabname = "{{survey_{$iSurveyID}}}_timings"; try { $execresult = $command->createTable($tabname, $column); } catch (CDbException $e) { return array('error' => 'timingstablecreation'); } } $aResult = array('status' => 'OK'); // create the survey directory where the uploaded files can be saved if ($createsurveydirectory) { if (!file_exists(Yii::app()->getConfig('uploaddir') . "/surveys/" . $iSurveyID . "/files")) { if (!mkdir(Yii::app()->getConfig('uploaddir') . "/surveys/" . $iSurveyID . "/files", 0777, true)) { $aResult['warning'] = 'nouploadsurveydir'; } else { file_put_contents(Yii::app()->getConfig('uploaddir') . "/surveys/" . $iSurveyID . "/files/index.html", '<html><head></head><body></body></html>'); } } } $acquery = "UPDATE {{surveys}} SET active='Y' WHERE sid=" . $iSurveyID; $acresult = Yii::app()->db->createCommand($acquery)->query(); return $aResult; }
public static function doesTableExist($tableName) { $tableExists = new CDbCommand(Yii::app()->getDb(), "\n show tables like '{$tableName}'\n "); try { $exists = $tableExists->queryColumn(); } catch (Exception $e) { $exists = false; } return $exists ? true : false; }
public function move($command) { $sql = 'ALTER TABLE ' . self::$db->quoteTableName($this->TABLE_NAME) . "\n" . "\t" . 'MODIFY ' . $this->getColumnDefinition() . ' ' . (substr($command, 0, 6) == 'AFTER ' ? 'AFTER ' . self::$db->quoteColumnName(substr($command, 6)) : 'FIRST') . ';'; $cmd = new CDbCommand(self::$db, $sql); try { $cmd->prepare(); $cmd->execute(); return $sql; } catch (CDbException $ex) { throw new DbException($cmd); } }
public function rightJoin($table, $conditions, $params = array()) { return parent::rightJoin($table, $this->processConditionsEx($conditions), $params); }
private static function fetchPosts(CDbCommand $cmd) { $cmd->from(TABLE_POST)->select(array('id', 'title', 'thumbnail', 'summary', 'content', 'create_time'))->order(array('create_time desc', 'id desc'))->limit(self::POST_COUNT); $rows = $cmd->queryAll(); return $rows; }
/** * @param string $table * @param string $columns * @return int */ public function dropPrimaryKey($table, $columns) { $name = DbHelper::getPrimaryKeyName($table, $columns); $table = DbHelper::addTablePrefix($table); return parent::dropPrimaryKey($name, $table); }
public function update($attributes = null) { if ($this->getIsNewRecord()) { throw new CDbException(Yii::t('core', 'The active record cannot be updated because it is new.')); } if (!$this->beforeSave()) { return false; } $sql = ''; $table = Table::model()->findByPk(array('TABLE_NAME' => self::$table, 'TABLE_SCHEMA' => self::$schema)); // Check if there has been changed any attribute $changedAttributes = array(); foreach ($this->originalAttributes as $column => $value) { if ($this->getAttribute($column) !== $value || $this->getFunction($column)) { // SET datatype $changedAttributes[$column] = $this->getAttribute($column); } } $changedAttributesCount = count($changedAttributes); if ($changedAttributesCount > 0) { $sql = 'UPDATE ' . self::$db->quoteTableName(self::$table) . ' SET ' . "\n"; foreach ($changedAttributes as $column => $value) { $columnInfo = $this->getColumnInfo($table, $column); $function = $this->getFunction($column); $sql .= "\t" . self::$db->quoteColumnName($column) . ' = '; if ($function !== null) { $sql .= self::$functions[$function] . '(' . ($value === null ? 'NULL' : self::$db->quoteValue($value)) . ')'; } elseif ($columnInfo->IS_NULLABLE === "YES" && is_null($value)) { $sql .= 'NULL'; } elseif ($this->isHex($column)) { $sql .= $value; } elseif (is_array($value)) { $sql .= self::$db->quoteValue(implode(",", $value)); } elseif ($columnInfo->DATA_TYPE == "int") { $sql .= (int) $value; } elseif ($columnInfo->DATA_TYPE == "bit") { $sql .= (int) $value; } else { $sql .= is_null($value) ? 'NULL' : self::$db->quoteValue($value); } $changedAttributesCount--; if ($changedAttributesCount > 0) { $sql .= ',' . "\n"; } } $sql .= "\n" . 'WHERE ' . "\n"; $identifier = $this->getOriginalIdentifier(); // Create find criteria $count = count($identifier); foreach ($identifier as $column => $value) { if (is_null($value)) { $sql .= "\t" . self::$db->quoteColumnName($column) . ' IS NULL '; } else { $sql .= "\t" . self::$db->quoteColumnName($column) . ' = ' . self::$db->quoteValue($this->originalAttributes[$column]) . ' '; } $count--; if ($count > 0) { $sql .= 'AND ' . "\n"; } } $sql .= "\n" . 'LIMIT 1;'; } $cmd = new CDbCommand(self::$db, $sql); try { $cmd->prepare(); $cmd->execute(); $this->afterSave(); return $sql; } catch (CDbException $ex) { throw new DbException($cmd); } }
/** * 打上标签,未了数据库的安全日志 */ public function setText($value) { $value .= ' /* mis-data-stream-secure-tag */'; return parent::setText($value); }
/** * @param string $table * @param string $columns * * @return int */ public function dropPrimaryKey($table, $columns) { $name = $this->getConnection()->getPrimaryKeyName($table, $columns); $table = $this->getConnection()->addTablePrefix($table); return parent::dropPrimaryKey($name, $table); }
private function createMeta(CDbCommand $command) { $matches = $command->queryAll(); $metaInfo = $this->db->createCommand("SHOW META")->queryAll(); $meta = array(); foreach ($metaInfo as $item) { list($name, $value) = array_values($item); $meta[$name] = $value; } $meta['matches'] = $matches; return $meta; }
/** * Constructor. * @param CDbCommand $command the command generating the query result */ public function __construct(CDbCommand $command) { $this->_statement = $command->getPdoStatement(); $this->_statement->setFetchMode(PDO::FETCH_ASSOC); }
/** * Execute a bookmark */ public function actionExecute() { $id = Yii::app()->getRequest()->getParam('id'); $response = new AjaxResponse(); $response->refresh = true; $bookmark = Yii::app()->user->settings->get('bookmarks', 'database', $this->schema, 'id', $id); try { $cmd = new CDbCommand($this->db, $bookmark['query']); $cmd->execute(); $response->addNotification('success', Yii::t('core', 'successExecuteBookmark', array('{name}' => $bookmark['name'])), null, $bookmark['query']); } catch (Exception $ex) { $response->addNotification('error', $ex->getMessage(), $bookmark['query'], array('isSticky' => true)); } $this->sendJSON($response); }
/** * Binds parameter values for an SQL command. * @param CDbCommand $command database command * @param array $values values for binding (integer-indexed array for question mark placeholders, string-indexed array for named placeholders) */ public function bindValues($command, $values) { if(($n=count($values))===0) return; if(isset($values[0])) // question mark placeholders { for($i=0;$i<$n;++$i) $command->bindValue($i+1,$values[$i]); } else // named placeholders { foreach($values as $name=>$value) { if($name[0]!==':') $name=':'.$name; $command->bindValue($name,$value); } } }
/** * Executes the SQL statement and returns all rows. * @param CDbCommand $cmd * @param array $params * @param boolean $fetchAssociative * @return array the record found. An empty array if none is found. */ public function queryAll(CDbCommand $cmd, $params = array(), $fetchAssociative = true) { $this->beforeFind(); $cmd->from($this->table() . ' t'); return $cmd->queryAll($fetchAssociative, $params); }