QueryBuilderclass. The class is then modified until you want to execute the query. Models use the query builder under the hood to make all of those calls. Many model methods actually return an instance of
QueryBuilderso you can continue to chain complex queries together.
connection_detailsdictionary you store in your
from_("users")is also a valid alias for the
table("users")method. Feel free to use whatever you feel is more expressive.
*). This is useful when doing joins:
asto the column select:
get()method instead of the
all()method to execute the query.
whenmethod. This method accepts a conditional as the first parameter and a callable as the second parameter. The code above would look like this:
JoinClausebehind the scenes for you.
where statements.There are currently 2 ways to perform an advanced where clause.
JoinClausefrom scratch and build up your own clause:
JoinClauseclass you can build up. This way is a bit more cleaner:
activein ascending order because it is the default but will sort email in descending order.
createmethod. This will perform an INSERT query:
statementmethod. This will simply execute a query directly and return the result rather than building up a query:
Rawexpression class to specify a raw expression. This can be used with the update query:
to_sql(). This method returns the full query without bindings. The actual query sent to the database is a "qmark query" (see below). This
to_sql()method is mainly for debugging purposes and should not be sent directly to a database as the result with have no query bindings and will be subject to SQL injection attacks. Use this method for debugging purposes only.
'?'). The values that should have been in the position of the question marks are stored in a tuple and sent along with the qmark query to help in sql injection. The qmark query is the actual query sent using the connection class.
Note: qmark queries will reset the query builder and remove things like aggregates and wheres from the builder class. Because of this, writing
to_qmarkwill result in incorrect queries (because things like wheres and aggregates will be missing from the final query). If you need to debug a query, please use the
to_sql()method which does not have this kind of resetting behavior.