PHP code example of mgcosta / mysql-to-cloud-spanner

1. Go to this page and download the library: Download mgcosta/mysql-to-cloud-spanner library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mgcosta / mysql-to-cloud-spanner example snippets


use MgCosta\MysqlParser\Parser;

$schemaParser = new Parser();

$tableName = 'users';

$table = [
    [
        'Field' => 'id',
        'Type' => 'biginteger unsigned',
        'Null' => 'NO',
        'Key' => 'PRI',
        'Default' => null,
        'Extra' => 'auto_increment'
    ],
    [
        'Field' => 'name',
        'Type' => 'varchar(255)',
        'Null' => 'NO',
        'Key' => '',
        'Default' => null,
        'Extra' => ''
    ],
    [
        'Field' => 'email',
        'Type' => 'varchar(255)',
        'Null' => 'NO',
        'Key' => 'UNI',
        'Default' => null,
        'Extra' => ''
    ],
    [
        'Field' => 'password',
        'Type' => 'varchar(255)',
        'Null' => 'NO',
        'Key' => '',
        'Default' => null,
        'Extra' => ''
    ]
];

$keys = [
    [
        'TABLE_NAME' => 'users',
        'COLUMN_NAME' => 'id',
        'CONSTRAINT_NAME' => 'PRIMARY',
        'REFERENCED_TABLE_NAME' => null,
        'REFERENCED_COLUMN_NAME' => null
    ],
    [
        'TABLE_NAME' => 'users',
        'COLUMN_NAME' => 'email',
        'CONSTRAINT_NAME' => 'users_email_unique',
        'REFERENCED_TABLE_NAME' => null,
        'REFERENCED_COLUMN_NAME' => null
    ]
];

$ddl = $schemaParser->setTableName($tableName)
                    ->setDescribedTable($table)
                    ->setKeys($keys)
                    ->toDDL();
                    
// it will output an array of DDL statements 


$schemaParser = (new Parser())->shouldAssignSemicolon(false);

$ddl = $schemaParser->setTableName($tableName)
                  ->setDescribedTable($table)
                  ->setKeys($keys)
                  ->toDDL();


use MgCosta\MysqlParser\Parser;
use MgCosta\MysqlParser\Dialect;
use MgCosta\MysqlParser\Exceptions\PrimaryKeyNotFoundException;

$schemaParser = new Parser();

$tableName = 'users';

$table = [
    [
        'Field' => 'name',
        'Type' => 'varchar(255)',
        'Null' => 'NO',
        'Key' => '',
        'Default' => null,
        'Extra' => ''
    ],
    [
        'Field' => 'email',
        'Type' => 'varchar(255)',
        'Null' => 'NO',
        'Key' => '',
        'Default' => null,
        'Extra' => ''
    ]
];

// define the default column id for a specific table
$ddl = $schemaParser->setDefaultID('column_id')
                  ->setTableName($tableName)
                  ->setDescribedTable($table)
                  ->setKeys($keys)
                  ->toDDL();

// disable the generation of default id
// it can lead on an exception

try {
    $schemaParser = (new Parser())->shouldAssignPrimaryKey(false);
    
    $ddl = $schemaParser->setTableName($tableName)
                    ->setDescribedTable($table)
                    ->setKeys($keys)
                    ->toDDL();

} catch(PrimaryKeyNotFoundException $e) {

}


use Illuminate\Support\Facades\DB;
use MgCosta\MysqlParser\Parser;
use MgCosta\MysqlParser\Dialect;

$schemaParser = new Parser();
$mysqlDialect = new Dialect();

$databaseName = 'my_database';
$tableName = 'users';

// you can extract the table details doing the following
$table = DB::select(
    DB::raw($mysqlDialect->generateTableDetails($tableName))
);
$keys = DB::select(
    DB::raw($mysqlDialect->generateTableKeysDetails($databaseName, $tableName))
);

$ddl = $schemaParser->setTableName($tableName)
                    ->setDescribedTable($table)
                    ->setKeys($keys)
                    ->toDDL();

use MgCosta\MysqlParser\Transformer\SpannerTransformer;

$table = [
    [
        'Field' => 'name',
        'Type' => 'varchar(255)',
        'Null' => 'NO',
        'Key' => '',
        'Default' => null,
        'Extra' => ''
    ],
    [
        'Field' => 'value',
        'Type' => 'decimal(8,2)',
        'Null' => 'NO',
        'Key' => '',
        'Default' => null,
        'Extra' => ''
    ]
];

$rows = [
     [
        'name' => 'product',
        'value' => '50.20'
    ]
];

$transformer = (new SpannerTransformer())->setDescribedTable($table);
$results = $transformer->setRows($rows)->transform();