KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
sqlite.php
Ir a la documentación de este archivo.
1 <?php
26 require_once CORE_PATH . 'libs/db/adapters/pdo.php';
27 
35 class DbPdoSQLite extends DbPDO
36 {
37 
41  protected $db_rbdm = "sqlite";
42 
47  const TYPE_INTEGER = "INTEGER";
48 
53  const TYPE_DATE = "DATE";
54 
59  const TYPE_VARCHAR = "VARCHAR";
60 
65  const TYPE_DECIMAL = "DECIMAL";
66 
71  const TYPE_DATETIME = "DATETIME";
72 
77  const TYPE_CHAR = "CHAR";
78 
83  public function initialize()
84  {
85 
86  }
87 
94  public function table_exists($table, $schema='')
95  {
96  $table = strtolower($table);
97  $num = $this->fetch_one("SELECT COUNT(*) FROM sqlite_master WHERE name = '$table'");
98  return $num[0];
99  }
100 
107  public function limit($sql)
108  {
109  $params = Util::getParams(func_get_args());
110  $sql_new = $sql;
111 
112  if (isset($params['limit']) && is_numeric($params['limit'])) {
113  $sql_new.=" LIMIT $params[limit]";
114  }
115 
116  if (isset($params['offset']) && is_numeric($params['offset'])) {
117  $sql_new.=" OFFSET $params[offset]";
118  }
119 
120  return $sql_new;
121  }
122 
129  public function drop_table($table, $if_exists=true)
130  {
131  if ($if_exists) {
132  return $this->query("DROP TABLE IF EXISTS $table");
133  } else {
134  return $this->query("DROP TABLE $table");
135  }
136  }
137 
151  public function create_table($table, $definition, $index=array())
152  {
153  $create_sql = "CREATE TABLE $table (";
154  if (!is_array($definition)) {
155  new KumbiaException("Definici&oacute;n invalida para crear la tabla '$table'");
156  return false;
157  }
158  $create_lines = array();
159  $index = array();
160  $unique_index = array();
161  $primary = array();
162  //$not_null = "";
163  //$size = "";
164  foreach ($definition as $field => $field_def) {
165  if (isset($field_def['not_null'])) {
166  $not_null = $field_def['not_null'] ? 'NOT NULL' : '';
167  } else {
168  $not_null = "";
169  }
170  if (isset($field_def['size'])) {
171  $size = $field_def['size'] ? '(' . $field_def['size'] . ')' : '';
172  } else {
173  $size = "";
174  }
175  if (isset($field_def['index'])) {
176  if ($field_def['index']) {
177  $index[] = "INDEX($field)";
178  }
179  }
180  if (isset($field_def['unique_index'])) {
181  if ($field_def['unique_index']) {
182  $index[] = "UNIQUE($field)";
183  }
184  }
185  if (isset($field_def['primary'])) {
186  if ($field_def['primary']) {
187  $primary[] = "$field";
188  }
189  }
190  if (isset($field_def['auto'])) {
191  if ($field_def['auto']) {
192  $not_null = "";
193  }
194  }
195  if (isset($field_def['extra'])) {
196  $extra = $field_def['extra'];
197  } else {
198  $extra = "";
199  }
200  $create_lines[] = "$field " . $field_def['type'] . $size . ' ' . $not_null . ' ' . $extra;
201  }
202  $create_sql.= join(',', $create_lines);
203  $last_lines = array();
204  if (count($primary)) {
205  $last_lines[] = 'PRIMARY KEY(' . join(",", $primary) . ')';
206  }
207  if (count($index)) {
208  $last_lines[] = join(',', $index);
209  }
210  if (count($unique_index)) {
211  $last_lines[] = join(',', $unique_index);
212  }
213  if (count($last_lines)) {
214  $create_sql.= ',' . join(',', $last_lines) . ')';
215  }
216  return $this->exec($create_sql);
217  }
218 
224  public function list_tables()
225  {
226  return $this->fetch_all("SELECT name FROM sqlite_master WHERE type='table' " .
227  "UNION ALL SELECT name FROM sqlite_temp_master " .
228  "WHERE type='table' ORDER BY name");
229  }
230 
237  public function describe_table($table, $schema='')
238  {
239  $fields = array();
240  if (!$schema) {
241  $results = $this->fetch_all("PRAGMA table_info($table)", self::DB_ASSOC);
242  } else {
243  $results = $this->fetch_all("PRAGMA table_info($schema.$table)", self::DB_ASSOC);
244  }
245  foreach ($results as $field) {
246  $fields[] = array(
247  "Field" => $field["name"],
248  "Type" => $field["type"],
249  "Null" => $field["notnull"] == 99 ? "YES" : "NO",
250  "Key" => $field['pk'] == 1 ? "PRI" : ""
251  );
252  }
253  return $fields;
254  }
255 
256 }