Skip to content

Commit

Permalink
Add raw method for SQL escape hatch
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Sep 22, 2017
1 parent 0bb926e commit d77a729
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
4 changes: 4 additions & 0 deletions models/Grammars/BaseGrammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,10 @@ component displayname="Grammar" accessors="true" {
}

function compileCreateColumn( column ) {
if ( utils.isExpression( column ) ) {
return column.getSql();
}

return arrayToList( arrayFilter( [
wrapColumn( column.getName() ),
generateType( column ),
Expand Down
5 changes: 5 additions & 0 deletions models/Schema/Blueprint.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ component accessors="true" {
} );
}

function raw( sql ) {
variables.columns.append( new qb.models.Query.Expression( sql ) );
return this;
}

function addColumn() {
var newColumn = new Column( this );
var indexMetadata = getMetadata( newColumn );
Expand Down
15 changes: 12 additions & 3 deletions tests/specs/Schema/SchemaBuilder+GrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,15 @@ component extends="testbox.system.BaseSpec" {
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""tags"" (""taggable_id"" INTEGER(10) UNSIGNED, ""taggable_type"" VARCHAR(255), INDEX ""taggable_index"" (""taggable_id"",""taggable_type""))" );
} );

xit( "raw", function() {
fail( "test not implemented yet" );
it( "raw", function() {
var schema = getBuilder();
var blueprint = schema.create( "users", function( table ) {
table.raw( "id BLOB NOT NULL" );
}, {}, false );
var statements = blueprint.toSql();
expect( statements ).toBeArray();
expect( statements ).toHaveLength( 1 );
expect( statements[ 1 ] ).toBeWithCase( "CREATE TABLE ""users"" (id BLOB NOT NULL)" );
} );

it( "smallIncrements", function() {
Expand Down Expand Up @@ -715,8 +722,10 @@ component extends="testbox.system.BaseSpec" {
}

private function getBuilder() {
var utils = getMockBox().createMock( "qb.models.Query.QueryUtils" );
var grammar = getMockBox()
.createMock( "qb.models.Grammars.BaseGrammar" );
.createMock( "qb.models.Grammars.BaseGrammar" )
.init( utils );
var builder = getMockBox().createMock( "qb.models.Schema.SchemaBuilder" )
.init( grammar );
return builder;
Expand Down
4 changes: 3 additions & 1 deletion tests/specs/Schema/SchemaBuilder+MySQLGrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ component extends="testbox.system.BaseSpec" {
}

private function getBuilder() {
var utils = getMockBox().createMock( "qb.models.Query.QueryUtils" );
var grammar = getMockBox()
.createMock( "qb.models.Grammars.MySQLGrammar" );
.createMock( "qb.models.Grammars.MySQLGrammar" )
.init( utils );
var builder = getMockBox().createMock( "qb.models.Schema.SchemaBuilder" )
.init( grammar );
return builder;
Expand Down
4 changes: 3 additions & 1 deletion tests/specs/Schema/SchemaBuilder+OracleGrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ component extends="testbox.system.BaseSpec" {
}

private function getBuilder() {
var utils = getMockBox().createMock( "qb.models.Query.QueryUtils" );
var grammar = getMockBox()
.createMock( "qb.models.Grammars.OracleGrammar" );
.createMock( "qb.models.Grammars.OracleGrammar" )
.init( utils );
var builder = getMockBox().createMock( "qb.models.Schema.SchemaBuilder" )
.init( grammar );
return builder;
Expand Down

0 comments on commit d77a729

Please sign in to comment.