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 DbPdoOracle extends DbPDO { 00027 00031 protected $db_rbdm = "oci"; 00032 00037 const TYPE_INTEGER = "INTEGER"; 00038 00043 const TYPE_DATE = "DATE"; 00044 00049 const TYPE_VARCHAR = "VARCHAR2"; 00050 00055 const TYPE_DECIMAL = "DECIMAL"; 00056 00061 const TYPE_DATETIME = "DATETIME"; 00062 00067 const TYPE_CHAR = "CHAR"; 00068 00069 00074 public function initialize(){ 00075 $this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, false); 00076 $this->exec("alter session set nls_date_format = 'YYYY-MM-DD'"); 00077 $this->begin(); 00078 } 00079 00086 public function limit($sql, $number){ 00087 if(!is_numeric($number)||$number<0){ 00088 return $sql; 00089 } 00090 if(eregi("ORDER[\t\n\r ]+BY", $sql)){ 00091 if(stripos($sql, "WHERE")){ 00092 return eregi_replace("ORDER[\t\n\r ]+BY", "AND ROWNUM <= $number ORDER BY", $sql); 00093 } else { 00094 return eregi_replace("ORDER[\t\n\r ]+BY", "WHERE ROWNUM <= $number ORDER BY", $sql); 00095 } 00096 } else { 00097 if(stripos($sql, "WHERE")){ 00098 return "$sql AND ROWNUM <= $number"; 00099 } else { 00100 return "$sql WHERE ROWNUM <= $number"; 00101 } 00102 } 00103 } 00104 00111 public function drop_table($table, $if_exists=true){ 00112 if($if_exists){ 00113 if($this->table_exists($table)){ 00114 return $this->query("DROP TABLE $table"); 00115 } else { 00116 return true; 00117 } 00118 } else { 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 $this->query("CREATE SEQUENCE {$table}_{$field}_seq START WITH 1"); 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 00210 function list_tables(){ 00211 return $this->fetch_all("SELECT table_name FROM all_tables"); 00212 } 00213 00219 public function last_insert_id($table='', $primary_key=''){ 00223 if($table&&$primary_key){ 00224 $sequence = $table."_".$primary_key."_seq"; 00225 $value = $this->fetch_one("SELECT $sequence.CURRVAL FROM dual"); 00226 return $value[0]; 00227 } 00228 return false; 00229 } 00230 00237 function table_exists($table, $schema=''){ 00238 $num = $this->fetch_one("SELECT COUNT(*) FROM ALL_TABLES WHERE TABLE_NAME = '".strtoupper($table)."'"); 00239 return $num[0]; 00240 } 00241 00248 public function describe_table($table, $schema=''){ 00252 $describe = $this->fetch_all("SELECT LOWER(ALL_TAB_COLUMNS.COLUMN_NAME) AS FIELD, LOWER(ALL_TAB_COLUMNS.DATA_TYPE) AS TYPE, ALL_TAB_COLUMNS.DATA_LENGTH AS LENGTH, (SELECT COUNT(*) FROM ALL_CONS_COLUMNS WHERE TABLE_NAME = '".strtoupper($table)."' AND ALL_CONS_COLUMNS.COLUMN_NAME = ALL_TAB_COLUMNS.COLUMN_NAME AND ALL_CONS_COLUMNS.POSITION IS NOT NULL) AS KEY, ALL_TAB_COLUMNS.NULLABLE AS ISNULL FROM ALL_TAB_COLUMNS WHERE ALL_TAB_COLUMNS.TABLE_NAME = '".strtoupper($table)."'"); 00253 $final_describe = array(); 00254 foreach($describe as $key => $value){ 00255 $final_describe[] = array( 00256 "Field" => $value["field"], 00257 "Type" => $value["type"], 00258 "Null" => $value["isnull"] == "Y" ? "YES" : "NO", 00259 "Key" => $value["key"] == 1 ? "PRI" : "" 00260 ); 00261 } 00262 return $final_describe; 00263 } 00264 00265 }