-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite.go
81 lines (79 loc) · 2.27 KB
/
sqlite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package models
import (
"fmt"
"io"
"strings"
)
// ModelToSQLiteScheme takess a model and renders the SQLite DB Schema to out.
// @param out: io.Writer, the target to render the text into
// @param model: *Model, the model to be rendered.
func ModelToSQLiteScheme(out io.Writer, model *Model) error {
if !IsValidVarname(model.Id) {
return fmt.Errorf("model id that can't be used for table name, %q", model.Id)
}
if model.Description != "" {
fmt.Fprintf(out, "-- %s\n", strings.ReplaceAll(model.Description, "\n", "\n-- "))
}
fmt.Fprintf(out, "create table %s if not exists (\n", model.Id)
addNL := false
for i, elem := range model.Elements {
if !IsValidVarname(elem.Id) {
return fmt.Errorf("element id can't be used for column name, %q", elem.Id)
}
if i > 0 {
fmt.Fprintf(out, ",\n")
addNL = true
}
//NOTE: Map HTML5 types to SQLite3 type
var columnType string
switch strings.ToLower(elem.Type) {
case "int":
columnType = "int"
case "integer":
columnType = "int"
case "float":
columnType = "real"
case "real":
columnType = "real"
case "numeric":
columnType = "num"
case "number":
columnType = "num"
case "date":
columnType = "text"
case "datetime-local":
columnType = "text"
case "checkbox":
columnType = "boolean"
default:
columnType = "text"
}
if elem.Generator != "" {
switch elem.Generator {
case "autoincrement":
columnType = fmt.Sprintf("%s autoincrement", columnType)
case "date":
columnType = fmt.Sprintf("%s default current_date not null", columnType)
case "created_date":
columnType = fmt.Sprintf("%s default current_date not null", columnType)
case "current_date":
columnType = fmt.Sprintf("%s default current_date not null", columnType)
case "timestamp":
columnType = fmt.Sprintf("%s default current_timestamp not null", columnType)
case "created_timestamp":
columnType = fmt.Sprintf("%s default current_timestamp not null", columnType)
case "current_timestamp":
columnType = fmt.Sprintf("%s default current_timestamp not null", columnType)
}
}
if elem.IsObjectId {
columnType = fmt.Sprintf(" %s primary key", columnType)
}
fmt.Fprintf(out, " %s %s", elem.Id, columnType)
}
if addNL {
fmt.Fprintf(out, "\n")
}
fmt.Fprintf(out, ");\n")
return nil
}