# Source for file Pgsql.php

Documentation is available at Pgsql.php

1. <?php
2. /*
3. * $Id: Pgsql.php 1889 2007-06-28 12:11:55Z zYne $
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.com>.
20. */
21. Doctrine::autoload('Doctrine_Import');
22. /**
23. * @package Doctrine
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: 1889 $
29. * @category Object Relational Mapping
30. * @link www.phpdoctrine.com
31. * @since 1.0
32. */
33. class Doctrine_Import_Pgsql extends Doctrine_Import
34. {
36. protected $sql = array(
37. 'listDatabases' => 'SELECT datname FROM pg_database',
38. 'listFunctions' => "SELECT
39. proname
40. FROM
41. pg_proc pr,
42. pg_type tp
43. WHERE
44. tp.oid = pr.prorettype
45. AND pr.proisagg = FALSE
46. AND tp.typname <> 'trigger'
47. AND pr.pronamespace IN
48. (SELECT oid FROM pg_namespace
49. WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema'",
50. 'listSequences' => "SELECT
51. relname
52. FROM
53. pg_class
54. WHERE relkind = 'S' AND relnamespace IN
55. (SELECT oid FROM pg_namespace
56. WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')",
57. 'listTables' => "SELECT
58. c.relname AS table_name
59. FROM pg_class c, pg_user u
60. WHERE c.relowner = u.usesysid
61. AND c.relkind = 'r'
62. AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname)
63. AND c.relname !~ '^(pg_|sql_)'
64. UNION
65. SELECT c.relname AS table_name
66. FROM pg_class c
67. WHERE c.relkind = 'r'
68. AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname)
69. AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner)
70. AND c.relname !~ '^pg_'",
71. 'listViews' => 'SELECT viewname FROM pg_views',
72. 'listUsers' => 'SELECT usename FROM pg_user',
73. 'listTableConstraints' => "SELECT
74. relname
75. FROM
76. pg_class
77. WHERE oid IN (
78. SELECT indexrelid
79. FROM pg_index, pg_class
80. WHERE pg_class.relname = %s
81. AND pg_class.oid = pg_index.indrelid
82. AND (indisunique = 't' OR indisprimary = 't')
83. )",
84. 'listTableIndexes' => "SELECT
85. relname
86. FROM
87. pg_class
88. WHERE oid IN (
89. SELECT indexrelid
90. FROM pg_index, pg_class
91. WHERE pg_class.relname = %s
92. AND pg_class.oid=pg_index.indrelid
93. AND indisunique != 't'
94. AND indisprimary != 't'
95. )",
96. 'listTableColumns' => "SELECT
97. a.attnum,
98. a.attname AS field,
99. t.typname AS type,
100. format_type(a.atttypid, a.atttypmod) AS complete_type,
101. a.attnotnull AS isnotnull,
102. (SELECT 't'
103. FROM pg_index
104. WHERE c.oid = pg_index.indrelid
105. AND pg_index.indkey[0] = a.attnum
106. AND pg_index.indisprimary = 't'
107. ) AS pri,
108. (SELECT pg_attrdef.adsrc
109. FROM pg_attrdef
110. WHERE c.oid = pg_attrdef.adrelid
111. AND pg_attrdef.adnum=a.attnum
112. ) AS default
113. FROM pg_attribute a, pg_class c, pg_type t
114. WHERE c.relname = %s
115. AND a.attnum > 0
116. AND a.attrelid = c.oid
117. AND a.atttypid = t.oid
118. ORDER BY a.attnum",
119. );
120. /**
121. * lists all database triggers
122. *
123. * @param string|null$database
124. * @return array
125. */
126. public function listTriggers($database = null)
127. {
129. }
130. /**
131. * lists table constraints
132. *
133. * @param string $table database table name
134. * @return array
135. */
136. public function listTableConstraints($table)
137. {
138. $table = $this->conn->quote($table);
139. $query = sprintf($this->sql['listTableConstraints'], $table);
141. return $this->conn->fetchColumn($query);
142. }
143. /**
144. * lists table constraints
145. *
146. * @param string $table database table name
147. * @return array
148. */
149. public function listTableColumns($table)
150. {
151. $table = $this->conn->quote($table);
152. $query = sprintf($this->sql['listTableColumns'], $table);
153. $result = $this->conn->fetchAssoc($query);
155. $columns = array();
156. foreach ($result as $key => $val) {
157. $val = array_change_key_case($val, CASE_LOWER);
159. if (strtolower($val['type']) === 'varchar') {
160. // get length from varchar definition
161. $length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $val['complete_type']);
162. $val['length'] = $length;
163. }
165. $decl = $this->conn->dataDict->getPortableDeclaration($val);
167. $description = array(
168. 'name' => $val['field'],
169. 'ntype' => $val['type'],
170. 'type' => $decl['type'][0],
171. 'alltypes' => $decl['type'],
172. 'length' => $decl['length'],
173. 'fixed' => $decl['fixed'],
174. 'unsigned' => $decl['unsigned'],
175. 'notnull' => ($val['isnotnull'] == ''),
176. 'default' => $val['default'],
177. 'primary' => ($val['pri'] == 't'),
178. );
179. $columns[$val['field']] = $description;
180. }
181. return $columns;
182. }
183. /**
184. * list all indexes in a table
185. *
186. * @param string $table database table name
187. * @return array
188. */
189. public function listTableIndexes($table)
190. {
191. $table = $this->conn->quote($table);
192. $query = sprintf($this->sql['listTableIndexes'], $table);
194. return $this->conn->fetchColumn($query);
195. }
196. /**
197. * lists tables
198. *
199. * @param string|null$database
200. * @return array
201. */
202. public function listTables($database = null)
203. {
204. return $this->conn->fetchColumn($this->sql['listTables']);
205. }
206. /**
207. * lists table triggers
208. *
209. * @param string $table database table name
210. * @return array
211. */
212. public function listTableTriggers($table)
213. {
214. $query = 'SELECT trg.tgname AS trigger_name
215. FROM pg_trigger trg,
216. pg_class tbl
217. WHERE trg.tgrelid = tbl.oid';
218. if ($table !== null) {
219. $table = $this->conn->quote(strtoupper($table), 'string');
220. $query .= " AND tbl.relname = $table";
221. }
222. return $this->conn->fetchColumn($query);
223. }
224. /**
225. * list the views in the database that reference a given table
226. *
227. * @param string $table database table name
228. * @return array
229. */
230. public function listTableViews($table)
231. {
232. return $this->conn->fetchColumn($query);
233. }
234. }