Created
February 1, 2019 04:19
-
-
Save abbadon1334/cd5394ccc8bf0b411c7d75a60215578e to your computer and use it in GitHub Desktop.
create transcode array for database schema in atk/schema using ikkez/f3-schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Copyright (c) 2019. | |
* | |
* Francesco "Abbadon1334" Danti <fdanti@gmail.com> | |
* | |
* Permission is hereby granted, free of charge, to any person | |
* obtaining a copy of this software and associated documentation | |
* files (the "Software"), to deal in the Software without | |
* restriction, including without limitation the rights to use, | |
* copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the | |
* Software is furnished to do so, subject to the following | |
* conditions: | |
* | |
* The above copyright notice and this permission notice shall be | |
* included in all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
* OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
$dataTypes = array( | |
'BOOLEAN' => array('mysql' => 'tinyint(1)', | |
'sqlite2?|pgsql' => 'BOOLEAN', | |
'mssql|sybase|dblib|odbc|sqlsrv' => 'bit', | |
'ibm' => 'numeric(1,0)', | |
), | |
'INT1' => array('mysql' => 'tinyint(4)', | |
'sqlite2?' => 'integer(4)', | |
'mssql|sybase|dblib|odbc|sqlsrv' => 'tinyint', | |
'pgsql|ibm' => 'smallint', | |
), | |
'INT2' => array('mysql' => 'smallint(6)', | |
'sqlite2?' => 'integer(6)', | |
'pgsql|ibm|mssql|sybase|dblib|odbc|sqlsrv' => 'smallint', | |
), | |
'INT4' => array('sqlite2?' => 'integer(11)', | |
'pgsql|ibm' => 'integer', | |
'mysql' => 'int(11)', | |
'mssql|dblib|sybase|odbc|sqlsrv' => 'int', | |
), | |
'INT8' => array('sqlite2?' => 'integer(20)', | |
'pgsql|mssql|sybase|dblib|odbc|sqlsrv|ibm' => 'bigint', | |
'mysql' => 'bigint(20)', | |
), | |
'FLOAT' => array('mysql|sqlite2?' => 'FLOAT', | |
'pgsql' => 'double precision', | |
'mssql|sybase|dblib|odbc|sqlsrv' => 'float', | |
'ibm' => 'decfloat' | |
), | |
'DOUBLE' => array('mysql|ibm' => 'decimal(18,6)', | |
'sqlite2?' => 'decimal(15,6)', // max 15-digit on sqlite | |
'pgsql' => 'numeric(18,6)', | |
'mssql|dblib|sybase|odbc|sqlsrv' => 'decimal(18,6)', | |
), | |
'VARCHAR128' => array('mysql|sqlite2?|ibm|mssql|sybase|dblib|odbc|sqlsrv' => 'varchar(128)', | |
'pgsql' => 'character varying(128)', | |
), | |
'VARCHAR256' => array('mysql|sqlite2?|ibm|mssql|sybase|dblib|odbc|sqlsrv' => 'varchar(255)', | |
'pgsql' => 'character varying(255)', | |
), | |
'VARCHAR512' => array('mysql|sqlite2?|ibm|mssql|sybase|dblib|odbc|sqlsrv' => 'varchar(512)', | |
'pgsql' => 'character varying(512)', | |
), | |
'TEXT' => array('mysql|sqlite2?|pgsql|mssql' => 'text', | |
'sybase|dblib|odbc|sqlsrv' => 'nvarchar(max)', | |
'ibm' => 'BLOB SUB_TYPE TEXT', | |
), | |
'LONGTEXT' => array('mysql' => 'LONGTEXT', | |
'sqlite2?|pgsql|mssql' => 'text', | |
'sybase|dblib|odbc|sqlsrv' => 'nvarchar(max)', | |
'ibm' => 'CLOB(2000000000)', | |
), | |
'DATE' => array('mysql|sqlite2?|pgsql|mssql|sybase|dblib|odbc|sqlsrv|ibm' => 'date', | |
), | |
'DATETIME' => array('pgsql' => 'timestamp without time zone', | |
'mysql|sqlite2?|mssql|sybase|dblib|odbc|sqlsrv' => 'datetime', | |
'ibm' => 'timestamp', | |
), | |
'TIMESTAMP' => array('mysql|ibm' => 'timestamp', | |
'pgsql|odbc' => 'timestamp without time zone', | |
'sqlite2?|mssql|sybase|dblib|sqlsrv'=>'DATETIME', | |
), | |
'BLOB' => array('mysql|odbc|sqlite2?|ibm' => 'blob', | |
'pgsql' => 'bytea', | |
'mssql|sybase|dblib' => 'image', | |
'sqlsrv' => 'varbinary(max)', | |
), | |
); | |
$res = []; | |
foreach($dataTypes as $type => $dt) | |
{ | |
foreach($dt as $driver_string => $TypeLen) | |
{ | |
$tl = explode('(',trim($TypeLen,')')); | |
$options = []; | |
$options['type'] = $tl[0]; | |
if(isset($tl[1])) | |
{ | |
$options['len'] = is_numeric($tl[1]) ? (int) $tl[1] : (string) $tl[1]; | |
} | |
foreach(explode('|',$driver_string) as $driver) | |
{ | |
if($driver === 'sqlite2?') $driver = 'sqlite'; | |
if($driver === 'imb') $driver = 'ibm'; | |
$res[$driver][$type] = $options; | |
} | |
} | |
} | |
foreach(explode(PHP_EOL,varexport($res,true)) as $line) | |
{ | |
$output = $ex; | |
if(substr($line,-2,1) == ']') | |
$ex = $line .PHP_EOL; | |
else | |
$ex = $line; | |
echo str_replace(['\t\t',"',"],['',"'"],$ex); | |
} | |
function varexport($expression, $return=FALSE) { | |
$export = var_export($expression, TRUE); | |
$export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export); | |
$array = preg_split("/\r\n|\n|\r/", $export); | |
$array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => ['], $array); | |
$export = join(PHP_EOL, array_filter(["["] + $array)); | |
if ((bool)$return) return $export; else echo $export; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment