mirror of
https://gitea.invidious.io/iv-org/shard-crystal-sqlite3.git
synced 2024-08-15 00:53:26 +00:00
Updated to Crystal 0.18.0
This commit is contained in:
parent
b1c6c32ae5
commit
135f33d9ea
5 changed files with 96 additions and 18 deletions
|
@ -84,6 +84,10 @@ class SQLite3::Database
|
|||
execute(sql, binds)
|
||||
end
|
||||
|
||||
def execute(sql, **binds)
|
||||
execute(sql, binds)
|
||||
end
|
||||
|
||||
# Executes the given SQL statement. If additional parameters are given, they are treated as bind variables,
|
||||
# and are bound to the placeholders in the query.
|
||||
#
|
||||
|
@ -97,6 +101,12 @@ class SQLite3::Database
|
|||
end
|
||||
end
|
||||
|
||||
def execute(sql, **binds, &block)
|
||||
execute(sql, binds) do |row|
|
||||
yield row
|
||||
end
|
||||
end
|
||||
|
||||
# Executes the given SQL statement. If additional parameters are given, they are treated as bind variables,
|
||||
# and are bound to the placeholders in the query.
|
||||
#
|
||||
|
@ -104,7 +114,7 @@ class SQLite3::Database
|
|||
# with the key being used as the name of the placeholder to bind the value to.
|
||||
#
|
||||
# Returns an `Array(Array(Value))`.
|
||||
def execute(sql, binds : Enumerable)
|
||||
def execute(sql, binds : Enumerable | NamedTuple)
|
||||
rows = [] of Array(Value)
|
||||
execute(sql, binds) do |row|
|
||||
rows << row
|
||||
|
@ -119,7 +129,7 @@ class SQLite3::Database
|
|||
# with the key being used as the name of the placeholder to bind the value to.
|
||||
#
|
||||
# Yields one `Array(Value)` for each result.
|
||||
def execute(sql, binds : Enumerable, &block)
|
||||
def execute(sql, binds : Enumerable | NamedTuple, &block)
|
||||
query(sql, binds) do |result_set|
|
||||
while result_set.next
|
||||
yield result_set.to_a
|
||||
|
@ -149,7 +159,7 @@ class SQLite3::Database
|
|||
end
|
||||
|
||||
# A convenience method that returns the first value of the first row of a query result.
|
||||
def get_first_value(sql, binds : Enumerable)
|
||||
def get_first_value(sql, binds : Enumerable | NamedTuple)
|
||||
query(sql, binds) do |result_set|
|
||||
if result_set.next
|
||||
return result_set[0]
|
||||
|
@ -172,17 +182,27 @@ class SQLite3::Database
|
|||
end
|
||||
|
||||
# Executes a query and gives back a `ResultSet`.
|
||||
def query(sql, binds : Enumerable)
|
||||
def query(sql, binds : Enumerable | NamedTuple)
|
||||
prepare(sql).execute(binds)
|
||||
end
|
||||
|
||||
# Executes a query and yields a `ResultSet` that will be closed at the end of the given block.
|
||||
def query(sql, binds : Enumerable, &block)
|
||||
def query(sql, binds : Enumerable | NamedTuple, &block)
|
||||
prepare(sql).execute(binds) do |result_set|
|
||||
yield result_set
|
||||
end
|
||||
end
|
||||
|
||||
def query(sql, **binds)
|
||||
query(sql, binds)
|
||||
end
|
||||
|
||||
def query(sql, **binds, &block)
|
||||
query(sql, binds) do |rs|
|
||||
yield rs
|
||||
end
|
||||
end
|
||||
|
||||
# Prepares an sql statement. Returns a `Statement`.
|
||||
def prepare(sql)
|
||||
Statement.new(self, sql)
|
||||
|
|
|
@ -61,22 +61,40 @@ class SQLite3::Statement
|
|||
# Executes this statement with the given binds and returns a `ResultSet`.
|
||||
def execute(binds : Enumerable)
|
||||
reset
|
||||
# TODO use offset after Crystal 0.6.2
|
||||
binds.each_with_index do |bind_value, index|
|
||||
self[index + 1] = bind_value
|
||||
binds.each_with_index(1) do |bind_value, index|
|
||||
self[index] = bind_value
|
||||
end
|
||||
ResultSet.new self
|
||||
end
|
||||
|
||||
# Executes this statement with the given binds and returns a `ResultSet`.
|
||||
def execute(binds : NamedTuple)
|
||||
reset
|
||||
binds.each do |name, bind_value|
|
||||
self[name] = bind_value
|
||||
end
|
||||
ResultSet.new self
|
||||
end
|
||||
|
||||
# Executes this statement with the given binds and yields a `ResultSet` that
|
||||
# will be closed at the end of the block.
|
||||
def execute(binds : Enumerable | Slice(UInt8), &block)
|
||||
def execute(binds : Enumerable | NamedTuple | Slice(UInt8), &block)
|
||||
result_set = execute(binds)
|
||||
yield result_set
|
||||
ensure
|
||||
close
|
||||
end
|
||||
|
||||
def execute(**binds)
|
||||
execute(binds)
|
||||
end
|
||||
|
||||
def execute(**binds, &block)
|
||||
execute(binds) do |rs|
|
||||
yield rs
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the value of the given column by index (1-based).
|
||||
def [](index : Int)
|
||||
case type = column_type(index)
|
||||
|
@ -156,6 +174,10 @@ class SQLite3::Statement
|
|||
end
|
||||
end
|
||||
|
||||
def []=(index : Int, tuple : Tuple(String | Symbol, U))
|
||||
self[tuple[0]] = tuple[1]
|
||||
end
|
||||
|
||||
# Returns the column names of this statement.
|
||||
def columns
|
||||
Array.new(column_count) { |i| column_name(i) }
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
# Each of the possible types of an SQLite3 column.
|
||||
enum SQLite3::Type
|
||||
INTEGER = 1
|
||||
FLOAT = 2
|
||||
BLOB = 4
|
||||
NULL = 5
|
||||
TEXT = 3
|
||||
FLOAT = 2
|
||||
BLOB = 4
|
||||
NULL = 5
|
||||
TEXT = 3
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue