forked from ibmdb/go_ibm_db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
119 lines (111 loc) · 3.95 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
package go_ibm_db
import (
"fmt"
"strings"
"unsafe"
"github.com/ibmdb/go_ibm_db/api"
)
// 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) {
if dbname == "" {
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"
return createDatabase(dbname, connStr, codeset, mode)
}
func createDatabase(dbname string, connStr string, codeset string, mode string) (bool, error) {
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)
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) {
if dbname == "" {
return false, fmt.Errorf("Database name cannot be empty")
}
connStr = connStr + ";" + "ATTACH=true"
return dropDatabase(dbname, connStr)
}
func dropDatabase(dbname string, connStr string) (bool, error) {
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
}