Class: Cliptic::Database::SQL

Inherits:
Object
  • Object
show all
Defined in:
lib/cliptic/database.rb

Direct Known Subclasses

Recents, Scores, State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table:) ⇒ SQL

Returns a new instance of SQL.



7
8
9
10
11
12
13
# File 'lib/cliptic/database.rb', line 7

def initialize(table:)
  make_db_dir
  @table = table
  @db    = SQLite3::Database.open(File_Path)
  db.results_as_hash = true
  self
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



6
7
8
# File 'lib/cliptic/database.rb', line 6

def db
  @db
end

#tableObject (readonly)

Returns the value of attribute table.



6
7
8
# File 'lib/cliptic/database.rb', line 6

def table
  @table
end

Instance Method Details

#delete(where: nil) ⇒ Object



31
32
33
# File 'lib/cliptic/database.rb', line 31

def delete(where:nil)
  db.execute(sql_delete(where:where), where&.values)
end

#dropObject



34
35
36
# File 'lib/cliptic/database.rb', line 34

def drop
  db.execute("DROP TABLE #{table}")
end

#insert(values:) ⇒ Object



24
25
26
27
# File 'lib/cliptic/database.rb', line 24

def insert(values:)
  db.execute(sql_insert(values:values), 
             values.values)
end

#make_tableObject



14
15
16
# File 'lib/cliptic/database.rb', line 14

def make_table
  db.execute(sql_make)
end

#select(cols: "*", where: nil, order: false, limit: false) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/cliptic/database.rb', line 17

def select(cols:"*", where:nil, order:false, limit:false)
  db.execute(
    sql_select(cols:cols, where:where, 
               order:order, limit:limit),
    where&.values&.map(&:to_s)
  )
end

#update(values:, where:) ⇒ Object



28
29
30
# File 'lib/cliptic/database.rb', line 28

def update(values:, where:)
  db.execute(sql_update(values:values, where:where), [values.values], [where.values])
end