Class: AocCli::Database::Query
- Inherits:
-
Object
- Object
- AocCli::Database::Query
- Defined in:
- lib/aoc_cli/database.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
-
#initialize(path:) ⇒ Query
constructor
A new instance of Query.
- #insert(t:, val:) ⇒ Object
- #select(t:, cols: "*", where:) ⇒ Object
- #table(t:, cols:) ⇒ Object
- #update(t:, val:, where:) ⇒ Object
Constructor Details
#initialize(path:) ⇒ Query
Returns a new instance of Query.
11 12 13 |
# File 'lib/aoc_cli/database.rb', line 11 def initialize(path:) @db = SQLite3::Database.open(path) end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
10 11 12 |
# File 'lib/aoc_cli/database.rb', line 10 def db @db end |
Instance Method Details
#insert(t:, val:) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/aoc_cli/database.rb', line 25 def insert(t:, val:) db.execute( "INSERT INTO #{t} "\ "VALUES(#{val.join(", ")})") self end |
#select(t:, cols: "*", where:) ⇒ Object
14 15 16 17 18 |
# File 'lib/aoc_cli/database.rb', line 14 def select(t:, cols:"*", where:) db.execute( "SELECT #{cols} FROM #{t} "\ "WHERE #{where.map{|k, v| "#{k} = #{v}"}.join(" AND ")}") end |
#table(t:, cols:) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/aoc_cli/database.rb', line 19 def table(t:, cols:) db.execute( "CREATE TABLE IF NOT EXISTS "\ "#{t}(#{cols.map{|c, t| "#{c} #{t}"}.join(", ")})") self end |
#update(t:, val:, where:) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/aoc_cli/database.rb', line 31 def update(t:, val:, where:) db.execute( "UPDATE #{t} "\ "SET #{val.map{|c, v| "#{c} = #{v}"}.join(", ")} "\ "WHERE #{where.map{|c, v| "#{c} = #{v}"}.join(" AND ")}") self end |