Skip to content

Instantly share code, notes, and snippets.

@kitloong
Last active March 27, 2020 16:46
Show Gist options
  • Save kitloong/6e72ad6e723cd9c52b48ffdf7c68db37 to your computer and use it in GitHub Desktop.
Save kitloong/6e72ad6e723cd9c52b48ffdf7c68db37 to your computer and use it in GitHub Desktop.
Type MySQL Postgre SQLite SQLServer
char char char() varchar nchar()
string varchar varchar() varchar nvarchar()
text text text text nvarchar(max)
mediumText mediumtext text text nvarchar(max)
longText longtext text text nvarchar(max)
bigInteger bigint bigint integer bigint
integer int integer integer int
mediumInteger mediumint integer integer int
tinyInteger tinyint smallint integer tinyint
smallInteger smallint smallint integer smallint
float double double precision float float
double double double precision float float
decimal decimal decimal(, ) numeric decimal(, )
boolean tinyint(1) boolean tinyint(1) bit
enum enum varchar(255) check ("" in ('')) varchar check ("" in ('')) nvarchar(255) check ("" in (N''))
set set - - -
json json json text nvarchar(max)
jsonb json jsonb text nvarchar(max)
date date date date date
dateTime datetime timestamp without time zone datetime datetime
dateTimeTz datetime timestamp with time zone datetime datetimeoffset
time time time without time zone time time
timeTz time time with time zone time time
timestamp timestamp timestamp without time zone datetime datetime
timestampTz timestamp timestamp with time zone datetime datetimeoffset
year year integer integer int
binary blob bytea blob varbinary(max)
uuid char(36) uuid varchar uniqueidentifier
ipAddress varchar(45) inet varchar nvarchar(45)
macAddress varchar(17) macaddr varchar nvarchar(17)
geometry geometry geography(geometry, 4326) geometry geography
point point geography(point, 4326) point geography
lineString linestring geography(linestring, 4326) linestring geography
polygon polygon geography(polygon, 4326) polygon geography
geometryCollection geometrycollection geography(geometrycollection, 4326) geometrycollection geography
multiPoint multipoint geography(multipoint, 4326) multipoint geography
multiLineString multilinestring geography(multilinestring, 4326) multilinestring geography
multiPolygon multipolygon geography(multipolygon, 4326) multipolygon geography
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Schema\Grammars\MySqlGrammar;
use Illuminate\Database\Schema\Grammars\PostgresGrammar;
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
use Illuminate\Database\Schema\Grammars\SqlServerGrammar;
use Illuminate\Support\Fluent;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionMethod;
class MigrationTypeList extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'migration:type:list';
/**
* The console command description.
*
* @var string
*/
protected $description = 'List migration type in different DB.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
* @throws \ReflectionException
*/
public function handle()
{
$mysqlGrammar = new MySqlGrammar();
$postgresGrammar = new PostgresGrammar();
$sqliteGrammar = new SQLiteGrammar();
$sqlServerGrammar = new SqlServerGrammar();
$mysql = new ReflectionClass(get_class($mysqlGrammar));
$postgresMethodsMap = $this->mapMethod((new ReflectionClass(get_class($postgresGrammar)))->getMethods());
$sqliteMethodsMap = $this->mapMethod((new ReflectionClass(get_class($sqliteGrammar)))->getMethods());
$sqlServerMethodsMap = $this->mapMethod((new ReflectionClass(get_class($sqlServerGrammar)))->getMethods());
$empty = new Fluent([]);
$this->info('|Type|MySQL|Postgre|SQLite|SQLServer|');
$this->info('|---|---|---|---|---|');
foreach ($mysql->getMethods() as $method) {
if (Str::startsWith($method->name, 'type')) {
if ($method->name === 'typeComputed') {
continue;
}
$method->setAccessible(true);
isset($postgresMethodsMap[$method->name]) && $postgresMethodsMap[$method->name]->setAccessible(true);
isset($sqliteMethodsMap[$method->name]) && $sqliteMethodsMap[$method->name]->setAccessible(true);
isset($sqlServerMethodsMap[$method->name]) && $sqlServerMethodsMap[$method->name]->setAccessible(true);
$this->info("|".
Str::camel(Str::substr($method->name, 4))."|".
$this->removeUnwantedBracket($method->invoke($mysqlGrammar, $empty))."|".
(isset($postgresMethodsMap[$method->name]) ?
$postgresMethodsMap[$method->name]->invoke($postgresGrammar, $empty) : '-')."|".
(isset($sqliteMethodsMap[$method->name]) ?
$sqliteMethodsMap[$method->name]->invoke($sqliteGrammar, $empty) : '-')."|".
(isset($sqlServerMethodsMap[$method->name]) ?
$sqlServerMethodsMap[$method->name]->invoke($sqlServerGrammar, $empty) : '-')."|"
);
}
}
return 0;
}
private function removeUnwantedBracket(string $text)
{
return str_replace(['()', "('')", '(, )'], '', $text);
}
/**
* @param ReflectionMethod[] $reflectionMethods
* @return ReflectionMethod[] $method->name as key
*/
private function mapMethod($reflectionMethods)
{
$methodMap = [];
foreach ($reflectionMethods as $method) {
$methodMap[$method->name] = $method;
}
return $methodMap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment