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