SqliteAdapter.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @package ActiveRecord
  4. */
  5. namespace ActiveRecord;
  6. use PDO;
  7. /**
  8. * Adapter for SQLite.
  9. *
  10. * @package ActiveRecord
  11. */
  12. class SqliteAdapter extends Connection
  13. {
  14. protected function __construct($info)
  15. {
  16. if (!file_exists($info->host))
  17. throw new DatabaseException("Could not find sqlite db: $info->host");
  18. $this->connection = new PDO("sqlite:$info->host",null,null,static::$PDO_OPTIONS);
  19. }
  20. public function limit($sql, $offset, $limit)
  21. {
  22. $offset = is_null($offset) ? '' : intval($offset) . ',';
  23. $limit = intval($limit);
  24. return "$sql LIMIT {$offset}$limit";
  25. }
  26. public function query_column_info($table)
  27. {
  28. return $this->query("pragma table_info($table)");
  29. }
  30. public function query_for_tables()
  31. {
  32. return $this->query("SELECT name FROM sqlite_master");
  33. }
  34. public function create_column($column)
  35. {
  36. $c = new Column();
  37. $c->inflected_name = Inflector::instance()->variablize($column['name']);
  38. $c->name = $column['name'];
  39. $c->nullable = $column['notnull'] ? false : true;
  40. $c->pk = $column['pk'] ? true : false;
  41. $c->auto_increment = in_array(
  42. strtoupper($column['type']),
  43. array('INT', 'INTEGER')
  44. ) && $c->pk;
  45. $column['type'] = preg_replace('/ +/',' ',$column['type']);
  46. $column['type'] = str_replace(array('(',')'),' ',$column['type']);
  47. $column['type'] = Utils::squeeze(' ',$column['type']);
  48. $matches = explode(' ',$column['type']);
  49. if (!empty($matches))
  50. {
  51. $c->raw_type = strtolower($matches[0]);
  52. if (count($matches) > 1)
  53. $c->length = intval($matches[1]);
  54. }
  55. $c->map_raw_type();
  56. if ($c->type == Column::DATETIME)
  57. $c->length = 19;
  58. elseif ($c->type == Column::DATE)
  59. $c->length = 10;
  60. // From SQLite3 docs: The value is a signed integer, stored in 1, 2, 3, 4, 6,
  61. // or 8 bytes depending on the magnitude of the value.
  62. // so is it ok to assume it's possible an int can always go up to 8 bytes?
  63. if ($c->type == Column::INTEGER && !$c->length)
  64. $c->length = 8;
  65. $c->default = $c->cast($column['dflt_value'],$this);
  66. return $c;
  67. }
  68. public function set_encoding($charset)
  69. {
  70. throw new ActiveRecordException("SqliteAdapter::set_charset not supported.");
  71. }
  72. public function accepts_limit_and_order_for_update_and_delete() { return true; }
  73. public function native_database_types()
  74. {
  75. return array(
  76. 'primary_key' => 'integer not null primary key',
  77. 'string' => array('name' => 'varchar', 'length' => 255),
  78. 'text' => array('name' => 'text'),
  79. 'integer' => array('name' => 'integer'),
  80. 'float' => array('name' => 'float'),
  81. 'decimal' => array('name' => 'decimal'),
  82. 'datetime' => array('name' => 'datetime'),
  83. 'timestamp' => array('name' => 'datetime'),
  84. 'time' => array('name' => 'time'),
  85. 'date' => array('name' => 'date'),
  86. 'binary' => array('name' => 'blob'),
  87. 'boolean' => array('name' => 'boolean')
  88. );
  89. }
  90. }
  91. ?>