Skip to content
Permalink
Browse files

Optimizing a bit more the default type inference in the Query

  • Loading branch information...
lorenzo committed Oct 29, 2015
1 parent 084ef76 commit c7fa2cca453a8c5908c0202aa62fa4cb9a0e69d4
Showing with 24 additions and 4 deletions.
  1. +20 −0 src/Database/Schema/Table.php
  2. +1 −1 src/Database/TypeMapTrait.php
  3. +3 −3 src/ORM/Query.php
@@ -46,6 +46,13 @@ class Table
*/
protected $_columns = [];
/**
* A map with columns to types
*
* @var array
*/
protected $_typeMap = [];
/**
* Indexes in the table.
*
@@ -297,6 +304,7 @@ public function addColumn($name, $attrs)
}
$attrs = array_intersect_key($attrs, $valid);
$this->_columns[$name] = $attrs + $valid;
$this->_typeMap[$name] = $this->_columns[$name]['type'];
return $this;
}
@@ -341,6 +349,7 @@ public function columnType($name, $type = null)
}
if ($type !== null) {
$this->_columns[$name]['type'] = $type;
$this->_typeMap[$name] = $type;
}
return $this->_columns[$name]['type'];
}
@@ -371,6 +380,17 @@ public function baseColumnType($column)
return $this->_columns[$column]['baseType'] = $type;
}
/**
* Returns an array where the kyes are the column names in the schema
* and the values the database type they have.
*
* @return array
*/
public function typeMap()
{
return $this->_typeMap;
}
/**
* Check whether or not a field is nullable
*
@@ -34,7 +34,7 @@ trait TypeMapTrait
*/
public function typeMap($typeMap = null)
{
if (!$this->_typeMap) {
if ($this->_typeMap === null) {
$this->_typeMap = new TypeMap();
}
if ($typeMap === null) {
@@ -170,10 +170,10 @@ public function select($fields = [], $overwrite = false)
public function addDefaultTypes(Table $table)
{
$alias = $table->alias();
$schema = $table->schema();
$map = $table->schema()->typeMap();
$fields = [];
foreach ($schema->columns() as $f) {
$fields[$f] = $fields[$alias . '.' . $f] = $schema->columnType($f);
foreach ($map as $f => $type) {
$fields[$f] = $fields[$alias . '.' . $f] = $type;
}
$this->typeMap()->addDefaults($fields);

0 comments on commit c7fa2cc

Please sign in to comment.
You can’t perform that action at this time.