public function create_column($column) { $column['column_name'] = strtolower($column['column_name']); $column['data_type'] = strtolower(preg_replace('/\\(.*?\\)/', '', $column['data_type'])); if ($column['data_default'] !== null) { $column['data_default'] = trim($column['data_default'], "' "); } if ($column['data_type'] == 'number') { if ($column['data_scale'] > 0) { $column['data_type'] = 'decimal'; } elseif ($column['data_scale'] == 0) { $column['data_type'] = 'int'; } } $c = new Column(); $c->inflected_name = Inflector::instance()->variablize($column['column_name']); $c->name = $column['column_name']; $c->nullable = $column['nullable'] == 'Y' ? true : false; $c->pk = $column['pk'] == 'P' ? true : false; $c->length = $column['data_length']; if ($column['data_type'] == 'timestamp') { $c->raw_type = 'datetime'; } else { $c->raw_type = $column['data_type']; } $c->map_raw_type(); $c->default = $c->cast($column['data_default']); return $c; }
public function create_column($column) { $c = new Column(); $c->inflected_name = Inflector::instance()->variablize($column['name']); $c->name = $column['name']; $c->nullable = $column['notnull'] ? false : true; $c->pk = $column['pk'] ? true : false; $c->auto_increment = $column['type'] == 'INTEGER' && $c->pk; $column['type'] = preg_replace('/ +/', ' ', $column['type']); $column['type'] = str_replace(array('(', ')'), ' ', $column['type']); $column['type'] = Utils::squeeze(' ', $column['type']); $matches = explode(' ', $column['type']); if (!empty($matches)) { $c->raw_type = strtolower($matches[0]); if (count($matches) > 1) { $c->length = intval($matches[1]); } } $c->map_raw_type(); if ($c->type == Column::DATETIME) { $c->length = 19; } elseif ($c->type == Column::DATE) { $c->length = 10; } // From SQLite3 docs: The value is a signed integer, stored in 1, 2, 3, 4, 6, // or 8 bytes depending on the magnitude of the value. // so is it ok to assume it's possible an int can always go up to 8 bytes? if ($c->type == Column::INTEGER && !$c->length) { $c->length = 8; } $c->default = $c->cast($column['dflt_value'], $this); return $c; }
public function createColumn(&$column) { $c = new Column(); $c->inflected_name = Inflector::instance()->variablize($column['field']); $c->name = $column['field']; $c->nullable = $column['null'] === 'YES' ? true : false; $c->pk = $column['key'] === 'PRI' ? true : false; $c->auto_increment = $column['extra'] === 'auto_increment' ? true : false; if ($column['type'] == 'timestamp' || $column['type'] == 'datetime') { $c->raw_type = 'datetime'; $c->length = 19; } elseif ($column['type'] == 'date') { $c->raw_type = 'date'; $c->length = 10; } elseif ($column['type'] == 'time') { $c->raw_type = 'time'; $c->length = 8; } else { \preg_match('/^([A-Za-z0-9_]+)(\\(([0-9]+(,[0-9]+)?)\\))?/', $column['type'], $matches); $c->raw_type = \count($matches) > 0 ? $matches[1] : $column['type']; if (\count($matches) >= 4) { $c->length = \intval($matches[3]); } } $c->mapRawType(); $c->default = $c->cast($column['default'], $this); return $c; }
public function create_column(&$column) { $c = new Column(); $c->inflected_name = Inflector::instance()->variablize($column['field']); $c->name = $column['field']; $c->nullable = $column['nullable'] === 'YES' ? true : false; $c->auto_increment = $column['extra'] === '1' ? true : false; $c->pk = $column['pk'] === 'PRIMARY KEY' ? true : false; $c->raw_type = $column['data_type']; $c->length = $column['length'] ? $column['length'] : $column['radix']; if ($c->raw_type == 'text') { $c->length = null; } if ($c->raw_type == 'datetime') { $c->length = 19; } $c->map_raw_type(); $c->default = $c->cast(preg_replace("#\\(+'?(.*?)'?\\)+#", '$1', $column['data_default']), $this); return $c; }
public function createColumn(&$column) { $c = new Column(); $c->inflected_name = Inflector::instance()->variablize($column['field']); $c->name = $column['field']; $c->nullable = $column['not_nullable'] ? false : true; $c->pk = $column['pk'] ? true : false; $c->auto_increment = false; if (\substr($column['type'], 0, 9) == 'timestamp') { $c->raw_type = 'datetime'; $c->length = 19; } elseif ($column['type'] == 'date') { $c->raw_type = 'date'; $c->length = 10; } else { \preg_match('/^([A-Za-z0-9_]+)(\\(([0-9]+(,[0-9]+)?)\\))?/', $column['type'], $matches); $c->raw_type = \count($matches) > 0 ? $matches[1] : $column['type']; $c->length = \count($matches) >= 4 ? \intval($matches[3]) : \intval($column['attlen']); if ($c->length < 0) { $c->length = null; } } $c->mapRawType(); if ($column['default']) { \preg_match("/^nextval\\('(.*)'\\)\$/", $column['default'], $matches); if (\count($matches) == 2) { $c->sequence = $matches[1]; } else { $c->default = $c->cast($column['default'], $this); } } return $c; }
public function test_empty_and_null_datetime_strings_should_return_null() { $column = new Column(); $column->type = Column::DATETIME; $this->assert_equals(null, $column->cast(null, $this->conn)); $this->assert_equals(null, $column->cast('', $this->conn)); }
public function test_ar_date_time_attribute_copies_exact_tz() { $dt = new DateTime(null, new \DateTimeZone('America/New_York')); $column = new Column(); $column->type = Column::DATETIME; $dt2 = $column->cast($dt, $this->conn); $this->assert_equals($dt->getTimestamp(), $dt2->getTimestamp()); $this->assert_equals($dt->getTimeZone(), $dt2->getTimeZone()); $this->assert_equals($dt->getTimeZone()->getName(), $dt2->getTimeZone()->getName()); }
public function testEmptyAndNullDatetimeStringsShouldReturnNull() { $column = new Column(); $column->type = Column::DATETIME; $this->assertEquals(null, $column->cast(null, $this->conn)); $this->assertEquals(null, $column->cast('', $this->conn)); }