Create object-oriented abstractions around SQL patterns. Explore new ways of organizing your code by passing around a query builder object that will compile down to the right SQL without you having to keep track of the order, whitespace, or other SQL gotchas!
3. What is qb anyway?
✖ A fluent, chainable syntax for building SQL
✖ An abstraction around different DB engines
✖ An abstraction around queryExecute
✖ Heavily inspired by Eloquent From Laravel
✖ Written by Eric Peterson from Ortus Solutions
3
4. What is qb anyway?
QueryBuilder@qb
✖ Queries your data
4
SchemaBuilder@qb
✖ Helps build DB schema
✖ Great for migrations
5. What is qb anyway?
QB is the core backbone of Quick.
Quick is an ORM (Object Relational Mapper)
5
12. How can QB help??
//using take
function loadPosts() {
return query.from(“posts”).take(5).get();
}
//using limit
function loadPosts() {
return query.from(“posts”).limit(5).get();
}
12
15. addselect
query.addSelect( “id”);
✖ Adds a column to the select list
✖ If the selection is currently *, then the added
column will be the only column selected
15
16. join
query.from( “posts”)
.join( “users”, “posts.user_id”, “=”, “users.id” );
✖ Add a join statement to the query
✖ Simple syntax for simple joins
✖ Complex joins can be specified using a callback
16
18. Where
query.where( “title”, “=”, “My Title” );
✖ Adds a where statement to the query
✖ AUTOMATICALLY params the value passed
✖ Accepts any SQL operator like <=, <>, OR like
18
27. groupBy
query.groupBy( "id" );
✖ Adds a group by statement
✖ Further calls add to any existing groupings
✖ Takes a single value, list, or array
27
28. Having
query.from( "users" )
.groupBy( "email" )
.having( builder.raw( "COUNT(email)" ), ">", 1 );
✖ Adds a having statement
✖ Works pretty much like a where statement
28
29. Limit & Offset
query.limit( 1 ) .offset( 10 );
✖ Adds a limit and offset to the query
✖ This one is a great example where normalizing database
engines is super nice.
29
30. mySQL
LIMIT
SELECT * FROM `users` LIMIT 3 LIMIT & OFFSET
LIMIT & OFFSET
SELECT * FROM `users` LIMIT 15 OFFSET 30
30
31. MSSQL
LIMIT
SELECT TOP (3) * FROM [users]
LIMIT & OFFSET
SELECT * FROM [users] ORDER BY 1 OFFSET 30 ROWS
FETCH NEXT 15 ROWS ONLY
31
32. ORACLE
LIMIT
SELECT * FROM ( SELECT results.*, ROWNUM AS
"QB_RN" FROM (SELECT * FROM "USERS") results )
WHERE "QB_RN" <= 3
LIMIT & OFFSET
SELECT * FROM ( SELECT results.*, ROWNUM AS
"QB_RN" FROM (SELECT * FROM "USERS") results )
WHERE "QB_RN" > 30 AND "QB_RN" <= 45
32
36. get
query.from( "users" ).get();
✖ Builds and executes the current query
✖ Can accept columns as a shortcut
✖ Also accepts any options that can be passed to
queryExecute
36
37. find
query.from( "users" ).find(1, ‘idcolumn’);
✖ Shortcut method for retrieving records by primary key
✖ Default idColumn is id
✖ idColumn can be specified as second argument
37
38. find
query.from( "users" ).find(1);
✖ Shortcut method for retrieving records by primary key
✖ Default idColumn is id
✖ idColumn can be specified as second argument
38
39. first
query.from( "users" ).first();
✖ Adds a limit( 1 ) to your query
✖ Also returns the struct value, not an array of one
✖ Returns an empty struct if nothing is found
39
40. exists
query.from( "logins" )
.where( "user_id", user.getId() ) .exists();
✖ Returns a boolean
✖ Works with any configured query
✖ Just call exists instead of get
40
42. Credits
Special thanks to all the people who made and
released these awesome resources for free:
✖ Presentation template by SlidesCarnival
✖ Photographs by Unsplash
42