Skip to content

2. Table definition

Bogdans edited this page Mar 18, 2018 · 2 revisions

All tables are defined in the special instructions files and then initialized in code

Instructions file example for ER Table

<?php

return [
    # location of the table file
    'location' => '~/storage/my-table',
    # table structure
    'structure' => [
        array (
            'name' => 'ID',
            'type' => 1,   # integer
            'size' => 4,   # size of integer
        ),
        array (
            'name' => 'Profit',
            'type' => 2,   # decimal
            'size' => 8,   # size of decimal
        ),
        array (
            'name' => 'ProductTypeReference',
            'type' => 1,   # integer
            'size' => 4,   # size of integer
        ),
        array (
            'name' => 'ProductTitle',
            'type' => 3,   # string
            'size' => 100, # size of string
        ),
    ]
];

Important notes for table definition

  • changing table structure currently is not supported
  • column names can be changed
  • column order in the structure cannot be changed
  • string takes 1 additional byte for end of string definition, which means that size of 5 allows 4 symbols to be written

Instructions file example generation for ER Table

Instructions file can be easily generated using the library means

<?php

use DataManagement\Model\EntityRelationship\Table;
use DataManagement\Model\TableHelper;

$table = new Table();
$table->addColumn('ID', TableHelper::COLUMN_TYPE_INTEGER);
$table->addColumn('Profit', TableHelper::COLUMN_TYPE_FLOAT);
$table->addColumn('ProductType', TableHelper::COLUMN_TYPE_INTEGER);
$table->addColumn('ProductTypeReference', TableHelper::COLUMN_TYPE_INTEGER);
$table->addColumn('ProductTitle', TableHelper::COLUMN_TYPE_STRING, 100);

$structure = var_export($table->structure(), true);
$location = '~/storage/my-table';

$instructions = <<<EOT
<?php
return [
    'location' => '{$location}',
    'structure' => {$structure}
];
EOT;
$instructionFileDestination = __DIR__ . '/../instructions/users.php';
# create the file
touch($instructionFileDestination);
# make it writable by everyone
chmod($instructionFileDestination, 0777);
# write the contents
file_put_contents($instructionFileDestination, $instructions);

Instructions file example for Columnar Table

Columnar table implements partitioning by default.

Difference in data definition from ER Table

  • you need to define exactly one partitioning column
  • location for Columnar table will be a folder, not a file
<?php

return [
    'location' => '~/storage/my-table',
    'structure' => [
            array (
                'name' => 'ID',
                'type' => 1,
                'size' => 4,
                'partition' => 0
            ),
            array (
                'name' => 'Date',
                'type' => 3,
                'size' => 11,
                'partition' => 1
            ),
    ]
];

Creating a table from instructions

<?php

# ER Table
$instructionFileDestinationER = '/project-root/my-table-instruction-er.php';
$erTable = \DataManagement\Model\EntityRelationship\Table::newFromInstructionsFile($instructionFileDestinationER);

# Columnar table
$instructionFileDestinationColumnar = '/project-root/my-table-instruction-columnar.php';
$columnarTable = \DataManagement\Model\Columnar\Table::newFromInstructionsFile($instructionFileDestinationColumnar);