📄 dbo_mssql.php
字号:
* @return boolean True on success, false on fail * (i.e. if the database/model does not support transactions). */ function begin(&$model) { if (parent::begin($model) && $this->execute('BEGIN TRANSACTION')) { $this->_transactionStarted = true; return true; } return false; }/** * Generates and executes an SQL INSERT statement for given model, fields, and values. * Removes Identity (primary key) column from update data before returning to parent, if * value is empty. * * @param Model $model * @param array $fields * @param array $values * @param mixed $conditions * @return array */ function create(&$model, $fields = null, $values = null) { if (!empty($values)) { $fields = array_combine($fields, $values); } if (array_key_exists($model->primaryKey, $fields)) { if (empty($fields[$model->primaryKey])) { unset($fields[$model->primaryKey]); } else { $this->_execute("SET IDENTITY_INSERT " . $this->fullTableName($model) . " ON"); } } $result = parent::create($model, array_keys($fields), array_values($fields)); if (array_key_exists($model->primaryKey, $fields) && !empty($fields[$model->primaryKey])) { $this->_execute("SET IDENTITY_INSERT " . $this->fullTableName($model) . " OFF"); } return $result; }/** * Generates and executes an SQL UPDATE statement for given model, fields, and values. * Removes Identity (primary key) column from update data before returning to parent. * * @param Model $model * @param array $fields * @param array $values * @param mixed $conditions * @return array */ function update(&$model, $fields = array(), $values = null, $conditions = null) { if (!empty($values)) { $fields = array_combine($fields, $values); } if (isset($fields[$model->primaryKey])) { unset($fields[$model->primaryKey]); } return parent::update($model, array_keys($fields), array_values($fields), $conditions); }/** * Returns a formatted error message from previous database operation. * * @return string Error message with error number */ function lastError() { $error = mssql_get_last_message($this->connection); if ($error) { if (strpos(strtolower($error), 'changed database') === false) { return $error; } } 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 mssql_rows_affected($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) { return @mssql_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 SCOPE_IDENTITY() AS insertID', false); return $id[0]['insertID']; }/** * 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), 'top') || strpos(strtolower($limit), 'top') === 0) { $rt = ' TOP'; } $rt .= ' ' . $limit; if (is_int($offset) && $offset > 0) { $rt .= ' OFFSET ' . $offset; } return $rt; } 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 = null; if (strpos($col, '(') !== false) { list($col, $limit) = explode('(', $col); } if (in_array($col, array('date', 'time', 'datetime', 'timestamp'))) { return $col; } if ($col == 'bit') { return 'boolean'; } if (strpos($col, 'int') !== false) { return 'integer'; } if (strpos($col, 'char') !== false) { return 'string'; } if (strpos($col, 'text') !== false) { return 'text'; } if (strpos($col, 'binary') !== false || $col == 'image') { return 'binary'; } if (in_array($col, array('float', 'real', 'decimal', 'numeric'))) { return 'float'; } return 'text'; }/** * Enter description here... * * @param unknown_type $results */ function resultSet(&$results) { $this->results =& $results; $this->map = array(); $num_fields = mssql_num_fields($results); $index = 0; $j = 0; while ($j < $num_fields) { $column = mssql_field_name($results, $j); if (strpos($column, '__')) { if (isset($this->__fieldMappings[$column]) && strpos($this->__fieldMappings[$column], '.')) { $map = explode('.', $this->__fieldMappings[$column]); } elseif (isset($this->__fieldMappings[$column])) { $map = array(0, $this->__fieldMappings[$column]); } else { $map = array(0, $column); } $this->map[$index++] = $map; } else { $this->map[$index++] = array(0, $column); } $j++; } }/** * Builds final SQL statement * * @param string $type Query type * @param array $data Query data * @return string */ function renderStatement($type, $data) { if (strtolower($type) == 'select') { extract($data); $fields = trim($fields); if (strpos($limit, 'TOP') !== false && strpos($fields, 'DISTINCT ') === 0) { $limit = 'DISTINCT ' . trim($limit); $fields = substr($fields, 9); } if (preg_match('/offset\s+([0-9]+)/i', $limit, $offset)) { $limit = preg_replace('/\s*offset.*$/i', '', $limit); preg_match('/top\s+([0-9]+)/i', $limit, $limitVal); $offset = intval($offset[1]) + intval($limitVal[1]); $rOrder = $this->__switchSort($order); list($order2, $rOrder) = array($this->__mapFields($order), $this->__mapFields($rOrder)); return "SELECT * FROM (SELECT {$limit} * FROM (SELECT TOP {$offset} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$order}) AS Set1 {$rOrder}) AS Set2 {$order2}"; } else { return "SELECT {$limit} {$fields} FROM {$table} {$alias} {$joins} {$conditions} {$order}"; } } else { return parent::renderStatement($type, $data); } }/** * Reverses the sort direction of ORDER statements to get paging offsets to work correctly * * @param string $order * @return string * @access private */ function __switchSort($order) { $order = preg_replace('/\s+ASC/i', '__tmp_asc__', $order); $order = preg_replace('/\s+DESC/i', ' ASC', $order); return preg_replace('/__tmp_asc__/', ' DESC', $order); }/** * Translates field names used for filtering and sorting to shortened names using the field map * * @param string $sql A snippet of SQL representing an ORDER or WHERE statement * @return string The value of $sql with field names replaced * @access private */ function __mapFields($sql) { if (empty($sql) || empty($this->__fieldMappings)) { return $sql; } foreach ($this->__fieldMappings as $key => $val) { $sql = preg_replace('/' . preg_quote($val) . '/', $this->name($key), $sql); $sql = preg_replace('/' . preg_quote($this->name($val)) . '/', $this->name($key), $sql); } return $sql; }/** * Returns an array of all result rows for a given SQL query. * Returns false if no rows matched. * * @param string $sql SQL statement * @param boolean $cache Enables returning/storing cached query results * @return array Array of resultset rows, or false if no rows matched */ function read(&$model, $queryData = array(), $recursive = null) { $results = parent::read($model, $queryData, $recursive); $this->__fieldMappings = array(); $this->__fieldMapBase = null; return $results; }/** * Fetches the next row from the current result set * * @return unknown */ function fetchResult() { if ($row = mssql_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; } }/** * Generate a database-native column schema string * * @param array $column An array structured like the following: array('name'=>'value', 'type'=>'value'[, options]), * where options can be 'default', 'length', or 'key'. * @return string */ function buildColumn($column) { $result = preg_replace('/(int|integer)\([0-9]+\)/i', '$1', parent::buildColumn($column)); $null = ( (isset($column['null']) && $column['null'] == true) || (array_key_exists('default', $column) && $column['default'] === null) || (array_keys($column) == array('type', 'name')) ); $stringKey = (isset($column['key']) && $column['key'] == 'primary' && $column['type'] != 'integer'); if ($null) { $result .= " NULL"; } return $result; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -