Class: Lolitado::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/lolitado/db.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ Database

Returns a new instance of Database.



6
7
8
# File 'lib/lolitado/db.rb', line 6

def initialize db
  @db = db
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



4
5
6
# File 'lib/lolitado/db.rb', line 4

def db
  @db
end

Instance Method Details

#execute(sql) ⇒ Object

Execute by sql, the sql is normally update or insert.

Parameters:

  • sql (String)

    the sql you wanna execute



91
92
93
# File 'lib/lolitado/db.rb', line 91

def execute sql
  db[sql]
end

#insert_by_data(data, table) ⇒ Object

Insert data by data.

Parameters:

  • data (String)

    the data wanna insert which is array or hash

  • table (String)

    the table wanna insert



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lolitado/db.rb', line 101

def insert_by_data data, table
  sql = "insert into #{table} "
  case data
    when Array
      data.each do |d|
        insert_by_data(d, table)
      end
    when Hash
      columns = data.keys.to_s.gsub('[','(').gsub(']',')').gsub('"','')
      values = data.values.to_s.gsub('[','(').gsub(']',')').gsub('nil','NULL')
      sql = sql + columns + " values " + values
      query(sql)
  end
end

#multiple_query(sql) ⇒ Object

Return the data by sql.

Parameters:

  • sql (String)

    the sql you wanna query, the multiple sql should be split by ;



25
26
27
28
29
30
# File 'lib/lolitado/db.rb', line 25

def multiple_query sql
  splited_sql = sql.split(';')
  splited_sql.each do |each_sql|
    query(each_sql)
  end
end

#query(sql) ⇒ Object

Return the data by sql.

Parameters:

  • sql (String)

    the sql you wanna query



15
16
17
18
# File 'lib/lolitado/db.rb', line 15

def query sql
  result = db[sql].all
  return result
end

#query_duration(sql, type = true, waiting_time = 10) ⇒ Object

Return the data and execute duration by sql.

Parameters:

  • sql (String)

    the sql you wanna query

  • type (Boolean) (defaults to: true)

    the default value is true that the expected result is not empty, otherwise, the expected result is empty

  • waiting_time (Number) (defaults to: 10)

    the default value is 10 that maxium execute duration is 10 second



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lolitado/db.rb', line 39

def query_duration sql, type = true, waiting_time = 10
  start = Time.now
  if type
    result = query_wait(sql, waiting_time)
  else
    result = query_empty(sql, waiting_time)
  end
  finish = Time.now
  msecs = (finish - start) * 1000.0
  hash = {'result' => result, 'duration' => msecs}
  return hash
end

#query_empty(sql, waiting_time = 10) ⇒ Object

Expect to return empty for the query. If the result is not empty then it will be called again until waiting_time reach.

Parameters:

  • sql (String)

    the sql you wanna query

  • waiting_time (Number) (defaults to: 10)

    the default value is 10 that maxium execute duration is 10 second



75
76
77
78
79
80
81
82
83
84
# File 'lib/lolitado/db.rb', line 75

def query_empty sql, waiting_time = 10
  result = db[sql].all
  if !result.empty?
    if waiting_time != 0
      sleep 1
      result = query(sql, waiting_time - 1)
    end
  end
  return result
end

#query_wait(sql, waiting_time = 10) ⇒ Object

Expect to return data for the query. If the result is empty then it will be called again until waiting_time reach.

Parameters:

  • sql (String)

    the sql you wanna query

  • waiting_time (Number) (defaults to: 10)

    the default value is 10 that maxium execute duration is 10 second



58
59
60
61
62
63
64
65
66
67
# File 'lib/lolitado/db.rb', line 58

def query_wait sql, waiting_time = 10
  result = db[sql].all
  if result.empty?
    if waiting_time != 0
      sleep 1
      result = query_wait(sql, waiting_time - 1)
    end
  end
  return result
end

#update(data, table, condition = {}) ⇒ Object

Update data by data.

Parameters:

  • data (Hash)

    the data wanna update which is hash

  • table (String)

    the table for the update

  • condition (Hash) (defaults to: {})

    the condition for the update



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lolitado/db.rb', line 123

def update data, table, condition = {}
  sql = "update #{table} set"
  data.each do |k,v|
    v = v.to_json if v.is_a?(Hash)
    if !!v == v
      sql = "#{sql} #{k}=#{v},"
    else
      sql = "#{sql} #{k}='#{v}',"
    end
  end
  sql = sql[0..-2] + " where"
  condition.each do |k,v|
    sql = "#{sql} #{k} = '#{v}' and"
  end
  query(sql[0..-4])
end