Skip to content

Commit

Permalink
Add integer, unsignedInteger, increments, and text types
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Sep 22, 2017
1 parent 3f80002 commit fb76853
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
8 changes: 6 additions & 2 deletions models/Grammars/Grammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -779,12 +779,16 @@ component displayname="Grammar" accessors="true" {
return "BIGINT";
}

function typeInteger( column ) {
return "INTEGER(#column.getPrecision()#)";
}

function typeString( column ) {
return "VARCHAR(#column.getLength()#)";
}

function typeInteger( column ) {
return "INT";
function typeText( column ) {
return "TEXT";
}

function typeTimestamp( column ) {
Expand Down
16 changes: 16 additions & 0 deletions models/Schema/Blueprint.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ component accessors="true" {

function bigIncrements( name ) {
arguments.autoIncrement = true;
addIndex( type = "primary", column = name );
return unsignedBigInteger( argumentCollection = arguments );
}

Expand All @@ -75,6 +76,11 @@ component accessors="true" {
return unsignedInt( argumentCollection = arguments );
}

function integer( name, precision = 10 ) {
arguments.type = "integer";
return addColumn( argumentCollection = arguments );
}

function tinyInteger( name, length = "" ) {
arguments.type = "tinyInteger";
return addColumn( argumentCollection = arguments );
Expand All @@ -85,6 +91,11 @@ component accessors="true" {
return bigInteger( argumentCollection = arguments );
}

function unsignedInteger( name ) {
arguments.unsigned = true;
return integer( argumentCollection = arguments );
}

function unsignedInt( name ) {
arguments.type = "integer";
arguments.unsigned = true;
Expand All @@ -99,6 +110,11 @@ component accessors="true" {
return addColumn( argumentCollection = arguments );
}

function text( name ) {
arguments.type = "text";
return addColumn( argumentCollection = arguments );
}

function timestamp( name ) {
arguments.type = "timestamp";
return addColumn( argumentCollection = arguments );
Expand Down
3 changes: 2 additions & 1 deletion models/Schema/Column.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ component accessors="true" {
property name="name";
property name="type";
property name="length" default="255";
property name="precision" default="10";
property name="nullable" default="false";
property name="unsigned" default="false";
property name="autoIncrement" default="false";
Expand All @@ -21,4 +22,4 @@ component accessors="true" {
return getBlueprint().addIndex( argumentCollection = arguments );
}

}
}
36 changes: 30 additions & 6 deletions tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ component extends="testbox.system.BaseSpec" {
table.timestamp( "created_date" ).setDefault( "CURRENT_TIMESTAMP" );
table.timestamp( "modified_date" ).setDefault( "CURRENT_TIMESTAMP" );
}, false );
expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INT UNSIGNED NOT NULL AUTO_INCREMENT, ""username"" VARCHAR(255) NOT NULL, ""first_name"" VARCHAR(255) NOT NULL, ""last_name"" VARCHAR(255) NOT NULL, ""password"" VARCHAR(100) NOT NULL, ""country_id"" INT UNSIGNED NOT NULL, ""created_date"" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, ""modified_date"" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (""id""), CONSTRAINT ""fk_country_id"" FOREIGN KEY (""country_id"") REFERENCES ""countries"" (""id"") ON UPDATE NONE ON DELETE CASCADE)" );
expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, ""username"" VARCHAR(255) NOT NULL, ""first_name"" VARCHAR(255) NOT NULL, ""last_name"" VARCHAR(255) NOT NULL, ""password"" VARCHAR(100) NOT NULL, ""country_id"" INTEGER(10) UNSIGNED NOT NULL, ""created_date"" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, ""modified_date"" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (""id""), CONSTRAINT ""fk_country_id"" FOREIGN KEY (""country_id"") REFERENCES ""countries"" (""id"") ON UPDATE NONE ON DELETE CASCADE)" );
} );

describe( "column types", function() {
Expand All @@ -39,7 +39,7 @@ component extends="testbox.system.BaseSpec" {
var blueprint = schema.create( "users", function( table ) {
table.bigIncrements( "id" );
}, false );
expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""id"" BIGINT UNSIGNED NOT NULL AUTO_INCREMENT)" );
expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""id"" BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (""id""))" );
} );

it( "bigInteger", function() {
Expand Down Expand Up @@ -87,11 +87,27 @@ component extends="testbox.system.BaseSpec" {
} );

it( "increments", function() {
fail( "test not implemented yet" );
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.increments( "id" );
}, false );
expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""id"" INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (""id""))" );
} );

it( "integer", function() {
fail( "test not implemented yet" );
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.integer( "age" );
}, false );
expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""age"" INTEGER(10) NOT NULL)" );
} );

it( "integer with precision", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.integer( "age", 2 );
}, false );
expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""age"" INTEGER(2) NOT NULL)" );
} );

it( "json", function() {
Expand Down Expand Up @@ -135,7 +151,11 @@ component extends="testbox.system.BaseSpec" {
} );

it( "text", function() {
fail( "test not implemented yet" );
var schema = getBuilder();
var blueprint = schema.create( "posts", function( table ) {
table.text( "body" );
}, false );
expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""posts"" (""body"" TEXT NOT NULL)" );
} );

it( "time", function() {
Expand Down Expand Up @@ -179,7 +199,11 @@ component extends="testbox.system.BaseSpec" {
} );

it( "unsignedInteger", function() {
fail( "test not implemented yet" );
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.unsignedInteger( "age" );
}, false );
expect( blueprint.toSql() ).toBeWithCase( "CREATE TABLE ""users"" (""age"" INTEGER(10) UNSIGNED NOT NULL)" );
} );

it( "unsignedMediumInteger", function() {
Expand Down

0 comments on commit fb76853

Please sign in to comment.