Skip to content

Instantly share code, notes, and snippets.

@kristianheljas
Last active November 2, 2018 12:48
Show Gist options
  • Save kristianheljas/bd55ae41690090929a0a6ebf30eacf14 to your computer and use it in GitHub Desktop.
Save kristianheljas/bd55ae41690090929a0a6ebf30eacf14 to your computer and use it in GitHub Desktop.
Create formatted PhpDoc @Property declarations from mysql table
SET @database = "<database>", @table = "<table>";
SET @maxTabs = (SELECT CEIL(MAX(LENGTH(`column_name`)) / 4) + 1
FROM information_schema.`COLUMNS`
WHERE `TABLE_SCHEMA` = @database AND `TABLE_NAME` = @table);
SELECT CONCAT(' * @property ', CASE
WHEN data_type IN ('char', 'varchar', 'tinytext', 'text', 'mediumtext', 'longtext', 'binary', 'varbinary', 'tinyblob', 'blob', 'mediumblob', 'json', 'enum', 'set') THEN 'string\t'
WHEN column_type = 'tinyint(1)' THEN 'bool\t'
WHEN data_type IN ('tinyint', 'smallint', 'mediumint', 'int', 'integer', 'bigint', 'year') THEN 'int\t'
WHEN data_type IN ('decimal', 'dec', 'numeric', 'fixed', 'float', 'double', 'real') THEN 'float\t'
WHEN data_type IN ('date', 'datetime', 'timestamp', 'time', '') THEN 'Carbon\t'
ELSE 'mixed\t'
END, '$', `column_name`, REPEAT('\t', @maxTabs - FLOOR((LENGTH(column_name) + 1) / 4)), column_type)
FROM information_schema.`COLUMNS`
WHERE `TABLE_SCHEMA` = @database AND `TABLE_NAME` = @table;
@kristianheljas
Copy link
Author

Replace <database> and <table> variables to the table you want to export

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment