Class: MysqlBuilder
Class Method Summary
collapse
Instance Method Summary
collapse
#insert, #select, #update
Constructor Details
Returns a new instance of MysqlBuilder.
25
26
|
# File 'lib/mdlsql/sockets/mysql.rb', line 25
def initialize
end
|
Class Method Details
.insert(values = {}) ⇒ Object
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/mdlsql/sockets/mysql.rb', line 88
def insert(values={})
cols = values[:cols]
tables = values[:tables]
where = values[:where]
vals = values[:values]
query = String.new
query = 'INSERT INTO'
if tables
tables.each do |tab|
query << ' ' << tab.name.to_s << ','
end
query.chop!
else
raise "No tables at insert query."
end
if cols && cols.count > 0
query << ' ('
cols.each do |key,col|
query << "#{col},"
end
query.chop! << ')'
end
query << ' VALUES'
if vals
vals.each do |row|
query << ' ('
row.each do |val|
query << "'#{val}'" << ','
end
query.chop!
query << '),'
end
query.chop!
else
raise 'No values to insert.'
end
return query
end
|
.select(values = {}) ⇒ Object
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
|
# File 'lib/mdlsql/sockets/mysql.rb', line 29
def select(values={})
cols = values[:cols]
tables = values[:tables]
where = values[:where]
join = values[:join]
query = String.new
query = "SELECT"
if cols
cols.each do |key,value|
query << " #{key} AS #{value}" << ','
end
query.chop!
else
query << " *"
end
if tables
query << "\nFROM"
tables.each do |tab|
query << ' ' << tab.to_mysql << ','
end
query.chop!
else
raise "No table at select query."
end
if join && join.length > 0
join.each do |j|
query << j.to_mysql
end
end
if where && where.length > 0
query << "\nWHERE"
first = true
where.each do |wh|
query << " #{wh.concat}" unless first
query << " #{wh.cond1} #{wh.op} #{wh.cond2}"
first = false
end
end
return query
end
|
.update(values = {}) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/mdlsql/sockets/mysql.rb', line 138
def update(values={})
tables = values[:tables]
set = values[:values]
where = values[:where]
query = String.new()
if tables
query << "UPDATE"
tables.each do |tab|
query << ' ' << tab.to_mysql << ','
end
query.chop!
else
raise "No table at update query."
end
query << "\nSET"
if set && set.count > 0
set.each do |key, value|
query << " #{key} = '#{value}',"
end
query.chop!
else
raise 'Nothing to be set.'
end
if where && where.length > 0
query << "\nWHERE"
first = true
where.each do |wh|
query << " #{wh.concat}" unless first
query << " #{wh.cond1} #{wh.op} #{wh.cond2}"
first = false
end
end
return query
end
|