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
|
@ -1,5 +1,5 @@
|
||||||
name: sqlite3
|
name: sqlite3
|
||||||
version: 0.3.0
|
version: 0.4.0
|
||||||
|
|
||||||
authors:
|
authors:
|
||||||
- Ary Borenszweig <aborenszweig@manas.com.ar>
|
- Ary Borenszweig <aborenszweig@manas.com.ar>
|
||||||
|
|
|
@ -72,23 +72,31 @@ describe Database do
|
||||||
it "executes and selects blob" do
|
it "executes and selects blob" do
|
||||||
rows = with_db(&.execute(%(select X'53514C697465')))
|
rows = with_db(&.execute(%(select X'53514C697465')))
|
||||||
row = rows[0]
|
row = rows[0]
|
||||||
cell = row[0] as Slice(UInt8)
|
cell = row[0].as(Slice(UInt8))
|
||||||
cell.to_a.should eq([0x53, 0x51, 0x4C, 0x69, 0x74, 0x65])
|
cell.to_a.should eq([0x53, 0x51, 0x4C, 0x69, 0x74, 0x65])
|
||||||
end
|
end
|
||||||
|
|
||||||
it "executes with named bind using symbol" do
|
it "executes with named bind using symbol" do
|
||||||
with_db(&.execute(%(select :value), {value: "hello"})).should eq([["hello"]])
|
with_db(&.execute(%(select :value), {:value => "hello"})).should eq([["hello"]])
|
||||||
end
|
end
|
||||||
|
|
||||||
it "executes with named bind using string" do
|
it "executes with named bind using string" do
|
||||||
with_db(&.execute(%(select :value), {"value": "hello"})).should eq([["hello"]])
|
with_db(&.execute(%(select :value), {"value" => "hello"})).should eq([["hello"]])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "executes with named bind using named argument" do
|
||||||
|
with_db(&.execute(%(select :value), {value: "hello"})).should eq([["hello"]])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "executes with named bind using named arguments" do
|
||||||
|
with_db(&.execute(%(select :value), value: "hello")).should eq([["hello"]])
|
||||||
end
|
end
|
||||||
|
|
||||||
it "executes with bind blob" do
|
it "executes with bind blob" do
|
||||||
ary = UInt8[0x53, 0x51, 0x4C, 0x69, 0x74, 0x65]
|
ary = UInt8[0x53, 0x51, 0x4C, 0x69, 0x74, 0x65]
|
||||||
rows = with_db(&.execute(%(select cast(? as BLOB)), Slice.new(ary.to_unsafe, ary.size)))
|
rows = with_db(&.execute(%(select cast(? as BLOB)), Slice.new(ary.to_unsafe, ary.size)))
|
||||||
row = rows[0]
|
row = rows[0]
|
||||||
cell = row[0] as Slice(UInt8)
|
cell = row[0].as(Slice(UInt8))
|
||||||
cell.to_a.should eq(ary)
|
cell.to_a.should eq(ary)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -113,6 +121,20 @@ describe Database do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "uses named arguments in statement execute" do
|
||||||
|
Database.new(":memory:") do |db|
|
||||||
|
db.execute "create table person (name string, age integer)"
|
||||||
|
db.execute %(insert into person values ("foo", 10))
|
||||||
|
db.execute %(insert into person values ("bar", 2))
|
||||||
|
stmt = db.prepare("select * from person where age > :age")
|
||||||
|
stmt.execute age: 5
|
||||||
|
stmt.step
|
||||||
|
stmt["age"].should eq(10)
|
||||||
|
stmt.types.should eq([Type::TEXT, Type::INTEGER])
|
||||||
|
stmt.close
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "gets column by name" do
|
it "gets column by name" do
|
||||||
Database.new(":memory:") do |db|
|
Database.new(":memory:") do |db|
|
||||||
db.execute "create table person (name string, age integer)"
|
db.execute "create table person (name string, age integer)"
|
||||||
|
@ -126,6 +148,20 @@ describe Database do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "uses named argument in query" do
|
||||||
|
Database.new(":memory:") do |db|
|
||||||
|
db.execute "create table person (name string, age integer)"
|
||||||
|
db.execute %(insert into person values ("foo", 10))
|
||||||
|
db.execute %(insert into person values ("bar", 2))
|
||||||
|
db.query("select * from person where age > :age", age: 5) do |result_set|
|
||||||
|
result_set.next.should be_true
|
||||||
|
result_set["name"].should eq("foo")
|
||||||
|
result_set["age"].should eq(10)
|
||||||
|
expect_raises { result_set["lala"] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
it "gets last insert row id" do
|
it "gets last insert row id" do
|
||||||
Database.new(":memory:") do |db|
|
Database.new(":memory:") do |db|
|
||||||
db.execute "create table person (name string, age integer)"
|
db.execute "create table person (name string, age integer)"
|
||||||
|
|
|
@ -84,6 +84,10 @@ class SQLite3::Database
|
||||||
execute(sql, binds)
|
execute(sql, binds)
|
||||||
end
|
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,
|
# 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.
|
# and are bound to the placeholders in the query.
|
||||||
#
|
#
|
||||||
|
@ -97,6 +101,12 @@ class SQLite3::Database
|
||||||
end
|
end
|
||||||
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,
|
# 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.
|
# 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.
|
# with the key being used as the name of the placeholder to bind the value to.
|
||||||
#
|
#
|
||||||
# Returns an `Array(Array(Value))`.
|
# Returns an `Array(Array(Value))`.
|
||||||
def execute(sql, binds : Enumerable)
|
def execute(sql, binds : Enumerable | NamedTuple)
|
||||||
rows = [] of Array(Value)
|
rows = [] of Array(Value)
|
||||||
execute(sql, binds) do |row|
|
execute(sql, binds) do |row|
|
||||||
rows << 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.
|
# with the key being used as the name of the placeholder to bind the value to.
|
||||||
#
|
#
|
||||||
# Yields one `Array(Value)` for each result.
|
# 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|
|
query(sql, binds) do |result_set|
|
||||||
while result_set.next
|
while result_set.next
|
||||||
yield result_set.to_a
|
yield result_set.to_a
|
||||||
|
@ -149,7 +159,7 @@ class SQLite3::Database
|
||||||
end
|
end
|
||||||
|
|
||||||
# A convenience method that returns the first value of the first row of a query result.
|
# 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|
|
query(sql, binds) do |result_set|
|
||||||
if result_set.next
|
if result_set.next
|
||||||
return result_set[0]
|
return result_set[0]
|
||||||
|
@ -172,17 +182,27 @@ class SQLite3::Database
|
||||||
end
|
end
|
||||||
|
|
||||||
# Executes a query and gives back a `ResultSet`.
|
# Executes a query and gives back a `ResultSet`.
|
||||||
def query(sql, binds : Enumerable)
|
def query(sql, binds : Enumerable | NamedTuple)
|
||||||
prepare(sql).execute(binds)
|
prepare(sql).execute(binds)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Executes a query and yields a `ResultSet` that will be closed at the end of the given block.
|
# 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|
|
prepare(sql).execute(binds) do |result_set|
|
||||||
yield result_set
|
yield result_set
|
||||||
end
|
end
|
||||||
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`.
|
# Prepares an sql statement. Returns a `Statement`.
|
||||||
def prepare(sql)
|
def prepare(sql)
|
||||||
Statement.new(self, sql)
|
Statement.new(self, sql)
|
||||||
|
|
|
@ -61,22 +61,40 @@ class SQLite3::Statement
|
||||||
# Executes this statement with the given binds and returns a `ResultSet`.
|
# Executes this statement with the given binds and returns a `ResultSet`.
|
||||||
def execute(binds : Enumerable)
|
def execute(binds : Enumerable)
|
||||||
reset
|
reset
|
||||||
# TODO use offset after Crystal 0.6.2
|
binds.each_with_index(1) do |bind_value, index|
|
||||||
binds.each_with_index do |bind_value, index|
|
self[index] = bind_value
|
||||||
self[index + 1] = 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
|
end
|
||||||
ResultSet.new self
|
ResultSet.new self
|
||||||
end
|
end
|
||||||
|
|
||||||
# Executes this statement with the given binds and yields a `ResultSet` that
|
# Executes this statement with the given binds and yields a `ResultSet` that
|
||||||
# will be closed at the end of the block.
|
# 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)
|
result_set = execute(binds)
|
||||||
yield result_set
|
yield result_set
|
||||||
ensure
|
ensure
|
||||||
close
|
close
|
||||||
end
|
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).
|
# Returns the value of the given column by index (1-based).
|
||||||
def [](index : Int)
|
def [](index : Int)
|
||||||
case type = column_type(index)
|
case type = column_type(index)
|
||||||
|
@ -156,6 +174,10 @@ class SQLite3::Statement
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def []=(index : Int, tuple : Tuple(String | Symbol, U))
|
||||||
|
self[tuple[0]] = tuple[1]
|
||||||
|
end
|
||||||
|
|
||||||
# Returns the column names of this statement.
|
# Returns the column names of this statement.
|
||||||
def columns
|
def columns
|
||||||
Array.new(column_count) { |i| column_name(i) }
|
Array.new(column_count) { |i| column_name(i) }
|
||||||
|
|
Loading…
Reference in a new issue