Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Db2.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Db/Adapter/Pdo/Ibm.php';
26 
28 #require_once 'Zend/Db/Statement/Pdo/Ibm.php';
29 
30 
39 {
43  protected $_adapter = null;
44 
53  public function __construct($adapter)
54  {
55  $this->_adapter = $adapter;
56  }
57 
63  public function listTables()
64  {
65  $sql = "SELECT tabname "
66  . "FROM SYSCAT.TABLES ";
67  return $this->_adapter->fetchCol($sql);
68  }
69 
77  public function describeTable($tableName, $schemaName = null)
78  {
79  $sql = "SELECT DISTINCT c.tabschema, c.tabname, c.colname, c.colno,
80  c.typename, c.default, c.nulls, c.length, c.scale,
81  c.identity, tc.type AS tabconsttype, k.colseq
82  FROM syscat.columns c
83  LEFT JOIN (syscat.keycoluse k JOIN syscat.tabconst tc
84  ON (k.tabschema = tc.tabschema
85  AND k.tabname = tc.tabname
86  AND tc.type = 'P'))
87  ON (c.tabschema = k.tabschema
88  AND c.tabname = k.tabname
89  AND c.colname = k.colname)
90  WHERE "
91  . $this->_adapter->quoteInto('UPPER(c.tabname) = UPPER(?)', $tableName);
92  if ($schemaName) {
93  $sql .= $this->_adapter->quoteInto(' AND UPPER(c.tabschema) = UPPER(?)', $schemaName);
94  }
95  $sql .= " ORDER BY c.colno";
96 
97  $desc = array();
98  $stmt = $this->_adapter->query($sql);
99 
103  $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
104 
109  $tabschema = 0;
110  $tabname = 1;
111  $colname = 2;
112  $colno = 3;
113  $typename = 4;
114  $default = 5;
115  $nulls = 6;
116  $length = 7;
117  $scale = 8;
118  $identityCol = 9;
119  $tabconstype = 10;
120  $colseq = 11;
121 
122  foreach ($result as $key => $row) {
123  list ($primary, $primaryPosition, $identity) = array(false, null, false);
124  if ($row[$tabconstype] == 'P') {
125  $primary = true;
126  $primaryPosition = $row[$colseq];
127  }
132  if ($row[$identityCol] == 'Y') {
133  $identity = true;
134  }
135 
136  $desc[$this->_adapter->foldCase($row[$colname])] = array(
137  'SCHEMA_NAME' => $this->_adapter->foldCase($row[$tabschema]),
138  'TABLE_NAME' => $this->_adapter->foldCase($row[$tabname]),
139  'COLUMN_NAME' => $this->_adapter->foldCase($row[$colname]),
140  'COLUMN_POSITION' => $row[$colno]+1,
141  'DATA_TYPE' => $row[$typename],
142  'DEFAULT' => $row[$default],
143  'NULLABLE' => (bool) ($row[$nulls] == 'Y'),
144  'LENGTH' => $row[$length],
145  'SCALE' => $row[$scale],
146  'PRECISION' => ($row[$typename] == 'DECIMAL' ? $row[$length] : 0),
147  'UNSIGNED' => false,
148  'PRIMARY' => $primary,
149  'PRIMARY_POSITION' => $primaryPosition,
150  'IDENTITY' => $identity
151  );
152  }
153 
154  return $desc;
155  }
156 
166  public function limit($sql, $count, $offset = 0)
167  {
168  $count = intval($count);
169  if ($count < 0) {
171  #require_once 'Zend/Db/Adapter/Exception.php';
172  throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
173  } else {
174  $offset = intval($offset);
175  if ($offset < 0) {
177  #require_once 'Zend/Db/Adapter/Exception.php';
178  throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
179  }
180 
181  if ($offset == 0 && $count > 0) {
182  $limit_sql = $sql . " FETCH FIRST $count ROWS ONLY";
183  return $limit_sql;
184  }
191  $limit_sql = "SELECT z2.*
192  FROM (
193  SELECT ROW_NUMBER() OVER() AS \"ZEND_DB_ROWNUM\", z1.*
194  FROM (
195  " . $sql . "
196  ) z1
197  ) z2
198  WHERE z2.zend_db_rownum BETWEEN " . ($offset+1) . " AND " . ($offset+$count);
199  }
200  return $limit_sql;
201  }
202 
209  public function lastSequenceId($sequenceName)
210  {
211  $sql = 'SELECT PREVVAL FOR '.$this->_adapter->quoteIdentifier($sequenceName).' AS VAL FROM SYSIBM.SYSDUMMY1';
212  $value = $this->_adapter->fetchOne($sql);
213  return $value;
214  }
215 
222  public function nextSequenceId($sequenceName)
223  {
224  $sql = 'SELECT NEXTVAL FOR '.$this->_adapter->quoteIdentifier($sequenceName).' AS VAL FROM SYSIBM.SYSDUMMY1';
225  $value = $this->_adapter->fetchOne($sql);
226  return $value;
227  }
228 }
$tableName
Definition: trigger.php:13
$count
Definition: recent.phtml:13
$adapter
Definition: webapi_user.php:16
$value
Definition: gender.phtml:16
const FETCH_NUM
Definition: Db.php:153
__construct($adapter)
Definition: Db2.php:53
limit($sql, $count, $offset=0)
Definition: Db2.php:166
describeTable($tableName, $schemaName=null)
Definition: Db2.php:77
nextSequenceId($sequenceName)
Definition: Db2.php:222
lastSequenceId($sequenceName)
Definition: Db2.php:209