Class: Sruby::Database
- Inherits:
-
Object
- Object
- Sruby::Database
- Defined in:
- lib/sruby/index.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
Instance Method Summary collapse
- #[](table_name) ⇒ Object
-
#all(table_name) ⇒ Array
Get all the values from a table.
-
#create(table_name) ⇒ Table
Creates a new Table in database db = Sruby::Database.new db.create_table(“users”).
-
#delete(table_name, name, path = nil) ⇒ Array
Deletes a row from a table db = Sruby::Database.new db.create_table(“users”) db.insert(“users”, “person” => “John”, “age” => “24”) db.delete(“users”, “person”).
-
#get(table_name, name, path = nil) ⇒ String
Get a value from a table db = Sruby::Database.new db.create_table(“users”) db.insert(“users”, “person” => “John”, “age” => “24”) db.get(“users”, “person”).
-
#initialize(options = Hash[:name => "sruby.db"]) ⇒ Database
constructor
Creates a new database object.
-
#insert(table_name, *values) ⇒ Array
Insert values into a table db = Sruby::Database.new db.create_table(“users”) db.insert(“users”, “person” => “John”, “age” => “24”) db.insert(“users”, Hash[“person” => “John”, “age” => “24”]) db.insert(“users”, “person”, “John”).
- #method_missing(symbol, *args) ⇒ Object
-
#update(table_name, *values) ⇒ Array
Updates values in a table db = Sruby::Database.new db.create_table(“users”) db.insert(“users”, “person” => “John”, “age” => “24”) db.update(“users”, “person” => “John”, “age” => “25”) db.update(“users”, Hash[“person” => “John”, “age” => “25”]).
Constructor Details
#initialize(options = Hash[:name => "sruby.db"]) ⇒ Database
Creates a new database object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sruby/index.rb', line 13 def initialize( = Hash[:name => "sruby.db"]) case when String @db = SQLite3::Database.new() when Hash @db = SQLite3::Database.new([:name]) when SQLite3::Database @db = else raise SrubyError, "Invalid argument: #{.inspect}" end @db.results_as_hash = true if .is_a?(Hash) && [:results_as_hash] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
50 51 52 53 |
# File 'lib/sruby/index.rb', line 50 def method_missing(symbol, *args) return self[symbol] if self[symbol] super end |
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
9 10 11 |
# File 'lib/sruby/index.rb', line 9 def db @db end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
9 10 11 |
# File 'lib/sruby/index.rb', line 9 def tables @tables end |
Instance Method Details
#[](table_name) ⇒ Object
45 46 47 48 |
# File 'lib/sruby/index.rb', line 45 def [](table_name) @tables ||= Hash.new @tables[table_name.to_s] end |
#all(table_name) ⇒ Array
Get all the values from a table
161 162 163 |
# File 'lib/sruby/index.rb', line 161 def all(table_name) @db.execute("SELECT * FROM #{table_name}") end |
#create(table_name) ⇒ Table
Creates a new Table in database db = Sruby::Database.new db.create_table(“users”)
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sruby/index.rb', line 33 def create(table_name) @db.execute <<~SQL CREATE TABLE IF NOT EXISTS #{table_name} ( name TEXT PRIMARY KEY, value TEXT ); SQL @tables ||= Hash.new @tables[table_name] = Table.new(@db, table_name) end |
#delete(table_name, name, path = nil) ⇒ Array
Deletes a row from a table db = Sruby::Database.new db.create_table(“users”) db.insert(“users”, “person” => “John”, “age” => “24”) db.delete(“users”, “person”)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sruby/index.rb', line 142 def delete(table_name, name, path = nil) if path.nil? @db.execute("DELETE FROM #{table_name} WHERE name = ?", name) else path_name = case path when Array "#{path.map(&:to_s).join(".")}.#{name}" when String "#{path}.#{name}" else name end @db.execute("DELETE FROM #{table_name} WHERE name = ?", path_name) end end |
#get(table_name, name, path = nil) ⇒ String
Get a value from a table db = Sruby::Database.new db.create_table(“users”) db.insert(“users”, “person” => “John”, “age” => “24”) db.get(“users”, “person”)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/sruby/index.rb', line 114 def get(table_name, name, path = nil) if path.nil? @db.execute("SELECT value FROM #{table_name} WHERE name = ?", name) else path_name = case path when Array "#{path.map(&:to_s).join(".")}.#{name}" when String "#{path}.#{name}" else name end data = @db.execute("SELECT value FROM #{table_name} WHERE name = ?", path_name) raise SrubyError, "No value found for #{path_name}" if data.empty? data end end |
#insert(table_name, *values) ⇒ Array
Insert values into a table db = Sruby::Database.new db.create_table(“users”) db.insert(“users”, “person” => “John”, “age” => “24”) db.insert(“users”, Hash[“person” => “John”, “age” => “24”]) db.insert(“users”, “person”, “John”)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/sruby/index.rb', line 65 def insert(table_name, *values) case values[0] when Hash values[0].stringify_keys! values_as_paths = values[0].paths values_as_paths.each do |path, value| p path, value @db.execute("INSERT INTO #{table_name} (name, value) VALUES (?, ?)", path.join("."), value) end else @db.execute("INSERT INTO #{table_name} (name, value) VALUES (?, ?)", values[0].to_s, values[1]) end rescue SQLite3::ConstraintException raise SrubyError, "Duplicated key" end |
#update(table_name, *values) ⇒ Array
Updates values in a table db = Sruby::Database.new db.create_table(“users”) db.insert(“users”, “person” => “John”, “age” => “24”) db.update(“users”, “person” => “John”, “age” => “25”) db.update(“users”, Hash[“person” => “John”, “age” => “25”])
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sruby/index.rb', line 91 def update(table_name, *values) case values[0] when Hash values[0].stringify_keys! values_as_paths = values[0].paths values_as_paths.each do |path, value| @db.execute("REPLACE INTO #{table_name} (name, value) VALUES (?, ?)", path.join("."), value) end else @db.execute("REPLACE INTO #{table_name} (name, value) VALUES (?, ?)", values[0].to_s, values[1]) end end |