Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Zend_Db_Adapter_Pdo_Mssql Class Reference
Inheritance diagram for Zend_Db_Adapter_Pdo_Mssql:
Zend_Db_Adapter_Pdo_Abstract Zend_Db_Adapter_Abstract

Public Member Functions

 listTables ()
 
 describeTable ($tableName, $schemaName=null)
 
 limit ($sql, $count, $offset=0)
 
 lastInsertId ($tableName=null, $primaryKey=null)
 
 getServerVersion ()
 
- Public Member Functions inherited from Zend_Db_Adapter_Pdo_Abstract
 isConnected ()
 
 closeConnection ()
 
 prepare ($sql)
 
 lastInsertId ($tableName=null, $primaryKey=null)
 
 query ($sql, $bind=array())
 
 exec ($sql)
 
 setFetchMode ($mode)
 
 supportsParameters ($type)
 
 getServerVersion ()
 
- Public Member Functions inherited from Zend_Db_Adapter_Abstract
 __construct ($config)
 
 getConnection ()
 
 getConfig ()
 
 setProfiler ($profiler)
 
 getProfiler ()
 
 getStatementClass ()
 
 setStatementClass ($class)
 
 query ($sql, $bind=array())
 
 beginTransaction ()
 
 commit ()
 
 rollBack ()
 
 insert ($table, array $bind)
 
 update ($table, array $bind, $where='')
 
 delete ($table, $where='')
 
 select ()
 
 getFetchMode ()
 
 fetchAll ($sql, $bind=array(), $fetchMode=null)
 
 fetchRow ($sql, $bind=array(), $fetchMode=null)
 
 fetchAssoc ($sql, $bind=array())
 
 fetchCol ($sql, $bind=array())
 
 fetchPairs ($sql, $bind=array())
 
 fetchOne ($sql, $bind=array())
 
 quote ($value, $type=null)
 
 quoteInto ($text, $value, $type=null, $count=null)
 
 quoteIdentifier ($ident, $auto=false)
 
 quoteColumnAs ($ident, $alias, $auto=false)
 
 quoteTableAs ($ident, $alias=null, $auto=false)
 
 getQuoteIdentifierSymbol ()
 
 lastSequenceId ($sequenceName)
 
 nextSequenceId ($sequenceName)
 
 foldCase ($key)
 
 __sleep ()
 
 __wakeup ()
 
 listTables ()
 
 describeTable ($tableName, $schemaName=null)
 
 isConnected ()
 
 closeConnection ()
 
 prepare ($sql)
 
 lastInsertId ($tableName=null, $primaryKey=null)
 
 setFetchMode ($mode)
 
 limit ($sql, $count, $offset=0)
 
 supportsParameters ($type)
 
 getServerVersion ()
 

Protected Member Functions

 _dsn ()
 
 _connect ()
 
 _beginTransaction ()
 
 _commit ()
 
 _rollBack ()
 
 _quote ($value)
 
- Protected Member Functions inherited from Zend_Db_Adapter_Pdo_Abstract
 _dsn ()
 
 _connect ()
 
 _quote ($value)
 
 _beginTransaction ()
 
 _commit ()
 
 _rollBack ()
 
- Protected Member Functions inherited from Zend_Db_Adapter_Abstract
 _checkRequiredOptions (array $config)
 
 _whereExpr ($where)
 
 _quote ($value)
 
 _quoteIdentifierAs ($ident, $alias=null, $auto=false, $as=' AS ')
 
 _quoteIdentifier ($value, $auto=false)
 
 _connect ()
 
 _beginTransaction ()
 
 _commit ()
 
 _rollBack ()
 

Protected Attributes

 $_pdoType = 'mssql'
 
 $_numericDataTypes
 
- Protected Attributes inherited from Zend_Db_Adapter_Pdo_Abstract
 $_defaultStmtClass = 'Zend_Db_Statement_Pdo'
 
- Protected Attributes inherited from Zend_Db_Adapter_Abstract
 $_config = array()
 
 $_fetchMode = Zend_Db::FETCH_ASSOC
 
 $_profiler
 
 $_defaultStmtClass = 'Zend_Db_Statement'
 
 $_defaultProfilerClass = 'Zend_Db_Profiler'
 
 $_connection = null
 
 $_caseFolding = Zend_Db::CASE_NATURAL
 
 $_autoQuoteIdentifiers = true
 
 $_numericDataTypes
 
 $_allowSerialization = true
 
 $_autoReconnectOnUnserialize = false
 

Detailed Description

Definition at line 39 of file Mssql.php.

Member Function Documentation

◆ _beginTransaction()

_beginTransaction ( )
protected

Begin a transaction.

It is necessary to override the abstract PDO transaction functions here, as the PDO driver for MSSQL does not support transactions.

Definition at line 147 of file Mssql.php.

148  {
149  $this->_connect();
150  $this->_connection->exec('BEGIN TRANSACTION');
151  return true;
152  }

◆ _commit()

_commit ( )
protected

Commit a transaction.

It is necessary to override the abstract PDO transaction functions here, as the PDO driver for MSSQL does not support transactions.

Definition at line 160 of file Mssql.php.

161  {
162  $this->_connect();
163  $this->_connection->exec('COMMIT TRANSACTION');
164  return true;
165  }

◆ _connect()

_connect ( )
protected
Returns
void

Definition at line 132 of file Mssql.php.

133  {
134  if ($this->_connection) {
135  return;
136  }
137  parent::_connect();
138  $this->_connection->exec('SET QUOTED_IDENTIFIER ON');
139  }

◆ _dsn()

_dsn ( )
protected

Creates a PDO DSN for the adapter from $this->_config settings.

Returns
string

Definition at line 80 of file Mssql.php.

81  {
82  // baseline of DSN parts
83  $dsn = $this->_config;
84 
85  // don't pass the username and password in the DSN
86  unset($dsn['username']);
87  unset($dsn['password']);
88  unset($dsn['options']);
89  unset($dsn['persistent']);
90  unset($dsn['driver_options']);
91 
92  if (isset($dsn['port'])) {
93  $seperator = ':';
94  if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
95  $seperator = ',';
96  }
97  $dsn['host'] .= $seperator . $dsn['port'];
98  unset($dsn['port']);
99  }
100 
101  // this driver supports multiple DSN prefixes
102  // @see http://www.php.net/manual/en/ref.pdo-dblib.connection.php
103  if (isset($dsn['pdoType'])) {
104  switch (strtolower($dsn['pdoType'])) {
105  case 'freetds':
106  case 'sybase':
107  $this->_pdoType = 'sybase';
108  break;
109  case 'mssql':
110  $this->_pdoType = 'mssql';
111  break;
112  case 'dblib':
113  default:
114  $this->_pdoType = 'dblib';
115  break;
116  }
117  unset($dsn['pdoType']);
118  }
119 
120  // use all remaining parts in the DSN
121  foreach ($dsn as $key => $val) {
122  $dsn[$key] = "$key=$val";
123  }
124 
125  $dsn = $this->_pdoType . ':' . implode(';', $dsn);
126  return $dsn;
127  }

◆ _quote()

_quote (   $value)
protected

Quote a raw string.

Parameters
string$valueRaw string
Returns
string Quoted string

Definition at line 430 of file Mssql.php.

431  {
432  if (!is_int($value) && !is_float($value)) {
433  // Fix for null-byte injection
434  $value = addcslashes($value, "\000\032");
435  }
436  return parent::_quote($value);
437  }
$value
Definition: gender.phtml:16

◆ _rollBack()

_rollBack ( )
protected

Roll-back a transaction.

It is necessary to override the abstract PDO transaction functions here, as the PDO driver for MSSQL does not support transactions.

Definition at line 173 of file Mssql.php.

173  {
174  $this->_connect();
175  $this->_connection->exec('ROLLBACK TRANSACTION');
176  return true;
177  }

◆ describeTable()

describeTable (   $tableName,
  $schemaName = null 
)

Returns the column descriptions for a table.

The return value is an associative array keyed by the column name, as returned by the RDBMS.

The value of each array element is an associative array with the following keys:

SCHEMA_NAME => string; name of database or schema TABLE_NAME => string; COLUMN_NAME => string; column name COLUMN_POSITION => number; ordinal position of column in table DATA_TYPE => string; SQL datatype name of column DEFAULT => string; default expression of column, null if none NULLABLE => boolean; true if column can have nulls LENGTH => number; length of CHAR/VARCHAR SCALE => number; scale of NUMERIC/DECIMAL PRECISION => number; precision of NUMERIC/DECIMAL UNSIGNED => boolean; unsigned property of an integer type PRIMARY => boolean; true if column is part of the primary key PRIMARY_POSITION => integer; position of column in primary key PRIMARY_AUTO => integer; position of auto-generated column in primary key

Todo:

Discover column primary key position.

Discover integer unsigned property.

Parameters
string$tableName
string$schemaNameOPTIONAL
Returns
array

Discover metadata information about this table.

Discover primary key column(s) for this table.

Definition at line 221 of file Mssql.php.

222  {
223  if ($schemaName != null) {
224  if (strpos($schemaName, '.') !== false) {
225  $result = explode('.', $schemaName);
226  $schemaName = $result[1];
227  }
228  }
232  $sql = "exec sp_columns @table_name = " . $this->quoteIdentifier($tableName, true);
233  if ($schemaName != null) {
234  $sql .= ", @table_owner = " . $this->quoteIdentifier($schemaName, true);
235  }
236 
237  $stmt = $this->query($sql);
238  $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
239 
240  $table_name = 2;
241  $column_name = 3;
242  $type_name = 5;
243  $precision = 6;
244  $length = 7;
245  $scale = 8;
246  $nullable = 10;
247  $column_def = 12;
248  $column_position = 16;
249 
253  $sql = "exec sp_pkeys @table_name = " . $this->quoteIdentifier($tableName, true);
254  if ($schemaName != null) {
255  $sql .= ", @table_owner = " . $this->quoteIdentifier($schemaName, true);
256  }
257 
258  $stmt = $this->query($sql);
259  $primaryKeysResult = $stmt->fetchAll(Zend_Db::FETCH_NUM);
260  $primaryKeyColumn = array();
261  $pkey_column_name = 3;
262  $pkey_key_seq = 4;
263  foreach ($primaryKeysResult as $pkeysRow) {
264  $primaryKeyColumn[$pkeysRow[$pkey_column_name]] = $pkeysRow[$pkey_key_seq];
265  }
266 
267  $desc = array();
268  $p = 1;
269  foreach ($result as $key => $row) {
270  $identity = false;
271  $words = explode(' ', $row[$type_name], 2);
272  if (isset($words[0])) {
273  $type = $words[0];
274  if (isset($words[1])) {
275  $identity = (bool) preg_match('/identity/', $words[1]);
276  }
277  }
278 
279  $isPrimary = array_key_exists($row[$column_name], $primaryKeyColumn);
280  if ($isPrimary) {
281  $primaryPosition = $primaryKeyColumn[$row[$column_name]];
282  } else {
283  $primaryPosition = null;
284  }
285 
286  $desc[$this->foldCase($row[$column_name])] = array(
287  'SCHEMA_NAME' => null, // @todo
288  'TABLE_NAME' => $this->foldCase($row[$table_name]),
289  'COLUMN_NAME' => $this->foldCase($row[$column_name]),
290  'COLUMN_POSITION' => (int) $row[$column_position],
291  'DATA_TYPE' => $type,
292  'DEFAULT' => $row[$column_def],
293  'NULLABLE' => (bool) $row[$nullable],
294  'LENGTH' => $row[$length],
295  'SCALE' => $row[$scale],
296  'PRECISION' => $row[$precision],
297  'UNSIGNED' => null, // @todo
298  'PRIMARY' => $isPrimary,
299  'PRIMARY_POSITION' => $primaryPosition,
300  'IDENTITY' => $identity
301  );
302  }
303  return $desc;
304  }
$tableName
Definition: trigger.php:13
$type
Definition: item.phtml:13
const FETCH_NUM
Definition: Db.php:153
quoteIdentifier($ident, $auto=false)
Definition: Abstract.php:959
query($sql, $bind=array())
Definition: Abstract.php:221

◆ getServerVersion()

getServerVersion ( )

Retrieve server version in PHP style Pdo_Mssql doesn't support getAttribute(PDO::ATTR_SERVER_VERSION)

Returns
string

Definition at line 410 of file Mssql.php.

411  {
412  try {
413  $stmt = $this->query("SELECT CAST(SERVERPROPERTY('productversion') AS VARCHAR)");
414  $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
415  if (count($result)) {
416  return $result[0][0];
417  }
418  return null;
419  } catch (PDOException $e) {
420  return null;
421  }
422  }
const FETCH_NUM
Definition: Db.php:153
query($sql, $bind=array())
Definition: Abstract.php:221

◆ lastInsertId()

lastInsertId (   $tableName = null,
  $primaryKey = null 
)

Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.

As a convention, on RDBMS brands that support sequences (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence from the arguments and returns the last id generated by that sequence. On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method returns the last value generated for such a column, and the table name argument is disregarded.

Microsoft SQL Server does not support sequences, so the arguments to this method are ignored.

Parameters
string$tableNameOPTIONAL Name of table.
string$primaryKeyOPTIONAL Name of primary key column.
Returns
string
Exceptions
Zend_Db_Adapter_Exception

Definition at line 399 of file Mssql.php.

400  {
401  $sql = 'SELECT SCOPE_IDENTITY()';
402  return (int)$this->fetchOne($sql);
403  }
fetchOne($sql, $bind=array())
Definition: Abstract.php:826

◆ limit()

limit (   $sql,
  $count,
  $offset = 0 
)

Adds an adapter-specific LIMIT clause to the SELECT statement.

string $sql integer $count integer $offset OPTIONAL Zend_Db_Adapter_Exception string

See also
Zend_Db_Adapter_Exception
Zend_Db_Adapter_Exception

Definition at line 317 of file Mssql.php.

318  {
319  $count = intval($count);
320  if ($count <= 0) {
322  #require_once 'Zend/Db/Adapter/Exception.php';
323  throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
324  }
325 
326  $offset = intval($offset);
327  if ($offset < 0) {
329  #require_once 'Zend/Db/Adapter/Exception.php';
330  throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
331  }
332 
333  $sql = preg_replace(
334  '/^SELECT\s+(DISTINCT\s)?/i',
335  'SELECT $1TOP ' . ($count+$offset) . ' ',
336  $sql
337  );
338 
339  if ($offset > 0) {
340  $orderby = stristr($sql, 'ORDER BY');
341 
342  if ($orderby !== false) {
343  $orderParts = explode(',', substr($orderby, 8));
344  $pregReplaceCount = null;
345  $orderbyInverseParts = array();
346  foreach ($orderParts as $orderPart) {
347  $orderPart = rtrim($orderPart);
348  $inv = preg_replace('/\s+desc$/i', ' ASC', $orderPart, 1, $pregReplaceCount);
349  if ($pregReplaceCount) {
350  $orderbyInverseParts[] = $inv;
351  continue;
352  }
353  $inv = preg_replace('/\s+asc$/i', ' DESC', $orderPart, 1, $pregReplaceCount);
354  if ($pregReplaceCount) {
355  $orderbyInverseParts[] = $inv;
356  continue;
357  } else {
358  $orderbyInverseParts[] = $orderPart . ' DESC';
359  }
360  }
361 
362  $orderbyInverse = 'ORDER BY ' . implode(', ', $orderbyInverseParts);
363  }
364 
365 
366 
367 
368  $sql = 'SELECT * FROM (SELECT TOP ' . $count . ' * FROM (' . $sql . ') AS inner_tbl';
369  if ($orderby !== false) {
370  $sql .= ' ' . $orderbyInverse . ' ';
371  }
372  $sql .= ') AS outer_tbl';
373  if ($orderby !== false) {
374  $sql .= ' ' . $orderby;
375  }
376  }
377 
378  return $sql;
379  }
$count
Definition: recent.phtml:13

◆ listTables()

listTables ( )

Returns a list of the tables in the database.

Returns
array

Definition at line 184 of file Mssql.php.

185  {
186  $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
187  return $this->fetchCol($sql);
188  }
fetchCol($sql, $bind=array())
Definition: Abstract.php:792

Field Documentation

◆ $_numericDataTypes

$_numericDataTypes
protected
Initial value:

Definition at line 59 of file Mssql.php.

◆ $_pdoType

$_pdoType = 'mssql'
protected

Definition at line 46 of file Mssql.php.


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