KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
pgsql.php
Ir a la documentación de este archivo.
1 <?php
24 require_once CORE_PATH . 'libs/db/adapters/pdo.php';
25 
33 class DbPdoPgSQL extends DbPDO
34 {
35 
39  protected $db_rbdm = "pgsql";
45  protected $db_port = 5432;
46 
51  const TYPE_INTEGER = "INTEGER";
52 
57  const TYPE_DATE = "DATE";
58 
63  const TYPE_VARCHAR = "VARCHAR";
64 
69  const TYPE_DECIMAL = "DECIMAL";
70 
75  const TYPE_DATETIME = "DATETIME";
76 
81  const TYPE_CHAR = "CHAR";
82 
87  public function initialize()
88  {
89 
90  }
91 
97  public function last_insert_id($table='', $primary_key='')
98  {
99  return $this->pdo->lastInsertId("{$table}_{$primary_key}_seq");
100  }
101 
108  function table_exists($table, $schema='')
109  {
110  $table = addslashes(strtolower($table));
111  if (strpos($table, ".")) {
112  list($schema, $table) = explode(".", $table);
113  }
114  if ($schema == '') {
115  $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'public' AND TABLE_NAME ='$table'");
116  } else {
117  $schema = addslashes(strtolower($schema));
118  $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$schema' AND TABLE_NAME ='$table'");
119  }
120  return $num[0];
121  }
122 
129  public function limit($sql)
130  {
131  $params = Util::getParams(func_get_args());
132  $sql_new = $sql;
133 
134  if (isset($params['limit']) && is_numeric($params['limit'])) {
135  $sql_new.=" LIMIT $params[limit]";
136  }
137 
138  if (isset($params['offset']) && is_numeric($params['offset'])) {
139  $sql_new.=" OFFSET $params[offset]";
140  }
141 
142  return $sql_new;
143  }
144 
151  public function drop_table($table, $if_exists=true)
152  {
153  if ($if_exists) {
154  if ($this->table_exists($table)) {
155  return $this->query("DROP TABLE $table");
156  } else {
157  return true;
158  }
159  } else {
160  return $this->query("DROP TABLE $table");
161  }
162  }
163 
177  public function create_table($table, $definition, $index=array())
178  {
179  $create_sql = "CREATE TABLE $table (";
180  if (!is_array($definition)) {
181  new KumbiaException("Definici&oacute;n invalida para crear la tabla '$table'");
182  return false;
183  }
184  $create_lines = array();
185  $index = array();
186  $unique_index = array();
187  $primary = array();
188  //$not_null = "";
189  //$size = "";
190  foreach ($definition as $field => $field_def) {
191  if (isset($field_def['not_null'])) {
192  $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
193  } else {
194  $not_null = "";
195  }
196  if (isset($field_def['size'])) {
197  $size = $field_def['size'] ? '(' . $field_def['size'] . ')' : '';
198  } else {
199  $size = "";
200  }
201  if (isset($field_def['index'])) {
202  if ($field_def['index']) {
203  $index[] = "INDEX($field)";
204  }
205  }
206  if (isset($field_def['unique_index'])) {
207  if ($field_def['unique_index']) {
208  $index[] = "UNIQUE($field)";
209  }
210  }
211  if (isset($field_def['primary'])) {
212  if ($field_def['primary']) {
213  $primary[] = "$field";
214  }
215  }
216  if (isset($field_def['auto'])) {
217  if ($field_def['auto']) {
218  $field_def['type'] = "SERIAL";
219  }
220  }
221  if (isset($field_def['extra'])) {
222  $extra = $field_def['extra'];
223  } else {
224  $extra = "";
225  }
226  $create_lines[] = "$field " . $field_def['type'] . $size . ' ' . $not_null . ' ' . $extra;
227  }
228  $create_sql.= join(',', $create_lines);
229  $last_lines = array();
230  if (count($primary)) {
231  $last_lines[] = 'PRIMARY KEY(' . join(",", $primary) . ')';
232  }
233  if (count($index)) {
234  $last_lines[] = join(',', $index);
235  }
236  if (count($unique_index)) {
237  $last_lines[] = join(',', $unique_index);
238  }
239  if (count($last_lines)) {
240  $create_sql.= ',' . join(',', $last_lines) . ')';
241  }
242  return $this->query($create_sql);
243  }
244 
250  public function list_tables()
251  {
252  return $this->fetch_all("SELECT c.relname AS table_name FROM pg_class c, pg_user u "
253  . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
254  . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
255  . "AND c.relname !~ '^(pg_|sql_)' UNION "
256  . "SELECT c.relname AS table_name FROM pg_class c "
257  . "WHERE c.relkind = 'r' "
258  . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
259  . "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) "
260  . "AND c.relname !~ '^pg_'");
261  }
262 
269  public function describe_table($table, $schema='')
270  {
271  $describe = $this->fetch_all("SELECT a.attname AS Field, t.typname AS Type,
272  CASE WHEN attnotnull=false THEN 'YES' ELSE 'NO' END AS Null,
273  CASE WHEN (select cc.contype FROM pg_catalog.pg_constraint cc WHERE
274  cc.conrelid = c.oid AND cc.conkey[1] = a.attnum)='p' THEN 'PRI' ELSE ''
275  END AS Key, CASE WHEN atthasdef=true THEN TRUE ELSE NULL END AS Default
276  FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a,
277  pg_catalog.pg_type t WHERE c.relname = '$table' AND c.oid = a.attrelid
278  AND a.attnum > 0 AND t.oid = a.atttypid order by a.attnum");
279  $final_describe = array();
280  foreach ($describe as $key => $value) {
281  $final_describe[] = array(
282  "Field" => $value["field"],
283  "Type" => $value["type"],
284  "Null" => $value["null"],
285  "Key" => $value["key"],
286  "Default" => $value["default"]
287  );
288  }
289  return $final_describe;
290  }
291 
292 }