| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Pgsql.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $ |
| 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 | Doctrine::autoload('Doctrine_Import'); |
| 22 | /** |
| 23 | * @package Doctrine |
| 24 | * @subpackage Import |
| 25 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 26 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 27 | * @author Paul Cooper <pgc@ucecom.com> |
| 28 | * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library) |
| 29 | * @version $Revision: 2963 $ |
| 30 | * @link www.phpdoctrine.org |
| 31 | * @since 1.0 |
| 32 | */ |
| 33 | class Doctrine_Import_Pgsql extends Doctrine_Import |
| 34 | { |
| 35 | |
| 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 | /** |
| 122 | * lists all database triggers |
| 123 | * |
| 124 | * @param string|null $database |
| 125 | * @return array |
| 126 | */ |
| 127 | public function listTriggers($database = null) |
| 128 | { |
| 129 | |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * lists table constraints |
| 134 | * |
| 135 | * @param string $table database table name |
| 136 | * @return array |
| 137 | */ |
| 138 | public function listTableConstraints($table) |
| 139 | { |
| 140 | $table = $this->conn->quote($table); |
| 141 | $query = sprintf($this->sql['listTableConstraints'], $table); |
| 142 | |
| 143 | return $this->conn->fetchColumn($query); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * lists table constraints |
| 148 | * |
| 149 | * @param string $table database table name |
| 150 | * @return array |
| 151 | */ |
| 152 | public function listTableColumns($table) |
| 153 | { |
| 154 | $table = $this->conn->quote($table); |
| 155 | $query = sprintf($this->sql['listTableColumns'], $table); |
| 156 | $result = $this->conn->fetchAssoc($query); |
| 157 | |
| 158 | $columns = array(); |
| 159 | foreach ($result as $key => $val) { |
| 160 | $val = array_change_key_case($val, CASE_LOWER); |
| 161 | |
| 162 | if (strtolower($val['type']) === 'varchar') { |
| 163 | // get length from varchar definition |
| 164 | $length = preg_replace('~.*\(([0-9]*)\).*~', '$1', $val['complete_type']); |
| 165 | $val['length'] = $length; |
| 166 | } |
| 167 | |
| 168 | $decl = $this->conn->dataDict->getPortableDeclaration($val); |
| 169 | |
| 170 | $description = array( |
| 171 | 'name' => $val['field'], |
| 172 | 'ntype' => $val['type'], |
| 173 | 'type' => $decl['type'][0], |
| 174 | 'alltypes' => $decl['type'], |
| 175 | 'length' => $decl['length'], |
| 176 | 'fixed' => $decl['fixed'], |
| 177 | 'unsigned' => $decl['unsigned'], |
| 178 | 'notnull' => ($val['isnotnull'] == ''), |
| 179 | 'default' => $val['default'], |
| 180 | 'primary' => ($val['pri'] == 't'), |
| 181 | ); |
| 182 | $columns[$val['field']] = $description; |
| 183 | } |
| 184 | return $columns; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * list all indexes in a table |
| 189 | * |
| 190 | * @param string $table database table name |
| 191 | * @return array |
| 192 | */ |
| 193 | public function listTableIndexes($table) |
| 194 | { |
| 195 | $table = $this->conn->quote($table); |
| 196 | $query = sprintf($this->sql['listTableIndexes'], $table); |
| 197 | |
| 198 | return $this->conn->fetchColumn($query); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * lists tables |
| 203 | * |
| 204 | * @param string|null $database |
| 205 | * @return array |
| 206 | */ |
| 207 | public function listTables($database = null) |
| 208 | { |
| 209 | return $this->conn->fetchColumn($this->sql['listTables']); |
| 210 | } |
| 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 | /** |
| 232 | * list the views in the database that reference a given table |
| 233 | * |
| 234 | * @param string $table database table name |
| 235 | * @return array |
| 236 | */ |
| 237 | public function listTableViews($table) |
| 238 | { |
| 239 | return $this->conn->fetchColumn($query); |
| 240 | } |
| 241 | } |