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

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

#dbObject (readonly)

Returns the value of attribute db.



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

def db
  @db
end

#tablesObject (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

Parameters:

  • table_name (String)

    The name of the table

Returns:

  • (Array)

    The values



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

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

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



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

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



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

Parameters:

  • table_name (String)

    The name of the table

  • values (String, Hash)

    The values to insert

Returns:

  • (Array)

    The database object



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

Parameters:

  • table_name (String)

    The name of the table

  • values (String, Hash)

    The values to update

Returns:

  • (Array)

    The database object



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