You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update README.md to enhance CarbunqleX documentation
- Strengthened explanations of CarbunqleX features.
- Added sections on parsing SQL queries and modifying WHERE clauses.
- Included handling of CTEs and subqueries.
- Introduced standardizing filtering and filtering with outer joins.
- Added examples for creating tables from SELECT queries.
- Explained managing INSERT, UPDATE, and DELETE queries using SELECT.
Let's start by parsing a simple SQL query into an AST using `QueryAstParser.Parse`. We will then convert it back to SQL with `ToSql` and inspect its structure using `ToTreeString`.
46
46
@@ -68,7 +68,7 @@ SELECT a.table_a_id, a.value FROM table_a AS a
68
68
69
69
This makes it easy to convert between them.
70
70
71
-
### 2. Modifying the WHERE Clause
71
+
### **2. Modifying the WHERE Clause**
72
72
73
73
Now, let's modify the query by injecting a `WHERE` condition.
74
74
@@ -91,7 +91,7 @@ WHERE a.value = 1;
91
91
92
92
CarbunqleX allows you to inject conditions while still maintaining the integrity of your SQL.
93
93
94
-
### 3. Handling CTEs and Subqueries
94
+
### **3. Handling CTEs and Subqueries**
95
95
96
96
Let's try to insert a `WHERE` condition into a query that contains a **CTE and a subquery**.
97
97
@@ -137,7 +137,7 @@ GROUP BY orders.region, orders.product;
137
137
138
138
CarbunqleX will **intelligently place the condition in the deepest related query**. It will also **dynamically merge CTEs**.
139
139
140
-
### 4. Standardizing filtering
140
+
### **4. Standardizing filtering**
141
141
142
142
Next, we will introduce a more advanced use case that dynamically filters data based on user permissions. We define the areas that users can access as reusable functions.
143
143
@@ -238,7 +238,7 @@ SELECT DISTINCT * FROM (SELECT id FROM table_a UNION ALL SELECT id FROM table_b)
238
238
239
239
Now managing huge union queries is not scary.
240
240
241
-
### 7. Filtering using outer joins**
241
+
### **7. Filtering using outer joins**
242
242
243
243
You can also dynamically insert outer join conditions to perform filtering.
244
244
@@ -276,6 +276,72 @@ query.From("region", isCurrentOnly: false, static from =>
276
276
varexpected="with regional_sales as (select orders.region, SUM(orders.amount) as total_sales from orders left join top_regions as tp on orders.region = tp.region where tp.region is null group by orders.region), top_regions as (select rs.region from regional_sales as rs where rs.total_sales > (select SUM(x.total_sales) / 10 from regional_sales as x)) select orders.region, orders.product, SUM(orders.quantity) as product_units, SUM(orders.amount) as product_sales from orders where orders.region in (select x.region from top_regions as x) group by orders.region, orders.product";
277
277
```
278
278
279
+
### **8. Create a table from a select query**
280
+
281
+
You can convert a select query into a `CREATE TABLE` statement. This is very useful when you want to create a new table based on the results of a query.
282
+
283
+
```csharp
284
+
varquery=QueryAstParser.Parse("SELECT a.table_a_id, 1 AS value FROM table_a AS a");
285
+
286
+
// Generate a CREATE TABLE query, specifying that the table is temporary
CREATE TEMPORARY TABLE table_b ASSELECTa.table_a_id, 1AS value FROM table_a AS a;
297
+
```
298
+
299
+
### 9. Manage Update Queries with Select Queries
300
+
301
+
The effects of insert, update, and delete queries can only be seen after they are executed, making it difficult to preview the expected results in advance.
302
+
303
+
CarbunqleX allows you to express these queries as select queries. Select queries simplify the debugging and validation process by allowing you to preview changes without actually modifying tables.
304
+
305
+
```csharp
306
+
varquery=QueryAstParser.Parse("SELECT a.table_a_id, 1 AS value FROM table_a AS a");
307
+
308
+
// Generate an INSERT INTO query for table_b with a RETURNING clause
The above example demonstrates how to express an insert query using a select query, but similar techniques can also be applied to update and delete queries.
324
+
325
+
For update queries:
326
+
327
+
```csharp
328
+
varquery=QueryAstParser.Parse("SELECT a.table_a_id, 1 AS value FROM table_a AS a");
varexpected="UPDATE table_b SET value = q.value FROM (SELECT a.table_a_id, 1 AS value FROM table_a AS a) AS q WHERE table_b.table_a_id = q.table_a_id";
333
+
```
334
+
335
+
For delete queries:
336
+
337
+
```csharp
338
+
varquery=QueryAstParser.Parse("SELECT a.table_a_id, 1 AS value FROM table_a AS a");
varexpected="DELETE FROM table_b WHERE table_b.table_a_id IN (SELECT q.table_a_id FROM (SELECT a.table_a_id, 1 AS value FROM table_a AS a) AS q)";
343
+
```
344
+
279
345
## 📌 Conclusion
280
346
281
347
CarbunqleX makes raw SQL **more maintainable, reusable, and dynamically modifiable** without sacrificing performance. Its AST-based transformations provide a powerful way to manipulate queries at scale, making it an essential tool for advanced SQL users.
0 commit comments