00001 <?php
00023 class DbPdoInformix extends DbPDO {
00024
00028 protected $db_rbdm = "informix";
00029
00034 const TYPE_INTEGER = "INTEGER";
00035
00040 const TYPE_DATE = "DATE";
00041
00046 const TYPE_VARCHAR = "VARCHAR";
00047
00052 const TYPE_DECIMAL = "DECIMAL";
00053
00058 const TYPE_DATETIME = "DATETIME";
00059
00064 const TYPE_CHAR = "CHAR";
00065
00070 public function initialize(){
00071
00072 }
00073
00080 public function table_exists($table, $schema=''){
00084 $table = addslashes("$table");
00085 $num = $this->fetch_one("SELECT COUNT(*) FROM systables WHERE tabname = '$table'");
00086 return (int) $num[0];
00087 }
00088
00095 public function limit($sql, $number){
00099 $number = (int) $number;
00100 $this->limit = $number;
00101 return "$sql -- LIMIT $number\n";
00102 }
00103
00110 public function drop_table($table, $if_exists=true){
00111 if($if_exists){
00112 if($this->table_exists($table)){
00113 return $this->query("DROP TABLE $table");
00114 } else {
00115 return true;
00116 }
00117 } else {
00118 $this->set_return_rows(false);
00119 return $this->query("DROP TABLE $table");
00120 }
00121 }
00122
00136 public function create_table($table, $definition, $index=array()){
00137 $create_sql = "CREATE TABLE $table (";
00138 if(!is_array($definition)){
00139 new KumbiaException("Definición invalida para crear la tabla '$table'");
00140 return false;
00141 }
00142 $create_lines = array();
00143 $index = array();
00144 $unique_index = array();
00145 $primary = array();
00146 $not_null = "";
00147 $size = "";
00148 foreach($definition as $field => $field_def){
00149 if(isset($field_def['not_null'])){
00150 $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
00151 } else {
00152 $not_null = "";
00153 }
00154 if(isset($field_def['size'])){
00155 $size = $field_def['size'] ? '('.$field_def['size'].')' : '';
00156 } else {
00157 $size = "";
00158 }
00159 if(isset($field_def['index'])){
00160 if($field_def['index']){
00161 $index[] = "INDEX($field)";
00162 }
00163 }
00164 if(isset($field_def['unique_index'])){
00165 if($field_def['unique_index']){
00166 $index[] = "UNIQUE($field)";
00167 }
00168 }
00169 if(isset($field_def['primary'])){
00170 if($field_def['primary']){
00171 $primary[] = "$field";
00172 }
00173 }
00174 if(isset($field_def['auto'])){
00175 if($field_def['auto']){
00176 $field_def['type'] = "SERIAL";
00177 }
00178 }
00179 if(isset($field_def['extra'])){
00180 $extra = $field_def['extra'];
00181 } else {
00182 $extra = "";
00183 }
00184 $create_lines[] = "$field ".$field_def['type'].$size.' '.$not_null.' '.$extra;
00185 }
00186 $create_sql.= join(',', $create_lines);
00187 $last_lines = array();
00188 if(count($primary)){
00189 $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')';
00190 }
00191 if(count($index)){
00192 $last_lines[] = join(',', $index);
00193 }
00194 if(count($unique_index)){
00195 $last_lines[] = join(',', $unique_index);
00196 }
00197 if(count($last_lines)){
00198 $create_sql.= ','.join(',', $last_lines).')';
00199 }
00200 return $this->query($create_sql);
00201
00202 }
00203
00209 public function list_tables(){
00210 return $this->fetch_all("SELECT tabname FROM systables WHERE tabtype = 'T' AND version <> 65537");
00211 }
00212
00219 public function describe_table($table, $schema=''){
00226 $describe = $this->fetch_all("SELECT c.colname AS Field, c.coltype AS Type,
00227 'YES' AS NULL, c.collength as Length
00228 FROM systables t, syscolumns c WHERE
00229 c.tabid = t.tabid AND t.tabname = '$table' ORDER BY c.colno");
00230 $final_describe = array();
00231 foreach($describe as $field){
00232
00233 if($field['field']=='id'){
00234 $field["key"] = 'PRI';
00235 $field["null"] = 'NO';
00236 } else {
00237 $field["key"] = '';
00238 }
00239 if(substr($field['field'], -3)=='_id'){
00240 $field["null"] = 'NO';
00241 }
00242 if($field['type']==262){
00243 $field['type'] = "integer";
00244 }
00245 if($field['type']==13){
00246 $field['type'] = "varchar(".$field['length'].")";
00247 }
00248 if($field['type']==2){
00249 $field['type'] = "int(".$field['length'].")";
00250 }
00251 if($field['type']==7){
00252 $field['type'] = "date";
00253 }
00254 $final_describe[] = array(
00255 "Field" => $field["field"],
00256 "Type" => $field["type"],
00257 "Null" => $field["null"],
00258 "Key" => $field["key"]
00259 );
00260 }
00261 return $final_describe;
00262 }
00263
00264 }