CI_DB_mssql_driver Class Reference

Inheritance diagram for CI_DB_mssql_driver:
Collaboration diagram for CI_DB_mssql_driver:

List of all members.


Public Member Functions

 db_connect ()
 Non-persistent database connection.
 db_pconnect ()
 Persistent database connection.
 db_select ()
 Select the database.
 db_set_charset ($charset, $collation)
 Set client character set.
 _execute ($sql)
 Execute the query.
 _prep_query ($sql)
 Prep the query.
 trans_begin ($test_mode=FALSE)
 Begin Transaction.
 trans_commit ()
 Commit Transaction.
 trans_rollback ()
 Rollback Transaction.
 escape_str ($str)
 Escape String.
 affected_rows ()
 Affected Rows.
 insert_id ()
 Insert ID.
 _parse_major_version ($version)
 Parse major version.
 _version ()
 Version number query string.
 count_all ($table= '')
 "Count All" query
 _list_tables ($prefix_limit=FALSE)
 List table query.
 _list_columns ($table= '')
 List column query.
 _field_data ($table)
 Field data query.
 _error_message ()
 The error message string.
 _error_number ()
 The error message number.
 _escape_table ($table)
 Escape Table Name.
 _protect_identifiers ($item, $first_word_only=FALSE)
 Protect Identifiers.
 _from_tables ($tables)
 From Tables.
 _insert ($table, $keys, $values)
 Insert statement.
 _update ($table, $values, $where, $orderby=array(), $limit=FALSE)
 Update statement.
 _truncate ($table)
 Truncate statement.
 _delete ($table, $where=array(), $like=array(), $limit=FALSE)
 Delete statement.
 _limit ($sql, $limit, $offset)
 Limit string.
 _close ($conn_id)
 Close DB Connection.

Public Attributes

 $_count_string = "SELECT COUNT(*) AS "
 The syntax to count rows is slightly different across different database engines, so this string appears in each driver and is used for the count_all() and count_all_results() functions.
 $_random_keyword = ' ASC'

Detailed Description

Definition at line 31 of file mssql_driver.php.


Member Function Documentation

CI_DB_mssql_driver::_close ( conn_id  ) 

Close DB Connection.

public

Parameters:
resource 
Returns:
void

Definition at line 636 of file mssql_driver.php.

00637         {
00638                 @mssql_close($conn_id);
00639         }       

CI_DB_mssql_driver::_delete ( table,
where = array(),
like = array(),
limit = FALSE 
)

Delete statement.

Generates a platform-specific delete string from the supplied data

public

Parameters:
string the table name
array the where clause
string the limit clause
Returns:
string

Definition at line 586 of file mssql_driver.php.

00587         {
00588                 $conditions = '';
00589 
00590                 if (count($where) > 0 OR count($like) > 0)
00591                 {
00592                         $conditions = "\nWHERE ";
00593                         $conditions .= implode("\n", $this->ar_where);
00594 
00595                         if (count($where) > 0 && count($like) > 0)
00596                         {
00597                                 $conditions .= " AND ";
00598                         }
00599                         $conditions .= implode("\n", $like);
00600                 }
00601 
00602                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
00603         
00604                 return "DELETE FROM ".$table.$conditions.$limit;
00605         }

CI_DB_mssql_driver::_error_message (  ) 

The error message string.

private

Returns:
string

Definition at line 374 of file mssql_driver.php.

00375         {
00376                 // Are errros even supported in MS SQL?
00377                 return '';
00378         }

CI_DB_mssql_driver::_error_number (  ) 

The error message number.

private

Returns:
integer

Definition at line 388 of file mssql_driver.php.

00389         {
00390                 // Are error numbers supported?
00391                 return '';
00392         }

CI_DB_mssql_driver::_escape_table ( table  ) 

Escape Table Name.

This function adds backticks if the table name has a period in it. Some DBs will get cranky unless periods are escaped

private

Parameters:
string the table name
Returns:
string

Definition at line 406 of file mssql_driver.php.

00407         {
00408                 // I don't believe this is necessary with MS SQL.  Not sure, though. - Rick
00409         
00410                 /*
00411                 if (strpos($table, '.') !== FALSE)
00412                 {
00413                         $table = '"' . str_replace('.', '"."', $table) . '"';
00414                 }
00415                 */
00416                 
00417                 return $table;
00418         }       

CI_DB_mssql_driver::_execute ( sql  ) 

Execute the query.

private called by the base class

Parameters:
string an SQL query
Returns:
resource

Definition at line 103 of file mssql_driver.php.

References _prep_query().

00104         {
00105                 $sql = $this->_prep_query($sql);
00106                 return @mssql_query($sql, $this->conn_id);
00107         }

Here is the call graph for this function:

CI_DB_mssql_driver::_field_data ( table  ) 

Field data query.

Generates a platform-specific query so that the column data can be retrieved

public

Parameters:
string the table name
Returns:
object

Definition at line 361 of file mssql_driver.php.

00362         {
00363                 return "SELECT TOP 1 * FROM ".$this->_escape_table($table);     
00364         }

CI_DB_mssql_driver::_from_tables ( tables  ) 

From Tables.

This function implicitly groups FROM tables so there is no confusion about operator precedence in harmony with SQL standards

public

Parameters:
type 
Returns:
type

Definition at line 493 of file mssql_driver.php.

00494         {
00495                 if ( ! is_array($tables))
00496                 {
00497                         $tables = array($tables);
00498                 }
00499                 
00500                 return implode(', ', $tables);
00501         }

CI_DB_mssql_driver::_insert ( table,
keys,
values 
)

Insert statement.

Generates a platform-specific insert string from the supplied data

public

Parameters:
string the table name
array the insert keys
array the insert values
Returns:
string

Definition at line 516 of file mssql_driver.php.

00517         {       
00518                 return "INSERT INTO ".$this->_escape_table($table)." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
00519         }

CI_DB_mssql_driver::_limit ( sql,
limit,
offset 
)

Limit string.

Generates a platform-specific LIMIT clause

public

Parameters:
string the sql query string
integer the number of rows to limit the query to
integer the offset value
Returns:
string

Definition at line 620 of file mssql_driver.php.

00621         {
00622                 $i = $limit + $offset;
00623         
00624                 return preg_replace('/(^\SELECT (DISTINCT)?)/i','\\1 TOP '.$i.' ', $sql);               
00625         }

CI_DB_mssql_driver::_list_columns ( table = ''  ) 

List column query.

Generates a platform-specific query string so that the column names can be fetched

private

Parameters:
string the table name
Returns:
string

Definition at line 345 of file mssql_driver.php.

00346         {
00347                 return "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$this->_escape_table($table)."'";        
00348         }

CI_DB_mssql_driver::_list_tables ( prefix_limit = FALSE  ) 

List table query.

Generates a platform-specific query string so that the table names can be fetched

private

Parameters:
boolean 
Returns:
string

Definition at line 320 of file mssql_driver.php.

00321         {
00322                 $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
00323                 
00324                 // for future compatibility
00325                 if ($prefix_limit !== FALSE AND $this->dbprefix != '')
00326                 {
00327                         //$sql .= " LIKE '".$this->dbprefix."%'";
00328                         return FALSE; // not currently supported
00329                 }
00330                 
00331                 return $sql;
00332         }

CI_DB_mssql_driver::_parse_major_version ( version  ) 

Parse major version.

Grabs the major version number from the database server version string passed in.

private

Parameters:
string $version
Returns:
int16 major version number

Definition at line 264 of file mssql_driver.php.

Referenced by insert_id().

00265         {
00266                 preg_match('/([0-9]+)\.([0-9]+)\.([0-9]+)/', $version, $ver_info);
00267                 return $ver_info[1]; // return the major version b/c that's all we're interested in.
00268         }

Here is the caller graph for this function:

CI_DB_mssql_driver::_prep_query ( sql  ) 

Prep the query.

If needed, each database adapter can prep the query string

private called by execute()

Parameters:
string an SQL query
Returns:
string

Definition at line 120 of file mssql_driver.php.

Referenced by _execute().

00121         {
00122                 return $sql;
00123         }

Here is the caller graph for this function:

CI_DB_mssql_driver::_protect_identifiers ( item,
first_word_only = FALSE 
)

Protect Identifiers.

This function adds backticks if appropriate based on db type

private

Parameters:
mixed the item(s)
boolean should spaces be backticked
boolean only affect the first word
Returns:
mixed the item with backticks

Definition at line 433 of file mssql_driver.php.

Referenced by count_all().

00434         {
00435                 if (is_array($item))
00436                 {
00437                         $escaped_array = array();
00438 
00439                         foreach($item as $k=>$v)
00440                         {
00441                                 $escaped_array[$this->_protect_identifiers($k)] = $this->_protect_identifiers($v, $first_word_only);
00442                         }
00443 
00444                         return $escaped_array;
00445                 }       
00446 
00447                 // This function may get "item1 item2" as a string, and so
00448                 // we may need ""item1" "item2"" and not ""item1 item2""
00449                 if (ctype_alnum($item) === FALSE)
00450                 {
00451                         if (strpos($item, '.') !== FALSE)
00452                         {
00453                                 $aliased_tables = implode(".",$this->ar_aliased_tables).'.';
00454                                 $table_name =  substr($item, 0, strpos($item, '.')+1);
00455                                 $item = (strpos($aliased_tables, $table_name) !== FALSE) ? $item = $item : $this->dbprefix.$item;
00456                         }
00457 
00458                         // This function may get "field >= 1", and need it to return ""field" >= 1"
00459                         $lbound = ($first_word_only === TRUE) ? '' : '|\s|\(';
00460 
00461                         $item = preg_replace('/(^'.$lbound.')([\w\d\-\_]+?)(\s|\)|$)/iS', '$1"$2"$3', $item);
00462                 }
00463                 else
00464                 {
00465                         return "\"{$item}\"";
00466                 }
00467 
00468                 $exceptions = array('AS', '/', '-', '%', '+', '*', 'OR', 'IS');
00469                 
00470                 foreach ($exceptions as $exception)
00471                 {
00472                 
00473                         if (stristr($item, " \"{$exception}\" ") !== FALSE)
00474                         {
00475                                 $item = preg_replace('/ "('.preg_quote($exception).')" /i', ' $1 ', $item);
00476                         }
00477                 }
00478                 return $item;
00479         }

Here is the caller graph for this function:

CI_DB_mssql_driver::_truncate ( table  ) 

Truncate statement.

Generates a platform-specific truncate string from the supplied data If the database does not support the truncate() command This function maps to "DELETE FROM table"

public

Parameters:
string the table name
Returns:
string

Definition at line 568 of file mssql_driver.php.

00569         {
00570                 return "TRUNCATE ".$this->_escape_table($table);
00571         }

CI_DB_mssql_driver::_update ( table,
values,
where,
orderby = array(),
limit = FALSE 
)

Update statement.

Generates a platform-specific update string from the supplied data

public

Parameters:
string the table name
array the update data
array the where clause
array the orderby clause
array the limit clause
Returns:
string

Definition at line 536 of file mssql_driver.php.

00537         {
00538                 foreach($values as $key => $val)
00539                 {
00540                         $valstr[] = $key." = ".$val;
00541                 }
00542                 
00543                 $limit = ( ! $limit) ? '' : ' LIMIT '.$limit;
00544                 
00545                 $orderby = (count($orderby) >= 1)?' ORDER BY '.implode(", ", $orderby):'';
00546         
00547                 $sql = "UPDATE ".$this->_escape_table($table)." SET ".implode(', ', $valstr);
00548                 $sql .= ($where != '' AND count($where) >=1) ? " WHERE ".implode(" ", $where) : '';
00549                 $sql .= $orderby.$limit;
00550                 
00551                 return $sql;
00552         }

CI_DB_mssql_driver::_version (  ) 

Version number query string.

public

Returns:
string

Definition at line 278 of file mssql_driver.php.

00279         {
00280                 return "SELECT @@VERSION AS ver";
00281         }

CI_DB_mssql_driver::affected_rows (  ) 

Affected Rows.

public

Returns:
integer

Definition at line 228 of file mssql_driver.php.

00229         {
00230                 return @mssql_rows_affected($this->conn_id);
00231         }

CI_DB_mssql_driver::count_all ( table = ''  ) 

"Count All" query

Generates a platform-specific query string that counts all records in the specified database

public

Parameters:
string 
Returns:
string

Definition at line 295 of file mssql_driver.php.

References _protect_identifiers().

00296         {
00297                 if ($table == '')
00298                         return '0';
00299         
00300                 $query = $this->query($this->_count_string . $this->_protect_identifiers('numrows'). " FROM " . $this->_protect_identifiers($this->dbprefix.$table));
00301 
00302                 if ($query->num_rows() == 0)
00303                         return '0';
00304 
00305                 $row = $query->row();
00306                 return $row->numrows;
00307         }

Here is the call graph for this function:

CI_DB_mssql_driver::db_connect (  ) 

Non-persistent database connection.

private called by the base class

Returns:
resource

Definition at line 47 of file mssql_driver.php.

00048         {
00049                 return @mssql_connect($this->hostname, $this->username, $this->password);
00050         }

CI_DB_mssql_driver::db_pconnect (  ) 

Persistent database connection.

private called by the base class

Returns:
resource

Definition at line 60 of file mssql_driver.php.

00061         {
00062                 return @mssql_pconnect($this->hostname, $this->username, $this->password);
00063         }

CI_DB_mssql_driver::db_select (  ) 

Select the database.

private called by the base class

Returns:
resource

Definition at line 73 of file mssql_driver.php.

00074         {
00075                 return @mssql_select_db($this->database, $this->conn_id);
00076         }

CI_DB_mssql_driver::db_set_charset ( charset,
collation 
)

Set client character set.

public

Parameters:
string 
string 
Returns:
resource

Definition at line 88 of file mssql_driver.php.

00089         {
00090                 // TODO - add support if needed
00091                 return TRUE;
00092         }

CI_DB_mssql_driver::escape_str ( str  ) 

Escape String.

public

Parameters:
string 
Returns:
string

Definition at line 214 of file mssql_driver.php.

00215         {       
00216                 // Escape single quotes
00217                 return str_replace("'", "''", $str);
00218         }

CI_DB_mssql_driver::insert_id (  ) 

Insert ID.

Returns the last id created in the Identity column.

public

Returns:
integer

Definition at line 243 of file mssql_driver.php.

References _parse_major_version().

00244         {
00245                 $ver = self::_parse_major_version($this->version());
00246                 $sql = ($ver >= 8 ? "SELECT SCOPE_IDENTITY() AS last_id" : "SELECT @@IDENTITY AS last_id");
00247                 $query = $this->query($sql);
00248                 $row = $query->row();
00249                 return $row->last_id;
00250         }

Here is the call graph for this function:

CI_DB_mssql_driver::trans_begin ( test_mode = FALSE  ) 

Begin Transaction.

public

Returns:
bool

Definition at line 133 of file mssql_driver.php.

00134         {
00135                 if ( ! $this->trans_enabled)
00136                 {
00137                         return TRUE;
00138                 }
00139                 
00140                 // When transactions are nested we only begin/commit/rollback the outermost ones
00141                 if ($this->_trans_depth > 0)
00142                 {
00143                         return TRUE;
00144                 }
00145 
00146                 // Reset the transaction failure flag.
00147                 // If the $test_mode flag is set to TRUE transactions will be rolled back
00148                 // even if the queries produce a successful result.
00149                 $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
00150 
00151                 $this->simple_query('BEGIN TRAN');
00152                 return TRUE;
00153         }

CI_DB_mssql_driver::trans_commit (  ) 

Commit Transaction.

public

Returns:
bool

Definition at line 163 of file mssql_driver.php.

00164         {
00165                 if ( ! $this->trans_enabled)
00166                 {
00167                         return TRUE;
00168                 }
00169 
00170                 // When transactions are nested we only begin/commit/rollback the outermost ones
00171                 if ($this->_trans_depth > 0)
00172                 {
00173                         return TRUE;
00174                 }
00175 
00176                 $this->simple_query('COMMIT TRAN');
00177                 return TRUE;
00178         }

CI_DB_mssql_driver::trans_rollback (  ) 

Rollback Transaction.

public

Returns:
bool

Definition at line 188 of file mssql_driver.php.

00189         {
00190                 if ( ! $this->trans_enabled)
00191                 {
00192                         return TRUE;
00193                 }
00194 
00195                 // When transactions are nested we only begin/commit/rollback the outermost ones
00196                 if ($this->_trans_depth > 0)
00197                 {
00198                         return TRUE;
00199                 }
00200 
00201                 $this->simple_query('ROLLBACK TRAN');
00202                 return TRUE;
00203         }


Member Data Documentation

CI_DB_mssql_driver::$_count_string = "SELECT COUNT(*) AS "

The syntax to count rows is slightly different across different database engines, so this string appears in each driver and is used for the count_all() and count_all_results() functions.

Definition at line 38 of file mssql_driver.php.

CI_DB_mssql_driver::$_random_keyword = ' ASC'

Definition at line 39 of file mssql_driver.php.


The documentation for this class was generated from the following file: