PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/libraries/3rdparty/Zend/Db/Adapter/Pdo/Ibm/Ids.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 301 lines | 173 code | 31 blank | 97 comment | 23 complexity | 9c0a6defd863904edfb5db41a0bc5705 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: Ids.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /** @see Zend_Db_Adapter_Pdo_Ibm */
  23. require_once 'Zend/Db/Adapter/Pdo/Ibm.php';
  24. /** @see Zend_Db_Statement_Pdo_Ibm */
  25. require_once 'Zend/Db/Statement/Pdo/Ibm.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Db
  29. * @subpackage Adapter
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Db_Adapter_Pdo_Ibm_Ids
  34. {
  35. /**
  36. * @var Zend_Db_Adapter_Abstract
  37. */
  38. protected $_adapter = null;
  39. /**
  40. * Construct the data server class.
  41. *
  42. * It will be used to generate non-generic SQL
  43. * for a particular data server
  44. *
  45. * @param Zend_Db_Adapter_Abstract $adapter
  46. */
  47. public function __construct($adapter)
  48. {
  49. $this->_adapter = $adapter;
  50. }
  51. /**
  52. * Returns a list of the tables in the database.
  53. *
  54. * @return array
  55. */
  56. public function listTables()
  57. {
  58. $sql = "SELECT tabname "
  59. . "FROM systables ";
  60. return $this->_adapter->fetchCol($sql);
  61. }
  62. /**
  63. * IDS catalog lookup for describe table
  64. *
  65. * @param string $tableName
  66. * @param string $schemaName OPTIONAL
  67. * @return array
  68. */
  69. public function describeTable($tableName, $schemaName = null)
  70. {
  71. // this is still a work in progress
  72. $sql= "SELECT DISTINCT t.owner, t.tabname, c.colname, c.colno, c.coltype,
  73. d.default, c.collength, t.tabid
  74. FROM syscolumns c
  75. JOIN systables t ON c.tabid = t.tabid
  76. LEFT JOIN sysdefaults d ON c.tabid = d.tabid AND c.colno = d.colno
  77. WHERE "
  78. . $this->_adapter->quoteInto('UPPER(t.tabname) = UPPER(?)', $tableName);
  79. if ($schemaName) {
  80. $sql .= $this->_adapter->quoteInto(' AND UPPER(t.owner) = UPPER(?)', $schemaName);
  81. }
  82. $sql .= " ORDER BY c.colno";
  83. $desc = array();
  84. $stmt = $this->_adapter->query($sql);
  85. $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  86. /**
  87. * The ordering of columns is defined by the query so we can map
  88. * to variables to improve readability
  89. */
  90. $tabschema = 0;
  91. $tabname = 1;
  92. $colname = 2;
  93. $colno = 3;
  94. $typename = 4;
  95. $default = 5;
  96. $length = 6;
  97. $tabid = 7;
  98. $primaryCols = null;
  99. foreach ($result as $key => $row) {
  100. $primary = false;
  101. $primaryPosition = null;
  102. if (!$primaryCols) {
  103. $primaryCols = $this->_getPrimaryInfo($row[$tabid]);
  104. }
  105. if (array_key_exists($row[$colno], $primaryCols)) {
  106. $primary = true;
  107. $primaryPosition = $primaryCols[$row[$colno]];
  108. }
  109. $identity = false;
  110. if ($row[$typename] == 6 + 256 ||
  111. $row[$typename] == 18 + 256) {
  112. $identity = true;
  113. }
  114. $desc[$this->_adapter->foldCase($row[$colname])] = array (
  115. 'SCHEMA_NAME' => $this->_adapter->foldCase($row[$tabschema]),
  116. 'TABLE_NAME' => $this->_adapter->foldCase($row[$tabname]),
  117. 'COLUMN_NAME' => $this->_adapter->foldCase($row[$colname]),
  118. 'COLUMN_POSITION' => $row[$colno],
  119. 'DATA_TYPE' => $this->_getDataType($row[$typename]),
  120. 'DEFAULT' => $row[$default],
  121. 'NULLABLE' => (bool) !($row[$typename] - 256 >= 0),
  122. 'LENGTH' => $row[$length],
  123. 'SCALE' => ($row[$typename] == 5 ? $row[$length]&255 : 0),
  124. 'PRECISION' => ($row[$typename] == 5 ? (int)($row[$length]/256) : 0),
  125. 'UNSIGNED' => false,
  126. 'PRIMARY' => $primary,
  127. 'PRIMARY_POSITION' => $primaryPosition,
  128. 'IDENTITY' => $identity
  129. );
  130. }
  131. return $desc;
  132. }
  133. /**
  134. * Map number representation of a data type
  135. * to a string
  136. *
  137. * @param int $typeNo
  138. * @return string
  139. */
  140. protected function _getDataType($typeNo)
  141. {
  142. $typemap = array(
  143. 0 => "CHAR",
  144. 1 => "SMALLINT",
  145. 2 => "INTEGER",
  146. 3 => "FLOAT",
  147. 4 => "SMALLFLOAT",
  148. 5 => "DECIMAL",
  149. 6 => "SERIAL",
  150. 7 => "DATE",
  151. 8 => "MONEY",
  152. 9 => "NULL",
  153. 10 => "DATETIME",
  154. 11 => "BYTE",
  155. 12 => "TEXT",
  156. 13 => "VARCHAR",
  157. 14 => "INTERVAL",
  158. 15 => "NCHAR",
  159. 16 => "NVARCHAR",
  160. 17 => "INT8",
  161. 18 => "SERIAL8",
  162. 19 => "SET",
  163. 20 => "MULTISET",
  164. 21 => "LIST",
  165. 22 => "Unnamed ROW",
  166. 40 => "Variable-length opaque type",
  167. 4118 => "Named ROW"
  168. );
  169. if ($typeNo - 256 >= 0) {
  170. $typeNo = $typeNo - 256;
  171. }
  172. return $typemap[$typeNo];
  173. }
  174. /**
  175. * Helper method to retrieve primary key column
  176. * and column location
  177. *
  178. * @param int $tabid
  179. * @return array
  180. */
  181. protected function _getPrimaryInfo($tabid)
  182. {
  183. $sql = "SELECT i.part1, i.part2, i.part3, i.part4, i.part5, i.part6,
  184. i.part7, i.part8, i.part9, i.part10, i.part11, i.part12,
  185. i.part13, i.part14, i.part15, i.part16
  186. FROM sysindexes i
  187. JOIN sysconstraints c ON c.idxname = i.idxname
  188. WHERE i.tabid = " . $tabid . " AND c.constrtype = 'P'";
  189. $stmt = $this->_adapter->query($sql);
  190. $results = $stmt->fetchAll();
  191. $cols = array();
  192. // this should return only 1 row
  193. // unless there is no primary key,
  194. // in which case, the empty array is returned
  195. if ($results) {
  196. $row = $results[0];
  197. } else {
  198. return $cols;
  199. }
  200. $position = 0;
  201. foreach ($row as $key => $colno) {
  202. $position++;
  203. if ($colno == 0) {
  204. return $cols;
  205. } else {
  206. $cols[$colno] = $position;
  207. }
  208. }
  209. }
  210. /**
  211. * Adds an IDS-specific LIMIT clause to the SELECT statement.
  212. *
  213. * @param string $sql
  214. * @param integer $count
  215. * @param integer $offset OPTIONAL
  216. * @throws Zend_Db_Adapter_Exception
  217. * @return string
  218. */
  219. public function limit($sql, $count, $offset = 0)
  220. {
  221. $count = intval($count);
  222. if ($count < 0) {
  223. /** @see Zend_Db_Adapter_Exception */
  224. require_once 'Zend/Db/Adapter/Exception.php';
  225. throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
  226. } else if ($count == 0) {
  227. $limit_sql = str_ireplace("SELECT", "SELECT * FROM (SELECT", $sql);
  228. $limit_sql .= ") WHERE 0 = 1";
  229. } else {
  230. $offset = intval($offset);
  231. if ($offset < 0) {
  232. /** @see Zend_Db_Adapter_Exception */
  233. require_once 'Zend/Db/Adapter/Exception.php';
  234. throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
  235. }
  236. if ($offset == 0) {
  237. $limit_sql = str_ireplace("SELECT", "SELECT FIRST $count", $sql);
  238. } else {
  239. $limit_sql = str_ireplace("SELECT", "SELECT SKIP $offset LIMIT $count", $sql);
  240. }
  241. }
  242. return $limit_sql;
  243. }
  244. /**
  245. * IDS-specific last sequence id
  246. *
  247. * @param string $sequenceName
  248. * @return integer
  249. */
  250. public function lastSequenceId($sequenceName)
  251. {
  252. $sql = 'SELECT '.$this->_adapter->quoteIdentifier($sequenceName).'.CURRVAL FROM '
  253. .'systables WHERE tabid = 1';
  254. $value = $this->_adapter->fetchOne($sql);
  255. return $value;
  256. }
  257. /**
  258. * IDS-specific sequence id value
  259. *
  260. * @param string $sequenceName
  261. * @return integer
  262. */
  263. public function nextSequenceId($sequenceName)
  264. {
  265. $sql = 'SELECT '.$this->_adapter->quoteIdentifier($sequenceName).'.NEXTVAL FROM '
  266. .'systables WHERE tabid = 1';
  267. $value = $this->_adapter->fetchOne($sql);
  268. return $value;
  269. }
  270. }