⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbo_mssql.php

📁 Cake Framwork , Excellent
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?php/* SVN FILE: $Id: dbo_mssql.php 7118 2008-06-04 20:49:29Z gwoo $ *//** * MS SQL layer for DBO * * Long description for file * * PHP versions 4 and 5 * * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/> * Copyright 2005-2008, Cake Software Foundation, Inc. *								1785 E. Sahara Avenue, Suite 490-204 *								Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright		Copyright 2005-2008, Cake Software Foundation, Inc. * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package			cake * @subpackage		cake.cake.libs.model.datasources.dbo * @since			CakePHP(tm) v 0.10.5.1790 * @version			$Revision: 7118 $ * @modifiedby		$LastChangedBy: gwoo $ * @lastmodified	$Date: 2008-06-04 13:49:29 -0700 (Wed, 04 Jun 2008) $ * @license			http://www.opensource.org/licenses/mit-license.php The MIT License *//** * Short description for class. * * Long description for class * * @package		cake * @subpackage	cake.cake.libs.model.datasources.dbo */class DboMssql extends DboSource {/** * Driver description * * @var string */	var $description = "MS SQL DBO Driver";/** * Starting quote character for quoted identifiers * * @var string */	var $startQuote = "[";/** * Ending quote character for quoted identifiers * * @var string */	var $endQuote = "]";/** * Creates a map between field aliases and numeric indexes.  Workaround for the * SQL Server driver's 30-character column name limitation. * * @var array */	var $__fieldMappings = array();/** * Base configuration settings for MS SQL driver * * @var array */	var $_baseConfig = array(		'persistent' => true,		'host' => 'localhost',		'login' => 'root',		'password' => '',		'database' => 'cake',		'port' => '1433',	);/** * MS SQL column definition * * @var array */	var $columns = array(		'primary_key' => array('name' => 'IDENTITY (1, 1) NOT NULL'),		'string'	=> array('name' => 'varchar', 'limit' => '255'),		'text'		=> array('name' => 'text'),		'integer'	=> array('name' => 'int', 'formatter' => 'intval'),		'float'		=> array('name' => 'numeric', 'formatter' => 'floatval'),		'datetime'	=> array('name' => 'datetime', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),		'timestamp' => array('name' => 'timestamp', 'format' => 'Y-m-d H:i:s', 'formatter' => 'date'),		'time'		=> array('name' => 'datetime', 'format' => 'H:i:s', 'formatter' => 'date'),		'date'		=> array('name' => 'datetime', 'format' => 'Y-m-d', 'formatter' => 'date'),		'binary'	=> array('name' => 'image'),		'boolean'	=> array('name' => 'bit')	);/** * MS SQL DBO driver constructor; sets SQL Server error reporting defaults * * @param array $config Configuration data from app/config/databases.php * @return boolean True if connected successfully, false on error */	function __construct($config, $autoConnect = true) {		if ($autoConnect) {			if (!function_exists('mssql_min_message_severity')) {				trigger_error("PHP SQL Server interface is not installed, cannot continue.  For troubleshooting information, see http://php.net/mssql/", E_USER_WARNING);			}			mssql_min_message_severity(15);			mssql_min_error_severity(2);		}		return parent::__construct($config, $autoConnect);	}/** * Connects to the database using options in the given configuration array. * * @return boolean True if the database could be connected, else false */	function connect() {		$config = $this->config;		$os = env('OS');		if (!empty($os) && strpos($os, 'Windows') !== false) {			$sep = ',';		} else {			$sep = ':';		}		$this->connected = false;		if (is_numeric($config['port'])) {			$port = $sep . $config['port'];	// Port number		} elseif ($config['port'] === null) {			$port = '';						// No port - SQL Server 2005		} else {			$port = '\\' . $config['port'];	// Named pipe		}		if (!$config['persistent']) {			$this->connection = mssql_connect($config['host'] . $port, $config['login'], $config['password'], true);		} else {			$this->connection = mssql_pconnect($config['host'] . $port, $config['login'], $config['password']);		}		if (mssql_select_db($config['database'], $this->connection)) {			$this->_execute("SET DATEFORMAT ymd");			$this->connected = true;		}		return $this->connected;	}/** * Disconnects from database. * * @return boolean True if the database could be disconnected, else false */	function disconnect() {		@mssql_free_result($this->results);		$this->connected = !@mssql_close($this->connection);		return !$this->connected;	}/** * Executes given SQL statement. * * @param string $sql SQL statement * @return resource Result resource identifier * @access protected */	function _execute($sql) {		return mssql_query($sql, $this->connection);	}/** * Returns an array of sources (tables) in the database. * * @return array Array of tablenames in the database */	function listSources() {		$cache = parent::listSources();		if ($cache != null) {			return $cache;		}		$result = $this->fetchAll('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES');		if (!$result || empty($result)) {			return array();		} else {			$tables = array();			foreach ($result as $table) {				$tables[] = $table[0]['TABLE_NAME'];			}			parent::listSources($tables);			return $tables;		}	}/** * Returns an array of the fields in given table name. * * @param Model $model Model object to describe * @return array Fields in table. Keys are name and type */	function describe(&$model) {		$cache = parent::describe($model);		if ($cache != null) {			return $cache;		}		$fields = false;		$cols = $this->fetchAll("SELECT COLUMN_NAME as Field, DATA_TYPE as Type, COL_LENGTH('" . $this->fullTableName($model, false) . "', COLUMN_NAME) as Length, IS_NULLABLE As [Null], COLUMN_DEFAULT as [Default], COLUMNPROPERTY(OBJECT_ID('" . $this->fullTableName($model, false) . "'), COLUMN_NAME, 'IsIdentity') as [Key], NUMERIC_SCALE as Size FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $this->fullTableName($model, false) . "'", false);		foreach ($cols as $column) {			$field = $column[0]['Field'];			$fields[$field] = array(				'type' => $this->column($column[0]['Type']),				'null' => (strtoupper($column[0]['Null']) == 'YES'),				'default' => preg_replace("/^\('?([^']*)?'?\)$/", "$1", $column[0]['Default']),				'length' => intval($column[0]['Length']),				'key'	=> ($column[0]['Key'] == '1')			);			if (in_array($fields[$field]['default'], array('null', '(null)'))) {				$fields[$field]['default'] = null;			}			if ($fields[$field]['key'] && $fields[$field]['type'] == 'integer') {				$fields[$field]['length'] = 11;			} elseif (!$fields[$field]['key']) {				unset($fields[$field]['key']);			}			if (in_array($fields[$field]['type'], array('date', 'time', 'datetime', 'timestamp'))) {				$fields[$field]['length'] = null;			}		}		$this->__cacheDescription($this->fullTableName($model, false), $fields);		return $fields;	}/** * Returns a quoted and escaped string of $data for use in an SQL statement. * * @param string $data String to be prepared for use in an SQL statement * @param string $column The column into which this data will be inserted * @param boolean $safe Whether or not numeric data should be handled automagically if no column data is provided * @return string Quoted and escaped data */	function value($data, $column = null, $safe = false) {		$parent = parent::value($data, $column, $safe);		if ($parent != null) {			return $parent;		}		if ($data === null) {			return 'NULL';		}		if ($data === '') {			return "''";		}		switch($column) {			case 'boolean':				$data = $this->boolean((bool)$data);			break;			default:				if (get_magic_quotes_gpc()) {					$data = stripslashes(str_replace("'", "''", $data));				} else {					$data = str_replace("'", "''", $data);				}			break;		}		if (in_array($column, array('integer', 'float')) && is_numeric($data)) {			return $data;		}		return "'" . $data . "'";	}/** * Generates the fields list of an SQL query. * * @param Model $model * @param string $alias Alias tablename * @param mixed $fields * @return array */	function fields(&$model, $alias = null, $fields = array(), $quote = true) {		if (empty($alias)) {			$alias = $model->alias;		}		$fields = parent::fields($model, $alias, $fields, false);		$count = count($fields);		if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false) {			for ($i = 0; $i < $count; $i++) {				$prepend = '';				if (strpos($fields[$i], 'DISTINCT') !== false) {					$prepend = 'DISTINCT ';					$fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));				}				$fieldAlias = count($this->__fieldMappings);				if (!preg_match('/\s+AS\s+/i', $fields[$i])) {					if (strpos($fields[$i], '.') === false) {						$this->__fieldMappings[$alias . '__' . $fieldAlias] = $alias . '.' . $fields[$i];						$fieldName  = $this->name($alias . '.' . $fields[$i]);						$fieldAlias = $this->name($alias . '__' . $fieldAlias);					} else {						$build = explode('.', $fields[$i]);						$this->__fieldMappings[$build[0] . '__' . $fieldAlias] = $fields[$i];						$fieldName  = $this->name($build[0] . '.' . $build[1]);						$fieldAlias = $this->name(preg_replace("/^\[(.+)\]$/", "$1", $build[0]) . '__' . $fieldAlias);					}					if ($model->getColumnType($fields[$i]) == 'datetime') {						$fieldName = "CONVERT(VARCHAR(20), {$fieldName}, 20)";					}					$fields[$i] =  "{$fieldName} AS {$fieldAlias}";				}				$fields[$i] = $prepend . $fields[$i];			}		}		return $fields;	}/** * Begin a transaction * * @param unknown_type $model

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -