Adapter MsSQL

De KumbiaPHP Framework Wiki
Revisión del 16:11 4 nov 2010 de Fidelio (discusión | contribuciones) (Página creada con '== Adaptador para las bases MS SQL == Este adaptador no utiliza PDO, es decir puedes configurar tu archivo database.ini de esta forma <source> [development] host = localhost ...')
(dif) ← Revisión anterior | Revisión actual (dif) | Revisión siguiente → (dif)

Adaptador para las bases MS SQL

Este adaptador no utiliza PDO, es decir puedes configurar tu archivo database.ini de esta forma

[development]
host = localhost
username = sa
password = 
name = master
type = '''mssql'''

el código es el siguiente.

class DbMsSQL extends DbBase implements DbBaseInterface  {

	/**
	 * Resource de la Conexion a MsSQL
	 *
	 * @var resource
	 */
	public $id_connection;

	/**
	 * Ultimo Resultado de una Query
	 *
	 * @var resource
	 */
	public $last_result_query;

	/**
	 * Ultima sentencia SQL enviada a MsSQL
	 *
	 * @var string
	 */
	private $last_query;

	/**
	 * Ultimo error generado por MsSQL
	 *
	 * @var string
	 */
	public $last_error;

	/**
	 * Resultado de Array Asociativo
	 *
	 */
	const DB_ASSOC = MSSQL_ASSOC;

	/**
	 * Resultado de Array Asociativo y Numerico
	 *
	 */
	const DB_BOTH = MSSQL_BOTH;

	/**
	 * Resultado de Array Numerico
	 *
	 */
	const DB_NUM = MSSQL_NUM;

	/**
	 * Tipo de Dato Integer
	 *
	 */
	const TYPE_INTEGER = 'INT';

	/**
	 * Tipo de Dato Date
	 *
	 */
	const TYPE_DATE = 'SMALLDATETIME';

	/**
	 * Tipo de Dato Varchar
	 *
	 */
	const TYPE_VARCHAR = 'VARCHAR';

	/**
	 * Tipo de Dato Decimal
	 *
	 */
	const TYPE_DECIMAL = 'DECIMAL';

	/**
	 * Tipo de Dato Datetime
	 *
	 */
	const TYPE_DATETIME = 'DATETIME';

	/**
	 * Tipo de Dato Char
	 *
	 */
	const TYPE_CHAR = 'CHAR';

	/**
	 * Hace una conexion a la base de datos de MsSQL
	 *
	 * @param array $config
	 * @return resource_connection
	 */
	public function connect($config){
		if(!extension_loaded('mssql')){
			throw new KumbiaException('Debe cargar la extensión de PHP llamada php_mssql');
		}
		if(!isset($config['port']) || !$config['port']) {
			$config['port'] = 1433;
		}
		if($this->id_connection = mssql_connect("{$config['host']},{$config['port']}", $config['username'], $config['password'], true)){
			if($config['name']!=='') {
				if(!mssql_select_db($config['name'], $this->id_connection)){
					throw new KumbiaException($this->error());
				}
			}
			return true;
		} else {
			throw new KumbiaException($this->error());
		}
	}
	/**
	 * Efectua operaciones SQL sobre la base de datos
	 *
	 * @param string $sqlQuery
	 * @return resource or false
	 */
	public function query($sql_query){
		$this->debug($sql_query);
		if($this->logger){
		    Logger::debug($sql_query);
		}
		if(!$this->id_connection){
			$this->connect();
			if(!$this->id_connection){
				return false;
			}
		}
		$this->last_query = $sql_query;
		if($result_query = mssql_query($sql_query, $this->id_connection)){
			$this->last_result_query = $result_query;
			return $result_query;
		}else{
			$this->last_result_query = false;
			throw new KumbiaException($this->error(" al ejecutar <em>\"$sql_query\"</em>"));
		}
	}
	/**
	 * Cierra la Conexión al Motor de Base de datos
	 */
	public function close(){
		if($this->id_connection) {
			return mssql_close();
		}
		return false;
	}
	/**
	 * Devuelve fila por fila el contenido de un select
	 *
	 * @param resource $result_query
	 * @param int $opt
	 * @return array
	 */
	public function fetch_array($result_query='', $opt=MSSQL_BOTH){
		if(!$this->id_connection){
			return false;
		}
		if(!$result_query){
			$result_query = $this->last_result_query;
			if(!$result_query){
				return false;
			}
		}
		return mssql_fetch_array($result_query, $opt);
	}
	/**
	 * Constructor de la Clase
	 *
	 * @param array $config
	 */
	public function __construct($config){
		$this->connect($config);
	}
	/**
	 * Devuelve el numero de filas de un select
	 */
	public function num_rows($result_query=''){
		if(!$this->id_connection){
			return false;
		}
		if(!$result_query){
			$result_query = $this->last_result_query;
			if(!$result_query){
				return false;
			}
		}
		if(($number_rows = mssql_num_rows($result_query))!==false){
			return $number_rows;
		} else {
			throw new KumbiaException($this->error());
		}
		return false;
	}

	/**
	 * Devuelve el nombre de un campo en el resultado de un select
	 *
	 * @param int $number
	 * @param resource $result_query
	 * @return string
	 */
	public function field_name($number, $result_query=''){
		if(!$this->id_connection){
			return false;
		}
		if(!$result_query){
			$result_query = $this->last_result_query;
			if(!$result_query){
				return false;
			}
		}
		if(($fieldName = mssql_field_name($result_query, $number))!==false){
			return $fieldName;
		} else {
			throw new KumbiaException($this->error());
		}
		return false;
	}
	/**
	 * Se Mueve al resultado indicado por $number en un select
	 *
	 * @param int $number
	 * @param resource $result_query
	 * @return boolean
	 */
	public function data_seek($number, $result_query=''){
		if(!$result_query){
			$result_query = $this->last_result_query;
			if(!$result_query){
				return false;
			}
		}
		if(($success = mssql_data_seek($result_query, $number))!==false){
			return $success;
		} else {
			throw new KumbiaException($this->error());
		}
		return false;
	}

	/**
	 * Numero de Filas afectadas en un insert, update o delete
	 *
	 * @param resource $result_query
	 * @return int
	 */
	public function affected_rows($result_query=''){
		if(($numberRows = mssql_affected_rows())!==false){
			return $numberRows;
		} else {
			$this->lastError = $this->error();
			throw new KumbiaException($this->error());
		}
		return false;
	}

	/**
	 * Devuelve el error de MsSQL
	 *
	 * @return string
	 */
	public function error($err=''){
		if(!$this->id_connection){
			$this->last_error = mssql_get_last_message() ? mssql_get_last_message() : "[Error Desconocido en MsSQL: $err]";
			if($this->logger){
				Logger::error($this->last_error);
			}
			return $this->last_error;
		}
		$this->last_error = mssql_get_last_message() ? mssql_get_last_message() : "[Error Desconocido en MsSQL: $err]";
		$this->last_error.= $err;
		if($this->logger){
		    Logger::error($this->last_error);
		}
		return $this->last_error;
	}
	/**
	 * Devuelve el no error de MsSQL
	 *
	 * @return int
	 */
	public function no_error(){
		if(!$this->id_connection){
			return false;
		}
		return mssql_errno();
	}
	/**
	 * Devuelve el ultimo id autonumerico generado en la BD
	 *
	 * @return int
	 */
	public function last_insert_id($table='', $primary_key=''){
		if(!$this->id_connection){
			return false;
		}
		$id = false;
		$result = mssql_query("select max({$primary_key}) from $table");
		if ($row = mssql_fetch_row($result)) {
			$this->id_connection = trim($row[0]);
		}
		mssql_free_result($res);
		return $this->id_connection;
	}
	/**
	 * Verifica si una tabla existe o no
	 *
	 * @param string $table
	 * @return boolean
	 */
	public function table_exists($table, $schema=''){
		$table = addslashes("$table");
		if($schema==''){
			$num = $this->fetch_one("SELECT COUNT(*) FROM
						INFORMATION_SCHEMA.TABLES
						WHERE TABLE_NAME = '$table'");
		} else {
			$schema = addslashes("$schema");
			$num = $this->fetch_one("SELECT COUNT(*) FROM
						INFORMATION_SCHEMA.TABLES
						WHERE TABLE_NAME = '$table'
						AND TABLE_SCHEMA = '$schema'");
		}
		return $num[0];
	}
	/**
	 * Devuelve un LIMIT valido para un SELECT del RBDM
	 *
	 * @param string $sql consulta sql
	 * @return string
	 */
	public function limit($sql){
		$params = Util::getParams(func_get_args());
		$sql_new = $sql;
		if(isset($params['limit']) && is_numeric($params['limit'])){
			$sql_new = str_replace("SELECT ", "SELECT TOP $params[limit] ", $sql);
		}		
		return $sql_new;
	}

	/**
	 * Borra una tabla de la base de datos
	 *
	 * @param string $table
	 * @return boolean
	 */
	public function drop_table($table, $if_exists=true){
		if($if_exists){
			$sql = "IF EXISTS(SELECT TABLE_NAME FROM
			INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table')
			DROP TABLE $table;";
			return $this->query($sql);
		} else {
			return $this->query("DROP TABLE $table");
		}
	}

	/**
	 * Crea una tabla utilizando SQL nativo del RDBM
	 *
	 * TODO:
	 * - Falta que el parametro index funcione. Este debe listar indices compuestos multipes y unicos
	 * - Agregar el tipo de tabla que debe usarse (MsSQL)
	 * - Soporte para campos autonumericos
	 * - Soporte para llaves foraneas
	 *
	 * @param string $table
	 * @param array $definition
	 * @return boolean
	 */
	public function create_table($table, $definition, $index=array()){
		$create_sql = "CREATE TABLE $table (";
		if(!is_array($definition)){
			throw new KumbiaException("Definición invalida para crear la tabla '$table'");
		}
		$create_lines = array();
		$index = array();
		$unique_index = array();
		$primary = array();
		$not_null = "";
		$size = "";
		foreach($definition as $field => $field_def){
			if(isset($field_def['not_null'])){
				$not_null = $field_def['not_null'] ? 'NOT NULL' : '';
			} else {
				$not_null = "";
			}
			if(isset($field_def['size'])){
				$size = $field_def['size'] ? '('.$field_def['size'].')' : '';
			} else {
				$size = "";
			}
			if(isset($field_def['index'])){
				if($field_def['index']){
					$index[] = "INDEX(`$field`)";
				}
			}
			if(isset($field_def['unique_index'])){
				if($field_def['unique_index']){
					$index[] = "UNIQUE(`$field`)";
				}
			}
			if(isset($field_def['primary'])){
				if($field_def['primary']){
					$primary[] = "`$field`";
				}
			}
			if(isset($field_def['auto'])){
				if($field_def['auto']){
					$field_def['extra'] = isset($field_def['extra']) ? $field_def['extra']." AUTO_INCREMENT" :  "AUTO_INCREMENT";
				}
			}
			if(isset($field_def['extra'])){
				$extra = $field_def['extra'];
			} else {
				$extra = "";
			}
			$create_lines[] = "`$field` ".$field_def['type'].$size.' '.$not_null.' '.$extra;
		}
		$create_sql.= join(',', $create_lines);
		$last_lines = array();
		if(count($primary)){
			$last_lines[] = 'PRIMARY KEY('.join(",", $primary).')';
		}
		if(count($index)){
			$last_lines[] = join(',', $index);
		}
		if(count($unique_index)){
			$last_lines[] = join(',', $unique_index);
		}
		if(count($last_lines)){
			$create_sql.= ','.join(',', $last_lines).')';
		}
		return $this->query($create_sql);

	}

	/**
	 * Listar las tablas en la base de datos
	 *
	 * @return array
	 */
	public function list_tables(){
		return $this->fetch_all("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES");
	}

	/**
	 * Listar los campos de una tabla
	 *
	 * @param string $table
	 * @return array
	 */
	public function describe_table($table, $schema=''){
		$describe_table = $this->fetch_all("exec sp_columns @table_name = '$table'");
		$final_describe = array();
		foreach($describe_table as $field){
			if($field["TYPE_NAME"]=='int identity'){
				$final_describe[] = array(
					"Field" => $field["COLUMN_NAME"],
					"Type" => "int(".$field['LENGTH'].")",
					"Null" => $field['NULLABLE'] == 1 ? "YES" : "NO",
					"Key" => 'PRI',
					'Default' => $field['COLUMN_DEF']
				);				
			}else{
				$final_describe[] = array(
					"Field" => $field["COLUMN_NAME"],
					"Type" => $field['LENGTH'] ? $field["TYPE_NAME"] : $field["TYPE_NAME"]."(".$field['LENGTH'].")",
					"Null" => $field['NULLABLE'] == 1 ? "YES" : "NO",
					"Key" => '',
					'Default' => $field['COLUMN_DEF']					
				);				
			}
		}
		return $final_describe;		
		//$sql = "SELECT A.name as Field,
		//	(case when A.isnullable=0 then 'NO' when A.isnullable=1 then 'YES' end) as 'Null',
		//	(case when A.colstat=1 then 'PRI' when A.colstat='' then '' end) as 'Key', 			
		//	(case when A.cdefault=0 then 'NULL' when A.cdefault<>0 then '0' end) as 'Default'
		//	FROM syscolumns A, sysobjects B WHERE A.ID = B.ID AND B.name = '$table'";
		//return $this->fetch_all($sql);
	}
    
	/**
	 * Devuelve fila por fila el contenido de un select
	 *
	 * @param resource $result_query
	 * @param string $class clase de objeto
	 * @return object 
	 */
	public function fetch_object($result_query=null, $class='stdClass'){
		if(!$result_query){
			$result_query = $this->last_result_query;
		}
		return mssql_fetch_object($result_query, $class);
	}
}

Realizado por: FiDeLio