Skip to content

Commit

Permalink
added last()
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano authored and elpete committed Jul 20, 2018
1 parent 347a37c commit 5b0fe28
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions models/Query/QueryBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,23 @@ component displayname="QueryBuilder" accessors="true" {
return results[ 1 ];
}

/**
* Returns the last record returned from a query.
*
* @options Any options to pass to `queryExecute`. Default: {}.
*
* @return any
*/
public any function last( struct options = {} ) {
var results = withReturnFormat( "array", function() {
return get( options = options );
} );
if ( arrayIsEmpty( results ) ) {
return {};
}
return results[ results.len() ];
}

/**
* Adds an id constraint to the query and returns the first record from the query.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/specs/Query/Abstract/QueryExecutionSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ component extends="testbox.system.BaseSpec" {
} );
} );

describe( "last", function() {
it( "retrieves the last record when calling `last`", function() {
var builder = getBuilder();
var expectedQuery = queryNew( "id,name", "integer,varchar", [ { id = 1, name = "foo" }, { id = 2, name = "test" } ] );
builder.$( "runQuery" ).$args(
sql = "SELECT * FROM ""users"" WHERE ""name"" = ? LIMIT 1",
options = {}
).$results( expectedQuery );

var results = builder.from( "users" ).last();

expect( results ).toBeStruct();
expect( results ).toBe( { id = 2, name = "test" } );
} );
} );

describe( "find", function() {
it( "returns the first result by id when calling `find`", function() {
var builder = getBuilder();
Expand Down

0 comments on commit 5b0fe28

Please sign in to comment.