mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
Allow query results to be read as named tuples directly (#56)
This commit is contained in:
parent
28b17b7dba
commit
d55a34e851
3 changed files with 82 additions and 0 deletions
|
@ -88,6 +88,21 @@ module DB
|
|||
end
|
||||
end
|
||||
|
||||
# Executes a *query* that expects a single row and returns it
|
||||
# as a named tuple of the given *types* (the keys of the named tuple
|
||||
# are not necessarily the column names).
|
||||
#
|
||||
# Raises `DB::Error` if there were no rows, or if there were more than one row.
|
||||
#
|
||||
# ```
|
||||
# db.query_one "select name, age from contacts where id = ?", 1, as: {name: String, age: Int32}
|
||||
# ```
|
||||
def query_one(query, *args, as types : NamedTuple)
|
||||
query_one(query, *args) do |rs|
|
||||
rs.read(**types)
|
||||
end
|
||||
end
|
||||
|
||||
# Executes a *query* that expects a single row
|
||||
# and returns the first column's value as the given *type*.
|
||||
#
|
||||
|
@ -141,6 +156,24 @@ module DB
|
|||
end
|
||||
end
|
||||
|
||||
# Executes a *query* that expects a single row and returns it
|
||||
# as a named tuple of the given *types* (the keys of the named tuple
|
||||
# are not necessarily the column names).
|
||||
#
|
||||
# Returns `nil` if there were no rows.
|
||||
#
|
||||
# Raises `DB::Error` if there were more than one row.
|
||||
#
|
||||
# ```
|
||||
# result = db.query_one? "select name, age from contacts where id = ?", 1, as: {age: String, name: Int32}
|
||||
# typeof(result) # => NamedTuple(age: String, name: Int32) | Nil
|
||||
# ```
|
||||
def query_one?(query, *args, as types : NamedTuple)
|
||||
query_one(query, *args) do |rs|
|
||||
rs.read(**types)
|
||||
end
|
||||
end
|
||||
|
||||
# Executes a *query* that expects a single row
|
||||
# and returns the first column's value as the given *type*.
|
||||
#
|
||||
|
@ -184,6 +217,19 @@ module DB
|
|||
end
|
||||
end
|
||||
|
||||
# Executes a *query* and returns an array where each row is
|
||||
# read as a named tuple of the given *types* (the keys of the named tuple
|
||||
# are not necessarily the column names).
|
||||
#
|
||||
# ```
|
||||
# contacts = db.query_all "select name, age from contacts", as: {name: String, age: Int32}
|
||||
# ```
|
||||
def query_all(query, *args, as types : NamedTuple)
|
||||
query_all(query, *args) do |rs|
|
||||
rs.read(**types)
|
||||
end
|
||||
end
|
||||
|
||||
# Executes a *query* and returns an array where the
|
||||
# value of each row is read as the given *type*.
|
||||
#
|
||||
|
|
|
@ -89,6 +89,11 @@ module DB
|
|||
internal_read(*types)
|
||||
end
|
||||
|
||||
# Reads the next columns and returns a named tuple of the values.
|
||||
def read(**types : Class)
|
||||
internal_read(**types)
|
||||
end
|
||||
|
||||
private def internal_read(*types : *T) forall T
|
||||
{% begin %}
|
||||
Tuple.new(
|
||||
|
@ -99,6 +104,16 @@ module DB
|
|||
{% end %}
|
||||
end
|
||||
|
||||
private def internal_read(**types : **T) forall T
|
||||
{% begin %}
|
||||
NamedTuple.new(
|
||||
{% for name, type in T %}
|
||||
{{ name }}: read({{type.instance}}),
|
||||
{% end %}
|
||||
)
|
||||
{% end %}
|
||||
end
|
||||
|
||||
# def read_blob
|
||||
# yield ... io ....
|
||||
# end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue