Skip to content

Commit 0ae57ac

Browse files
committed
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.
1 parent a914698 commit 0ae57ac

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

README.md

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ NuGet\Install-Package Carbunqlex
4040

4141
## 📖 Documentation
4242

43-
### 1. Parsing a SQL Query
43+
### **1. Parsing a SQL Query**
4444

4545
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`.
4646

@@ -68,7 +68,7 @@ SELECT a.table_a_id, a.value FROM table_a AS a
6868

6969
This makes it easy to convert between them.
7070

71-
### 2. Modifying the WHERE Clause
71+
### **2. Modifying the WHERE Clause**
7272

7373
Now, let's modify the query by injecting a `WHERE` condition.
7474

@@ -91,7 +91,7 @@ WHERE a.value = 1;
9191

9292
CarbunqleX allows you to inject conditions while still maintaining the integrity of your SQL.
9393

94-
### 3. Handling CTEs and Subqueries
94+
### **3. Handling CTEs and Subqueries**
9595

9696
Let's try to insert a `WHERE` condition into a query that contains a **CTE and a subquery**.
9797

@@ -137,7 +137,7 @@ GROUP BY orders.region, orders.product;
137137

138138
CarbunqleX will **intelligently place the condition in the deepest related query**. It will also **dynamically merge CTEs**.
139139

140-
### 4. Standardizing filtering
140+
### **4. Standardizing filtering**
141141

142142
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.
143143

@@ -238,7 +238,7 @@ SELECT DISTINCT * FROM (SELECT id FROM table_a UNION ALL SELECT id FROM table_b)
238238

239239
Now managing huge union queries is not scary.
240240

241-
### 7. Filtering using outer joins**
241+
### **7. Filtering using outer joins**
242242

243243
You can also dynamically insert outer join conditions to perform filtering.
244244

@@ -276,6 +276,72 @@ query.From("region", isCurrentOnly: false, static from =>
276276
var expected = "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";
277277
```
278278

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+
var query = 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
287+
var createTableQuery = query.ToCreateTableQuery("table_b", isTemporary: true);
288+
289+
// Print the generated SQL query
290+
Console.WriteLine(createTableQuery.ToSql());
291+
```
292+
293+
#### 🔍 Expected SQL output
294+
295+
```sql
296+
CREATE TEMPORARY TABLE table_b AS SELECT a.table_a_id, 1 AS 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+
var query = 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
309+
var insertTableQuery = query.ToInsertQuery("table_b", hasReturning: true);
310+
311+
// Print the generated SQL query
312+
Console.WriteLine(insertTableQuery.ToSql());
313+
```
314+
315+
#### 🔍 Expected SQL output
316+
317+
```sql
318+
INSERT INTO table_b(table_a_id, value)
319+
SELECT a.table_a_id, 1 AS value FROM table_a AS a
320+
RETURNING *;
321+
```
322+
323+
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+
var query = QueryAstParser.Parse("SELECT a.table_a_id, 1 AS value FROM table_a AS a");
329+
330+
var updateQuery = query.ToUpdateQuery("table_b", new[] { "table_a_id" });
331+
332+
var expected = "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+
var query = QueryAstParser.Parse("SELECT a.table_a_id, 1 AS value FROM table_a AS a");
339+
340+
var deleteQuery = query.ToDeleteQuery("table_b", new[] { "table_a_id" });
341+
342+
var expected = "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+
279345
## 📌 Conclusion
280346

281347
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

Comments
 (0)