-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdbi.jl
115 lines (97 loc) · 3.19 KB
/
dbi.jl
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
function Base.connect(::Type{MySQL5},
host::String,
user::String,
passwd::String,
db::String, # TODO: Let this be optional?
port::Integer = 0,
unix_socket::Any = C_NULL,
client_flag::Integer = 0)
mysqlptr = C_NULL
mysqlptr = mysql_init(mysqlptr)
if mysqlptr == C_NULL
error("Failed to connect to MySQL database")
end
mysqlptr = mysql_real_connect(mysqlptr,
host,
user,
passwd,
db,
convert(Cint, port), # TODO: Confirm type here
unix_socket,
convert(Uint64, client_flag)) # TODO: Confirm type here
if mysqlptr == C_NULL
error("Failed to connect to MySQL database")
end
return MySQLDatabaseHandle(mysqlptr, 0)
end
function DBI.disconnect(db::MySQLDatabaseHandle)
mysql_close(db.ptr)
return
end
function DBI.columninfo(db::MySQLDatabaseHandle,
table::String,
column::String)
error("DBI API not fully implemented")
end
function DBI.prepare(db::MySQLDatabaseHandle, sql::String)
stmtptr = mysql_stmt_init(db.ptr)
if stmtptr == C_NULL
error("Failed to allocate statement handle")
end
status = mysql_stmt_prepare(stmtptr, sql)
db.status = status
if status != 0
msg = bytestring(mysql_stmt_error(stmtptr))
error(msg)
end
stmt = MySQLStatementHandle(db, stmtptr)
return stmt
end
function DBI.errcode(db::MySQLDatabaseHandle)
return int(mysql_errno(db.ptr))
end
# TODO: Make a copy here?
function DBI.errstring(db::MySQLDatabaseHandle)
return bytestring(mysql_error(db.ptr))
end
function DBI.execute(stmt::MySQLStatementHandle)
status = mysql_stmt_execute(stmt.ptr)
stmt.db.status = status
if status != 0
error(errstring(stmt.db))
else
stmt.executed += 1
end
return
end
function DBI.fetchall(stmt::MySQLStatementHandle)
error("DBI API not fully implemented")
end
function DBI.fetchdf(stmt::MySQLStatementHandle)
error("DBI API not fully implemented")
end
function DBI.fetchrow(stmt::MySQLStatementHandle)
error("DBI API not fully implemented")
end
function DBI.finish(stmt::MySQLStatementHandle)
failed = mysql_stmt_close(stmt.ptr)
if failed
error("Failed to close MySQL statement handle")
end
return
end
function DBI.lastinsertid(db::MySQLDatabaseHandle)
return int64(mysql_insert_id(db.ptr))
end
# TODO: Rename this
function DBI.sqlescape(db::MySQLDatabaseHandle, dirtysql::String)
to = Array(Uint8, 4 * length(dirtysql))
writelength = mysql_real_escape_string(db.ptr,
to,
dirtysql,
convert(Uint32, length(dirtysql)))
return bytestring(to[1:writelength])
end
function DBI.tableinfo(db::MySQLDatabaseHandle, table::String)
error("DBI API not fully implemented")
end