-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLite_baseTypes.go
54 lines (47 loc) · 1.09 KB
/
SQLite_baseTypes.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
package main
import (
"database/sql"
"fmt"
)
// SQLiteTable is implementation of `SQLTable` for `SQLite`.
type SQLiteTable struct {
name string
columns []SQLColumn
}
func (p *SQLiteTable) addColumn(c []SQLColumn) {
for _, col := range c {
p.columns = append(p.columns, col)
}
}
func (p *SQLiteTable) create(db *sql.DB) (string, error) {
exec := "CREATE TABLE IF NOT EXISTS "
exec += p.name + " ("
for _, col := range p.columns {
val, _ := col.get()
exec += val + ", "
}
exec = exec[0:len(exec)-2] + ");"
_, err := db.Exec(exec)
if err != nil {
fmt.Println(err.Error())
return exec, err
}
return exec, nil
}
func (p *SQLiteTable) setName(name string) {
p.name = name
}
// SQLiteColumn represents a `SQLite` column in `SQLTable`.
// Implements `SQLColumn`.
type SQLiteColumn struct {
name string
sqlType string
args string
}
// get returns readable (for SQL) part of commend.
func (p SQLiteColumn) get() (string, error) {
if p.name == "" || p.sqlType == "" {
return "", StringError{"ERROR: SQLiteColumn is Invalid."}
}
return p.name + " " + p.sqlType + " " + p.args, nil
}