Skip to content

Commit 6ec3a13

Browse files
committed
Rename mysql_connect to mysql_init_and_connect, + docstrings for prep statement APIs
1 parent 8e4056d commit 6ec3a13

File tree

3 files changed

+36
-38
lines changed

3 files changed

+36
-38
lines changed

src/api.jl

+24-26
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ function mysql_stmt_init(db::MySQLDatabaseHandle)
163163
return mysql_stmt_init(db.ptr)
164164
end
165165

166-
# Creates the prepared statement. There should be only 1 statement
166+
"""
167+
Creates the prepared statement. There should be only 1 statement
168+
"""
167169
function mysql_stmt_prepare(stmtptr::Ptr{Cuchar}, sql::String)
168170
s = utf8(sql)
169171
return ccall((:mysql_stmt_prepare, mysql_lib),
@@ -172,42 +174,62 @@ function mysql_stmt_prepare(stmtptr::Ptr{Cuchar}, sql::String)
172174
stmtptr, s, length(s))
173175
end
174176

175-
# Returns the error message for the recently invoked statement API
177+
"""
178+
Returns the error message for the recently invoked statement API
179+
"""
176180
function mysql_stmt_error(stmtptr::Ptr{Cuchar})
177181
return ccall((:mysql_stmt_error, mysql_lib),
178182
Ptr{Cuchar},
179183
(Ptr{Cuchar}, ),
180184
stmtptr)
181185
end
182186

187+
"""
188+
Store the entire result returned by the prepared statement in the
189+
bind datastructure provided by mysql_stmt_bind_result.
190+
"""
183191
function mysql_stmt_store_result(stmtptr::Ptr{Cuchar})
184192
return ccall((:mysql_stmt_store_result, mysql_lib),
185193
Cint,
186194
(Ptr{Cuchar}, ),
187195
stmtptr)
188196
end
189197

198+
"""
199+
Return the metadata for the results that will be received from
200+
the execution of the prepared statement.
201+
"""
190202
function mysql_stmt_result_metadata(stmtptr::Ptr{Cuchar})
191203
return ccall((:mysql_stmt_result_metadata, mysql_lib),
192204
Ptr{Cuchar},
193205
(Ptr{Cuchar}, ),
194206
stmtptr)
195207
end
196208

209+
"""
210+
Equivalent of `mysql_num_rows` for prepared statements.
211+
"""
197212
function mysql_stmt_num_rows(stmtptr::Ptr{Cuchar})
198213
return ccall((:mysql_stmt_num_rows, mysql_lib),
199214
Culonglong,
200215
(Ptr{Cuchar}, ),
201216
stmtptr)
202217
end
203218

219+
"""
220+
Equivalent of `mysql_fetch_row` for prepared statements.
221+
"""
204222
function mysql_stmt_fetch_row(stmtptr::Ptr{Cuchar})
205223
return ccall((:mysql_stmt_fetch, mysql_lib),
206224
Cint,
207225
(Ptr{Cuchar}, ),
208226
stmtptr)
209227
end
210228

229+
"""
230+
Bind the returned data from execution of the prepared statement
231+
to a preallocated datastructure `bind`.
232+
"""
211233
function mysql_stmt_bind_result(stmtptr::Ptr{Uint8}, bind::Ptr{Cuchar})
212234
return ccall((:mysql_stmt_bind_result, mysql_lib),
213235
Cchar,
@@ -502,27 +524,3 @@ function mysql_warning_count(mysqlptr::MYSQL)
502524
return ccall((:mysql_warning_count, mysql_lib), Cuint, (Ptr{Void},),
503525
mysqlptr)
504526
end
505-
506-
# Handy function to make things easier.
507-
# function prepstmt_getResultsAsDataFrame(stmtptr::Ptr{Cuchar}, sql::String)
508-
# response = mysql_stmt_prepare(stmtptr, sql)
509-
#
510-
# if (response == 0)
511-
# results = mysql_stmt_result_metadata(stmtptr)
512-
# response = mysql_stmt_execute(stmtptr)
513-
#
514-
# if (response == 0)
515-
# println("Query executed successfully !!!")
516-
# return obtainResultsAsDataFrame(results, true, stmtptr)
517-
# else
518-
# println("Query execution failed !!!")
519-
# error = bytestring(mysql_stmt_error(stmtptr))
520-
# println("The error is ::: $error")
521-
# end
522-
#
523-
# else
524-
# println("Error in preparing the query !!!")
525-
# mysql_stmt_error(stmtptr)
526-
# end
527-
#
528-
# end

src/handy.jl

+11-11
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
A handy function that wraps mysql_init and mysql_real_connect. Also does error
55
checking on the pointers returned by init and real_connect.
66
"""
7-
function mysql_connect(host::String,
8-
user::String,
9-
passwd::String,
10-
db::String,
11-
port::Integer = 0,
12-
unix_socket::Any = C_NULL,
13-
client_flag::Integer = 0)
7+
function mysql_init_and_connect(host::String,
8+
user::String,
9+
passwd::String,
10+
db::String,
11+
port::Integer = 0,
12+
unix_socket::Any = C_NULL,
13+
client_flag::Integer = 0)
1414

1515
mysqlptr::Ptr{Cuchar} = C_NULL
1616
mysqlptr = mysql_init(mysqlptr)
@@ -39,9 +39,9 @@ end
3939
Wrapper over mysql_real_connect with CLIENT_MULTI_STATEMENTS passed
4040
as client flag options.
4141
"""
42-
function mysql_connect(hostName::String, userName::String, password::String, db::String)
43-
return mysql_connect(hostName, userName, password, db, 0,
44-
C_NULL, MySQL.CLIENT_MULTI_STATEMENTS)
42+
function mysql_init_and_connect(hostName::String, userName::String, password::String, db::String)
43+
return mysql_init_and_connect(hostName, userName, password, db, 0,
44+
C_NULL, MySQL.CLIENT_MULTI_STATEMENTS)
4545
end
4646

4747
"""
@@ -50,4 +50,4 @@ MySQL.mysql_connect.
5050
"""
5151
function mysql_disconnect(db::MySQLDatabaseHandle)
5252
mysql_close(db.ptr)
53-
end
53+
end

test/test.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function show_as_dataframe()
8181
end
8282

8383
function run_test()
84-
global con = MySQL.mysql_connect(HOST, USER, PASSWD, DBNAME)
84+
global con = MySQL.mysql_init_and_connect(HOST, USER, PASSWD, DBNAME)
8585
create_table()
8686

8787
insert_values()

0 commit comments

Comments
 (0)