Skip to content

Commit 4af5139

Browse files
authored
Make mysql_execute type stable (#86)
* Return Vector{DataFrame} only * Wrap number of affected rows * Fix for prep statement execute, fix tests
1 parent 99da113 commit 4af5139

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/handy.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ function mysql_execute(hndl, command; opformat=MYSQL_DATA_FRAME)
146146

147147
if opformat == MYSQL_DATA_FRAME
148148
convfunc = mysql_result_to_dataframe
149+
narfunc = n -> DataFrame(num_affected_rows=[n])
149150
elseif opformat == MYSQL_TUPLES
150151
convfunc = mysql_get_result_as_tuples
152+
narfunc = n -> (n, )
151153
else
152154
throw(MySQLInterfaceError("Invalid output format: $opformat"))
153155
end
@@ -159,7 +161,8 @@ function mysql_execute(hndl, command; opformat=MYSQL_DATA_FRAME)
159161
push!(data, retval)
160162

161163
elseif mysql_field_count(hndl.mysqlptr) == 0
162-
push!(data, Int(mysql_affected_rows(hndl.mysqlptr)))
164+
n = Int(mysql_affected_rows(hndl.mysqlptr))
165+
push!(data, narfunc(n))
163166
else
164167
throw(MySQLInterfaceError("Query expected to produce results but did not."))
165168
end
@@ -172,9 +175,6 @@ function mysql_execute(hndl, command; opformat=MYSQL_DATA_FRAME)
172175
end
173176
end
174177

175-
if length(data) == 1
176-
return data[1]
177-
end
178178
return data
179179
end
180180

@@ -188,9 +188,9 @@ function mysql_execute(hndl::MySQLHandle; opformat=MYSQL_DATA_FRAME)
188188
naff = mysql_stmt_affected_rows(hndl)
189189
naff != typemax(typeof(naff)) && return naff # Not a SELECT query
190190
if opformat == MYSQL_DATA_FRAME
191-
return mysql_result_to_dataframe(hndl)
191+
return [mysql_result_to_dataframe(hndl)]
192192
elseif opformat == MYSQL_TUPLES
193-
return mysql_get_result_as_tuples(hndl)
193+
return [mysql_get_result_as_tuples(hndl)]
194194
else
195195
throw(MySQLInterfaceError("Invalid output format: $opformat"))
196196
end

test/test_basic.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ end
5858

5959
function show_results()
6060
command = """SELECT * FROM Employee;"""
61-
dframe = mysql_execute(hndl, command)
61+
dframe = mysql_execute(hndl, command)[1]
6262
println("\n *** Results as Dataframe: \n", dframe)
6363
println("\n *** Expected Result: \n", DataFrameResults)
6464
@test dfisequal(dframe, DataFrameResults)
@@ -72,7 +72,7 @@ function show_results()
7272
end
7373

7474
println("\n *** Results as tuples: \n")
75-
tupres = mysql_execute(hndl, command; opformat=MYSQL_TUPLES)
75+
tupres = mysql_execute(hndl, command; opformat=MYSQL_TUPLES)[1]
7676
println(tupres)
7777
for i in length(tupres)
7878
@test compare_rows(tupres[i], ArrayResults[i])

test/test_prep.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ end
8484
function show_results()
8585
command = "SELECT * FROM Employee WHERE ID > ?;"
8686
mysql_stmt_prepare(hndl, command)
87-
dframe = mysql_execute(hndl, [MYSQL_TYPE_LONG], [0])
87+
dframe = mysql_execute(hndl, [MYSQL_TYPE_LONG], [0])[1]
8888
println("\n *** Results as dataframe: \n", dframe)
8989
println("\n *** Expected result: \n", DataFrameResultsPrep)
9090
@test dfisequal(dframe, DataFrameResultsPrep)
@@ -100,7 +100,7 @@ function show_results()
100100

101101
mysql_stmt_prepare(hndl, command)
102102
println("\n *** Results as tuples: \n")
103-
tupres = mysql_execute(hndl, [MYSQL_TYPE_LONG], [0]; opformat=MYSQL_TUPLES)
103+
tupres = mysql_execute(hndl, [MYSQL_TYPE_LONG], [0]; opformat=MYSQL_TUPLES)[1]
104104
println(tupres)
105105
for i in length(tupres)
106106
@test compare_rows(tupres[i], ArrayResultsPrep[i])

0 commit comments

Comments
 (0)