PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/doctrine/Doctrine/Import/Pgsql.php

https://github.com/icz/OpenPNE3
PHP | 282 lines | 184 code | 25 blank | 73 comment | 11 complexity | 321b82c5009e9b106ce3d7c753cda885 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. * $Id: Pgsql.php 6132 2009-07-20 19:30:18Z jwage $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.phpdoctrine.org>.
  20. */
  21. /**
  22. * @package Doctrine
  23. * @subpackage Import
  24. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  25. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  26. * @author Paul Cooper <pgc@ucecom.com>
  27. * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  28. * @version $Revision: 6132 $
  29. * @link www.phpdoctrine.org
  30. * @since 1.0
  31. */
  32. class Doctrine_Import_Pgsql extends Doctrine_Import
  33. {
  34. protected $sql = array(
  35. 'listDatabases' => 'SELECT datname FROM pg_database',
  36. 'listFunctions' => "SELECT
  37. proname
  38. FROM
  39. pg_proc pr,
  40. pg_type tp
  41. WHERE
  42. tp.oid = pr.prorettype
  43. AND pr.proisagg = FALSE
  44. AND tp.typname <> 'trigger'
  45. AND pr.pronamespace IN
  46. (SELECT oid FROM pg_namespace
  47. WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema'",
  48. 'listSequences' => "SELECT
  49. relname
  50. FROM
  51. pg_class
  52. WHERE relkind = 'S' AND relnamespace IN
  53. (SELECT oid FROM pg_namespace
  54. WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')",
  55. 'listTables' => "SELECT
  56. c.relname AS table_name
  57. FROM pg_class c, pg_user u
  58. WHERE c.relowner = u.usesysid
  59. AND c.relkind = 'r'
  60. AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname)
  61. AND c.relname !~ '^(pg_|sql_)'
  62. UNION
  63. SELECT c.relname AS table_name
  64. FROM pg_class c
  65. WHERE c.relkind = 'r'
  66. AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname)
  67. AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner)
  68. AND c.relname !~ '^pg_'",
  69. 'listViews' => 'SELECT viewname FROM pg_views',
  70. 'listUsers' => 'SELECT usename FROM pg_user',
  71. 'listTableConstraints' => "SELECT
  72. relname
  73. FROM
  74. pg_class
  75. WHERE oid IN (
  76. SELECT indexrelid
  77. FROM pg_index, pg_class
  78. WHERE pg_class.relname = %s
  79. AND pg_class.oid = pg_index.indrelid
  80. AND (indisunique = 't' OR indisprimary = 't')
  81. )",
  82. 'listTableIndexes' => "SELECT
  83. relname
  84. FROM
  85. pg_class
  86. WHERE oid IN (
  87. SELECT indexrelid
  88. FROM pg_index, pg_class
  89. WHERE pg_class.relname = %s
  90. AND pg_class.oid=pg_index.indrelid
  91. AND indisunique != 't'
  92. AND indisprimary != 't'
  93. )",
  94. 'listTableColumns' => "SELECT
  95. a.attnum,
  96. a.attname AS field,
  97. t.typname AS type,
  98. format_type(a.atttypid, a.atttypmod) AS complete_type,
  99. a.attnotnull AS isnotnull,
  100. (SELECT 't'
  101. FROM pg_index
  102. WHERE c.oid = pg_index.indrelid
  103. AND pg_index.indkey[0] = a.attnum
  104. AND pg_index.indisprimary = 't'
  105. ) AS pri,
  106. (SELECT pg_attrdef.adsrc
  107. FROM pg_attrdef
  108. WHERE c.oid = pg_attrdef.adrelid
  109. AND pg_attrdef.adnum=a.attnum
  110. ) AS default
  111. FROM pg_attribute a, pg_class c, pg_type t
  112. WHERE c.relname = %s
  113. AND a.attnum > 0
  114. AND a.attrelid = c.oid
  115. AND a.atttypid = t.oid
  116. ORDER BY a.attnum",
  117. 'listTableRelations' => "SELECT pg_catalog.pg_get_constraintdef(oid, true) as condef
  118. FROM pg_catalog.pg_constraint r
  119. WHERE r.conrelid =
  120. (
  121. SELECT c.oid
  122. FROM pg_catalog.pg_class c
  123. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  124. WHERE c.relname ~ ? AND pg_catalog.pg_table_is_visible(c.oid)
  125. )
  126. AND r.contype = 'f'"
  127. );
  128. /**
  129. * lists all database triggers
  130. *
  131. * @param string|null $database
  132. * @return array
  133. */
  134. public function listTriggers($database = null)
  135. {
  136. }
  137. /**
  138. * lists table constraints
  139. *
  140. * @param string $table database table name
  141. * @return array
  142. */
  143. public function listTableConstraints($table)
  144. {
  145. $table = $this->conn->quote($table);
  146. $query = sprintf($this->sql['listTableConstraints'], $table);
  147. return $this->conn->fetchColumn($query);
  148. }
  149. /**
  150. * lists table constraints
  151. *
  152. * @param string $table database table name
  153. * @return array
  154. */
  155. public function listTableColumns($table)
  156. {
  157. $table = $this->conn->quote($table);
  158. $query = sprintf($this->sql['listTableColumns'], $table);
  159. $result = $this->conn->fetchAssoc($query);
  160. $columns = array();
  161. foreach ($result as $key => $val) {
  162. $val = array_change_key_case($val, CASE_LOWER);
  163. if (strtolower($val['type']) === 'varchar') {
  164. // get length from varchar definition
  165. $length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $val['complete_type']);
  166. $val['length'] = $length;
  167. }
  168. $decl = $this->conn->dataDict->getPortableDeclaration($val);
  169. $description = array(
  170. 'name' => $val['field'],
  171. 'ntype' => $val['type'],
  172. 'type' => $decl['type'][0],
  173. 'alltypes' => $decl['type'],
  174. 'length' => $decl['length'],
  175. 'fixed' => (bool) $decl['fixed'],
  176. 'unsigned' => (bool) $decl['unsigned'],
  177. 'notnull' => ($val['isnotnull'] == true),
  178. 'default' => $val['default'],
  179. 'primary' => ($val['pri'] == 't'),
  180. );
  181. $matches = array();
  182. if (preg_match("/^nextval\('(.*)'(::.*)?\)$/", $description['default'], $matches)) {
  183. $description['sequence'] = $this->conn->formatter->fixSequenceName($matches[1]);
  184. $description['default'] = null;
  185. }
  186. $columns[$val['field']] = $description;
  187. }
  188. return $columns;
  189. }
  190. /**
  191. * list all indexes in a table
  192. *
  193. * @param string $table database table name
  194. * @return array
  195. */
  196. public function listTableIndexes($table)
  197. {
  198. $table = $this->conn->quote($table);
  199. $query = sprintf($this->sql['listTableIndexes'], $table);
  200. return $this->conn->fetchColumn($query);
  201. }
  202. /**
  203. * lists tables
  204. *
  205. * @param string|null $database
  206. * @return array
  207. */
  208. public function listTables($database = null)
  209. {
  210. return $this->conn->fetchColumn($this->sql['listTables']);
  211. }
  212. /**
  213. * lists table triggers
  214. *
  215. * @param string $table database table name
  216. * @return array
  217. */
  218. public function listTableTriggers($table)
  219. {
  220. $query = 'SELECT trg.tgname AS trigger_name
  221. FROM pg_trigger trg,
  222. pg_class tbl
  223. WHERE trg.tgrelid = tbl.oid';
  224. if ($table !== null) {
  225. $table = $this->conn->quote(strtoupper($table), 'string');
  226. $query .= " AND tbl.relname = $table";
  227. }
  228. return $this->conn->fetchColumn($query);
  229. }
  230. /**
  231. * list the views in the database that reference a given table
  232. *
  233. * @param string $table database table name
  234. * @return array
  235. */
  236. public function listTableViews($table)
  237. {
  238. return $this->conn->fetchColumn($table);
  239. }
  240. public function listTableRelations($table)
  241. {
  242. $sql = $this->sql['listTableRelations'];
  243. $param = array('^(' . $table . ')$');
  244. $relations = array();
  245. $results = $this->conn->fetchAssoc($sql, $param);
  246. foreach ($results as $result)
  247. {
  248. preg_match('/FOREIGN KEY \((.+)\) REFERENCES (.+)\((.+)\)/', $result['condef'], $values);
  249. if ((strpos(',', $values[1]) === false) && (strpos(',', $values[3]) === false)) {
  250. $relations[] = array('table' => $values[2],
  251. 'local' => $values[1],
  252. 'foreign' => $values[3]);
  253. }
  254. }
  255. return $relations;
  256. }
  257. }