00001 <?php 00023 class DbPdoSQLite extends DbPDO { 00024 00028 protected $db_rbdm = "sqlite"; 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=''){ 00081 $table = strtolower($table); 00082 $num = $this->fetch_one("SELECT COUNT(*) FROM sqlite_master WHERE name = '$table'"); 00083 return $num[0]; 00084 } 00085 00092 public function limit($sql){ 00093 $params = Util::getParams(func_get_args()); 00094 $sql_new = $sql; 00095 00096 if(isset($params['limit']) && is_numeric($params['limit'])){ 00097 $sql_new.=" LIMIT $params[limit]"; 00098 } 00099 00100 if(isset($params['offset']) && is_numeric($params['offset'])){ 00101 $sql_new.=" OFFSET $params[offset]"; 00102 } 00103 00104 return $sql_new; 00105 } 00106 00113 public function drop_table($table, $if_exists=true){ 00114 if($if_exists){ 00115 return $this->query("DROP TABLE IF EXISTS $table"); 00116 } else { 00117 return $this->query("DROP TABLE $table"); 00118 } 00119 } 00120 00134 public function create_table($table, $definition, $index=array()){ 00135 $create_sql = "CREATE TABLE $table ("; 00136 if(!is_array($definition)){ 00137 new KumbiaException("Definición invalida para crear la tabla '$table'"); 00138 return false; 00139 } 00140 $create_lines = array(); 00141 $index = array(); 00142 $unique_index = array(); 00143 $primary = array(); 00144 $not_null = ""; 00145 $size = ""; 00146 foreach($definition as $field => $field_def){ 00147 if(isset($field_def['not_null'])){ 00148 $not_null = $field_def['not_null'] ? 'NOT NULL' : ''; 00149 } else { 00150 $not_null = ""; 00151 } 00152 if(isset($field_def['size'])){ 00153 $size = $field_def['size'] ? '('.$field_def['size'].')' : ''; 00154 } else { 00155 $size = ""; 00156 } 00157 if(isset($field_def['index'])){ 00158 if($field_def['index']){ 00159 $index[] = "INDEX($field)"; 00160 } 00161 } 00162 if(isset($field_def['unique_index'])){ 00163 if($field_def['unique_index']){ 00164 $index[] = "UNIQUE($field)"; 00165 } 00166 } 00167 if(isset($field_def['primary'])){ 00168 if($field_def['primary']){ 00169 $primary[] = "$field"; 00170 } 00171 } 00172 if(isset($field_def['auto'])){ 00173 if($field_def['auto']){ 00174 $not_null = ""; 00175 } 00176 } 00177 if(isset($field_def['extra'])){ 00178 $extra = $field_def['extra']; 00179 } else { 00180 $extra = ""; 00181 } 00182 $create_lines[] = "$field ".$field_def['type'].$size.' '.$not_null.' '.$extra; 00183 } 00184 $create_sql.= join(',', $create_lines); 00185 $last_lines = array(); 00186 if(count($primary)){ 00187 $last_lines[] = 'PRIMARY KEY('.join(",", $primary).')'; 00188 } 00189 if(count($index)){ 00190 $last_lines[] = join(',', $index); 00191 } 00192 if(count($unique_index)){ 00193 $last_lines[] = join(',', $unique_index); 00194 } 00195 if(count($last_lines)){ 00196 $create_sql.= ','.join(',', $last_lines).')'; 00197 } 00198 return $this->exec($create_sql); 00199 00200 } 00201 00207 public function list_tables(){ 00208 return $this->fetch_all("SELECT name FROM sqlite_master WHERE type='table' ". 00209 "UNION ALL SELECT name FROM sqlite_temp_master ". 00210 "WHERE type='table' ORDER BY name"); 00211 } 00212 00219 public function describe_table($table, $schema=''){ 00220 $fields = array(); 00221 if(!$schema){ 00222 $results = $this->fetch_all("PRAGMA table_info($table)", DB::DB_ASSOC); 00223 } else { 00224 $results = $this->fetch_all("PRAGMA table_info($schema.$table)", DB::DB_ASSOC); 00225 } 00226 foreach($results as $field){ 00227 $fields[] = array( 00228 "Field" => $field["name"], 00229 "Type" => $field["type"], 00230 "Null" => $field["notnull"] == 99 ? "YES" : "NO", 00231 "Key" => $field['pk'] == 1 ? "PRI" : "" 00232 ); 00233 } 00234 return $fields; 00235 } 00236 00237 }