Skip to content

Commit

Permalink
feat(SchemaBuilder): Add more column types
Browse files Browse the repository at this point in the history
Included column types are:
- datetimeTz
- lineString
- nullableTimestamps
- point
- polygon
- softDeletes
- softDeletesTz
- timeTz
- timestamps
- timestampTz
- timestampsTz
- withCurrent
  • Loading branch information
elpete committed Oct 4, 2019
1 parent 88bfe81 commit c9c4678
Show file tree
Hide file tree
Showing 12 changed files with 468 additions and 10 deletions.
24 changes: 24 additions & 0 deletions models/Grammars/BaseGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,10 @@ component displayname="Grammar" accessors="true" singleton {
return "DATETIME";
}

function typeDatetimeTz( column ) {
return typeDatetime( column );
}

function typeDecimal( column ) {
return "DECIMAL(#column.getLength()#,#column.getPrecision()#)";
}
Expand Down Expand Up @@ -1229,6 +1233,10 @@ component displayname="Grammar" accessors="true" singleton {
return typeChar( column, 36 );
}

function typeLineString( column ) {
return "GEOGRAPHY";
}

function typeMediumInteger( column ) {
return arrayToList( arrayFilter( [
"MEDIUMINT",
Expand All @@ -1246,6 +1254,14 @@ component displayname="Grammar" accessors="true" singleton {
return "TEXT";
}

function typePoint( column ) {
return "GEOGRAPHY";
}

function typePolygon( column ) {
return "GEOGRAPHY";
}

function typeSmallInteger( column ) {
return arrayToList( arrayFilter( [
"SMALLINT",
Expand Down Expand Up @@ -1275,10 +1291,18 @@ component displayname="Grammar" accessors="true" singleton {
return "TIME";
}

function typeTimeTz( column ) {
return "TIME";
}

function typeTimestamp( column ) {
return "TIMESTAMP";
}

function typeTimestampTz( column ) {
return typeTimestamp( column );
}

function typeTinyInteger( column ) {
return arrayToList( arrayFilter( [
"TINYINT",
Expand Down
10 changes: 9 additions & 1 deletion models/Grammars/MSSQLGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,11 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
}

function typeDatetime( column ) {
return "DATETIME2";
return typeTimestamp( column );
}

function typeDatetimeTz( column ) {
return typeTimestampTz( column );
}

function typeUUID( column ) {
Expand Down Expand Up @@ -404,6 +408,10 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
return "DATETIME2";
}

function typeTimestampTz( column ) {
return "DATETIMEOFFSET";
}

function typeTinyInteger( column ) {
if ( !isNull( column.getPrecision() ) ) {
return "NUMERIC(#column.getPrecision()#)";
Expand Down
20 changes: 18 additions & 2 deletions models/Grammars/MySQLGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,12 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
}

function generateDefault( column ) {
if ( column.getDefault() == "" && column.getType() == "TIMESTAMP" ) {
column.setDefault( "CURRENT_TIMESTAMP" );
if (
column.getDefault() == "" &&
column.getType().findNoCase( "TIMESTAMP" ) > 0 &&
! column.getNullable()
) {
column.withCurrent();
}
return super.generateDefault( column );
}
Expand All @@ -118,4 +122,16 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
return "NCHAR(#column.getLength()#)";
}

function typeLineString( column ) {
return "LINESTRING";
}

function typePoint( column ) {
return "POINT";
}

function typePolygon( column ) {
return "POLYGON";
}

}
28 changes: 26 additions & 2 deletions models/Grammars/OracleGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
}

function typeDatetime( column ) {
return "DATE";
return typeTimestamp( column );
}

function typeDatetimeTz( column ) {
return typeTimestampTz( column );
}

function typeDecimal( column ) {
Expand Down Expand Up @@ -335,6 +339,10 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
return "CLOB";
}

function typeLineString( column ) {
return "SDO_GEOMETRY";
}

function typeLongText( column ) {
return "CLOB";
}
Expand All @@ -352,6 +360,14 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
return "CLOB";
}

function typePoint( column ) {
return "SDO_GEOMETRY";
}

function typePolygon( column ) {
return "SDO_GEOMETRY";
}

function typeSmallInteger( column ) {
var precision = isNull( column.getPrecision() ) ? 5 : column.getPrecision();
return "NUMBER(#precision#, 0)";
Expand All @@ -374,13 +390,21 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
}

function typeTime( column ) {
return "DATE";
return typeTimestamp( column );
}

function typeTimeTz( column ) {
return typeTimestampTz( column );
}

function typeTimestamp( column ) {
return "DATE";
}

function typeTimestampTz( column ) {
return "TIMESTAMP WITH TIME ZONE";
}

function typeTinyInteger( column ) {
var precision = isNull( column.getPrecision() ) ? 3 : column.getPrecision();
return "NUMBER(#precision#, 0)";
Expand Down
30 changes: 29 additions & 1 deletion models/Grammars/PostgresGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
}

function typeDatetime( column ) {
return "TIMESTAMP";
return typeTimestamp( column );
}

function typeDatetimeTz( column ) {
return typeTimestampTz( column );
}

function typeEnum( column ) {
Expand Down Expand Up @@ -226,6 +230,10 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
return "BIGINT";
}

function typeLineString( column ) {
return formatPostGisType( "linestring" );
}

function typeMediumInteger( column ) {
if ( column.getAutoIncrement() ) {
return "SERIAL";
Expand All @@ -238,6 +246,14 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
return "INTEGER";
}

function typePoint( column ) {
return formatPostGisType( "point" );
}

function typePolygon( column ) {
return formatPostGisType( "polygon" );
}

function typeSmallInteger( column ) {
if ( column.getAutoIncrement() ) {
return "SERIAL";
Expand All @@ -258,6 +274,14 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
return typeText( argumentCollection = arguments );
}

function typeTimeTz( column ) {
return "TIME WITH TIME ZONE";
}

function typeTimestampTz( column ) {
return typeTimestamp( column ) & " WITH TIME ZONE";
}

function typeTinyInteger( column ) {
if ( column.getAutoIncrement() ) {
return "SERIAL";
Expand All @@ -270,6 +294,10 @@ component extends="qb.models.Grammars.BaseGrammar" singleton {
return "SMALLINT";
}

private function formatPostGisType( type ) {
return "GEOGRAPHY(#uCase( type )#, 4326)";
}

/*===================================
= Index Types =
===================================*/
Expand Down
58 changes: 58 additions & 0 deletions models/Schema/Blueprint.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ component accessors="true" {
return appendColumn( argumentCollection = arguments );
}

function datetimeTz( name ) {
arguments.type = "datetimeTz";
return appendColumn( argumentCollection = arguments );
}

function decimal( name, length = 10, precision = 0 ) {
arguments.type = "decimal";
return appendColumn( argumentCollection = arguments );
Expand Down Expand Up @@ -133,6 +138,11 @@ component accessors="true" {
return appendColumn( argumentCollection = arguments );
}

function lineString( name ) {
arguments.type = "lineString";
return appendColumn( argumentCollection = arguments );
}

function morphs( name ) {
unsignedInteger( "#name#_id" );
string( "#name#_type" );
Expand All @@ -155,6 +165,22 @@ component accessors="true" {
return this;
}

function nullableTimestamps() {
appendColumn( name = "createdDate", type = "timestamp", nullable = true );
appendColumn( name = "modifiedDate", type = "timestamp", nullable = true );
return this;
}

function point( name ) {
arguments.type = "point";
return appendColumn( argumentCollection = arguments );
}

function polygon( name ) {
arguments.type = "polygon";
return appendColumn( argumentCollection = arguments );
}

function raw( sql ) {
var expression = new qb.models.Query.Expression( sql );
variables.columns.append( expression );
Expand All @@ -173,6 +199,16 @@ component accessors="true" {
return appendColumn( argumentCollection = arguments );
}

function softDeletes() {
appendColumn( name = "deletedDate", type = "timestamp", nullable = true );
return this;
}

function softDeletesTz() {
appendColumn( name = "deletedDate", type = "timestampTz", nullable = true );
return this;
}

function string( name, length ) {
arguments.type = "string";
if ( isNull( arguments.length ) ) {
Expand Down Expand Up @@ -204,11 +240,33 @@ component accessors="true" {
return appendColumn( argumentCollection = arguments );
}

function timeTz( name ) {
arguments.type = "timeTz";
return appendColumn( argumentCollection = arguments );
}

function timestamp( name ) {
arguments.type = "timestamp";
return appendColumn( argumentCollection = arguments );
}

function timestamps() {
appendColumn( name = "createdDate", type = "timestamp" );
appendColumn( name = "modifiedDate", type = "timestamp" );
return this;
}

function timestampTz( name ) {
arguments.type = "timestampTz";
return appendColumn( argumentCollection = arguments );
}

function timestampsTz() {
appendColumn( name = "createdDate", type = "timestampTz" );
appendColumn( name = "modifiedDate", type = "timestampTz" );
return this;
}

function tinyIncrements( name, indexName ) {
arguments.autoIncrement = true;
arguments.indexName = isNull( indexName ) ? "pk_#getTable()#_#name#" : arguments.indexName;
Expand Down
12 changes: 11 additions & 1 deletion models/Schema/Column.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,22 @@ component accessors="true" {
setUnique( true );
return this;
}

/**
* @returns true if the object is a column
*/
function isColumn() {
return true;
}

/**
* Sets the default equal to CURRENT_TIMESTAMP
*
* @returns Column
*/
function withCurrent() {
setDefault( "CURRENT_TIMESTAMP" );
return this;
}

}
Loading

0 comments on commit c9c4678

Please sign in to comment.