mdlsql 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54db588948e8891012d0bd13b5b730b06b18dcbb
4
- data.tar.gz: a48e8ec0749dd72f240ab76da53a84b0b6e84d9c
3
+ metadata.gz: d81ad485397fd79dab4de4988239053c78fbe1e7
4
+ data.tar.gz: a57bf5bd17b329fdab82feef4b375f936ae141c8
5
5
  SHA512:
6
- metadata.gz: 41a30944eb98f5f5223c4c861f08067e3ec27c530623017271a84de3550ab2c99a63440a7e8275482bae0b9587a18b3358e9f9a34264d6b39ba2f3e3a6cf5a9a
7
- data.tar.gz: 602dc6357b9b4e6f277ca0a073bb753eb36ad092715741a29713eb762a24d2e04267217240ee012df18e0b45067b705067ad0eb6182e4b9e200e536a4e8fd10b
6
+ metadata.gz: de378dd70963af23811c945b7f86059f71cd330e5d921670c1539250039bc36d4ef8800cd26074ef17f60dac813b4662369acf493e9b795d740d0aea567cb844
7
+ data.tar.gz: 9dc63e28e7b8003f2b9e667e2c4d63163a9919a3e7e2abe62db6672432aee9bf51eec7c121b91188b069c10f8ab0117bc6afe39ff4fef962e9a51ce902a83400
@@ -38,20 +38,20 @@ class MysqlBuilder < QueryBuilder
38
38
  # Columns (with alias)
39
39
  if cols
40
40
  cols.each do |key,value|
41
- query << " #{value} AS #{key}"
41
+ query << " #{value} AS #{key}" << ','
42
42
  end
43
+ query.chop!
43
44
  else
44
45
  query << " *"
45
46
  end
46
47
 
47
48
  # From (with possible alias)
48
49
  if tables
49
- query << ' FROM'
50
+ query << "\nFROM"
50
51
  tables.each do |tab|
51
- query << ' ' << tab.to_mysql
52
+ query << ' ' << tab.to_mysql << ','
52
53
  # query << " #{tab.name}"
53
54
  # query << " AS #{tab.as}" if tab.as
54
- query << ","
55
55
  end
56
56
  query.chop!
57
57
 
@@ -87,19 +87,29 @@ class MysqlBuilder < QueryBuilder
87
87
 
88
88
  def insert(values={})
89
89
  # INSERT INTO table (column1, column2) VALUES (v1c1, v1c2), (v2c1, v2c2)
90
+ cols = values[:cols]
91
+ tables = values[:tables]
92
+ where = values[:where]
93
+ vals = values[:values]
90
94
 
91
95
  query = String.new
92
96
  query = 'INSERT INTO'
93
97
 
94
- if @from
95
- query << " #{@from}"
98
+ if tables
99
+ tables.each do |tab|
100
+ query << ' ' << tab.name.to_s << ','
101
+ # query << " #{tab.name}"
102
+ # query << " AS #{tab.as}" if tab.as
103
+ end
104
+ query.chop!
105
+
96
106
  else
97
- raise "No table at insert query."
107
+ raise "No tables at insert query."
98
108
  end
99
109
 
100
- if @cols && @cols.count > 0
110
+ if cols && cols.count > 0
101
111
  query << ' ('
102
- @cols.each do |key,col|
112
+ cols.each do |key,col|
103
113
  query << "#{col},"
104
114
  end
105
115
 
@@ -108,8 +118,8 @@ class MysqlBuilder < QueryBuilder
108
118
 
109
119
  query << ' VALUES'
110
120
 
111
- if @values
112
- @values.each do |row|
121
+ if vals
122
+ vals.each do |row|
113
123
  query << ' ('
114
124
  row.each do |val|
115
125
  query << "'#{val}'" << ','
@@ -128,19 +138,26 @@ class MysqlBuilder < QueryBuilder
128
138
  def update(values={})
129
139
  # UPDATE example SET age='22' WHERE age='21'
130
140
 
131
- table = values[:table]
141
+ tables = values[:tables]
132
142
  set = values[:values]
133
143
  where = values[:where]
134
144
 
135
145
  query = String.new()
136
146
 
137
- if table
138
- query << "UPDATE #{table} "
147
+ if tables
148
+ query << "UPDATE"
149
+ tables.each do |tab|
150
+ query << ' ' << tab.to_mysql << ','
151
+ # query << " #{tab.name}"
152
+ # query << " AS #{tab.as}" if tab.as
153
+ end
154
+ query.chop!
155
+
139
156
  else
140
157
  raise "No table at update query."
141
158
  end
142
159
 
143
- query << 'SET'
160
+ query << "\nSET"
144
161
 
145
162
  if set && set.count > 0
146
163
  set.each do |key, value|
@@ -151,13 +168,17 @@ class MysqlBuilder < QueryBuilder
151
168
  raise 'Nothing to be set.'
152
169
  end
153
170
 
154
- query << ' WHERE'
155
-
156
- if where && where.count > 0
157
- where.each do |con|
158
- query << ' ' << con << ','
171
+ if where && where.length > 0
172
+ query << "\nWHERE"
173
+ # where.each do |dec|
174
+ # query << " #{dec}"
175
+ # end
176
+ first = true
177
+ where.each do |wh|
178
+ query << " #{wh.concat}" unless first
179
+ query << " #{wh.cond1} #{wh.op} #{wh.cond2}"
180
+ first = false
159
181
  end
160
- query.chop!
161
182
  else
162
183
  raise 'No WHERE condition in update.'
163
184
  end
@@ -77,7 +77,14 @@ module MdlSql
77
77
 
78
78
  def update(table=nil)
79
79
  @method = :update
80
- from table if table
80
+
81
+ if table.is_a? Symbol
82
+ from table => table
83
+ elsif table.is_a? String
84
+ from table.to_sym => table.to_sym
85
+ elsif table.is_a? Hash
86
+ from table
87
+ end
81
88
  return self
82
89
  end
83
90
 
@@ -124,7 +131,7 @@ module MdlSql
124
131
  # table_alias = table_alias if table_alias.is_a? String
125
132
 
126
133
  @tables ||= Array.new
127
- tables.each do |table,table_alias|
134
+ tables.each do |table_alias,table|
128
135
  @tables.push Table.new table, table_alias
129
136
  end
130
137
 
@@ -149,7 +156,7 @@ module MdlSql
149
156
  #
150
157
  # @todo Add IN, BETWEEN and LIKE (can be done with actual where).
151
158
  def where(cond1, cond2, opts={})
152
- opts[:op] ||= '='
159
+ opts[:op] ||= :'='
153
160
  opts[:concat] ||= :AND
154
161
 
155
162
  # if cond1.is_a? Hash
@@ -177,9 +184,8 @@ module MdlSql
177
184
  :table => table,
178
185
  :cond1 => cond1,
179
186
  :cond2 => cond2,
180
- :op => opts[:op],
181
- :type => opts[:type]
182
187
  }
188
+ vars.merge! opts
183
189
  @join.push Join.new vars
184
190
 
185
191
  return self
@@ -245,15 +251,13 @@ module MdlSql
245
251
  :where => @where,
246
252
  :cols => @cols,
247
253
  :values => @values,
248
- :join => @join
249
- })
254
+ :join => @join}
255
+ )
250
256
 
251
- if @@debug
252
- return query
253
- else
254
- @result = client.query query
255
- return @result
256
- end
257
+ puts query if @@debug
258
+
259
+ @result = client.query query
260
+ return @result
257
261
 
258
262
  end
259
263
  end
@@ -1,3 +1,3 @@
1
1
  module MdlSql
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/mdlsql/where.rb CHANGED
@@ -28,6 +28,7 @@ module MdlSql
28
28
  @cond2 = opts[:cond2]
29
29
 
30
30
  opts[:op].to_sym if opts[:op].is_a? String
31
+ opts[:concat].to_sym if opts[:concat].is_a? String
31
32
  @op = opts[:op]
32
33
  @concat = opts[:concat]
33
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mdlsql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sikian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-08 00:00:00.000000000 Z
11
+ date: 2013-07-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2