Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce executemultiple for returning multiple resultsets from #26

Merged
merged 1 commit into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ DBInterface.execute(stmt, (col1=1, col2=3.14)) # execute the prepared INSERT sta

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

results = DBInterface.executemultiple(conn, sql) # where sql is a query that returns multiple resultsets

# first iterate through resultsets
for result in results
# for each resultset, we can iterate through resultset rows
for row in result
@show propertynames(row)
row.col1
row[1]
end
end

DBInterface.close!(stmt) # close the prepared statement
```

Expand All @@ -50,5 +62,6 @@ DBInterface.close!
DBInterface.Statement
DBInterface.prepare
DBInterface.execute
DBInterface.executemultiple
DBInterface.lastrowid
```
18 changes: 17 additions & 1 deletion src/DBInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ which themselves must be property-accessible (i.e. implement `propertynames` and
and indexable (i.e. implement `length` and `getindex` for value access by index). These "result" objects do not need
to subtype `DBInterface.Cursor` explicitly as long as they satisfy the interface. For DDL/DML SQL statements, which typically
do not return results, an iterator is still expected to be returned that just iterates `nothing`, i.e. an "empty" iterator.

Note that `DBInterface.execute` returns ***a single*** `DBInterface.Cursor`, which represents a single resultset from the database.
For use-cases involving multiple resultsets from a single query, see `DBInterface.executemultiple`.
"""
function execute end

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

"""
DBInterface.executemany(conn::DBInterface.Connect, sql::AbstractString, [params]) => Nothing
DBInterface.executemany(conn::DBInterface.Connection, sql::AbstractString, [params]) => Nothing
DBInterface.executemany(stmt::DBInterface.Statement, [params]) => Nothing

Similar in usage to `DBInterface.execute`, but allows passing multiple sets of parameters to be executed in sequence.
Expand All @@ -117,6 +120,19 @@ end

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

"""
DBInterface.executemultiple(conn::DBInterface.Connection, sql::AbstractString, [params]) => Cursor-iterator
DBInterface.executemultiple(stmt::DBInterface.Statement, [params]) => Cursor-iterator

Some databases allow returning multiple resultsets from a "single" query (typically semi-colon (`;`) separated statements, or from calling stored procedures).
This function takes the exact same arguments as `DBInterface.execute`, but instead of returning a single `Cursor`, it returns an iterator of `Cursor`s.
This function defines a generic fallback that just returns `(DBInterface.execute(stmt, params),)`, a length-1 tuple for a single `Cursor` resultset.
"""
function executemultiple end

executemultiple(stmt::Statement, params=()) = (DBInterface.execute(stmt, params),)
executemultiple(conn::Connection, sql::AbstractString, params=()) = executemultiple(prepare(conn, sql), params)

"""
DBInterface.close!(stmt::DBInterface.Statement)

Expand Down