Skip to content

Commit b3c2a44

Browse files
authored
Merge pull request #26 from JuliaDatabases/jq/executemultiple
Introduce `executemultiple` for returning multiple resultsets from
2 parents 4988224 + 17e015e commit b3c2a44

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ DBInterface.execute(stmt, (col1=1, col2=3.14)) # execute the prepared INSERT sta
3838

3939
DBInterface.executemany(stmt, (col1=[1,2,3,4,5], col2=[3.14, 1.23, 2.34 3.45, 4.56])) # execute the prepared statement multiple times for each set of named parameters; each named parameter must be an indexable collection
4040

41+
results = DBInterface.executemultiple(conn, sql) # where sql is a query that returns multiple resultsets
42+
43+
# first iterate through resultsets
44+
for result in results
45+
# for each resultset, we can iterate through resultset rows
46+
for row in result
47+
@show propertynames(row)
48+
row.col1
49+
row[1]
50+
end
51+
end
52+
4153
DBInterface.close!(stmt) # close the prepared statement
4254
```
4355

@@ -50,5 +62,6 @@ DBInterface.close!
5062
DBInterface.Statement
5163
DBInterface.prepare
5264
DBInterface.execute
65+
DBInterface.executemultiple
5366
DBInterface.lastrowid
5467
```

src/DBInterface.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ which themselves must be property-accessible (i.e. implement `propertynames` and
7373
and indexable (i.e. implement `length` and `getindex` for value access by index). These "result" objects do not need
7474
to subtype `DBInterface.Cursor` explicitly as long as they satisfy the interface. For DDL/DML SQL statements, which typically
7575
do not return results, an iterator is still expected to be returned that just iterates `nothing`, i.e. an "empty" iterator.
76+
77+
Note that `DBInterface.execute` returns ***a single*** `DBInterface.Cursor`, which represents a single resultset from the database.
78+
For use-cases involving multiple resultsets from a single query, see `DBInterface.executemultiple`.
7679
"""
7780
function execute end
7881

@@ -91,7 +94,7 @@ Base.size(x::LazyIndex) = (length(x.x),)
9194
Base.getindex(x::LazyIndex, i::Int) = x.x[i][x.i]
9295

9396
"""
94-
DBInterface.executemany(conn::DBInterface.Connect, sql::AbstractString, [params]) => Nothing
97+
DBInterface.executemany(conn::DBInterface.Connection, sql::AbstractString, [params]) => Nothing
9598
DBInterface.executemany(stmt::DBInterface.Statement, [params]) => Nothing
9699
97100
Similar in usage to `DBInterface.execute`, but allows passing multiple sets of parameters to be executed in sequence.
@@ -117,6 +120,19 @@ end
117120

118121
executemany(conn::Connection, sql::AbstractString, params=()) = executemany(prepare(conn, sql), params)
119122

123+
"""
124+
DBInterface.executemultiple(conn::DBInterface.Connection, sql::AbstractString, [params]) => Cursor-iterator
125+
DBInterface.executemultiple(stmt::DBInterface.Statement, [params]) => Cursor-iterator
126+
127+
Some databases allow returning multiple resultsets from a "single" query (typically semi-colon (`;`) separated statements, or from calling stored procedures).
128+
This function takes the exact same arguments as `DBInterface.execute`, but instead of returning a single `Cursor`, it returns an iterator of `Cursor`s.
129+
This function defines a generic fallback that just returns `(DBInterface.execute(stmt, params),)`, a length-1 tuple for a single `Cursor` resultset.
130+
"""
131+
function executemultiple end
132+
133+
executemultiple(stmt::Statement, params=()) = (DBInterface.execute(stmt, params),)
134+
executemultiple(conn::Connection, sql::AbstractString, params=()) = executemultiple(prepare(conn, sql), params)
135+
120136
"""
121137
DBInterface.close!(stmt::DBInterface.Statement)
122138

0 commit comments

Comments
 (0)