-
Notifications
You must be signed in to change notification settings - Fork 2
/
stmt.jl
302 lines (236 loc) · 10.1 KB
/
stmt.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
@inline column_name(query_info::OraQueryInfo) = unsafe_string(query_info.name, query_info.name_length)
function ncol(stmt::QueryStmt)
if stmt.columns_info == nothing
init_columns_info!(stmt)
end
return length(stmt.columns_info)
end
function columns_info(stmt::QueryStmt) :: Vector{OraQueryInfo}
if stmt.columns_info == nothing
init_columns_info!(stmt)
end
return stmt.columns_info
end
function column_info(stmt::QueryStmt, column_index::Integer) :: OraQueryInfo
if stmt.columns_info == nothing
init_columns_info!(stmt)
end
return stmt.columns_info[column_index]
end
@inline function oracle_type(stmt::QueryStmt, column_index::Integer) :: OraOracleTypeNum
return column_info(stmt, column_index).type_info.oracle_type_num
end
function init_columns_info!(stmt::QueryStmt)
function get_stmt_num_columns(ctx::Context, stmt_handle::Ptr{Cvoid})
num_columns_ref = Ref{UInt32}(0)
result = dpiStmt_getNumQueryColumns(stmt_handle, num_columns_ref)
error_check(ctx, result)
return num_columns_ref[]
end
function get_query_column_info(ctx::Context, stmt_handle::Ptr{Cvoid}, column_index::Integer)
query_info_ref = Ref{OraQueryInfo}()
result = dpiStmt_getQueryInfo(stmt_handle, UInt32(column_index), query_info_ref)
error_check(context(stmt), result)
return query_info_ref[]
end
columns_info = Vector{OraQueryInfo}()
for col in 1:get_stmt_num_columns(context(stmt), stmt.handle)
push!(columns_info, get_query_column_info(context(stmt), stmt.handle, col))
end
stmt.columns_info = columns_info
nothing
end
function Stmt(connection::Connection, sql::AbstractString; scrollable::Bool=false, tag::AbstractString="", fetch_array_size::Integer=ORA_DEFAULT_FETCH_ARRAY_SIZE)
stmt_handle_ref = Ref{Ptr{Cvoid}}()
result = dpiConn_prepareStmt(connection.handle, scrollable, String(sql), String(tag), stmt_handle_ref)
error_check(connection.context, result)
return Stmt(connection, stmt_handle_ref[], scrollable, fetch_array_size=fetch_array_size)
end
function stmt(f::Function, connection::Connection, sql::AbstractString; scrollable::Bool=false, tag::AbstractString="", fetch_array_size::Integer=ORA_DEFAULT_FETCH_ARRAY_SIZE)
stmt = Stmt(connection, sql, scrollable=scrollable, tag=tag, fetch_array_size=fetch_array_size)
try
f(stmt)
finally
close(stmt)
destroy!(stmt)
end
nothing
end
"Number of affected rows in a DML statement."
function row_count(stmt::Stmt)
row_count_ref = Ref{UInt64}()
result = dpiStmt_getRowCount(stmt.handle, row_count_ref)
error_check(context(stmt), result)
return row_count_ref[]
end
"""
execute(stmt::Stmt; exec_mode::dpiExecMode=ORA_MODE_EXEC_DEFAULT) :: UInt32
Returns the number of columns which are being queried.
If the statement does not refer to a query, the value is set to 0.
"""
function execute(stmt::Stmt; exec_mode::OraExecMode=ORA_MODE_EXEC_DEFAULT) :: UInt32
num_columns_ref = Ref{UInt32}(0)
result = dpiStmt_execute(stmt.handle, exec_mode, num_columns_ref)
error_check(context(stmt), result)
return num_columns_ref[]
end
"""
execute(connection::Connection, sql::AbstractString;
scrollable::Bool=false,
tag::AbstractString="",
exec_mode::OraExecMode=ORA_MODE_EXEC_DEFAULT
) :: UInt32
Execute a single `sql` statement.
Returns the number of columns which are being queried.
If the statement does not refer to a query, the value is set to 0.
"""
function execute(connection::Connection, sql::AbstractString; scrollable::Bool=false, tag::AbstractString="", exec_mode::OraExecMode=ORA_MODE_EXEC_DEFAULT) :: UInt32
local result::UInt32
stmt(connection, sql, scrollable=scrollable, tag=tag) do stmt
result = execute(stmt, exec_mode=exec_mode)
end
return result
end
function execute_script(connection::Connection, filepath::AbstractString; scrollable::Bool=false, tag::AbstractString="", exec_mode::OraExecMode=ORA_MODE_EXEC_DEFAULT) :: UInt32
@assert isfile(filepath) "Couldn't find script file $filepath."
local sql::String
open(filepath, "r") do io
sql = read(io, String)
end
return execute(connection, sql, scrollable=scrollable, tag=tag, exec_mode=exec_mode)
end
# execute many
function execute_many(connection::Connection, sql::AbstractString, columns::Vector; scrollable::Bool=false, tag::AbstractString="", exec_mode::OraExecMode=ORA_MODE_EXEC_DEFAULT) :: UInt32
local result::UInt32
stmt(connection, sql, scrollable=scrollable, tag=tag) do stmt
result = execute(stmt, columns, exec_mode=exec_mode)
end
return result
end
# execute many
function execute_many(stmt::Stmt, input_columns::Vector; exec_mode::OraExecMode=ORA_MODE_EXEC_DEFAULT) :: UInt32
@assert !isempty(input_columns) "Cannot bind empty columns to statement."
@assert eltype(input_columns) <: Vector "`input_columns` argument is expected to be a vector of vectors."
function check_columns_length(columns::Vector)
columns_count = length(columns)
if columns_count <= 1
return
end
first_column_length = length(columns[1])
for i in 2:length(columns)
@inbounds @assert length(columns[i]) == first_column_length
end
nothing
end
check_columns_length(input_columns)
for (c, column) in enumerate(input_columns)
stmt[c] = Variable(stmt.connection, column)
end
# execute
num_iters = length(input_columns[1])
execute_many(stmt, num_iters, exec_mode | ORA_MODE_EXEC_ARRAY_DML_ROWCOUNTS)
# get row counts
function row_counts(stmt::Stmt) :: Vector{UInt64}
row_counts_length_ref = Ref{UInt32}()
row_counts_array_ref = Ref{Ptr{UInt64}}()
result = dpiStmt_getRowCounts(stmt.handle, row_counts_length_ref, row_counts_array_ref)
error_check(context(stmt), result)
row_counts_length = row_counts_length_ref[]
row_counts_array_as_ptr = row_counts_array_ref[]
row_counts = Vector{UInt64}(undef, row_counts_length)
for i in 1:row_counts_length
@inbounds row_counts[i] = unsafe_load(row_counts_array_as_ptr, i)
end
return row_counts
end
return sum(row_counts(stmt))
end
# DEPRECATE
function execute(connection::Connection, sql::AbstractString, columns::Vector; scrollable::Bool=false, tag::AbstractString="", exec_mode::OraExecMode=ORA_MODE_EXEC_DEFAULT) :: UInt32
execute_many(connection, sql, columns, scrollable=scrollable, tag=tag, exec_mode=exec_mode)
end
# DEPRECATE
function execute(stmt::Stmt, input_columns::Vector; exec_mode::OraExecMode=ORA_MODE_EXEC_DEFAULT) :: UInt32
execute_many(stmt, input_columns, exec_mode=exec_mode)
end
@inline function execute_many(stmt::Stmt, num_iters::Integer, exec_mode::OraExecMode)
result = dpiStmt_executeMany(stmt.handle, exec_mode, UInt32(num_iters))
error_check(context(stmt), result)
return
end
function execute_many(stmt::Stmt,
num_iters::Integer,
variables::Dict{K,V}
; exec_mode::OraExecMode=ORA_MODE_EXEC_DEFAULT) where {K<:Union{Integer, Symbol}, V<:Variable}
for (k, v) in variables
stmt[k] = v
end
execute_many(stmt, num_iters, exec_mode)
end
function execute_many(stmt::Stmt,
num_iters::Integer,
variables::Vector{V}
; exec_mode::OraExecMode=ORA_MODE_EXEC_DEFAULT) where {V<:Variable}
for (k, v) in enumerate(variables)
stmt[k] = v
end
execute_many(stmt, num_iters, exec_mode)
end
function close(stmt::Stmt; tag::AbstractString="")
if stmt.is_open
result = dpiStmt_close(stmt.handle, tag=String(tag))
error_check(context(stmt), result)
stmt.is_open = false
end
nothing
end
function fetch_array_size(stmt::Stmt) :: UInt32
array_size_ref = Ref{UInt32}()
result = dpiStmt_getFetchArraySize(stmt.handle, array_size_ref)
error_check(context(stmt), result)
return array_size_ref[]
end
"""
fetch_array_size!(stmt::Stmt, new_size::Integer)
Sets the array size used for performing fetches.
All variables defined for fetching must have this many (or more) elements allocated for them.
The higher this value is the less network round trips are required to fetch rows from the database
but more memory is also required.
A value of zero will reset the array size to the default value of DPI_DEFAULT_FETCH_ARRAY_SIZE.
"""
function fetch_array_size!(stmt::Stmt, new_size::Integer)
result = dpiStmt_setFetchArraySize(stmt.handle, UInt32(new_size))
error_check(context(stmt), result)
nothing
end
reset_fetch_array_size!(stmt::Stmt) = fetch_array_size!(stmt, UInt32(0))
"""
fetch(stmt::Stmt) :: FetchResult
Fetches a single row from the statement.
"""
function fetch(stmt::Stmt) :: FetchResult
found_ref = Ref{Int32}(0)
buffer_row_index_ref = Ref{UInt32}(0) # This index is used as the array position for getting values from the variables that have been defined for the statement.
result = dpiStmt_fetch(stmt.handle, found_ref, buffer_row_index_ref)
error_check(context(stmt), result)
local found::Bool = false
if found_ref[] != 0
found = true
end
return FetchResult(found, buffer_row_index_ref[])
end
function fetchrows(stmt::Stmt, max_rows::Integer=ORA_DEFAULT_FETCH_ARRAY_SIZE) :: FetchRowsResult
buffer_row_index_ref = Ref{UInt32}()
num_rows_fetched_ref = Ref{UInt32}()
more_rows_ref = Ref{Int32}()
result = dpiStmt_fetchRows(stmt.handle, UInt32(max_rows), buffer_row_index_ref, num_rows_fetched_ref, more_rows_ref)
error_check(context(stmt), result)
return FetchRowsResult(buffer_row_index_ref[], num_rows_fetched_ref[], more_rows_ref[])
end
function query_oracle_value(stmt::Stmt, column_index::Integer) :: ExternOracleValue
native_type_ref = Ref{OraNativeTypeNum}()
data_handle_ref = Ref{Ptr{OraData}}()
result = dpiStmt_getQueryValue(stmt.handle, UInt32(column_index), native_type_ref, data_handle_ref)
error_check(context(stmt), result)
return ExternOracleValue(stmt, oracle_type(stmt, column_index), native_type_ref[], data_handle_ref[]; use_add_ref=true)
end