|
105 | 105 | - [4. What about bidirectional queries?](#4-what-about-bidirectional-queries) |
106 | 106 | - [1. Child to Parent](#1-child-to-parent) |
107 | 107 | - [2. Parent to Child](#2-parent-to-child) |
| 108 | +- [Query Builder.](#query-builder) |
| 109 | + - [Why Use Query Builder?](#why-use-query-builder) |
108 | 110 | - [What is coming next?](#what-is-coming-next) |
109 | 111 | - [Contributing](#contributing) |
110 | 112 | - [License](#license) |
@@ -2112,10 +2114,81 @@ print(user_post) """ ? = |
2112 | 2114 |
|
2113 | 2115 | ``` |
2114 | 2116 |
|
| 2117 | +### Query Builder. |
| 2118 | + |
| 2119 | +Dataloom exposes a method called `getQueryBuilder`, which allows you to obtain a `qb` object. This object enables you to execute SQL queries directly from SQL scripts. |
| 2120 | + |
| 2121 | +```py |
| 2122 | +qb = loom.getQueryBuilder() |
| 2123 | + |
| 2124 | +print(qb) # ? = Loom QB<mysql> |
| 2125 | +``` |
| 2126 | + |
| 2127 | +The `qb` object contains the method called `run`, which is used to execute SQL scripts or SQL queries. |
| 2128 | + |
| 2129 | +```py |
| 2130 | +ids = qb.run("select id from posts;", fetchall=True) |
| 2131 | +print(ids) # ? = [(1,), (2,), (3,), (4,)] |
| 2132 | +``` |
| 2133 | + |
| 2134 | +You can also execute SQL files. In the following example, we will demonstrate how you can execute SQL scripts using the `qb`. Let's say we have an SQL file called `qb.sql` which contains the following SQL code: |
| 2135 | + |
| 2136 | +```SQL |
| 2137 | +SELECT id, title FROM posts WHERE id IN (1, 3, 2, 4) LIMIT 4 OFFSET 1; |
| 2138 | +SELECT COUNT(*) FROM ( |
| 2139 | + SELECT DISTINCT `id` |
| 2140 | + FROM `posts` |
| 2141 | + WHERE `id` < 5 |
| 2142 | + LIMIT 3 OFFSET 2 |
| 2143 | +) AS subquery; |
| 2144 | +``` |
| 2145 | + |
| 2146 | +We can use the query builder to execute the SQL as follows: |
| 2147 | + |
| 2148 | +```py |
| 2149 | +with open("qb.sql", "r") as reader: |
| 2150 | + sql = reader.read() |
| 2151 | +res = qb.run( |
| 2152 | + sql, |
| 2153 | + fetchall=True, |
| 2154 | + is_script=True, |
| 2155 | +) |
| 2156 | +print(res) |
| 2157 | +``` |
| 2158 | + |
| 2159 | +> 👍 **Pro Tip:** Executing a script using query builder does not return a result. The result value is always `None`. |
| 2160 | +
|
| 2161 | +The `run` method takes the following as arguments: |
| 2162 | + |
| 2163 | +| Argument | Description | Type | Required | Default | |
| 2164 | +| --------------- | -------------------------------------------------------------------------------------- | ---------------------------------------------- | -------- | ------- | |
| 2165 | +| `sql` | SQL query to execute. | `str` | Yes | | |
| 2166 | +| `args` | Parameters for the SQL query. | `Any \| None` | No | `None` | |
| 2167 | +| `fetchone` | Whether to fetch only one result. | `bool` | No | `False` | |
| 2168 | +| `fetchmany` | Whether to fetch multiple results. | `bool` | No | `False` | |
| 2169 | +| `fetchall` | Whether to fetch all results. | `bool` | No | `False` | |
| 2170 | +| `mutation` | Whether the query is a mutation (insert, update, delete). | `bool` | No | `True` | |
| 2171 | +| `bulk` | Whether the query is a bulk operation. | `bool` | No | `False` | |
| 2172 | +| `affected_rows` | Whether to return affected rows. | `bool` | No | `False` | |
| 2173 | +| `operation` | Type of operation being performed. | `'insert', 'update', 'delete', 'read' \| None` | No | `None` | |
| 2174 | +| `verbose` | Verbosity level for logging . Set this option to `0` if you don't want logging at all. | `int` | No | `1` | |
| 2175 | +| `is_script` | Whether the SQL is a script. | `bool` | No | `False` | |
| 2176 | + |
| 2177 | +#### Why Use Query Builder? |
| 2178 | + |
| 2179 | +- The query builder empowers developers to seamlessly execute `SQL` queries directly. |
| 2180 | +- While Dataloom primarily utilizes `subqueries` for eager data fetching on models, developers may prefer to employ JOIN operations, which are achievable through the `qb` object. |
| 2181 | + |
| 2182 | + ```python |
| 2183 | + qb = loom.getQueryBuilder() |
| 2184 | + result = qb.run("SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.table1_id;") |
| 2185 | + print(result) |
| 2186 | + ``` |
| 2187 | + |
2115 | 2188 | ### What is coming next? |
2116 | 2189 |
|
2117 | 2190 | 1. N-N associations |
2118 | | -2. Query Builder |
| 2191 | +2. Self relations |
2119 | 2192 |
|
2120 | 2193 | ### Contributing |
2121 | 2194 |
|
|
0 commit comments