00001 <?php
00023 class DbPdoAccess extends DbPDO {
00024
00028 protected $db_rbdm = "odbc";
00029
00034 const TYPE_INTEGER = "INTEGER";
00035
00040 const TYPE_DATE = "DATETIME";
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(){
00074
00075 }
00076
00083 public function table_exists($table, $schema=''){
00084 $table = addslashes("$table");
00085 $num = $this->fetch_one("SELECT COUNT(*) FROM sysobjects WHERE type = 'U' AND name = '$table'");
00086 return $num[0];
00087 }
00088
00095 public function limit($sql, $number){
00096 if(!is_numeric($number)){
00097 return $sql;
00098 }
00099 $orderby = stristr($sql, 'ORDER BY');
00100 if($orderby!==false){
00101 $sort = (stripos($orderby, 'desc') !== false) ? 'desc' : 'asc';
00102 $order = str_ireplace('ORDER BY', '', $orderby);
00103 $order = trim(preg_replace('/ASC|DESC/i', '', $order));
00104 }
00105 $sql = preg_replace('/^SELECT\s/i', 'SELECT TOP '.($number).' ', $sql);
00106 $sql = 'SELECT * FROM (SELECT TOP '.$number. ' * FROM ('.$sql.') AS itable';
00107 if($orderby !== false) {
00108 $sql.= ' ORDER BY '.$order.' ';
00109 $sql.= (stripos($sort, 'asc') !== false) ? 'DESC' : 'ASC';
00110 }
00111 $sql.= ') AS otable';
00112 if ($orderby!==false) {
00113 $sql.=' ORDER BY '.$order.' '.$sort;
00114 }
00115 return $sql;
00116
00117 }
00118
00125 public function drop_table($table, $if_exists=true){
00126 if($if_exists){
00127 if($this->table_exists($table)){
00128 return $this->query("DROP TABLE $table");
00129 } else {
00130 return true;
00131 }
00132 } else {
00133 return $this->query("DROP TABLE $table");
00134 }
00135 }
00136
00150 public function create_table($table, $definition, $index=array()){
00151 $create_sql = "CREATE TABLE $table (";
00152 if(!is_array($definition)){
00153 new KumbiaException("Definición invalida para crear la tabla '$table'");
00154 return false;
00155 }
00156 $create_lines = array();
00157 $index = array();
00158 $unique_index = array();
00159 $primary = array();
00160 $not_null = "";
00161 $size = "";
00162 foreach($definition as $field => $field_def){
00163 if(isset($field_def['not_null'])){
00164 $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
00165 } else {
00166 $not_null = "";
00167 }
00168 if(isset($field_def['size'])){
00169 $size = $field_def['size'] ? '('.$field_def['size'].')' : '';
00170 } else {
00171 $size = "";
00172 }
00173 if(isset($field_def['index'])){
00174 if($field_def['index']){
00175 $index[] = "INDEX($field)";
00176 }
00177 }
00178 if(isset($field_def['unique_index'])){
00179 if($field_def['unique_index']){
00180 $index[] = "UNIQUE($field)";
00181 }
00182 }
00183 if(isset($field_def['primary'])){
00184 if($field_def['primary']){
00185 $primary[] = "$field";
00186 }
00187 }
00188 if(isset($field_def['auto'])){
00189 if($field_def['auto']){
00190 $field_def['extra'] = isset($field_def['extra']) ? $field_def['extra']." IDENTITY" : "IDENTITY";
00191 }
00192 }
00193 if(isset($field_def['extra'])){
00194 $extra = $field_def['extra'];
00195 } else {
00196 $extra = "";
00197 }
00198 $create_lines[] = "$field ".$field_def['type'].$size.' '.$not_null.' '.$extra;
00199 }
00200 $create_sql.= join(',', $create_lines);
00201 $last_lines = array();
00202 if(count($primary)){
00203 $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')';
00204 }
00205 if(count($index)){
00206 $last_lines[] = join(',', $index);
00207 }
00208 if(count($unique_index)){
00209 $last_lines[] = join(',', $unique_index);
00210 }
00211 if(count($last_lines)){
00212 $create_sql.= ','.join(',', $last_lines).')';
00213 }
00214 return $this->query($create_sql);
00215
00216 }
00217
00223 public function list_tables(){
00224 return $this->fetch_all("SELECT name FROM sysobjects WHERE type = 'U' ORDER BY name");
00225 }
00226
00233 public function describe_table($table, $schema=''){
00234 $describe_table = $this->fetch_all("exec sp_columns @table_name = '$table'");
00235 $final_describe = array();
00236 foreach($describe_table as $field){
00237 $final_describe[] = array(
00238 "Field" => $field["COLUMN_NAME"],
00239 "Type" => $field['LENGTH'] ? $field["TYPE_NAME"] : $field["TYPE_NAME"]."(".$field['LENGTH'].")",
00240 "Null" => $field['NULLABLE'] == 1 ? "YES" : "NO"
00241 );
00242 }
00243 $describe_keys = $this->fetch_all("exec sp_pkeys @table_name = '$table'");
00244 foreach($describe_keys as $field){
00245 for($i=0;$i<=count($final_describe)-1;$i++){
00246 if($final_describe[$i]['Field']==$field['COLUMN_NAME']){
00247 $final_describe[$i]['Key'] = 'PRI';
00248 } else {
00249 $final_describe[$i]['Key'] = "";
00250 }
00251 }
00252 }
00253 return $final_describe;
00254 }
00255
00261 public function last_insert_id($table='', $primary_key=''){
00265 $num = $this->fetch_one("SELECT MAX($primary_key) FROM $table");
00266 return (int) $num[0];
00267 }
00268
00269
00270 }