dbo_mysql.php
来自「Cake Framwork , Excellent」· PHP 代码 · 共 577 行 · 第 1/2 页
PHP
577 行
$table = $this->fullTableName($model); $joins = implode(' ', $this->_getJoins($model)); if (empty($conditions)) { $alias = $joins = false; } $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model); if ($conditions === false) { return false; } if ($this->execute($this->renderStatement('delete', compact('alias', 'table', 'joins', 'conditions'))) === false) { $model->onError(); return false; } return true; }/** * Returns a formatted error message from previous database operation. * * @return string Error message with error number */ function lastError() { if (mysql_errno($this->connection)) { return mysql_errno($this->connection).': '.mysql_error($this->connection); } return null; }/** * Returns number of affected rows in previous database operation. If no previous operation exists, * this returns false. * * @return integer Number of affected rows */ function lastAffected() { if ($this->_result) { return mysql_affected_rows($this->connection); } return null; }/** * Returns number of rows in previous resultset. If no previous resultset exists, * this returns false. * * @return integer Number of rows in resultset */ function lastNumRows() { if ($this->_result and is_resource($this->_result)) { return @mysql_num_rows($this->_result); } return null; }/** * Returns the ID generated from the previous INSERT operation. * * @param unknown_type $source * @return in */ function lastInsertId($source = null) { $id = $this->fetchRow('SELECT LAST_INSERT_ID() AS insertID', false); if ($id !== false && !empty($id) && !empty($id[0]) && isset($id[0]['insertID'])) { return $id[0]['insertID']; } return null; }/** * Converts database-layer column types to basic types * * @param string $real Real database-layer column type (i.e. "varchar(255)") * @return string Abstract column type (i.e. "string") */ function column($real) { if (is_array($real)) { $col = $real['name']; if (isset($real['limit'])) { $col .= '('.$real['limit'].')'; } return $col; } $col = str_replace(')', '', $real); $limit = $this->length($real); if (strpos($col, '(') !== false) { list($col, $vals) = explode('(', $col); } if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) { return $col; } if ($col == 'tinyint' && $limit == 1) { return 'boolean'; } if (strpos($col, 'int') !== false) { return 'integer'; } if (strpos($col, 'char') !== false || $col == 'tinytext') { return 'string'; } if (strpos($col, 'text') !== false) { return 'text'; } if (strpos($col, 'blob') !== false || $col == 'binary') { return 'binary'; } if (in_array($col, array('float', 'double', 'decimal'))) { return 'float'; } if (strpos($col, 'enum') !== false) { return "enum($vals)"; } if ($col == 'boolean') { return $col; } return 'text'; }/** * Enter description here... * * @param unknown_type $results */ function resultSet(&$results) { $this->results =& $results; $this->map = array(); $num_fields = mysql_num_fields($results); $index = 0; $j = 0; while ($j < $num_fields) { $column = mysql_fetch_field($results,$j); if (!empty($column->table)) { $this->map[$index++] = array($column->table, $column->name); } else { $this->map[$index++] = array(0, $column->name); } $j++; } }/** * Fetches the next row from the current result set * * @return unknown */ function fetchResult() { if ($row = mysql_fetch_row($this->results)) { $resultRow = array(); $i = 0; foreach ($row as $index => $field) { list($table, $column) = $this->map[$index]; $resultRow[$table][$column] = $row[$index]; $i++; } return $resultRow; } else { return false; } }/** * Sets the database encoding * * @param string $enc Database encoding */ function setEncoding($enc) { return $this->_execute('SET NAMES ' . $enc) != false; }/** * Gets the database encoding * * @return string The database encoding */ function getEncoding() { return mysql_client_encoding($this->connection); }/** * Inserts multiple values into a table * * @param string $table * @param string $fields * @param array $values */ function insertMulti($table, $fields, $values) { $table = $this->fullTableName($table); if (is_array($fields)) { $fields = join(', ', array_map(array(&$this, 'name'), $fields)); } $values = implode(', ', $values); $this->query("INSERT INTO {$table} ({$fields}) VALUES {$values}"); }/** * Returns an array of the indexes in given table name. * * @param string $model Name of model to inspect * @return array Fields in table. Keys are column and unique */ function index($model) { $index = array(); $table = $this->fullTableName($model, false); if($table) { $indexes = $this->query('SHOW INDEX FROM ' . $table); $keys = Set::extract($indexes, '{n}.STATISTICS'); foreach ($keys as $i => $key) { if(!isset($index[$key['Key_name']])) { $index[$key['Key_name']]['column'] = $key['Column_name']; $index[$key['Key_name']]['unique'] = ife($key['Non_unique'] == 0, 1, 0); } else { if(!is_array($index[$key['Key_name']]['column'])) { $col[] = $index[$key['Key_name']]['column']; } $col[] = $key['Column_name']; $index[$key['Key_name']]['column'] = $col; } } } return $index; }/** * Generate a MySQL Alter Table syntax for the given Schema comparison * * @param unknown_type $schema * @return unknown */ function alterSchema($compare, $table = null) { if(!is_array($compare)) { return false; } $out = ''; $colList = array(); foreach($compare as $curTable => $types) { if (!$table || $table == $curTable) { $out .= 'ALTER TABLE ' . $this->fullTableName($curTable) . " \n"; foreach($types as $type => $column) { switch($type) { case 'add': foreach($column as $field => $col) { $col['name'] = $field; $alter = 'ADD '.$this->buildColumn($col); if(isset($col['after'])) { $alter .= ' AFTER '. $this->name($col['after']); } $colList[] = $alter; } break; case 'drop': foreach($column as $field => $col) { $col['name'] = $field; $colList[] = 'DROP '.$this->name($field); } break; case 'change': foreach($column as $field => $col) { if(!isset($col['name'])) { $col['name'] = $field; } $colList[] = 'CHANGE '. $this->name($field).' '.$this->buildColumn($col); } break; } } $out .= "\t" . join(",\n\t", $colList) . ";\n\n"; } } return $out; }/** * Generate a MySQL "drop table" statement for the given Schema object * * @param object $schema An instance of a subclass of CakeSchema * @param string $table Optional. If specified only the table name given will be generated. * Otherwise, all tables defined in the schema are generated. * @return string */ function dropSchema($schema, $table = 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 (!$table || $table == $curTable) { $out .= 'DROP TABLE IF EXISTS ' . $this->fullTableName($curTable) . ";\n"; } } return $out; }}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?