PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Db/Adapter/Pdo/Pgsql.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 336 lines | 176 code | 25 blank | 135 comment | 19 complexity | 89bcdfe3accebf85df36ed456ef3978e MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Db
  17. * @subpackage Adapter
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Pgsql.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @see Zend_Db_Adapter_Pdo_Abstract
  24. */
  25. require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
  26. /**
  27. * Class for connecting to PostgreSQL databases and performing common operations.
  28. *
  29. * @category Zend
  30. * @package Zend_Db
  31. * @subpackage Adapter
  32. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Db_Adapter_Pdo_Pgsql extends Zend_Db_Adapter_Pdo_Abstract
  36. {
  37. /**
  38. * PDO type.
  39. *
  40. * @var string
  41. */
  42. protected $_pdoType = 'pgsql';
  43. /**
  44. * Keys are UPPERCASE SQL datatypes or the constants
  45. * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
  46. *
  47. * Values are:
  48. * 0 = 32-bit integer
  49. * 1 = 64-bit integer
  50. * 2 = float or decimal
  51. *
  52. * @var array Associative array of datatypes to values 0, 1, or 2.
  53. */
  54. protected $_numericDataTypes = array(
  55. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  56. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  57. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  58. 'INTEGER' => Zend_Db::INT_TYPE,
  59. 'SERIAL' => Zend_Db::INT_TYPE,
  60. 'SMALLINT' => Zend_Db::INT_TYPE,
  61. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  62. 'BIGSERIAL' => Zend_Db::BIGINT_TYPE,
  63. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  64. 'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
  65. 'NUMERIC' => Zend_Db::FLOAT_TYPE,
  66. 'REAL' => Zend_Db::FLOAT_TYPE
  67. );
  68. /**
  69. * Creates a PDO object and connects to the database.
  70. *
  71. * @return void
  72. * @throws Zend_Db_Adapter_Exception
  73. */
  74. protected function _connect()
  75. {
  76. if ($this->_connection) {
  77. return;
  78. }
  79. parent::_connect();
  80. if (!empty($this->_config['charset'])) {
  81. $sql = "SET NAMES '" . $this->_config['charset'] . "'";
  82. $this->_connection->exec($sql);
  83. }
  84. }
  85. /**
  86. * Returns a list of the tables in the database.
  87. *
  88. * @return array
  89. */
  90. public function listTables()
  91. {
  92. // @todo use a better query with joins instead of subqueries
  93. $sql = "SELECT c.relname AS table_name "
  94. . "FROM pg_class c, pg_user u "
  95. . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
  96. . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
  97. . "AND c.relname !~ '^(pg_|sql_)' "
  98. . "UNION "
  99. . "SELECT c.relname AS table_name "
  100. . "FROM pg_class c "
  101. . "WHERE c.relkind = 'r' "
  102. . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
  103. . "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) "
  104. . "AND c.relname !~ '^pg_'";
  105. return $this->fetchCol($sql);
  106. }
  107. /**
  108. * Returns the column descriptions for a table.
  109. *
  110. * The return value is an associative array keyed by the column name,
  111. * as returned by the RDBMS.
  112. *
  113. * The value of each array element is an associative array
  114. * with the following keys:
  115. *
  116. * SCHEMA_NAME => string; name of database or schema
  117. * TABLE_NAME => string;
  118. * COLUMN_NAME => string; column name
  119. * COLUMN_POSITION => number; ordinal position of column in table
  120. * DATA_TYPE => string; SQL datatype name of column
  121. * DEFAULT => string; default expression of column, null if none
  122. * NULLABLE => boolean; true if column can have nulls
  123. * LENGTH => number; length of CHAR/VARCHAR
  124. * SCALE => number; scale of NUMERIC/DECIMAL
  125. * PRECISION => number; precision of NUMERIC/DECIMAL
  126. * UNSIGNED => boolean; unsigned property of an integer type
  127. * PRIMARY => boolean; true if column is part of the primary key
  128. * PRIMARY_POSITION => integer; position of column in primary key
  129. * IDENTITY => integer; true if column is auto-generated with unique values
  130. *
  131. * @todo Discover integer unsigned property.
  132. *
  133. * @param string $tableName
  134. * @param string $schemaName OPTIONAL
  135. * @return array
  136. */
  137. public function describeTable($tableName, $schemaName = null)
  138. {
  139. $sql = "SELECT
  140. a.attnum,
  141. n.nspname,
  142. c.relname,
  143. a.attname AS colname,
  144. t.typname AS type,
  145. a.atttypmod,
  146. FORMAT_TYPE(a.atttypid, a.atttypmod) AS complete_type,
  147. d.adsrc AS default_value,
  148. a.attnotnull AS notnull,
  149. a.attlen AS length,
  150. co.contype,
  151. ARRAY_TO_STRING(co.conkey, ',') AS conkey
  152. FROM pg_attribute AS a
  153. JOIN pg_class AS c ON a.attrelid = c.oid
  154. JOIN pg_namespace AS n ON c.relnamespace = n.oid
  155. JOIN pg_type AS t ON a.atttypid = t.oid
  156. LEFT OUTER JOIN pg_constraint AS co ON (co.conrelid = c.oid
  157. AND a.attnum = ANY(co.conkey) AND co.contype = 'p')
  158. LEFT OUTER JOIN pg_attrdef AS d ON d.adrelid = c.oid AND d.adnum = a.attnum
  159. WHERE a.attnum > 0 AND c.relname = ".$this->quote($tableName);
  160. if ($schemaName) {
  161. $sql .= " AND n.nspname = ".$this->quote($schemaName);
  162. }
  163. $sql .= ' ORDER BY a.attnum';
  164. $stmt = $this->query($sql);
  165. // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
  166. $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  167. $attnum = 0;
  168. $nspname = 1;
  169. $relname = 2;
  170. $colname = 3;
  171. $type = 4;
  172. $atttypemod = 5;
  173. $complete_type = 6;
  174. $default_value = 7;
  175. $notnull = 8;
  176. $length = 9;
  177. $contype = 10;
  178. $conkey = 11;
  179. $desc = array();
  180. foreach ($result as $key => $row) {
  181. $defaultValue = $row[$default_value];
  182. if ($row[$type] == 'varchar' || $row[$type] == 'bpchar' ) {
  183. if (preg_match('/character(?: varying)?(?:\((\d+)\))?/', $row[$complete_type], $matches)) {
  184. if (isset($matches[1])) {
  185. $row[$length] = $matches[1];
  186. } else {
  187. $row[$length] = null; // unlimited
  188. }
  189. }
  190. if (preg_match("/^'(.*?)'::(?:character varying|bpchar)$/", $defaultValue, $matches)) {
  191. $defaultValue = $matches[1];
  192. }
  193. }
  194. list($primary, $primaryPosition, $identity) = array(false, null, false);
  195. if ($row[$contype] == 'p') {
  196. $primary = true;
  197. $primaryPosition = array_search($row[$attnum], explode(',', $row[$conkey])) + 1;
  198. $identity = (bool) (preg_match('/^nextval/', $row[$default_value]));
  199. }
  200. $desc[$this->foldCase($row[$colname])] = array(
  201. 'SCHEMA_NAME' => $this->foldCase($row[$nspname]),
  202. 'TABLE_NAME' => $this->foldCase($row[$relname]),
  203. 'COLUMN_NAME' => $this->foldCase($row[$colname]),
  204. 'COLUMN_POSITION' => $row[$attnum],
  205. 'DATA_TYPE' => $row[$type],
  206. 'DEFAULT' => $defaultValue,
  207. 'NULLABLE' => (bool) ($row[$notnull] != 't'),
  208. 'LENGTH' => $row[$length],
  209. 'SCALE' => null, // @todo
  210. 'PRECISION' => null, // @todo
  211. 'UNSIGNED' => null, // @todo
  212. 'PRIMARY' => $primary,
  213. 'PRIMARY_POSITION' => $primaryPosition,
  214. 'IDENTITY' => $identity
  215. );
  216. }
  217. return $desc;
  218. }
  219. /**
  220. * Adds an adapter-specific LIMIT clause to the SELECT statement.
  221. *
  222. * @param string $sql
  223. * @param integer $count
  224. * @param integer $offset OPTIONAL
  225. * @return string
  226. */
  227. public function limit($sql, $count, $offset = 0)
  228. {
  229. $count = intval($count);
  230. if ($count <= 0) {
  231. /**
  232. * @see Zend_Db_Adapter_Exception
  233. */
  234. require_once 'Zend/Db/Adapter/Exception.php';
  235. throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
  236. }
  237. $offset = intval($offset);
  238. if ($offset < 0) {
  239. /**
  240. * @see Zend_Db_Adapter_Exception
  241. */
  242. require_once 'Zend/Db/Adapter/Exception.php';
  243. throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
  244. }
  245. $sql .= " LIMIT $count";
  246. if ($offset > 0) {
  247. $sql .= " OFFSET $offset";
  248. }
  249. return $sql;
  250. }
  251. /**
  252. * Return the most recent value from the specified sequence in the database.
  253. * This is supported only on RDBMS brands that support sequences
  254. * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.
  255. *
  256. * @param string $sequenceName
  257. * @return string
  258. */
  259. public function lastSequenceId($sequenceName)
  260. {
  261. $this->_connect();
  262. $sequenceName = str_replace($this->getQuoteIdentifierSymbol(), '', (string) $sequenceName);
  263. $value = $this->fetchOne("SELECT CURRVAL("
  264. . $this->quote($this->quoteIdentifier($sequenceName, true))
  265. . ")");
  266. return $value;
  267. }
  268. /**
  269. * Generate a new value from the specified sequence in the database, and return it.
  270. * This is supported only on RDBMS brands that support sequences
  271. * (e.g. Oracle, PostgreSQL, DB2). Other RDBMS brands return null.
  272. *
  273. * @param string $sequenceName
  274. * @return string
  275. */
  276. public function nextSequenceId($sequenceName)
  277. {
  278. $this->_connect();
  279. $sequenceName = str_replace($this->getQuoteIdentifierSymbol(), '', (string) $sequenceName);
  280. $value = $this->fetchOne("SELECT NEXTVAL("
  281. . $this->quote($this->quoteIdentifier($sequenceName, true))
  282. . ")");
  283. return $value;
  284. }
  285. /**
  286. * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
  287. *
  288. * As a convention, on RDBMS brands that support sequences
  289. * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
  290. * from the arguments and returns the last id generated by that sequence.
  291. * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
  292. * returns the last value generated for such a column, and the table name
  293. * argument is disregarded.
  294. *
  295. * @param string $tableName OPTIONAL Name of table.
  296. * @param string $primaryKey OPTIONAL Name of primary key column.
  297. * @return string
  298. */
  299. public function lastInsertId($tableName = null, $primaryKey = null)
  300. {
  301. if ($tableName !== null) {
  302. $sequenceName = $tableName;
  303. if ($primaryKey) {
  304. $sequenceName .= "_$primaryKey";
  305. }
  306. $sequenceName .= '_seq';
  307. return $this->lastSequenceId($sequenceName);
  308. }
  309. return $this->_connection->lastInsertId($tableName);
  310. }
  311. }