📄 dbo_source.php
字号:
if (strtoupper(trim($key)) == 'NOT') { $key = 'AND ' . trim($key); } $not = 'NOT '; } else { $not = null; } $out[] = $not . '(' . join(') ' . strtoupper($key) . ' (', $value) . ')'; } else { if (is_object($value) && isset($value->type)) { if ($value->type == 'identifier') { $data .= $this->name($key) . ' = ' . $this->name($value->value); } elseif ($value->type == 'identifier') { $data .= $this->name($key) . ' = ' . $value->value; } } elseif (is_array($value) && !empty($value) && !$valueInsert) { $keys = array_keys($value); if (array_keys($value) === array_values(array_keys($value))) { $data = $this->name($key) . ' IN ('; if ($quoteValues || strpos($value[0], '-!') !== 0) { if (is_object($model)) { $columnType = $model->getColumnType($key); } $data .= join(', ', $this->value($value, $columnType)); } elseif (strpos($value[0], '-!') === 0) { trigger_error(__('Escape flag (-!) deprecated, use Model::query()', true), E_USER_WARNING); $data .= $this->value(str_replace('-!', '', $value[0])); } $data .= ')'; } else { $ret = $this->conditionKeysToString($value, $quoteValues, $model); if (count($ret) > 1) { $data = '(' . join(') AND (', $ret) . ')'; } elseif (isset($ret[0])) { $data = $ret[0]; } } } elseif (is_numeric($key) && !empty($value)) { $data = $this->__quoteFields($value); } else { $data = $this->__parseKey($model, trim($key), $value); } if ($data != null) { if (preg_match('/^\(\(\((.+)\)\)\)$/', $data)) { $data = substr($data, 1, strlen($data) - 2); } $out[] = $data; $data = null; } } $c++; } return $out; }/** * Extracts a Model.field identifier and an SQL condition operator from a string, formats and inserts values, * and composes them into an SQL snippet. * * @param Model $model Model object initiating the query * @param string $key An SQL key snippet containing a field and optional SQL operator * @param mixed $value The value(s) to be inserted in the string * @return string * @access private */ function __parseKey($model, $key, $value) { if (!strpos($key, ' ')) { $operator = '='; } else { list($key, $operator) = explode(' ', $key, 2); } $type = (is_object($model) ? $model->getColumnType($key) : null); $null = ($value === null || (is_array($value) && empty($value))); if (strtolower($operator) === 'not') { $data = $this->conditionKeysToString(array($operator => array($key => $value)), true, $model); return $data[0]; } if (!preg_match('/^((' . join(')|(', $this->__sqlOps) . '\\x20)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)/is', trim($operator))) { $operator .= ' ='; } if ( (is_array($value) && is_string($value[0]) && strpos($value[0], '-!') === 0) || (is_string($value) && strpos($value, '-!') === 0) ) { trigger_error(__('Escape flag (-!) deprecated, use Model::query()', true), E_USER_WARNING); $value = str_replace('-!', '', $value); } else { $value = $this->value($value, $type); } $operator = trim($operator); $key = (strpos($key, '(') !== false || strpos($key, ')') !== false) ? $this->__quoteFields($key) : $key = $this->name($key); if (strpos($operator, '?') !== false || (is_array($value) && strpos($operator, ':') !== false)) { return "{$key} " . String::insert($operator, $value); } elseif (is_array($value)) { $value = join(', ', $value); switch ($operator) { case '=': $operator = 'IN'; break; case '!=': case '<>': $operator = 'NOT IN'; break; } $value = "({$value})"; } elseif ($null) { switch ($operator) { case '=': $operator = 'IS'; break; case '!=': case '<>': $operator = 'NOT IS'; break; } } return "{$key} {$operator} {$value}"; }/** * Quotes Model.fields * * @param string $conditions * @return string or false if no match * @access private */ function __quoteFields($conditions) { $start = $end = null; $original = $conditions; if (!empty($this->startQuote)) { $start = preg_quote($this->startQuote); } if (!empty($this->endQuote)) { $end = preg_quote($this->endQuote); } $conditions = str_replace(array($start, $end), '', $conditions); preg_match_all('/(?:[\'\"][^\'\"\\\]*(?:\\\.[^\'\"\\\]*)*[\'\"])|([a-z0-9_' . $start . $end . ']*\\.[a-z0-9_' . $start . $end . ']*)/i', $conditions, $replace, PREG_PATTERN_ORDER); if (isset($replace['1']['0'])) { $pregCount = count($replace['1']); for ($i = 0; $i < $pregCount; $i++) { if (!empty($replace['1'][$i]) && !is_numeric($replace['1'][$i])) { $conditions = preg_replace('/\b' . preg_quote($replace['1'][$i]) . '\b/', $this->name($replace['1'][$i]), $conditions); } } return $conditions; } return $original; }/** * Returns a limit statement in the correct format for the particular database. * * @param integer $limit Limit of results returned * @param integer $offset Offset from which to start results * @return string SQL limit/offset statement */ function limit($limit, $offset = null) { if ($limit) { $rt = ''; if (!strpos(strtolower($limit), 'limit') || strpos(strtolower($limit), 'limit') === 0) { $rt = ' LIMIT'; } if ($offset) { $rt .= ' ' . $offset . ','; } $rt .= ' ' . $limit; return $rt; } return null; }/** * Returns an ORDER BY clause as a string. * * @param string $key Field reference, as a key (i.e. Post.title) * @param string $direction Direction (ASC or DESC) * @return string ORDER BY clause */ function order($keys, $direction = 'ASC') { if (is_string($keys) && strpos($keys, ',') && !preg_match('/\(.+\,.+\)/', $keys)) { $keys = array_map('trim', explode(',', $keys)); } if (is_array($keys)) { foreach ($keys as $key => $val) { if (is_numeric($key) && empty($val)) { unset ($keys[$key]); } } } if (empty($keys) || (is_array($keys) && count($keys) && isset($keys[0]) && empty($keys[0]))) { return ''; } if (is_array($keys)) { if (Set::countDim($keys) > 1) { $new = array(); foreach ($keys as $val) { $val = $this->order($val); $new[] = $val; } $keys = $new; } foreach ($keys as $key => $value) { if (is_numeric($key)) { $value = ltrim(str_replace('ORDER BY ', '', $this->order($value))); $key = $value; if (!preg_match('/\\x20ASC|\\x20DESC/i', $key)) { $value = ' ' . $direction; } else { $value = ''; } } else { $value = ' ' . $value; } if (!preg_match('/^.+\\(.*\\)/', $key) && !strpos($key, ',')) { $dir = ''; $hasDir = preg_match('/\\x20ASC|\\x20DESC/i', $key, $dir); if ($hasDir) { $dir = $dir[0]; $key = preg_replace('/\\x20ASC|\\x20DESC/i', '', $key); } else { $dir = ''; } $key = trim($this->name(trim($key)) . ' ' . trim($dir)); } $order[] = $this->order($key . $value); } return ' ORDER BY ' . trim(str_replace('ORDER BY', '', join(',', $order))); } else { $keys = preg_replace('/ORDER\\x20BY/i', '', $keys); if (strpos($keys, '.')) { preg_match_all('/([a-zA-Z0-9_]{1,})\\.([a-zA-Z0-9_]{1,})/', $keys, $result, PREG_PATTERN_ORDER); $pregCount = count($result['0']); for ($i = 0; $i < $pregCount; $i++) { $keys = preg_replace('/' . $result['0'][$i] . '/', $this->name($result['0'][$i]), $keys); } if (preg_match('/\\x20ASC|\\x20DESC/i', $keys)) { return ' ORDER BY ' . $keys; } else { return ' ORDER BY ' . $keys . ' ' . $direction; } } elseif (preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match)) { $direction = $match['1']; $keys = preg_replace('/' . $match['1'] . '/', '', $keys); return ' ORDER BY ' . $keys . $direction; } else { $direction = ' ' . $direction; } return ' ORDER BY ' . $keys . $direction; } }/** * Create a GROUP BY SQL clause * * @param string $group Group By Condition * @return mixed string condition or null */ function group($group) { if ($group) { return ' GROUP BY ' . $this->__quoteFields($group); } return null; }/** * Disconnects database, kills the connection and says the connection is closed, * and if DEBUG is turned on, the log for this object is shown. * */ function close() { if (Configure::read() > 1) { $this->showLog(); } $this->disconnect(); }/** * Checks if the specified table contains any record matching specified SQL * * @param Model $model Model to search * @param string $sql SQL WHERE clause (condition only, not the "WHERE" part) * @return boolean True if the table has a matching record, else false */ function hasAny(&$Model, $sql) { $sql = $this->conditions($sql); $table = $this->fullTableName($Model); $where = $sql ? "WHERE {$sql}" : 'WHERE 1 = 1'; $id = $Model->primaryKey; $out = $this->fetchRow("SELECT COUNT({$id}) {$this->alias}count FROM {$table} {$where}"); if (is_array($out)) { return $out[0]['count']; } return false; }/** * Gets the length of a database-native column description, or null if no length * * @param string $real Real database-layer column type (i.e. "varchar(255)") * @return mixed An integer or string representing the length of the column */ function length($real) { if (!preg_match_all('/([\w\s]+)(?:\((\d+)(?:,(\d+))?\))?(\sunsigned)?(\szerofill)?/', $real, $result)) { trigger_error(__('FIXME: Can\'t parse field: ' . $real, true), E_USER_WARNING); $col = str_replace(array(')', 'unsigned'), '', $real); $limit = null; if (strpos($col, '(') !== false) { list($col, $limit) = explode('(', $col); } if ($limit != null) { return intval($limit); } return null; } $types = array( 'int' => 1, 'tinyint' => 1, 'smallint' => 1, 'mediumint' => 1, 'integer' => 1, 'bigint' => 1 ); list($real, $type, $length, $offset, $sign, $zerofill) = $result; $typeArr = $type; $type = $type[0]; $length = $length[0]; $offset = $offset[0]; $isFloat = in_array($type, array('dec', 'decimal', 'float', 'numeric', 'double')); if ($isFloat && $offset) { return $length.','.$offset; } if (($real[0] == $type) && (count($real) == 1)) { return null; } if (isset($types[$type])) { $length += $types[$type]; if (!empty($sign)) { $length--; } } elseif (in_array($type, array('enum', 'set'))) { $length = 0; foreach ($typeArr as $key => $enumValue) { if ($key == 0) { continue; } $tmpLength = strlen($enumValue); if ($tmpLength > $length) { $length = $tmpLength; } } } return intval($length); }/** * Translates between PHP boolean values and Database (faked) boolean values * * @param mixed $data Value to be translated * @return mixed Converted boolean value */ function boolean($data) { if ($data === true || $data === false) { if ($data === true) { return 1; } return 0; } else { return !empty($data); } }/** * Inserts multiple values into a table * * @param string $table * @param string $fields * @param array $values * @access protected */ function insertMulti($table, $fields, $values) { $table = $this->fullTableName($table); if (is_array($fields)) { $fields = join(', ', array_map(array(&$this, 'name'), $fields)); } $count = count($values); for ($x = 0; $x < $count; $x++) { $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values[$x]}"); } }/** * Returns an array of the indexes in given datasource name. * * @param string $model Name of model to inspect * @return array Fields in table. Keys are column and unique */ function index($model) { return false; }/** * Generate a database-native schema for the given Schema object * * @param object $schema An instance of a subclass of CakeSchema * @param string $tableName Optional. If specified only the table name given will be generated. * Otherwise, all tables defined in the schema are generated. * @return string */ function createSchema($schema, $tableName = null) { if (!is_a($schema, 'CakeSchema')) { trigger_error(__('Invalid schema object', true), E_USER_WARNING); return null; } $out = ''; foreach ($schema->tables as $curTable => $columns) { if (!$tableName || $tableName == $curTable) { $cols = $colList = $indexes
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -