Skip to content

Commit

Permalink
Merge pull request #52 from k1LoW/hide-fts-shadow-tables
Browse files Browse the repository at this point in the history
Support SQLite FTS3/FTS4 Virtual Table
  • Loading branch information
k1LoW authored Aug 7, 2018
2 parents 4616d3c + cbd032b commit 0644406
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 7 deletions.
33 changes: 32 additions & 1 deletion drivers/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
)

var reFK = regexp.MustCompile(`FOREIGN KEY \((.+)\) REFERENCES ([^\s]+)\s?\((.+)\)`)
var reFTS = regexp.MustCompile(`(?i)USING\s+fts([34])`)

var shadowTables []string

// Sqlite struct
type Sqlite struct{}
Expand Down Expand Up @@ -52,6 +55,18 @@ WHERE name != 'sqlite_sequence' AND (type = 'table' OR type = 'view');`)
return errors.WithStack(err)
}

if reFTS.MatchString(tableDef) {
tableType = "virtual table"
matches := reFTS.FindStringSubmatch(tableDef)
shadowTables = append(shadowTables, fmt.Sprintf("%s_content", tableName))
shadowTables = append(shadowTables, fmt.Sprintf("%s_segdir", tableName))
shadowTables = append(shadowTables, fmt.Sprintf("%s_segments", tableName))
if matches[1] == "4" {
shadowTables = append(shadowTables, fmt.Sprintf("%s_stat", tableName))
shadowTables = append(shadowTables, fmt.Sprintf("%s_docsize", tableName))
}
}

table := &schema.Table{
Name: tableName,
Type: tableType,
Expand Down Expand Up @@ -301,7 +316,14 @@ SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND tbl_name = ?;
tables = append(tables, table)
}

s.Tables = tables
filtered := []*schema.Table{}
for _, t := range tables {
if !contains(shadowTables, t.Name) {
filtered = append(filtered, t)
}
}

s.Tables = filtered

// Relations
for _, r := range relations {
Expand Down Expand Up @@ -392,3 +414,12 @@ func parseCheckConstraints(sql string) []*schema.Constraint {

return constraints
}

func contains(s []string, e string) bool {
for _, v := range s {
if e == v {
return true
}
}
return false
}
12 changes: 6 additions & 6 deletions output/md/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ var _Assetsac44302fb6150a621aa9d04a0350aac972bf7e18 = "# {{ .Table.Name }}\n\n##

// Assets returns go-assets FileSystem
var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"index.md.tmpl", "table.md.tmpl"}}, map[string]*assets.File{
"/index.md.tmpl": &assets.File{
Path: "/index.md.tmpl",
FileMode: 0x1a4,
Mtime: time.Unix(1532239511, 1532239511000000000),
Data: []byte(_Assets43889384df1c6f74d764c29d91b9d5637eb46061),
}, "/table.md.tmpl": &assets.File{
"/table.md.tmpl": &assets.File{
Path: "/table.md.tmpl",
FileMode: 0x1a4,
Mtime: time.Unix(1532785399, 1532785399000000000),
Expand All @@ -26,4 +21,9 @@ var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"index.md.tm
FileMode: 0x800001ed,
Mtime: time.Unix(1532785399, 1532785399000000000),
Data: nil,
}, "/index.md.tmpl": &assets.File{
Path: "/index.md.tmpl",
FileMode: 0x1a4,
Mtime: time.Unix(1532239511, 1532239511000000000),
Data: []byte(_Assets43889384df1c6f74d764c29d91b9d5637eb46061),
}}, "")
2 changes: 2 additions & 0 deletions sample/sqlite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
| [post_comments](post_comments.md) | 7 | post and comments View table | view |
| [CamelizeTable](CamelizeTable.md) | 2 | | table |
| [check_constraints](check_constraints.md) | 6 | | table |
| [syslog](syslog.md) | 1 | | virtual table |
| [access_log](access_log.md) | 1 | | virtual table |

## Relations

Expand Down
34 changes: 34 additions & 0 deletions sample/sqlite/access_log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# access_log

## Description


<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE VIRTUAL TABLE access_log USING fts4(logs)
```

</details>


## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| logs | | | true | | | |







## Relations

![er](access_log.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/sqlite/access_log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sample/sqlite/schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions sample/sqlite/syslog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# syslog

## Description


<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE VIRTUAL TABLE syslog USING fts3(logs)
```

</details>


## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| logs | | | true | | | |







## Relations

![er](syslog.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/sqlite/syslog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
PRAGMA foreign_keys = ON;
DROP TRIGGER IF EXISTS update_posts_updated;
DROP VIEW IF EXISTS post_comments;
DROP TABLE IF EXISTS access_log;
DROP TABLE IF EXISTS syslog;
DROP TABLE IF EXISTS check_constraints;
DROP TABLE IF EXISTS CamelizeTable;
DROP TABLE IF EXISTS logs;
Expand Down Expand Up @@ -93,3 +95,6 @@ CREATE TABLE check_constraints (
NULL check(length(nl) > 4 OR
nl != 'ln')
);

CREATE VIRTUAL TABLE syslog USING fts3(logs);
CREATE VIRTUAL TABLE access_log USING fts4(logs);

0 comments on commit 0644406

Please sign in to comment.