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/ */
$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();