KumbiaPHP beta2-dev
Framework PHP en español
|
00001 <?php 00002 // @see DbPdo Padre de Drivers Pdo 00003 require_once CORE_PATH . 'libs/db/adapters/pdo.php'; 00004 00026 class DbPdoInformix extends DbPDO { 00027 00031 protected $db_rbdm = "informix"; 00032 00037 const TYPE_INTEGER = "INTEGER"; 00038 00043 const TYPE_DATE = "DATE"; 00044 00049 const TYPE_VARCHAR = "VARCHAR"; 00050 00055 const TYPE_DECIMAL = "DECIMAL"; 00056 00061 const TYPE_DATETIME = "DATETIME"; 00062 00067 const TYPE_CHAR = "CHAR"; 00068 00073 public function initialize(){ 00074 00075 } 00076 00083 public function table_exists($table, $schema=''){ 00087 $table = addslashes("$table"); 00088 $num = $this->fetch_one("SELECT COUNT(*) FROM systables WHERE tabname = '$table'"); 00089 return (int) $num[0]; 00090 } 00091 00098 public function limit($sql, $number){ 00102 $number = (int) $number; 00103 $this->limit = $number; 00104 return "$sql -- LIMIT $number\n"; 00105 } 00106 00113 public function drop_table($table, $if_exists=true){ 00114 if($if_exists){ 00115 if($this->table_exists($table)){ 00116 return $this->query("DROP TABLE $table"); 00117 } else { 00118 return true; 00119 } 00120 } else { 00121 $this->set_return_rows(false); 00122 return $this->query("DROP TABLE $table"); 00123 } 00124 } 00125 00139 public function create_table($table, $definition, $index=array()){ 00140 $create_sql = "CREATE TABLE $table ("; 00141 if(!is_array($definition)){ 00142 new KumbiaException("Definición invalida para crear la tabla '$table'"); 00143 return false; 00144 } 00145 $create_lines = array(); 00146 $index = array(); 00147 $unique_index = array(); 00148 $primary = array(); 00149 $not_null = ""; 00150 $size = ""; 00151 foreach($definition as $field => $field_def){ 00152 if(isset($field_def['not_null'])){ 00153 $not_null = $field_def['not_null'] ? 'NOT NULL' : ''; 00154 } else { 00155 $not_null = ""; 00156 } 00157 if(isset($field_def['size'])){ 00158 $size = $field_def['size'] ? '('.$field_def['size'].')' : ''; 00159 } else { 00160 $size = ""; 00161 } 00162 if(isset($field_def['index'])){ 00163 if($field_def['index']){ 00164 $index[] = "INDEX($field)"; 00165 } 00166 } 00167 if(isset($field_def['unique_index'])){ 00168 if($field_def['unique_index']){ 00169 $index[] = "UNIQUE($field)"; 00170 } 00171 } 00172 if(isset($field_def['primary'])){ 00173 if($field_def['primary']){ 00174 $primary[] = "$field"; 00175 } 00176 } 00177 if(isset($field_def['auto'])){ 00178 if($field_def['auto']){ 00179 $field_def['type'] = "SERIAL"; 00180 } 00181 } 00182 if(isset($field_def['extra'])){ 00183 $extra = $field_def['extra']; 00184 } else { 00185 $extra = ""; 00186 } 00187 $create_lines[] = "$field ".$field_def['type'].$size.' '.$not_null.' '.$extra; 00188 } 00189 $create_sql.= join(',', $create_lines); 00190 $last_lines = array(); 00191 if(count($primary)){ 00192 $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')'; 00193 } 00194 if(count($index)){ 00195 $last_lines[] = join(',', $index); 00196 } 00197 if(count($unique_index)){ 00198 $last_lines[] = join(',', $unique_index); 00199 } 00200 if(count($last_lines)){ 00201 $create_sql.= ','.join(',', $last_lines).')'; 00202 } 00203 return $this->query($create_sql); 00204 00205 } 00206 00212 public function list_tables(){ 00213 return $this->fetch_all("SELECT tabname FROM systables WHERE tabtype = 'T' AND version <> 65537"); 00214 } 00215 00222 public function describe_table($table, $schema=''){ 00229 $describe = $this->fetch_all("SELECT c.colname AS Field, c.coltype AS Type, 00230 'YES' AS NULL, c.collength as Length 00231 FROM systables t, syscolumns c WHERE 00232 c.tabid = t.tabid AND t.tabname = '$table' ORDER BY c.colno"); 00233 $final_describe = array(); 00234 foreach($describe as $field){ 00235 //Serial 00236 if($field['field']=='id'){ 00237 $field["key"] = 'PRI'; 00238 $field["null"] = 'NO'; 00239 } else { 00240 $field["key"] = ''; 00241 } 00242 if(substr($field['field'], -3)=='_id'){ 00243 $field["null"] = 'NO'; 00244 } 00245 if($field['type']==262){ 00246 $field['type'] = "integer"; 00247 } 00248 if($field['type']==13){ 00249 $field['type'] = "varchar(".$field['length'].")"; 00250 } 00251 if($field['type']==2){ 00252 $field['type'] = "int(".$field['length'].")"; 00253 } 00254 if($field['type']==7){ 00255 $field['type'] = "date"; 00256 } 00257 $final_describe[] = array( 00258 "Field" => $field["field"], 00259 "Type" => $field["type"], 00260 "Null" => $field["null"], 00261 "Key" => $field["key"] 00262 ); 00263 } 00264 return $final_describe; 00265 } 00266 00267 }