-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.go
204 lines (179 loc) · 4.42 KB
/
database.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package fb
/*
#include <ibase.h>
#include <stdlib.h>
*/
import "C"
import (
"errors"
"time"
"unsafe"
)
import (
"bytes"
"fmt"
"strconv"
"strings"
)
type Error struct {
Code int
Message string
}
func (this Error) Error() string {
return this.Message
}
type Database struct {
Database string
Username string
Password string
Role string
Charset string
LowercaseNames bool
PageSize int
TimeZone string
}
func MapFromConnectionString(parms string) (map[string]string, error) {
m := make(map[string]string)
kva := strings.Split(parms, ";")
for _, kv := range kva {
pair := strings.SplitN(kv, "=", 2)
if len(pair) != 2 {
continue
}
k, v := strings.TrimSpace(pair[0]), strings.TrimSpace(pair[1])
if k != "" && v != "" {
m[k] = v
}
}
return m, nil
}
func New(parms string) (db *Database, err error) {
p, err := MapFromConnectionString(parms)
database, ok := p["database"]
if !ok {
return nil, errors.New("database parm required")
}
username, ok := p["username"]
if !ok {
return nil, errors.New("username parm required")
}
password, ok := p["password"]
if !ok {
return nil, errors.New("password parm required")
}
charset, _ := p["charset"]
role, _ := p["role"]
lowercaseNames := false
sLowercaseNames, ok := p["lowercase_names"]
if ok {
lowercaseNames, _ = strconv.ParseBool(sLowercaseNames)
}
pageSize := 1024
sPageSize, ok := p["page_size"]
if ok {
pageSize, err = strconv.Atoi(sPageSize)
if err != nil {
return nil, errors.New("Invalid page_size: " + err.Error())
}
}
timezone, _ := p["timezone"]
db = &Database{database, username, password, role, charset, lowercaseNames, pageSize, timezone}
return db, nil
}
func (db *Database) CreateStatement() string {
var defaultCharset string
if db.Charset != "" {
defaultCharset = fmt.Sprintf("DEFAULT CHARACTER SET %s", db.Charset)
}
return fmt.Sprintf("CREATE DATABASE '%s' USER '%s' PASSWORD '%s' PAGE_SIZE = %d %s;",
db.Database, db.Username, db.Password, db.PageSize, defaultCharset)
}
func (db *Database) Create() (*Connection, error) {
var isc_status [20]C.ISC_STATUS
var handle C.isc_db_handle = 0
var local_transact C.isc_tr_handle = 0
sql := C.CString(db.CreateStatement())
sql2 := (*C.ISC_SCHAR)(unsafe.Pointer(sql))
defer C.free(unsafe.Pointer(sql))
if C.isc_dsql_execute_immediate(&isc_status[0], &handle, &local_transact, 0, sql2, 3, nil) != 0 {
return nil, fbErrorCheck(&isc_status)
}
location, err := time.LoadLocation(db.TimeZone)
if err != nil {
location = time.Local
}
return &Connection{database: db, db: handle, Location: location}, nil
}
func Create(parms string) (conn *Connection, err error) {
db, err := New(parms)
if err != nil {
return
}
conn, err = db.Create()
return
}
func (db *Database) createDbp() string {
var buf bytes.Buffer
buf.WriteByte(C.isc_dpb_version1)
buf.WriteByte(C.isc_dpb_user_name)
buf.WriteByte(byte(len(db.Username)))
buf.WriteString(db.Username)
buf.WriteByte(C.isc_dpb_password)
buf.WriteByte(byte(len(db.Password)))
buf.WriteString(db.Password)
if db.Charset != "" {
buf.WriteByte(C.isc_dpb_lc_ctype)
buf.WriteByte(byte(len(db.Charset)))
buf.WriteString(db.Charset)
}
if db.Role != "" {
buf.WriteByte(C.isc_dpb_sql_role_name)
buf.WriteByte(byte(len(db.Role)))
buf.WriteString(db.Role)
}
return buf.String()
}
func (db *Database) Connect() (*Connection, error) {
var isc_status [20]C.ISC_STATUS
var handle C.isc_db_handle = 0
database := C.CString(db.Database)
database2 := (*C.ISC_SCHAR)(unsafe.Pointer(database))
defer C.free(unsafe.Pointer(database))
dbp := db.createDbp()
dbp2 := C.CString(dbp)
dbp3 := (*C.ISC_SCHAR)(unsafe.Pointer(dbp2))
defer C.free(unsafe.Pointer(dbp2))
var length C.short = C.short(len(dbp))
C.isc_attach_database(&isc_status[0], 0, database2, &handle, length, dbp3)
if err := fbErrorCheck(&isc_status); err != nil {
return nil, err
}
location, err := time.LoadLocation(db.TimeZone)
if err != nil {
location = time.Local
}
return &Connection{database: db, db: handle, Location: location}, nil
}
func Connect(parms string) (conn *Connection, err error) {
db, err := New(parms)
if err != nil {
return
}
conn, err = db.Connect()
return
}
func (db *Database) Drop() (err error) {
conn, err := db.Connect()
if err != nil {
return
}
err = conn.Drop()
return
}
func Drop(parms string) (err error) {
db, err := New(parms)
if err != nil {
return
}
return db.Drop()
}