Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method createFromFillable refactored for more databases support #51

Merged

Conversation

vs0uz4
Copy link
Contributor

@vs0uz4 vs0uz4 commented Aug 17, 2021

createFromFillable method refactored to support the databases provided by the laravel framework.

As the method initially made use of DB::select, using the SQL describe table_name, but describe is not supported by PostgreSQL for example so I decided to use Facade Schema's getDoctrineColumn method.

getDoctrineColumn method returns the instance of \Doctrine\DBAL\Schema\Column class.

Couple of methods from \Doctrine\DBAL\Schema\Column class:

$type->getName();    // returns string
$type->getNotnull(); // returns true/false
$type->getDefault(); // returns string or null
$type->getType();    // returns instance of \Doctrine\DBAL\Types\Type
$type->getLength();  // returns int or null

The method that interests us is the getType, but in the future if we need to check if a certain column is nullable or for example its size, we will have this flexibility.

In this way, all information about the column is retrieved with:

$column = Schema::getConnection()->getDoctrineColumn($model->getTable(), $field);

And column type checks, we can easily retrieve as follows:

$column->getType()->getName()  // returns a string containing the column type

Therefore, the column type checks, where we actually formatted and assembled the livewire powergrid component, looked like this

if ($column->getType()->getName() === 'datetime')  {
    $dataSource .= "\n" . '            ->addColumn(\'' . $field . '_formatted\', function(' . $modelLastName . ' $model) { ' . "\n" . '                return Carbon::parse($model->' . $field . ')->format(\'d/m/Y H:i:s\');' . "\n" . '            })';
    $columns .= '            Column::add()' . "\n" . '                ->title(__(\'' . $title . '\'))' . "\n" . '                ->field(\'' . $field . '_formatted\')' . "\n" . '                ->searchable()' . "\n" . '                ->sortable()' . "\n" . '                ->makeInputDatePicker(\'' . $field . '\'),' . "\n\n";

    continue;
}

if ($column->getType()->getName() === 'date') {
    $dataSource .= "\n" . '            ->addColumn(\'' . $field . '_formatted\', function(' . $modelLastName . ' $model) { ' . "\n" . '                return Carbon::parse($model->' . $field . ')->format(\'d/m/Y\');' . "\n" . '            })';
    $columns .= '            Column::add()' . "\n" . '                ->title(__(\'' . $title . '\'))' . "\n" . '                ->field(\'' . $field . '_formatted\')' . "\n" . '                ->searchable()' . "\n" . '                ->sortable()' . "\n" . '                ->makeInputDatePicker(\'' . $field . '\'),' . "\n\n";

    continue;
}

if ($column->getType()->getName() === 'boolean') {
    $dataSource .= "\n" . '            ->addColumn(\'' . $field . '\')';
    $columns    .= '            Column::add()' . "\n" . '                ->title(__(\'' . $title . '\'))' . "\n" . '                ->field(\'' . $field . '\')' . "\n" . '                ->toggleable(),' . "\n\n";

    continue;
}

if (in_array($column->getType()->getName(), ['smallint', 'integer', 'bigint'])) {
    $dataSource .= "\n" . '            ->addColumn(\'' . $field . '\')';
    $columns    .= '            Column::add()' . "\n" . '                ->title(__(\'' . $title . '\'))' . "\n" . '                ->field(\'' . $field . '\')' . "\n" . '                ->makeInputRange(),' . "\n\n";

    continue;
}

if ($column->getType()->getName() === 'string') {
    $dataSource .= "\n" . '            ->addColumn(\'' . $field . '\')';
    $columns    .= '            Column::add()' . "\n" . '                ->title(__(\'' . $title . '\'))' . "\n" . '                ->field(\'' . $field . '\')' . "\n" . '                ->sortable()' . "\n" . '                ->searchable()' . "\n" . '                ->makeInputText(),' . "\n\n";

    continue;
}

To test the changes made, I created a repository with code for a demo laravel application. This repository can be found at URL https://github.com/vs0uz4/powergrid-demo.

For information, the migration code that creates the table used in the tests is shown below

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->id();
        $table->integer('code');
        $table->string('name');
        $table->string('description');
        $table->tinyInteger('status')->default(0);
        $table->boolean('enabled')->default(false);
        $table->timestamps();
        $table->date('publication_at');
        $table->dateTime('enabled_at');
    });
}

And by way of comparison, the values ​​referring to the types of columns returned using the describe SQL sentence or using the getDoctrineColumn() method are displayed in the table below.

tabela de tipos de colunas

⚠️ As you can see problems with MS SQL Server support were encountered.

As the demo application uses Laravel Sail and it does not support the Microsoft database, it was not possible to reproduce the tests in this environment.

📓 However, in the near future, I intend to create the application environment with MS SQL Server support and I will be giving greater feedbaks.

For information, follow the structures of the category table in the tested databases.

MySQL
mysql_categories_table

PostgreSQL
postgresql_categories_table

SQLite
sqlite_categories_table

Result is!
powergrid

@luanfreitasdev
Copy link
Collaborator

Cool! This completely solves the creation problem with Postgress and SQLite, it looks really good. Congratulations and thanks!

@luanfreitasdev luanfreitasdev merged commit 0065b4a into Power-Components:component-not-found Aug 17, 2021
@luanfreitasdev luanfreitasdev added the enhancement New feature or request label Oct 2, 2021
@vs0uz4 vs0uz4 deleted the component-not-found branch November 2, 2021 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants