Skip to content

Commit

Permalink
Release v0.2.4
Browse files Browse the repository at this point in the history
Add Common Table Expression.
  • Loading branch information
mithrandie committed Jul 5, 2017
2 parents ca0f38c + 75b0996 commit ddee3b7
Show file tree
Hide file tree
Showing 27 changed files with 7,828 additions and 6,470 deletions.
1 change: 1 addition & 0 deletions docs/_includes/side_menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<li><a href="{{ '/reference/delete-query.html' | relative_url }}">Delete Query</a></li>
<li><a href="{{ '/reference/create-table-query.html' | relative_url }}">Create Table Query</a></li>
<li><a href="{{ '/reference/alter-table-query.html' | relative_url }}">Alter Table Query</a></li>
<li><a href="{{ '/reference/common-table.html' | relative_url }}">Common Table</a></li>
<li><a href="{{ '/reference/variable.html' | relative_url }}">Variable</a></li>
<li><a href="{{ '/reference/row-value.html' | relative_url }}">Row Value</a></li>
<li><a href="{{ '/reference/cursor.html' | relative_url }}">Cursor</a></li>
Expand Down
75 changes: 75 additions & 0 deletions docs/_posts/2006-01-02-common-table.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
layout: default
title: Common Table - Reference Manual - csvq
category: reference
---

# Common Table

A Common Table is a temporary view that can be referenced in a single query.
You can use common tables in a [Select Query]({{ '/reference/select-query.html' | relative_url }}), [Insert Query]({{ '/reference/insert-query.html' | relative_url }}), [Update Query]({{ '/reference/update-query.html' | relative_url }}), or [Delete Query]({{ '/reference/delete-query.html' | relative_url }}).

## Syntax

```sql
common_table_clause
: WITH common_table [, common_table ...]

common_table
: [RECURSIVE] table_name [(column_name [, column_name ...])] AS (select_query)
```

_table_name_
: [identifier]({{ '/reference/statement.html#parsing' | relative_url }})

_column_name_
: [identifier]({{ '/reference/statement.html#parsing' | relative_url }})

_select_query_
: [select_query]({{ '/reference/select-query.html' | relative_url }})

### Recursion

If you specified a RECURSIVE keyword, the _select_query_ in the common table clause can retrieve the result recursively.
A RECURSIVE keyword is usually used with a [UNION]({{ '/reference/set-operators.html#union' | relative_url }}) operator.

```sql
WITH
RECURSIVE table_name [(column_name [, column_name ...])]
AS (
base_select_query
UNION [ALL]
recursive_select_query
)
```

At first, the result set of the _base_select_query_ is stored in the _temporary view_ for recursion.
Next, the _recursive_select_query_ that reference the _temporary view_ is excuted and the _temporary view_ is replaced by the result set of the _recursive_select_query_.
The execution of the _recursive_select_query is iterated until the result set is empty.
All the result sets are combined by the [UNION]({{ '/reference/set-operators.html#union' | relative_url }}) operator.

Example:
```sql
WITH RECURSIVE ct (n)
AS (
SELECT 1
UNION ALL
SELECT n + 1
FROM ct
WHERE n < 5
)
SELECT n FROM ct;


/* Result Set
+---+
| n |
+---+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+---+
*/
```
10 changes: 8 additions & 2 deletions docs/_posts/2006-01-02-delete-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ Delete query is used to delete records on csv files.
## Delete on a sigle file.

```sql
DELETE
[common_table_clause] DELETE
FROM table_name
[where_clause]
```

_common_table_clause_
: [Common Table Clause]({{ '/reference/common-table.html' | relative_url }})

_table_name_
: [identifier]({{ '/reference/statement.html#parsing' | relative_url }})

Expand All @@ -25,11 +28,14 @@ _where_clause_
## Delete on multiple files

```sql
DELETE table_name [, table_name ...]
[common_table_clause] DELETE table_name [, table_name ...]
from_clause
[where_clause]
```

_common_table_clause_
: [Common Table Clause]({{ '/reference/common-table.html' | relative_url }})

_table_name_
: [identifier]({{ '/reference/statement.html#parsing' | relative_url }})

Expand Down
10 changes: 8 additions & 2 deletions docs/_posts/2006-01-02-insert-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ Insert query is used to insert records to a csv file.
## Insert Values

```sql
INSERT INTO table_name
[common_table_clause] INSERT INTO table_name
[(column_name [, column_name ...])]
VALUES row_value [, row_value ...]
```

_common_table_clause_
: [Common Table Clause]({{ '/reference/common-table.html' | relative_url }})

_table_name_
: [identifier]({{ '/reference/statement.html#parsing' | relative_url }})

Expand All @@ -28,11 +31,14 @@ _row_value_
## Insert From Select Query

```sql
INSERT INTO table_name
[common_table_clause] INSERT INTO table_name
[(column_name [, column_name ...])]
select_query
```

_common_table_clause_
: [Common Table Clause]({{ '/reference/common-table.html' | relative_url }})

_table_name_
: [identifier]({{ '/reference/statement.html#parsing' | relative_url }})

Expand Down
6 changes: 5 additions & 1 deletion docs/_posts/2006-01-02-select-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Select query is used to retrieve data from csv files.

```
select_query
: select_entity
: [common_table_clause]
select_entity
[order_by_clause]
[limit_clause]
[offset_clause]
Expand All @@ -28,6 +29,9 @@ select_set_entity
| (select_query)
```

_common_table_clause_
: [Common Table Clause]({{ '/reference/common-table.html' | relative_url }})

_select_clause_
: [Select Clause](#select_clause)

Expand Down
2 changes: 1 addition & 1 deletion docs/_posts/2006-01-02-statement.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ LAST LEFT LIKE LIMIT
NATURAL NOT NULL NULLS
OFFSET ON OPEN OR ORDER OUTER OVER
PARTITION PERCENT PRINT
RENAME RIGHT ROLLBACK
RECURSIVE RENAME RIGHT ROLLBACK
SELECT SET SEPARATOR STDIN
TABLE THEN TIES TO
UNION UPDATE USING
Expand Down
10 changes: 8 additions & 2 deletions docs/_posts/2006-01-02-update-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ Update query is used to update records on csv files.
## Update on a single file

```sql
UPDATE table_name
[common_table_clause] UPDATE table_name
SET column_name = value [, column_name = value ...]
[where_clause]
```

_common_table_clause_
: [Common Table Clause]({{ '/reference/common-table.html' | relative_url }})

_table_name_
: [identifier]({{ '/reference/statement.html#parsing' | relative_url }})

Expand All @@ -31,12 +34,15 @@ _where_clause_
## Update on multiple files

```sql
UPDATE table_name [, table_name ...]
[common_table_clause] UPDATE table_name [, table_name ...]
SET column_name = value [, column_name = value ...]
from_clause
[where_clause]
```

_common_table_clause_
: [Common Table Clause]({{ '/reference/common-table.html' | relative_url }})

_table_name_
: [identifier]({{ '/reference/statement.html#parsing' | relative_url }})

Expand Down
1 change: 1 addition & 0 deletions docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ title: Reference Manual - csvq
* [Delete Query]({{ '/reference/delete-query.html' | relative_url }})
* [Create Table Query]({{ '/reference/create-table-query.html' | relative_url }})
* [Alter Table Query]({{ '/reference/alter-table-query.html' | relative_url }})
* [Common Table]({{ '/reference/common-table.html' | relative_url }})
* [Variable]({{ '/reference/variable.html' | relative_url }})
* [Row Value]({{ '/reference/row-value.html' | relative_url }})
* [Cursor]({{ '/reference/cursor.html' | relative_url }})
Expand Down
14 changes: 9 additions & 5 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference.html</loc>
<lastmod>2017-07-02T23:29:47+00:00</lastmod>
<lastmod>2017-07-05T21:28:49+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/install.html</loc>
Expand All @@ -30,19 +30,19 @@
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/select-query.html</loc>
<lastmod>2017-06-30T04:36:43+00:00</lastmod>
<lastmod>2017-07-05T21:28:49+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/insert-query.html</loc>
<lastmod>2017-06-29T17:08:49+00:00</lastmod>
<lastmod>2017-07-05T21:28:49+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/update-query.html</loc>
<lastmod>2017-06-29T17:08:49+00:00</lastmod>
<lastmod>2017-07-05T21:28:49+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/delete-query.html</loc>
<lastmod>2017-06-29T17:08:49+00:00</lastmod>
<lastmod>2017-07-05T21:28:49+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/create-table-query.html</loc>
Expand All @@ -52,6 +52,10 @@
<loc>https://mithrandie.github.io/csvq/reference/alter-table-query.html</loc>
<lastmod>2017-06-29T17:08:49+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/common-table.html</loc>
<lastmod>2017-07-05T21:28:49+00:00</lastmod>
</url>
<url>
<loc>https://mithrandie.github.io/csvq/reference/variable.html</loc>
<lastmod>2017-06-29T17:08:49+00:00</lastmod>
Expand Down
11 changes: 9 additions & 2 deletions lib/action/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ func Calc(expr string) error {
selectEntity, _ := program[0].(parser.SelectQuery).SelectEntity.(parser.SelectEntity)

view := query.NewView()
err = view.Load(selectEntity.FromClause.(parser.FromClause), nil)
err = view.Load(selectEntity.FromClause.(parser.FromClause), query.Filter{})
if err != nil {
return err
}

clause := selectEntity.SelectClause.(parser.SelectClause)

var filter query.Filter = []query.FilterRecord{{View: view, RecordIndex: 0}}
filter := query.Filter{
Records: []query.FilterRecord{
{
View: view,
RecordIndex: 0,
},
},
}
values := make([]string, len(clause.Fields))
for i, v := range clause.Fields {
field := v.(parser.Field)
Expand Down
Loading

0 comments on commit ddee3b7

Please sign in to comment.