mdlsql 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f4b47e84e03b2688dec439802afb5753beb7145f
4
+ data.tar.gz: 44b74787cdae12822cea936cde79184c476e0de8
5
+ SHA512:
6
+ metadata.gz: efca8e547b004629a23c2c16458c2f2b69fda84d88fd9373e0dde986acc3d21b76df6fd197e1dafecda7d9748858a995d6263b0005aaf1818b2dafa8463991d6
7
+ data.tar.gz: eae8ca9196c88355be64ec4d2a63e0bdcecf6a25b371969e09e1e5859650a45fb2bbdea6321076a790cfd7682b63ec124ce99a220c6dff4c04b9be7c82aba995
data/.gitignore ADDED
@@ -0,0 +1,52 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ md
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ # Configs and tests
21
+ test.rb
22
+ *.yml
23
+ *.yaml
24
+
25
+ # Packages #
26
+ ############
27
+ # it's better to unpack these files and commit the raw source
28
+ # git has its own built in compression methods
29
+ *.7z
30
+ *.dmg
31
+ *.gz
32
+ *.iso
33
+ *.jar
34
+ *.rar
35
+ *.tar
36
+ *.zip
37
+
38
+ # Linux generated files #
39
+ #########################
40
+ *~
41
+
42
+ # OS generated files #
43
+ ######################
44
+ .DS_Store
45
+ .DS_Store?
46
+ ._*
47
+ .Spotlight-V100
48
+ .Trashes
49
+ Icon?
50
+ ehthumbs.db
51
+ Thumbs.db
52
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mdlsql.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mdlsql (0.0.1)
5
+ mysql2
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ mysql2 (0.3.11)
11
+ rake (10.1.0)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.3)
18
+ mdlsql!
19
+ rake
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sikian
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,22 @@
1
+ = MdlSql
2
+
3
+ Modular Sql
4
+
5
+ Modular Sql is a modular query builder that enables a high database compatibility, usage easiness and dynamic construction. It is intended to allow any kind of query in any database, but will, at the moment, only handle relatively simple ones to most common databases.
6
+
7
+ <b>Actual situation:</b>
8
+ Simple mysql queries (select, insert, update)
9
+ No joins
10
+
11
+ <b>Mysql output:</b> MysqlResult object, more info @ https://github.com/brianmario/mysql2usage
12
+
13
+ == Usage
14
+
15
+ Simple select:
16
+ result = Array.new
17
+ result = MdlSql::select.from(:users).where(:id, 1, '=').execute
18
+
19
+ When selecting columns use .cols()/.columns, which can receive a Hash or an Array. In case a Hash is passed, the key will be the alias and the value the actual name of the column (f.e. 'users.username').
20
+
21
+ Simple insert
22
+ result = MdlSql::insert.into(:users).cols(:user, :role).values('admin',2).execute
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,135 @@
1
+ class QueryBuilder
2
+ def select; end
3
+ def insert; end
4
+ def update; end
5
+ end
6
+
7
+ class MysqlBuilder < QueryBuilder
8
+ def initialize
9
+ end
10
+
11
+ class << self
12
+ def select(values={})
13
+ cols = values[:cols]
14
+ table = values[:table]
15
+ where = values[:where]
16
+
17
+ query = String.new
18
+ query = "SELECT"
19
+
20
+ # Columns (with alias)
21
+ if cols
22
+ cols.each do |key,value|
23
+ query << " #{value} AS #{key}"
24
+ end
25
+ else
26
+ query << " *"
27
+ end
28
+
29
+ # From (with possible alias)
30
+ if table
31
+ query << " FROM #{table}"
32
+ # query << " AS #{table_alias}" if table_alias
33
+ else
34
+ raise "No table at select query."
35
+ end
36
+
37
+ # @leftjoin = {:tablealias => {:name => "tablename", :on => "oncondition"}...}
38
+ if @leftjoin && @leftjoin.length > 0
39
+ @leftjoin.each do |key, value|
40
+ query << " LEFT JOIN #{value[:name]} AS #{key} ON #{value[:on]}"
41
+ end
42
+ end
43
+ # @where = Array
44
+ if where && where.length > 0
45
+ query << " WHERE"
46
+ where.each do |dec|
47
+ query << " #{dec}"
48
+ end
49
+ end
50
+
51
+ return query
52
+ end
53
+
54
+ def insert(values={})
55
+ # INSERT INTO table (column1, column2) VALUES (v1c1, v1c2), (v2c1, v2c2)
56
+
57
+ query = String.new
58
+ query = 'INSERT INTO'
59
+
60
+ if @from
61
+ query << " #{@from}"
62
+ else
63
+ raise "No table at insert query."
64
+ end
65
+
66
+ puts @cols.inspect
67
+ if @cols && @cols.count > 0
68
+ query << ' ('
69
+ @cols.each do |key,col|
70
+ query << "#{col},"
71
+ end
72
+
73
+ query.chop! << ')'
74
+ end
75
+
76
+ query << ' VALUES'
77
+
78
+ if @values
79
+ @values.each do |row|
80
+ query << ' ('
81
+ row.each do |val|
82
+ query << "'#{val}'" << ','
83
+ end
84
+ query.chop!
85
+ query << '),'
86
+ end
87
+ query.chop!
88
+ else
89
+ raise 'No values to insert.'
90
+ end
91
+
92
+ return query
93
+ end
94
+
95
+ def update(values={})
96
+ # UPDATE example SET age='22' WHERE age='21'
97
+
98
+ table = values[:table]
99
+ set = values[:values]
100
+ where = values[:where]
101
+
102
+ query = String.new()
103
+
104
+ if table
105
+ query << "UPDATE #{table} "
106
+ else
107
+ raise "No table at update query."
108
+ end
109
+
110
+ query << 'SET'
111
+
112
+ if set && set.count > 0
113
+ set.each do |key, value|
114
+ query << " #{key} = '#{value}',"
115
+ end
116
+ query.chop!
117
+ else
118
+ raise 'Nothing to be set.'
119
+ end
120
+
121
+ query << ' WHERE'
122
+
123
+ if where && where.count > 0
124
+ where.each do |con|
125
+ query << ' ' << con << ','
126
+ end
127
+ query.chop!
128
+ else
129
+ raise 'No WHERE condition in update.'
130
+ end
131
+
132
+ return query
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,200 @@
1
+ require_relative './sockets/mysql.rb'
2
+ require 'yaml'
3
+
4
+ module MdlSql
5
+ class SqlQuery
6
+
7
+ def initialize
8
+ # Initializes query as a string
9
+ # @return self [SqlQuery] so other methods may be concatenated.
10
+
11
+ return self
12
+ end
13
+
14
+ ###
15
+ # Configuration
16
+ #
17
+
18
+ def self.config(values={})
19
+ # Does config need to be parsed from a config file?
20
+ # @todo check relative paths so file is correctly found.
21
+ # Maybe should be wise to use full paths.
22
+ if values[:parse]
23
+ if values[:parse] == :yml || values[:parse] == :yaml
24
+ if values[:file]
25
+ values = YAML.load_file(values[:file])
26
+
27
+ # Convert keys to symbols
28
+ values = values.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
29
+ else
30
+ raise ArgumentError, "File value missing, config file could not be parsed."
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ @@host = values[:host]
37
+ @@username = values[:username]
38
+ @@password = values[:password]
39
+ @@db = values[:database]
40
+ @@socket = values[:socket]
41
+
42
+ puts values.inspect
43
+ end
44
+
45
+ ###
46
+ # Method selection:
47
+ # select()
48
+ # insert()
49
+ # update(table=nil)
50
+ #
51
+
52
+ def select()
53
+ # Sets method to select
54
+ # @return (@see initialize)
55
+ @method = :select
56
+
57
+ return self
58
+ end
59
+
60
+ def insert()
61
+ @method = :insert
62
+ return self
63
+ end
64
+
65
+ def update(table=nil)
66
+ @method = :update
67
+ from table if table
68
+ return self
69
+ end
70
+
71
+ ###
72
+ # Parameters setting:
73
+ # columns / cols
74
+ # from / into
75
+ # where
76
+ # values
77
+ # set
78
+ #
79
+
80
+ def columns(*values)
81
+ # @todo check column uniqueness in hash
82
+ # @todo revise
83
+ # @return (@see initialize)
84
+ @cols ||= Hash.new
85
+
86
+
87
+ if values[0].is_a? Hash
88
+ values.each do |val|
89
+ @cols.update(val)
90
+ end
91
+
92
+ else
93
+ values.each do |val|
94
+ @cols.update({val => val})
95
+ end
96
+ end
97
+ return self
98
+ end
99
+
100
+ alias_method :cols, :column
101
+
102
+ def from(table, table_alias=nil)
103
+ # Selects table from which to select (or insert, update) with possible alias.
104
+ # @todo use a hash here to allow many tables (for a select, for example).
105
+ # @note use into() when inserting for readability.
106
+ # @return (@see initialize)
107
+ table = table.to_sym if table.is_a? String
108
+ table_alias = table_alias if table_alias.is_a? String
109
+
110
+ @table = table
111
+ @table_alias = table_alias unless table_alias.nil?
112
+ return self
113
+ end
114
+
115
+ # alias into() and from() select table
116
+ alias_method :into, :from
117
+
118
+
119
+ def where(first, second=nil, comp=nil)
120
+ # @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
121
+
122
+ @where ||= Array.new
123
+ if second.nil?
124
+ @where << first
125
+ else
126
+ if comp.nil?
127
+ raise "Don't know which logical operator to use in the where statement."
128
+ else
129
+ @where << "#{first} #{comp} '#{second}'"
130
+ end
131
+ end
132
+
133
+ return self
134
+ end
135
+
136
+ def values(*val)
137
+ if @method == :insert
138
+ @values ||= Array.new
139
+
140
+ @values << val
141
+ else
142
+ raise 'Use values() only for #insert.'
143
+ end
144
+
145
+ return self
146
+ end
147
+
148
+ def set(val={})
149
+ if @method == :update
150
+ @values ||= Hash.new
151
+
152
+ @values.update val
153
+ else
154
+ raise 'Use set() only for #update.'
155
+ end
156
+
157
+ return self
158
+ end
159
+
160
+ ###
161
+ # Exacution command
162
+
163
+ def execute
164
+ # @todo return true/false when inserting/updating
165
+ # @todo config for different db
166
+ unless @@host && @@username && @@password && @@db
167
+ raise 'MdlSql has not been correctly configured, please use config() to set host, username, password and db.'
168
+ end
169
+ client = Mysql2::Client.new(
170
+ :host => @host,
171
+ :username => @@username,
172
+ :password => @@password,
173
+ :database => @@db,
174
+ :symbolize_keys => true
175
+ )
176
+
177
+ query = String.new
178
+
179
+ @@socket ||= :mysql
180
+
181
+ case @@socket
182
+ when :mysql
183
+ sock = MysqlBuilder
184
+ puts sock.class
185
+ end
186
+
187
+ query = sock.send("#{@method}",
188
+ {:table => @table,
189
+ :where => @where,
190
+ :cols => @cols,
191
+ :values => @values
192
+ })
193
+
194
+
195
+ @result = client.query query
196
+ return @result
197
+
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,3 @@
1
+ module MdlSql
2
+ VERSION = "0.0.1"
3
+ end
data/lib/mdlsql.rb ADDED
@@ -0,0 +1,46 @@
1
+ path = File.dirname(__FILE__) + "/mdlsql/"
2
+
3
+ [
4
+ "version"
5
+ "sqlquery",
6
+
7
+ "sockets/mysql"
8
+ ].each do |library|
9
+ require path + library
10
+ puts path + library
11
+ end
12
+
13
+ module MdlSql
14
+ # @!method select()
15
+ # @!method insert()
16
+ # @!method update()
17
+ # @!method config()
18
+ # Calls SqlQuery.config to configurate futures queries.
19
+ # @todo Allow many simultaneous configurations. For the moment being, use different config files.
20
+ # @option values [Symbol]
21
+
22
+ # module_function :select, :insert, :update, :config
23
+
24
+ @host = String.new
25
+
26
+ def select()
27
+ query = SqlQuery.new.select()
28
+ return query
29
+ end
30
+
31
+ def insert
32
+ query = SqlQuery.new.insert()
33
+ return query
34
+ end
35
+
36
+ def update(table=nil)
37
+ query = SqlQuery.new.update(table)
38
+ return query
39
+ end
40
+
41
+ def config(values={})
42
+ SqlQuery.config(values)
43
+ end
44
+
45
+ module_function :config, :select, :insert, :update
46
+ end
data/mdlsql.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mdlsql/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mdlsql"
8
+ spec.version = MdlSql::VERSION
9
+ spec.authors = ["Sikian"]
10
+ spec.email = ["sikian@gmail.com"]
11
+ spec.homepage = "https://github.com/Sikian/mdlsql"
12
+ spec.summary = %q{A modular query builder to enable a high database compatibility, usage easiness and dynamic construction.
13
+ }
14
+ spec.description = %q{Modular Sql is a modular query builder that enables a high database compatibility, usage easiness and dynamic construction. It is intended to allow any kind of query in any database, but will, at the moment, only handle relatively simple ones to most common databases.}
15
+
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "mysql2"
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mdlsql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sikian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-06-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mysql2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Modular Sql is a modular query builder that enables a high database compatibility,
56
+ usage easiness and dynamic construction. It is intended to allow any kind of query
57
+ in any database, but will, at the moment, only handle relatively simple ones to
58
+ most common databases.
59
+ email:
60
+ - sikian@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.rdoc
70
+ - Rakefile
71
+ - lib/mdlsql.rb
72
+ - lib/mdlsql/sockets/mysql.rb
73
+ - lib/mdlsql/sqlquery.rb
74
+ - lib/mdlsql/version.rb
75
+ - mdlsql.gemspec
76
+ homepage: https://github.com/Sikian/mdlsql
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: A modular query builder to enable a high database compatibility, usage easiness
100
+ and dynamic construction.
101
+ test_files: []
102
+ has_rdoc: