mdlsql 0.0.2 → 0.0.3

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: 7a3f1ee3724c8d5dcbd702d9d4f3a2cd61a2c54a
4
- data.tar.gz: 41929822db8d6b30ccbf4436de1744c36c669940
3
+ metadata.gz: 8cc48c0d204d23a1b157084b6c2020dbf6a1ba54
4
+ data.tar.gz: f0a78964c5ca8fe37e67168f5044a60e391e1c34
5
5
  SHA512:
6
- metadata.gz: 13d5056116222b9ece1fe97b9d7972a2061b541206fc4f246fb0c0860c01bb7bf017ebf828e6dd3d5e51432e873595a1fe5c756a835cd492d775f2ca15c73ebe
7
- data.tar.gz: bbb2aa3d807bb5aec7327af9d505bdd0723a187f961c664a472a3424cc199a3ae9f03ec0219f5d3034f8cb2bb11ef0c366d2e791099d9b1b65a4f1b4a0daef02
6
+ metadata.gz: 1e1e2b2da31b46fdf530691510f7e60f5abd3cfa7468dfdb31cae147ec4a35c0d547f4cb45f089ef47da671aaa051a7efd6cbb6ed6cb782a8b714f6d94ac2691
7
+ data.tar.gz: f10f5794bb5978bbd6e92e4be5c4e528a394252d0b477eae20d5d7eadaaecea660b6611cfdb97fca4b8f601e5eff38db438152b363af17581ff4de77217d565b
data/README.rdoc CHANGED
@@ -7,8 +7,9 @@ Modular Sql is a modular query builder that enables a high database compatibilit
7
7
  It actually does a similar job to ActiveRecord, but I believe it may allow a better control in some aspects as it lies somehow between raw queries and that uberuseful gem.
8
8
 
9
9
  <b>Actual situation:</b>
10
- * Simple mysql queries (select, insert, update)
11
- * No joins
10
+ * Simple mysql queries (select, insert, update).
11
+ * No <i>joins</i>.
12
+ * Just one <i>where</i> condition (if you need more than one, use #where(all_conditions_together).
12
13
 
13
14
  == Usage
14
15
 
@@ -33,14 +34,34 @@ When selecting columns use .cols()/.columns(), which can receive a Hash or an Ar
33
34
 
34
35
  <b>Update:</b>
35
36
  result = MdlSql::update(:users).set(:role => '1').where(@username = 'default'").execute
36
-
37
- Updating follows mysql building way. Remember that you can still use .table() if you feel more comfortable.
38
37
 
38
+ Updating follows mysql building way. Remember that you can still use .table() if you feel more comfortable.
39
39
 
40
-
40
+ == Fast documentation
41
+
42
+ ===#table(table_name, table_alias) (#from, #into):
43
+ Select table to use.
44
+
45
+ === #where(first_term, sencond_term, operator):
46
+ Where condition.
47
+ This can be used both passing each separate term (recommended) and introducing the whole comparison as the first_term (i.e. .where('id = 1')). The first way will provide a higher compatibility with other databases, but may not be as useful when needing more than a simple condition.
48
+
49
+ TODO: expand where to include AND, OR, etc. in order to be able to concatenate many conditions.
50
+
51
+ === #columns(*values)
52
+
53
+
54
+ == Installing
55
+
56
+ gem install mdlsql
57
+
58
+ If you're going to use mysql, mysql2 is required. Please follow these instructions to install: https://github.com/brianmario/mysql2#installing (really worth it, even if you're not going to use mdlsql ;) ).
59
+
41
60
  == Useful Links
42
61
 
43
62
  * Mysql2: https://github.com/brianmario/mysql2usage
63
+
64
+ * RubyGems: https://rubygems.org/gems/mdlsql
44
65
 
45
66
  == License
46
67
 
data/lib/mdlsql/col.rb ADDED
@@ -0,0 +1,38 @@
1
+ # Copyright (C) 2013 All MdlSql contributers
2
+
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ # In order to contact the author of this gem, please write to sikian@gmail.com.
17
+
18
+ module MdlSql
19
+ class Col
20
+ # @!attribute table [Table]
21
+ # @!attribute col [Symbol]
22
+ attr_accessor :col, :table
23
+
24
+ # @param table [Table]
25
+ # @param col [Symbol]
26
+ def initialize col, table
27
+ # table = table.to_sym if table.is_a? String
28
+ col = col.to_sym if col.is_a? String
29
+
30
+ if table.is_a? String || table.is_a?
31
+ table = Table.new table
32
+ end
33
+
34
+ @table = table
35
+ @col = col
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,50 @@
1
+ # Copyright (C) 2013 All MdlSql contributers
2
+
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ # In order to contact the author of this gem, please write to sikian@gmail.com.
17
+
18
+ module MdlSql
19
+ class Join
20
+ # @!attribute type [Symbol]
21
+ # @!attribute table [Table]
22
+ # @!attribute col1 [Symbol/String]
23
+ # @!attribute col2 [Symbol/String]
24
+ # @!attribute op [Symbol]
25
+
26
+ attr_accessor :type, :table, :cond1, :cond2, :op
27
+
28
+ def initialize opts={}
29
+ @cond1 = opts[:cond1]
30
+ @cond2 = opts[:cond2]
31
+ @table = Table.new opts[:table], opts[:as]
32
+ @type = opts[:type]
33
+
34
+ opts[:op].to_sym if opts[:op].is_a? String
35
+ @op = opts[:op]
36
+ @op ||= '='.to_sym
37
+ end
38
+
39
+ # Literals must be made explicit with apostrophes.
40
+ def to_mysql
41
+ # query = String.new
42
+ query = "\n"
43
+ query << @type.to_s.upcase << ' ' if @type
44
+ query << 'JOIN'
45
+ query << " " << @table.to_mysql
46
+ query << "\nON #{@cond1} #{@op} #{@cond2}"
47
+ return query
48
+ end
49
+ end
50
+ end
@@ -28,8 +28,9 @@ class MysqlBuilder < QueryBuilder
28
28
  class << self
29
29
  def select(values={})
30
30
  cols = values[:cols]
31
- table = values[:table]
31
+ tables = values[:tables]
32
32
  where = values[:where]
33
+ join = values[:join]
33
34
 
34
35
  query = String.new
35
36
  query = "SELECT"
@@ -44,24 +45,40 @@ class MysqlBuilder < QueryBuilder
44
45
  end
45
46
 
46
47
  # From (with possible alias)
47
- if table
48
- query << " FROM #{table}"
49
- # query << " AS #{table_alias}" if table_alias
48
+ if tables
49
+ query << ' FROM'
50
+ tables.each do |tab|
51
+ query << ' ' << tab.to_mysql
52
+ # query << " #{tab.name}"
53
+ # query << " AS #{tab.as}" if tab.as
54
+ query << ","
55
+ end
56
+ query.chop!
57
+
50
58
  else
51
59
  raise "No table at select query."
52
60
  end
53
61
 
54
- # @leftjoin = {:tablealias => {:name => "tablename", :on => "oncondition"}...}
55
- if @leftjoin && @leftjoin.length > 0
56
- @leftjoin.each do |key, value|
57
- query << " LEFT JOIN #{value[:name]} AS #{key} ON #{value[:on]}"
62
+ # @join, see Join
63
+ if join && join.length > 0
64
+ join.each do |j|
65
+ query << j.to_mysql
66
+ # query << ' ' << value[:type] if value[:type]
67
+ # query << ' ' << value[:table].to_s
68
+ # query << " ON #{value[:cond1]} #{value[:op]} #{value[:cond2]}"
58
69
  end
59
70
  end
60
71
  # @where = Array
61
72
  if where && where.length > 0
62
- query << " WHERE"
63
- where.each do |dec|
64
- query << " #{dec}"
73
+ query << "\nWHERE"
74
+ # where.each do |dec|
75
+ # query << " #{dec}"
76
+ # end
77
+ first = true
78
+ where.each do |wh|
79
+ query << " #{wh.concat}" unless first
80
+ query << " #{wh.cond1} #{wh.op} #{wh.cond2}"
81
+ first = false
65
82
  end
66
83
  end
67
84
 
@@ -80,7 +97,6 @@ class MysqlBuilder < QueryBuilder
80
97
  raise "No table at insert query."
81
98
  end
82
99
 
83
- puts @cols.inspect
84
100
  if @cols && @cols.count > 0
85
101
  query << ' ('
86
102
  @cols.each do |key,col|
@@ -15,9 +15,6 @@
15
15
 
16
16
  # In order to contact the author of this gem, please write to sikian@gmail.com.
17
17
 
18
- require_relative './sockets/mysql.rb'
19
- require 'yaml'
20
-
21
18
  module MdlSql
22
19
  class SqlQuery
23
20
 
@@ -55,8 +52,6 @@ module MdlSql
55
52
  @@password = values[:password]
56
53
  @@db = values[:database]
57
54
  @@socket = values[:socket]
58
-
59
- puts values.inspect
60
55
  end
61
56
 
62
57
  ###
@@ -68,7 +63,7 @@ module MdlSql
68
63
 
69
64
  def select()
70
65
  # Sets method to select
71
- # @return (@see initialize)
66
+ # @return (@see initialize)
72
67
  @method = :select
73
68
 
74
69
  return self
@@ -94,10 +89,10 @@ module MdlSql
94
89
  # set
95
90
  #
96
91
 
97
- def columns(*values)
98
- # @todo check column uniqueness in hash
99
- # @todo revise
92
+ # @todo check column uniqueness in hash
93
+ # @todo revise
100
94
  # @return (@see initialize)
95
+ def columns(*values)
101
96
  @cols ||= Hash.new
102
97
 
103
98
 
@@ -114,43 +109,85 @@ module MdlSql
114
109
  return self
115
110
  end
116
111
 
117
- alias_method :cols, :column
112
+ alias_method :cols, :columns
118
113
 
119
- def table(table, table_alias=nil)
120
- # Selects table from which to select (or insert, update) with possible alias.
121
- # @todo use a hash here to allow many tables (for a select, for example).
122
- # @note use into() when inserting for readability.
123
- # @return (@see initialize)
124
- table = table.to_sym if table.is_a? String
125
- table_alias = table_alias if table_alias.is_a? String
114
+ ###
115
+ #
116
+ # Selects table from which to select (or insert, update) with possible alias.
117
+ # @todo use a hash here to allow many tables (for a select, for example).
118
+ # @note use from() when selecting & into() when inserting for readability.
119
+ # @return (@see initialize)
120
+ def tables(tables = {})
121
+ # table = table.to_sym if table.is_a? String
122
+ # table_alias = table_alias if table_alias.is_a? String
123
+
124
+ @tables ||= Array.new
125
+ tables.each do |table,table_alias|
126
+ @tables.push Table.new table, table_alias
127
+ end
126
128
 
127
- @table = table
128
- @table_alias = table_alias unless table_alias.nil?
129
+ # @table = table
130
+ # @table_alias = table_alias unless table_alias.nil?
129
131
  return self
130
132
  end
131
133
 
132
134
  # alias into() and from() select table
133
- alias_method :into, :table
134
- alias_method :from, :table
135
+ alias_method :into, :tables
136
+ alias_method :from, :tables
135
137
 
136
138
 
137
- def where(first, second=nil, comp=nil)
138
- # @param first [String, Array] as a string it can be the whole WHERE declaration or just the first element. As an Array, it contains a list of where declarations
139
-
139
+ # Generates a where clause
140
+ # Maybe it's a good idea to make a Where object.
141
+ #
142
+ # FFS, HAVE A CLEAR IDEA BEFORE WRITING!
143
+ #
144
+ # @option opts [String] :op default is =
145
+ # @option opts [Symbol] :concat AND, OR..., default is AND.
146
+ # @note First where clause's concat is ignored.
147
+ #
148
+ # @todo Add IN, BETWEEN and LIKE (can be done with actual where).
149
+ def where(cond1, cond2, opts={})
150
+ opts[:op] ||= '='
151
+ opts[:concat] ||= :AND
152
+
153
+ # if cond1.is_a? Hash
154
+ # if cond1[:table] && cond1[:col]
155
+ # cond1 = Col.new cond[:col], cond[:table]
156
+
140
157
  @where ||= Array.new
141
- if second.nil?
142
- @where << first
143
- else
144
- if comp.nil?
145
- raise "Don't know which logical operator to use in the where statement."
146
- else
147
- @where << "#{first} #{comp} '#{second}'"
148
- end
149
- end
158
+ wh = Where.new(
159
+ :cond1 => cond1,
160
+ :cond2 => cond2,
161
+ :op => opts[:op],
162
+ :concat => opts[:concat]
163
+ )
164
+ @where.push wh
150
165
 
151
166
  return self
152
167
  end
153
168
 
169
+ # @option opts [Symbol/String] :op
170
+ # @option opts [Symbol] :type
171
+ def join(table, cond1, cond2, opts={})
172
+ @join ||= Array.new
173
+
174
+ vars = {
175
+ :table => table,
176
+ :cond1 => cond1,
177
+ :cond2 => cond2,
178
+ :op => opts[:op],
179
+ :type => opts[:type]
180
+ }
181
+ @join.push Join.new vars
182
+
183
+ return self
184
+ end
185
+
186
+ def leftjoin(table, cond1, cond2, opts={})
187
+ opts.update({:type => :left})
188
+ join(table,cond1,cond2, opts)
189
+ end
190
+
154
191
  def values(*val)
155
192
  if @method == :insert
156
193
  @values ||= Array.new
@@ -176,12 +213,12 @@ module MdlSql
176
213
  end
177
214
 
178
215
  ###
216
+ # @!method execute()
179
217
  # Exacution command
180
-
218
+ # @todo return true/false when inserting/updating
219
+ # @todo config for different db
181
220
  def execute
182
- # @todo return true/false when inserting/updating
183
- # @todo config for different db
184
- unless @@host && @@username && @@password && @@db
221
+ if @@host.nil? || @@username.nil? || @@password.nil? || @@db.nil?
185
222
  raise 'MdlSql has not been correctly configured, please use config() to set host, username, password and db.'
186
223
  end
187
224
  client = Mysql2::Client.new(
@@ -199,20 +236,23 @@ module MdlSql
199
236
  case @@socket
200
237
  when :mysql
201
238
  sock = MysqlBuilder
202
- puts sock.class
203
239
  end
204
240
 
205
241
  query = sock.send("#{@method}",
206
- {:table => @table,
242
+ {:tables => @tables,
207
243
  :where => @where,
208
244
  :cols => @cols,
209
- :values => @values
245
+ :values => @values,
246
+ :join => @join
210
247
  })
211
248
 
212
249
 
213
250
  @result = client.query query
214
251
  return @result
215
252
 
253
+ return query
216
254
  end
217
255
  end
218
256
  end
257
+
258
+
@@ -0,0 +1,36 @@
1
+ # Copyright (C) 2013 All MdlSql contributers
2
+
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ # In order to contact the author of this gem, please write to sikian@gmail.com.
17
+
18
+ module MdlSql
19
+ class Table
20
+ attr_accessor :name, :as
21
+ def initialize name, as=nil
22
+ name = name.to_sym if name.is_a? String
23
+ as = as.to_sym if as.is_a? String
24
+
25
+ @name = name
26
+ @as = as if as
27
+ end
28
+
29
+ def to_mysql
30
+ s = String.new
31
+ s << @name.to_s
32
+ s << ' AS ' << @as.to_s if @as
33
+ return s
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module MdlSql
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,35 @@
1
+ # Copyright (C) 2013 All MdlSql contributers
2
+
3
+ # This program is free software: you can redistribute it and/or modify
4
+ # it under the terms of the GNU General Public License as published by
5
+ # the Free Software Foundation, either version 3 of the License, or
6
+ # (at your option) any later version.
7
+
8
+ # This program is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ # GNU General Public License for more details.
12
+
13
+ # You should have received a copy of the GNU General Public License
14
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
+
16
+ # In order to contact the author of this gem, please write to sikian@gmail.com.
17
+
18
+ module MdlSql
19
+ class Where
20
+ # @!attribute col1 [Symbol/String]
21
+ # @!attribute col2 [Symbol/String]
22
+ # @!attribute op [Symbol]
23
+
24
+ attr_accessor :cond1, :cond2, :op, :concat
25
+
26
+ def initialize opts={}
27
+ @cond1 = opts[:cond1]
28
+ @cond2 = opts[:cond2]
29
+
30
+ opts[:op].to_sym if opts[:op].is_a? String
31
+ @op = opts[:op]
32
+ @concat = opts[:concat]
33
+ end
34
+ end
35
+ end
data/lib/mdlsql.rb CHANGED
@@ -18,26 +18,23 @@
18
18
  path = File.dirname(__FILE__) + "/mdlsql/"
19
19
 
20
20
  [
21
- "version"
21
+ "version",
22
22
  "sqlquery",
23
23
 
24
- "sockets/mysql"
24
+ "sockets/mysql",
25
+
26
+ "table",
27
+ "col",
28
+ "where",
29
+ "join"
25
30
  ].each do |library|
26
31
  require path + library
27
- puts path + library
28
32
  end
29
33
 
30
- module MdlSql
31
- # @!method select()
32
- # @!method insert()
33
- # @!method update()
34
- # @!method config()
35
- # Calls SqlQuery.config to configurate futures queries.
36
- # @todo Allow many simultaneous configurations. For the moment being, use different config files.
37
- # @option values [Symbol]
38
-
39
- # module_function :select, :insert, :update, :config
34
+ require 'yaml'
35
+ require 'mysql2'
40
36
 
37
+ module MdlSql
41
38
  @host = String.new
42
39
 
43
40
  def select()
@@ -55,6 +52,9 @@ module MdlSql
55
52
  return query
56
53
  end
57
54
 
55
+ # Calls SqlQuery.config to configurate futures queries.
56
+ # @todo Allow many simultaneous configurations. For the moment being, use different config files.
57
+ # @option values [Symbol]
58
58
  def config(values={})
59
59
  SqlQuery.config(values)
60
60
  end
@@ -0,0 +1,20 @@
1
+ require_relative '../lib/mdlsql'
2
+
3
+ MdlSql.config({
4
+ :host => 'localhost',
5
+ :username => 'root',
6
+ :password => 'candyholaesternocow',
7
+ :database => 'anvyl'
8
+ })
9
+
10
+ puts results = MdlSql.select
11
+ .from(:users => :u)
12
+ .join(:jobs, :'u.uid', 'jobs.uid')
13
+ .where(:status, 1, :op => :>)
14
+ .execute
15
+ # .where(:'u.id', 3)
16
+ #
17
+
18
+ results.each do |r|
19
+ puts "#{r[:username]} :: #{r[:name]}"
20
+ 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.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sikian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-27 00:00:00.000000000 Z
11
+ date: 2013-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2
@@ -70,10 +70,15 @@ files:
70
70
  - README.rdoc
71
71
  - Rakefile
72
72
  - lib/mdlsql.rb
73
+ - lib/mdlsql/col.rb
74
+ - lib/mdlsql/join.rb
73
75
  - lib/mdlsql/sockets/mysql.rb
74
76
  - lib/mdlsql/sqlquery.rb
77
+ - lib/mdlsql/table.rb
75
78
  - lib/mdlsql/version.rb
79
+ - lib/mdlsql/where.rb
76
80
  - mdlsql.gemspec
81
+ - test/test_tables.rb
77
82
  homepage: https://github.com/Sikian/mdlsql
78
83
  licenses:
79
84
  - GPL-3.0
@@ -99,5 +104,6 @@ signing_key:
99
104
  specification_version: 4
100
105
  summary: A modular query builder to enable a high database compatibility, usage easiness
101
106
  and dynamic construction.
102
- test_files: []
107
+ test_files:
108
+ - test/test_tables.rb
103
109
  has_rdoc: