Skip to content

Commit

Permalink
support for additional data types in flat table creation
Browse files Browse the repository at this point in the history
  • Loading branch information
sunel committed Sep 7, 2018
1 parent 8edb584 commit d399275
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public function getBackendTable()
if ($this->dataTable === null) {
$backendTable = trim($this->getAttribute('backend_table'));
if (empty($backendTable)) {
$backendTable = $this->getEntity()->getEntityTableName().'_'.$this->getAttribute('backend_type');
$backendTable = $this->getEntity()->getEntityTableName().'_'.strtolower($this->getAttribute('backend_type'));
}
$this->dataTable = $backendTable;
}
Expand Down
24 changes: 0 additions & 24 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Eav;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;

Expand Down Expand Up @@ -224,27 +223,4 @@ public function defaultAttributeSet()
{
return $this->hasOne(AttributeSet::class, 'attribute_set_id', 'default_attribute_set_id');
}

/**
* Describe the table structure, this is used while creating flat table.
*
* @return Illuminate\Support\Collection
*/
public function describe()
{
$table = $this->getAttribute('entity_table');

$connection = \DB::connection();

$database = $connection->getDatabaseName();

$table = $connection->getTablePrefix().$table;

$result = \DB::table('information_schema.columns')
->where('table_schema', $database)
->where('table_name', $table)
->get();

return new Collection(json_decode(json_encode($result), true));
}
}
56 changes: 46 additions & 10 deletions src/Flat/Entity/Complier.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Str;
use Eav\Migrations\SchemaParser;
use Eav\Migrations\SyntaxBuilder;
use Illuminate\Support\Collection;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Command\Command;

Expand Down Expand Up @@ -109,8 +110,8 @@ protected function resolve($file)
}

protected function buildSchema()
{
$table = $this->entity->describe()->map(function ($attribute) {
{
$table = $this->describe($this->entity->entity_table)->map(function ($attribute) {
if ($attribute['COLUMN_KEY'] == 'PRI') {
$schema = "{$attribute['COLUMN_NAME']}:{$this->getColumn($attribute['DATA_TYPE'])}:unsigned";
} else {
Expand Down Expand Up @@ -141,16 +142,22 @@ protected function buildSchema()
}

return $schema;
});


$attributes = $this->collectAttributes()->where('backend_type', '!=', 'static')->get()->map(function ($attribute) {
});

$attributes = $this->collectAttributes()->get()->map(function ($attribute) {

$table = $this->describe($attribute->getBackendTable(), function ($query) {
return $query->where('COLUMN_NAME', 'value');
})->first();

$schema = "{$attribute->getAttributeCode()}";

if ($attribute->getBackendType() == 'decimal') {
$schema .= ":{$this->getColumn($attribute->getBackendType())}(12 , 4)";
if ($attribute->getBackendType() == 'char' || $attribute->getBackendType() == 'string') {
$schema .= ":{$attribute->getBackendType()}({$table['CHARACTER_MAXIMUM_LENGTH']})";
} else if (in_array($attribute->getBackendType(), ['decimal', 'double', 'float', 'unsignedDecimal'])) {
$schema .= ":{$attribute->getBackendType()}({$table['NUMERIC_PRECISION']}, {$table['NUMERIC_SCALE']})";
} else {
$schema .= ":{$this->getColumn($attribute->getBackendType())}";
$schema .= ":{$attribute->getBackendType()}";
}

$schema .= ":nullable";
Expand All @@ -169,6 +176,34 @@ protected function buildSchema()
return (new SyntaxBuilder)->create($schema);
}

/**
* Describe the table structure, this is used while creating flat table.
*
* @return Illuminate\Support\Collection
*/
public function describe($table, $clouser = null)
{
if(is_null($clouser)) {
$clouser = function ($query) {
return $query;
};
}

$connection = \DB::connection();

$database = $connection->getDatabaseName();

$table = $connection->getTablePrefix().$table;

$result = \DB::table('information_schema.columns')
->where('table_schema', $database)
->where('table_name', $table)
->where($clouser)
->get();

return new Collection(json_decode(json_encode($result), true));
}

/**
* Build the directory for the class if necessary.
*
Expand Down Expand Up @@ -271,6 +306,7 @@ protected function getColumn($type)

protected function collectAttributes()
{
return $this->entity->attributes();
return $this->entity->attributes()
->whereNotIn('backend_type', ['static', '']);
}
}

0 comments on commit d399275

Please sign in to comment.