KumbiaPHP beta2-dev
Framework PHP en español
pgsql.php
Ir a la documentación de este archivo.
00001 <?php
00002 // @see DbPdo Padre de Drivers Pdo
00003 require_once CORE_PATH . 'libs/db/adapters/pdo.php';
00004 
00026 class DbPdoPgSQL extends DbPDO {
00027 
00031         protected $db_rbdm = "pgsql";
00032 
00038         protected $db_port = 5432;
00039 
00044         const TYPE_INTEGER = "INTEGER";
00045 
00050         const TYPE_DATE = "DATE";
00051 
00056         const TYPE_VARCHAR = "VARCHAR";
00057 
00062         const TYPE_DECIMAL = "DECIMAL";
00063 
00068         const TYPE_DATETIME = "DATETIME";
00069 
00074         const TYPE_CHAR = "CHAR";
00075 
00080         public function initialize(){
00081 
00082         }
00083 
00089         public function last_insert_id($table='', $primary_key=''){
00090                 if(!$this->pdo){
00091                         return false;
00092                 }
00093                 return $this->pdo->lastInsertId("{$table}_{$primary_key}_seq");
00094         }
00095 
00102         function table_exists($table, $schema=''){
00103                 $table = addslashes(strtolower($table));
00104                 if(strpos($table, ".")){
00105                         list($schema, $table) = explode(".", $table);
00106                 }
00107                 if($schema==''){
00108                         $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'public' AND TABLE_NAME ='$table'");
00109                 } else {
00110                         $schema = addslashes(strtolower($schema));
00111                         $num = $this->fetch_one("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$schema' AND TABLE_NAME ='$table'");
00112                 }
00113                 return $num[0];
00114         }
00115 
00122         public function limit($sql){
00123                 $params = Util::getParams(func_get_args());
00124                 $sql_new = $sql;
00125         
00126                 if(isset($params['limit']) && is_numeric($params['limit'])){
00127                         $sql_new.=" LIMIT $params[limit]";
00128                 }
00129                 
00130                 if(isset($params['offset']) && is_numeric($params['offset'])){
00131                         $sql_new.=" OFFSET $params[offset]";
00132                 }
00133                 
00134                 return $sql_new;
00135         }
00136 
00143         public function drop_table($table, $if_exists=true){
00144                 if($if_exists){
00145                         if($this->table_exists($table)){
00146                                 return $this->query("DROP TABLE $table");
00147                         } else {
00148                                 return true;
00149                         }
00150                 } else {
00151                         return $this->query("DROP TABLE $table");
00152                 }
00153         }
00154 
00168         public function create_table($table, $definition, $index=array()){
00169                 $create_sql = "CREATE TABLE $table (";
00170                 if(!is_array($definition)){
00171                         new KumbiaException("Definici&oacute;n invalida para crear la tabla '$table'");
00172                         return false;
00173                 }
00174                 $create_lines = array();
00175                 $index = array();
00176                 $unique_index = array();
00177                 $primary = array();
00178                 $not_null = "";
00179                 $size = "";
00180                 foreach($definition as $field => $field_def){
00181                         if(isset($field_def['not_null'])){
00182                                 $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
00183                         } else {
00184                                 $not_null = "";
00185                         }
00186                         if(isset($field_def['size'])){
00187                                 $size = $field_def['size'] ? '('.$field_def['size'].')' : '';
00188                         } else {
00189                                 $size = "";
00190                         }
00191                         if(isset($field_def['index'])){
00192                                 if($field_def['index']){
00193                                         $index[] = "INDEX($field)";
00194                                 }
00195                         }
00196                         if(isset($field_def['unique_index'])){
00197                                 if($field_def['unique_index']){
00198                                         $index[] = "UNIQUE($field)";
00199                                 }
00200                         }
00201                         if(isset($field_def['primary'])){
00202                                 if($field_def['primary']){
00203                                         $primary[] = "$field";
00204                                 }
00205                         }
00206                         if(isset($field_def['auto'])){
00207                                 if($field_def['auto']){
00208                                         $field_def['type'] = "SERIAL";
00209                                 }
00210                         }
00211                         if(isset($field_def['extra'])){
00212                                 $extra = $field_def['extra'];
00213                         } else {
00214                                 $extra = "";
00215                         }
00216                         $create_lines[] = "$field ".$field_def['type'].$size.' '.$not_null.' '.$extra;
00217                 }
00218                 $create_sql.= join(',', $create_lines);
00219                 $last_lines = array();
00220                 if(count($primary)){
00221                         $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')';
00222                 }
00223                 if(count($index)){
00224                         $last_lines[] = join(',', $index);
00225                 }
00226                 if(count($unique_index)){
00227                         $last_lines[] = join(',', $unique_index);
00228                 }
00229                 if(count($last_lines)){
00230                         $create_sql.= ','.join(',', $last_lines).')';
00231                 }
00232                 return $this->query($create_sql);
00233 
00234         }
00235 
00241         public function list_tables(){
00242                 return $this->fetch_all("SELECT c.relname AS table_name FROM pg_class c, pg_user u "
00243              ."WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
00244              ."AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
00245              ."AND c.relname !~ '^(pg_|sql_)' UNION "
00246              ."SELECT c.relname AS table_name FROM pg_class c "
00247              ."WHERE c.relkind = 'r' "
00248              ."AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
00249              ."AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) "
00250              ."AND c.relname !~ '^pg_'");
00251         }
00252 
00259         public function describe_table($table, $schema=''){
00260                 $describe = $this->fetch_all("SELECT a.attname AS Field, t.typname AS Type,
00261                                 CASE WHEN attnotnull=false THEN 'YES' ELSE 'NO' END AS Null,
00262                                 CASE WHEN (select cc.contype FROM pg_catalog.pg_constraint cc WHERE
00263                                 cc.conrelid = c.oid AND cc.conkey[1] = a.attnum)='p' THEN 'PRI' ELSE ''
00264                                 END AS Key FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a,
00265                                 pg_catalog.pg_type t WHERE c.relname = '$table' AND c.oid = a.attrelid
00266                                 AND a.attnum > 0 AND t.oid = a.atttypid order by a.attnum");
00267                 $final_describe = array();
00268                 foreach($describe as $key => $value){
00269                         $final_describe[] = array(
00270                                 "Field" => $value["field"],
00271                                 "Type" => $value["type"],
00272                                 "Null" => $value["null"],
00273                                 "Key" => $value["key"]
00274                         );
00275                 }
00276                 return $final_describe;
00277         }
00278 
00279 }
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Enumeraciones