-
Notifications
You must be signed in to change notification settings - Fork 4
/
engine.i_dbengine.go
105 lines (79 loc) · 2.55 KB
/
engine.i_dbengine.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package sqlite
import (
"strconv"
"github.com/mxk/go-sqlite/sqlite3"
"github.com/ottemo/commerce/db"
"github.com/ottemo/commerce/env"
)
// GetName returns current DB engine name
func (it *DBEngine) GetName() string {
return "Sqlite3"
}
// HasCollection checks if collection(table) already exists
func (it *DBEngine) HasCollection(collectionName string) bool {
// collectionName = strings.ToLower(collectionName)
SQL := "SELECT name FROM sqlite_master WHERE type='table' AND name='" + collectionName + "'"
stmt, err := connectionQuery(SQL)
defer closeStatement(stmt)
if err == nil {
return true
}
return false
}
// CreateCollection creates cllection(table) by it's name
func (it *DBEngine) CreateCollection(collectionName string) error {
// collectionName = strings.ToLower(collectionName)
SQL := "CREATE TABLE " + collectionName + " (_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL)"
if ConstUseUUIDids {
SQL = "CREATE TABLE " + collectionName + " (_id NCHAR(24) PRIMARY KEY NOT NULL)"
}
err := connectionExec(SQL)
if err == nil {
return nil
}
return env.ErrorDispatch(err)
}
// GetCollection returns collection(table) by name or creates new one
func (it *DBEngine) GetCollection(collectionName string) (db.InterfaceDBCollection, error) {
if !ConstSQLNameValidator.MatchString(collectionName) {
return nil, env.ErrorNew(ConstErrorModule, ConstErrorLevel, "71bf95dd-d271-4ea9-99a0-52fe561b72bf", "not valid collection name for DB engine")
}
if !it.HasCollection(collectionName) {
if err := it.CreateCollection(collectionName); err != nil {
return nil, env.ErrorDispatch(err)
}
}
collection := &DBCollection{
Name: collectionName,
FilterGroups: make(map[string]*StructDBFilterGroup),
Order: make([]string, 0),
ResultColumns: make([]string, 0),
}
if _, present := it.attributeTypes[collectionName]; !present {
collection.ListColumns()
}
return collection, nil
}
// RawQuery returns collection(table) by name or creates new one
func (it *DBEngine) RawQuery(query string) (map[string]interface{}, error) {
result := make([]map[string]interface{}, 0, 10)
row := make(sqlite3.RowMap)
stmt, err := connectionQuery(query)
defer closeStatement(stmt)
if err == nil {
return nil, env.ErrorDispatch(err)
}
for ; err == nil; err = stmt.Next() {
if err := stmt.Scan(row); err == nil {
if ConstUseUUIDids {
if _, present := row["_id"]; present {
row["_id"] = strconv.FormatInt(row["_id"].(int64), 10)
}
}
result = append(result, row)
} else {
return result[0], nil
}
}
return result[0], nil
}