-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.rb
121 lines (103 loc) · 3.41 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require './rittle'
module Rittle
DBH = Mysql.real_connect("localhost", "root", "", "test")
class Base
def self.get_object(table_name)
m = model_for_table(table_name)
res = DBH.list_fields("#{table_name}")
set_fields(m, res)
end
def self.model_for_table(table_name)
klass = Class.new Base
model_name = get_model_name(table_name)
model_name = Object.const_set(model_name.capitalize, klass)
end
def self.set_fields(m, res)
column_names = res.fetch_fields.collect{|r| r.name}
m.instance_eval do
column_names.each do |cname|
attr_accessor cname
end
end
return m.new
end
def insert(options = {})
values_and_columns = get_columns_and_values(options)
DBH.query("INSERT INTO #{get_table_name} (#{values_and_columns.keys.join(',')})
VALUES
(#{values_and_columns.values.join(',')})")
end
def update_column(to_update)
update_string = []
to_update.each do |key, value|
instance_variable_set("@#{key}",value)
update_string << "#{key} = '#{value}'"
end
DBH.query("UPDATE #{get_table_name} SET #{update_string.join(', ')} where id=#{id}")
end
def self.update_column(condition = {}, options = {})
update_string = []
options.each do |key, value|
instance_variable_set("@#{key}",value)
update_string << "#{key} = '#{value}'"
end
DBH.query("UPDATE #{self.new.get_table_name} SET #{update_string.join(', ')} #{self.build_conditions(condition)}")
end
def get_columns_and_values(options)
calumn_values = {}
column_values = instance_variables.inject({}){|column_values, iv| column_values.merge!(iv.to_s.gsub("@", "").to_sym => "'#{instance_variable_get(iv)}'")}
opt = {}
options.each do |key, value|
opt[key] = "'#{value}'"
end
column_values.merge!(opt)
end
def self.get_row(options = {})
get_rows(options).first
end
def self.get_rows(options = {})
res = DBH.query("SELECT * FROM #{new.get_table_name} #{self.build_conditions(options)}")
fill_value(res)
end
def get_table_name
word = self.class.to_s.dup
word.gsub!(/::/, '/')
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
word.tr!("-", "_")
word.downcase!
word
end
def remove
DBH.query("Delete from #{get_table_name} where id=#{id}")
end
def self.remove(options = {})
DBH.query("Delete from #{new.get_table_name} #{build_conditions(options)}")
end
def self.fill_value(res)
arr = []
res.each_hash do |row|
a = self.new
row.each do |key, value|
a.instance_variable_set("@#{key}",value)
end
arr << a
end
arr
end
def self.get_model_name(table_name)
DBH.list_tables(table_name).size ? table_name.split("_").map{|x| x.capitalize}.join : "#{table_name.split("_").map{|x| x.capitalize}.join}s"
end
def self.build_conditions(options)
where = ""
conditions = []
unless options.empty?
where = "Where"
options.each do |key, value|
conditions << "#{key}='#{value}'"
end
end
return "#{where} #{conditions.join(' and ')}"
end
end
end