Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mssql.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
28 
29 
40 {
46  protected $_pdoType = 'mssql';
47 
59  protected $_numericDataTypes = array(
63  'INT' => Zend_Db::INT_TYPE,
64  'SMALLINT' => Zend_Db::INT_TYPE,
65  'TINYINT' => Zend_Db::INT_TYPE,
66  'BIGINT' => Zend_Db::BIGINT_TYPE,
67  'DECIMAL' => Zend_Db::FLOAT_TYPE,
68  'FLOAT' => Zend_Db::FLOAT_TYPE,
69  'MONEY' => Zend_Db::FLOAT_TYPE,
70  'NUMERIC' => Zend_Db::FLOAT_TYPE,
71  'REAL' => Zend_Db::FLOAT_TYPE,
72  'SMALLMONEY' => Zend_Db::FLOAT_TYPE
73  );
74 
80  protected function _dsn()
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  }
128 
132  protected function _connect()
133  {
134  if ($this->_connection) {
135  return;
136  }
137  parent::_connect();
138  $this->_connection->exec('SET QUOTED_IDENTIFIER ON');
139  }
140 
147  protected function _beginTransaction()
148  {
149  $this->_connect();
150  $this->_connection->exec('BEGIN TRANSACTION');
151  return true;
152  }
153 
160  protected function _commit()
161  {
162  $this->_connect();
163  $this->_connection->exec('COMMIT TRANSACTION');
164  return true;
165  }
166 
173  protected function _rollBack() {
174  $this->_connect();
175  $this->_connection->exec('ROLLBACK TRANSACTION');
176  return true;
177  }
178 
184  public function listTables()
185  {
186  $sql = "SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name";
187  return $this->fetchCol($sql);
188  }
189 
221  public function describeTable($tableName, $schemaName = null)
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  }
305 
317  public function limit($sql, $count, $offset = 0)
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  }
380 
399  public function lastInsertId($tableName = null, $primaryKey = null)
400  {
401  $sql = 'SELECT SCOPE_IDENTITY()';
402  return (int)$this->fetchOne($sql);
403  }
404 
410  public function getServerVersion()
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  }
423 
430  protected function _quote($value)
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  }
438 }
$tableName
Definition: trigger.php:13
fetchOne($sql, $bind=array())
Definition: Abstract.php:826
fetchCol($sql, $bind=array())
Definition: Abstract.php:792
const BIGINT_TYPE
Definition: Db.php:69
const INT_TYPE
Definition: Db.php:68
$count
Definition: recent.phtml:13
lastInsertId($tableName=null, $primaryKey=null)
Definition: Mssql.php:399
describeTable($tableName, $schemaName=null)
Definition: Mssql.php:221
const FLOAT_TYPE
Definition: Db.php:70
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
const FETCH_NUM
Definition: Db.php:153
limit($sql, $count, $offset=0)
Definition: Mssql.php:317
quoteIdentifier($ident, $auto=false)
Definition: Abstract.php:959
query($sql, $bind=array())
Definition: Abstract.php:221