Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Pgsql.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
28 
29 
40 {
41 
47  protected $_pdoType = 'pgsql';
48 
60  protected $_numericDataTypes = array(
64  'INTEGER' => Zend_Db::INT_TYPE,
65  'SERIAL' => Zend_Db::INT_TYPE,
66  'SMALLINT' => Zend_Db::INT_TYPE,
67  'BIGINT' => Zend_Db::BIGINT_TYPE,
68  'BIGSERIAL' => Zend_Db::BIGINT_TYPE,
69  'DECIMAL' => Zend_Db::FLOAT_TYPE,
70  'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
71  'NUMERIC' => Zend_Db::FLOAT_TYPE,
72  'REAL' => Zend_Db::FLOAT_TYPE
73  );
74 
81  protected function _connect()
82  {
83  if ($this->_connection) {
84  return;
85  }
86 
87  parent::_connect();
88 
89  if (!empty($this->_config['charset'])) {
90  $sql = "SET NAMES '" . $this->_config['charset'] . "'";
91  $this->_connection->exec($sql);
92  }
93  }
94 
100  public function listTables()
101  {
102  // @todo use a better query with joins instead of subqueries
103  $sql = "SELECT c.relname AS table_name "
104  . "FROM pg_class c, pg_user u "
105  . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
106  . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
107  . "AND c.relname !~ '^(pg_|sql_)' "
108  . "UNION "
109  . "SELECT c.relname AS table_name "
110  . "FROM pg_class c "
111  . "WHERE c.relkind = 'r' "
112  . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
113  . "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) "
114  . "AND c.relname !~ '^pg_'";
115 
116  return $this->fetchCol($sql);
117  }
118 
149  public function describeTable($tableName, $schemaName = null)
150  {
151  $sql = "SELECT
152  a.attnum,
153  n.nspname,
154  c.relname,
155  a.attname AS colname,
156  t.typname AS type,
157  a.atttypmod,
158  FORMAT_TYPE(a.atttypid, a.atttypmod) AS complete_type,
159  d.adsrc AS default_value,
160  a.attnotnull AS notnull,
161  a.attlen AS length,
162  co.contype,
163  ARRAY_TO_STRING(co.conkey, ',') AS conkey
164  FROM pg_attribute AS a
165  JOIN pg_class AS c ON a.attrelid = c.oid
166  JOIN pg_namespace AS n ON c.relnamespace = n.oid
167  JOIN pg_type AS t ON a.atttypid = t.oid
168  LEFT OUTER JOIN pg_constraint AS co ON (co.conrelid = c.oid
169  AND a.attnum = ANY(co.conkey) AND co.contype = 'p')
170  LEFT OUTER JOIN pg_attrdef AS d ON d.adrelid = c.oid AND d.adnum = a.attnum
171  WHERE a.attnum > 0 AND c.relname = ".$this->quote($tableName);
172  if ($schemaName) {
173  $sql .= " AND n.nspname = ".$this->quote($schemaName);
174  }
175  $sql .= ' ORDER BY a.attnum';
176 
177  $stmt = $this->query($sql);
178 
179  // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
180  $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
181 
182  $attnum = 0;
183  $nspname = 1;
184  $relname = 2;
185  $colname = 3;
186  $type = 4;
187  $atttypemod = 5;
188  $complete_type = 6;
189  $default_value = 7;
190  $notnull = 8;
191  $length = 9;
192  $contype = 10;
193  $conkey = 11;
194 
195  $desc = array();
196  foreach ($result as $key => $row) {
197  $defaultValue = $row[$default_value];
198  if ($row[$type] == 'varchar' || $row[$type] == 'bpchar' ) {
199  if (preg_match('/character(?: varying)?(?:\((\d+)\))?/', $row[$complete_type], $matches)) {
200  if (isset($matches[1])) {
201  $row[$length] = $matches[1];
202  } else {
203  $row[$length] = null; // unlimited
204  }
205  }
206  if (preg_match("/^'(.*?)'::(?:character varying|bpchar)$/", $defaultValue, $matches)) {
207  $defaultValue = $matches[1];
208  }
209  }
210  list($primary, $primaryPosition, $identity) = array(false, null, false);
211  if ($row[$contype] == 'p') {
212  $primary = true;
213  $primaryPosition = array_search($row[$attnum], explode(',', $row[$conkey])) + 1;
214  $identity = (bool) (preg_match('/^nextval/', $row[$default_value]));
215  }
216  $desc[$this->foldCase($row[$colname])] = array(
217  'SCHEMA_NAME' => $this->foldCase($row[$nspname]),
218  'TABLE_NAME' => $this->foldCase($row[$relname]),
219  'COLUMN_NAME' => $this->foldCase($row[$colname]),
220  'COLUMN_POSITION' => $row[$attnum],
221  'DATA_TYPE' => $row[$type],
222  'DEFAULT' => $defaultValue,
223  'NULLABLE' => (bool) ($row[$notnull] != 't'),
224  'LENGTH' => $row[$length],
225  'SCALE' => null, // @todo
226  'PRECISION' => null, // @todo
227  'UNSIGNED' => null, // @todo
228  'PRIMARY' => $primary,
229  'PRIMARY_POSITION' => $primaryPosition,
230  'IDENTITY' => $identity
231  );
232  }
233  return $desc;
234  }
235 
236 
245  public function limit($sql, $count, $offset = 0)
246  {
247  $count = intval($count);
248  if ($count <= 0) {
252  #require_once 'Zend/Db/Adapter/Exception.php';
253  throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
254  }
255 
256  $offset = intval($offset);
257  if ($offset < 0) {
261  #require_once 'Zend/Db/Adapter/Exception.php';
262  throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
263  }
264 
265  $sql .= " LIMIT $count";
266  if ($offset > 0) {
267  $sql .= " OFFSET $offset";
268  }
269 
270  return $sql;
271  }
272 
281  public function lastSequenceId($sequenceName)
282  {
283  $this->_connect();
284  $sequenceName = str_replace($this->getQuoteIdentifierSymbol(), '', (string) $sequenceName);
285  $value = $this->fetchOne("SELECT CURRVAL("
286  . $this->quote($this->quoteIdentifier($sequenceName, true))
287  . ")");
288  return $value;
289  }
290 
299  public function nextSequenceId($sequenceName)
300  {
301  $this->_connect();
302  $sequenceName = str_replace($this->getQuoteIdentifierSymbol(), '', (string) $sequenceName);
303  $value = $this->fetchOne("SELECT NEXTVAL("
304  . $this->quote($this->quoteIdentifier($sequenceName, true))
305  . ")");
306  return $value;
307  }
308 
323  public function lastInsertId($tableName = null, $primaryKey = null)
324  {
325  if ($tableName !== null) {
326  $sequenceName = $tableName;
327  if ($primaryKey) {
328  $sequenceName .= "_$primaryKey";
329  }
330  $sequenceName .= '_seq';
331  return $this->lastSequenceId($sequenceName);
332  }
333  return $this->_connection->lastInsertId($tableName);
334  }
335 
336 }
$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
quote($value, $type=null)
Definition: Abstract.php:859
const INT_TYPE
Definition: Db.php:68
$count
Definition: recent.phtml:13
const FLOAT_TYPE
Definition: Db.php:70
$type
Definition: item.phtml:13
describeTable($tableName, $schemaName=null)
Definition: Pgsql.php:149
$value
Definition: gender.phtml:16
const FETCH_NUM
Definition: Db.php:153
limit($sql, $count, $offset=0)
Definition: Pgsql.php:245
lastInsertId($tableName=null, $primaryKey=null)
Definition: Pgsql.php:323
nextSequenceId($sequenceName)
Definition: Pgsql.php:299
quoteIdentifier($ident, $auto=false)
Definition: Abstract.php:959
lastSequenceId($sequenceName)
Definition: Pgsql.php:281
query($sql, $bind=array())
Definition: Abstract.php:221