Class: Sruby::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/sruby/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = Hash[:name => "sruby.db"]) ⇒ Database

Creates a new database object

Parameters:

  • options (String, Hash, Sqlite3::Database) (defaults to: Hash[:name => "sruby.db"])

    The database file to open, or a hash of options



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sruby/index.rb', line 13

def initialize(options = Hash[:name => "sruby.db"])
  case options
  when String
    @db = SQLite3::Database.new(options)
  when Hash
    @db = SQLite3::Database.new(options[:name])
  when SQLite3::Database
    @db = options
  else
    raise SrubyError, "Invalid argument: #{options.inspect}"
  end
  @db.results_as_hash = true if options.is_a?(Hash) && options[:results_as_hash]
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



9
10
11
# File 'lib/sruby/index.rb', line 9

def db
  @db
end

Instance Method Details

#all(table_name) ⇒ Array

Get all the values from a table

Parameters:

  • table_name (String)

    The name of the table

Returns:

  • (Array)

    The values



153
154
155
# File 'lib/sruby/index.rb', line 153

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”)

Parameters:

  • table_name (String)

    The name of the table

Returns:

  • (Table)

    The database object



33
34
35
36
37
38
39
40
41
42
43
44
# 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

  Database.attr_accessor table_name
  instance_variable_set("@#{table_name}", Table.new(@db, table_name))
  instance_variable_get("@#{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”)

Parameters:

  • table_name (String)

    The name of the table

  • name (String)

    The name of the row to delete

  • path (Nil, String) (defaults to: nil)

    The path to delete

Returns:

  • (Array)

    The database object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/sruby/index.rb', line 134

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”)

Parameters:

  • table_name (String)

    The name of the table

  • name (String)

    The value to get

  • path (Nil, String) (defaults to: nil)

    The path to get

Returns:

  • (String)

    The value



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sruby/index.rb', line 106

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”)

Parameters:

  • table_name (String)

    The name of the table

  • values (String, Hash)

    The values to insert

Returns:

  • (Array)

    The database object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sruby/index.rb', line 56

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”])

Parameters:

  • table_name (String)

    The name of the table

  • values (String, Hash)

    The values to update

Returns:

  • (Array)

    The database object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sruby/index.rb', line 82

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|
      p 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