-
Notifications
You must be signed in to change notification settings - Fork 37
/
database.go
137 lines (125 loc) · 4.72 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
package go_ibm_db
import (
"fmt"
"strings"
"unsafe"
"github.com/ibmdb/go_ibm_db/api"
trc "github.com/ibmdb/go_ibm_db/log2"
)
// CreateDb function will take the db name and user details as parameters
// and create the database.
func CreateDb(dbname string, connStr string, options ...string) (bool, error) {
trc.Trace1("database.go: CreateDb() - ENTRY")
trc.Trace1(fmt.Sprintf("dbname=%s, connStr=%s", dbname, connStr))
if dbname == "" {
trc.Trace1("Error: Database name cannot be empty")
trc.Trace1("database.go: CreateDb() - EXIT")
return false, fmt.Errorf("Database name cannot be empty")
}
var codeset, mode string
count := len(options)
if count > 0 {
for i := 0; i < count; i++ {
opt := strings.Split(options[i], "=")
if opt[0] == "codeset" {
codeset = opt[1]
} else if opt[0] == "mode" {
mode = opt[1]
} else {
return false, fmt.Errorf("not a valid parameter")
}
}
}
connStr = connStr + ";" + "ATTACH=true"
trc.Trace1("database.go: CreateDb() - EXIT")
return createDatabase(dbname, connStr, codeset, mode)
}
func createDatabase(dbname string, connStr string, codeset string, mode string) (bool, error) {
trc.Trace1("database.go: createDatabase() - ENTRY")
trc.Trace1(fmt.Sprintf("dbname=%s, connStr=%s, codeset=%s, mode=%s", dbname, connStr, codeset, mode))
var out api.SQLHANDLE
in := api.SQLHANDLE(api.SQL_NULL_HANDLE)
bufDBN := api.StringToUTF16(dbname)
bufCS := api.StringToUTF16(connStr)
bufC := api.StringToUTF16(codeset)
bufM := api.StringToUTF16(mode)
ret := api.SQLAllocHandle(api.SQL_HANDLE_ENV, in, &out)
if IsError(ret) {
return false, NewError("SQLAllocHandle", api.SQLHENV(in))
}
drvH := api.SQLHENV(out)
ret = api.SQLAllocHandle(api.SQL_HANDLE_DBC, api.SQLHANDLE(drvH), &out)
if IsError(ret) {
defer releaseHandle(drvH)
return false, NewError("SQLAllocHandle", drvH)
}
hdbc := api.SQLHDBC(out)
ret = api.SQLDriverConnect(hdbc, 0,
(*api.SQLWCHAR)(unsafe.Pointer(&bufCS[0])), api.SQLSMALLINT(len(bufCS)),
nil, 0, nil, api.SQL_DRIVER_NOPROMPT)
if IsError(ret) {
defer releaseHandle(hdbc)
return false, NewError("SQLDriverConnect", hdbc)
}
if codeset == "" && mode == "" {
ret = api.SQLCreateDb(hdbc, (*api.SQLWCHAR)(unsafe.Pointer(&bufDBN[0])), api.SQLINTEGER(len(bufDBN)), nil, 0, nil, 0)
} else if codeset == "" {
ret = api.SQLCreateDb(hdbc, (*api.SQLWCHAR)(unsafe.Pointer(&bufDBN[0])), api.SQLINTEGER(len(bufDBN)), nil, 0, (*api.SQLWCHAR)(unsafe.Pointer(&bufM[0])), api.SQLINTEGER(len(bufM)))
} else if mode == "" {
ret = api.SQLCreateDb(hdbc, (*api.SQLWCHAR)(unsafe.Pointer(&bufDBN[0])), api.SQLINTEGER(len(bufDBN)), (*api.SQLWCHAR)(unsafe.Pointer(&bufC[0])), api.SQLINTEGER(len(bufC)), nil, 0)
} else {
ret = api.SQLCreateDb(hdbc, (*api.SQLWCHAR)(unsafe.Pointer(&bufDBN[0])), api.SQLINTEGER(len(bufDBN)), (*api.SQLWCHAR)(unsafe.Pointer(&bufC[0])), api.SQLINTEGER(len(bufC)), (*api.SQLWCHAR)(unsafe.Pointer(&bufM[0])), api.SQLINTEGER(len(bufM)))
}
if IsError(ret) {
defer releaseHandle(hdbc)
return false, NewError("SQLCreateDb", hdbc)
}
defer releaseHandle(hdbc)
trc.Trace1("database.go: createDatabase() - EXIT")
return true, nil
}
// DropDb function will take the db name and user details as parameters
// and drop the database.
func DropDb(dbname string, connStr string) (bool, error) {
trc.Trace1("database.go: DropDb() - ENTRY")
trc.Trace1(fmt.Sprintf("dbname=%s, connStr=%s", dbname, connStr))
if dbname == "" {
return false, fmt.Errorf("Database name cannot be empty")
}
connStr = connStr + ";" + "ATTACH=true"
trc.Trace1("database.go: DropDb() - EXIT")
return dropDatabase(dbname, connStr)
}
func dropDatabase(dbname string, connStr string) (bool, error) {
trc.Trace1("database.go: dropDatabase() - ENTRY")
trc.Trace1(fmt.Sprintf("dbname=%s, connStr=%s", dbname, connStr))
var out api.SQLHANDLE
in := api.SQLHANDLE(api.SQL_NULL_HANDLE)
bufDBN := api.StringToUTF16(dbname)
bufCS := api.StringToUTF16(connStr)
ret := api.SQLAllocHandle(api.SQL_HANDLE_ENV, in, &out)
if IsError(ret) {
return false, NewError("SQLAllocHandle", api.SQLHENV(in))
}
drvH := api.SQLHENV(out)
ret = api.SQLAllocHandle(api.SQL_HANDLE_DBC, api.SQLHANDLE(drvH), &out)
if IsError(ret) {
defer releaseHandle(drvH)
return false, NewError("SQLAllocHandle", drvH)
}
hdbc := api.SQLHDBC(out)
ret = api.SQLDriverConnect(hdbc, 0,
(*api.SQLWCHAR)(unsafe.Pointer(&bufCS[0])), api.SQLSMALLINT(len(bufCS)),
nil, 0, nil, api.SQL_DRIVER_NOPROMPT)
if IsError(ret) {
defer releaseHandle(hdbc)
return false, NewError("SQLDriverConnect", hdbc)
}
ret = api.SQLDropDb(hdbc, (*api.SQLWCHAR)(unsafe.Pointer(&bufDBN[0])), api.SQLINTEGER(len(bufDBN)))
if IsError(ret) {
defer releaseHandle(hdbc)
return false, NewError("SQLDropDb", hdbc)
}
defer releaseHandle(hdbc)
return true, nil
}