MysqlAdapter.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @package ActiveRecord
  4. */
  5. namespace ActiveRecord;
  6. /**
  7. * Adapter for MySQL.
  8. *
  9. * @package ActiveRecord
  10. */
  11. class MysqlAdapter extends Connection
  12. {
  13. static $DEFAULT_PORT = 3306;
  14. public function limit($sql, $offset, $limit)
  15. {
  16. $offset = is_null($offset) ? '' : intval($offset) . ',';
  17. $limit = intval($limit);
  18. return "$sql LIMIT {$offset}$limit";
  19. }
  20. public function query_column_info($table)
  21. {
  22. return $this->query("SHOW COLUMNS FROM $table");
  23. }
  24. public function query_for_tables()
  25. {
  26. return $this->query('SHOW TABLES');
  27. }
  28. public function create_column(&$column)
  29. {
  30. $c = new Column();
  31. $c->inflected_name = Inflector::instance()->variablize($column['field']);
  32. $c->name = $column['field'];
  33. $c->nullable = ($column['null'] === 'YES' ? true : false);
  34. $c->pk = ($column['key'] === 'PRI' ? true : false);
  35. $c->auto_increment = ($column['extra'] === 'auto_increment' ? true : false);
  36. if ($column['type'] == 'timestamp' || $column['type'] == 'datetime')
  37. {
  38. $c->raw_type = 'datetime';
  39. $c->length = 19;
  40. }
  41. elseif ($column['type'] == 'date')
  42. {
  43. $c->raw_type = 'date';
  44. $c->length = 10;
  45. }
  46. elseif ($column['type'] == 'time')
  47. {
  48. $c->raw_type = 'time';
  49. $c->length = 8;
  50. }
  51. else
  52. {
  53. preg_match('/^([A-Za-z0-9_]+)(\(([0-9]+(,[0-9]+)?)\))?/',$column['type'],$matches);
  54. $c->raw_type = (count($matches) > 0 ? $matches[1] : $column['type']);
  55. if (count($matches) >= 4)
  56. $c->length = intval($matches[3]);
  57. }
  58. $c->map_raw_type();
  59. $c->default = $c->cast($column['default'],$this);
  60. return $c;
  61. }
  62. public function set_encoding($charset)
  63. {
  64. $params = array($charset);
  65. $this->query('SET NAMES ?',$params);
  66. }
  67. public function accepts_limit_and_order_for_update_and_delete() { return true; }
  68. public function native_database_types()
  69. {
  70. return array(
  71. 'primary_key' => 'int(11) UNSIGNED DEFAULT NULL auto_increment PRIMARY KEY',
  72. 'string' => array('name' => 'varchar', 'length' => 255),
  73. 'text' => array('name' => 'text'),
  74. 'integer' => array('name' => 'int', 'length' => 11),
  75. 'float' => array('name' => 'float'),
  76. 'datetime' => array('name' => 'datetime'),
  77. 'timestamp' => array('name' => 'datetime'),
  78. 'time' => array('name' => 'time'),
  79. 'date' => array('name' => 'date'),
  80. 'binary' => array('name' => 'blob'),
  81. 'boolean' => array('name' => 'tinyint', 'length' => 1)
  82. );
  83. }
  84. }
  85. ?>