rubyq 0.1.0
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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rubyq.rb +185 -0
- data/lib/rubyq/version.rb +3 -0
- data/rubyq.gemspec +29 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a2f88ac896e5fcfc87d722a0eb2009b52d52dd7fdf181e04c3de58f7871ff086
|
4
|
+
data.tar.gz: 53ae281fa36fb443cdabaf3dd63e480e3c326f20b0e9633c262b6f07855a86b9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c403a6468c97b72fd98258720cb117822feb0da3abca9479dfeb7357ab1eff7866f9c1abc0367751015cff17535fba02ed84fefd38ad5af8932c5e2414835134
|
7
|
+
data.tar.gz: 5a397243dcb381502396a5ec4c3aab1ed44deae274f1af0806eae8897ea0f5fa27e00fbe5b289d8ec334ca3d0bd9d0896e592c8a99d86efb89ab3f6dcb0af262
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Indra Gunawan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
[](https://travis-ci.org/IndraGunawan/rubyq)
|
2
|
+
|
3
|
+
# Rubyq
|
4
|
+
|
5
|
+
This library is for building simple query strings via an object oriented
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'rubyq'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install rubyq
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'rubyq'
|
27
|
+
|
28
|
+
query_builder = Rubyq::QueryBuilder.new
|
29
|
+
|
30
|
+
# Select
|
31
|
+
query_builder.select('id').from('my_table').where('id = 1')
|
32
|
+
# will output: SELECT id FROM my_table WHERE id = 1
|
33
|
+
|
34
|
+
# Update
|
35
|
+
query_builder.update('my_table').set('name', 'newname').where('id = 1')
|
36
|
+
# will output: UPDATE my_table SET name = 'newname' WHERE id = 1
|
37
|
+
|
38
|
+
# Delete
|
39
|
+
query_builder.delete('my_table').where('id = 1')
|
40
|
+
# will output: DELETE my_table WHERE id = 1
|
41
|
+
```
|
42
|
+
|
43
|
+
## Development
|
44
|
+
|
45
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
46
|
+
|
47
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/IndraGunawan/rubyq.
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rubyq"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/rubyq.rb
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
require "rubyq/version"
|
2
|
+
|
3
|
+
module Rubyq
|
4
|
+
class QueryBuilder
|
5
|
+
def initialize
|
6
|
+
reset
|
7
|
+
end
|
8
|
+
|
9
|
+
def select(fields)
|
10
|
+
@type = :select
|
11
|
+
|
12
|
+
@parts[:select] = [*fields]
|
13
|
+
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def update(name)
|
18
|
+
@type = :update
|
19
|
+
|
20
|
+
@parts[:from] = name
|
21
|
+
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def delete(name)
|
26
|
+
@type = :delete
|
27
|
+
|
28
|
+
@parts[:from] = name
|
29
|
+
|
30
|
+
self
|
31
|
+
end
|
32
|
+
|
33
|
+
def distinct(flag = true)
|
34
|
+
@parts[:distinct] = flag
|
35
|
+
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
def from(name)
|
40
|
+
raise 'This method only for SELECT' unless @type == :select
|
41
|
+
|
42
|
+
@parts[:from].push(name)
|
43
|
+
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def set(field, value)
|
48
|
+
@parts[:set].push({ field: field, value: value })
|
49
|
+
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def where(condition)
|
54
|
+
@parts[:where] = [ { condition: condition, operator: 'AND' } ]
|
55
|
+
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def and_where(condition)
|
60
|
+
@parts[:where].push({ condition: condition, operator: 'AND' })
|
61
|
+
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
def or_where(condition)
|
66
|
+
@parts[:where].push({ condition: condition, operator: 'OR' })
|
67
|
+
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
def order_by(field, sort = nil)
|
72
|
+
@parts[:order_by].push({ field: field, sort: sort })
|
73
|
+
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
def group_by(field)
|
78
|
+
@parts[:group_by].push(field)
|
79
|
+
|
80
|
+
self
|
81
|
+
end
|
82
|
+
|
83
|
+
def get_query
|
84
|
+
query =
|
85
|
+
case @type
|
86
|
+
when :select then build_query_for_select
|
87
|
+
when :update then build_query_for_update
|
88
|
+
when :delete then build_query_for_delete
|
89
|
+
end
|
90
|
+
|
91
|
+
reset
|
92
|
+
|
93
|
+
query
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def reset
|
99
|
+
@type = :select
|
100
|
+
|
101
|
+
@parts = {
|
102
|
+
select: [],
|
103
|
+
distinct: false,
|
104
|
+
from: [],
|
105
|
+
set: [],
|
106
|
+
where: [],
|
107
|
+
group_by: [],
|
108
|
+
order_by: [],
|
109
|
+
limit: nil,
|
110
|
+
offset: nil,
|
111
|
+
}
|
112
|
+
end
|
113
|
+
|
114
|
+
def build_query_for_select
|
115
|
+
query = "SELECT#{@parts[:distinct] == true ? ' DISTINCT ' : ' '}#{@parts[:select].empty? ? '*' : @parts[:select].join(', ')}"
|
116
|
+
|
117
|
+
if @parts[:from].any?
|
118
|
+
query << " FROM #{@parts[:from].join(', ')}"
|
119
|
+
end
|
120
|
+
|
121
|
+
if @parts[:where].any?
|
122
|
+
query << " WHERE#{build_where}"
|
123
|
+
end
|
124
|
+
|
125
|
+
if @parts[:group_by].any?
|
126
|
+
query << " GROUP BY #{@parts[:group_by].join(', ')}"
|
127
|
+
end
|
128
|
+
|
129
|
+
if @parts[:order_by].any?
|
130
|
+
query << " ORDER BY "
|
131
|
+
query << @parts[:order_by].map { |order| order[:field] + (order[:sort].nil? ? '' : " #{order[:sort]}")}.join(', ')
|
132
|
+
end
|
133
|
+
|
134
|
+
query
|
135
|
+
end
|
136
|
+
|
137
|
+
def build_query_for_update
|
138
|
+
query = "UPDATE #{@parts[:from]}"
|
139
|
+
|
140
|
+
if @parts[:set].any?
|
141
|
+
query << " SET #{@parts[:set].map { |set| "#{set[:field]} = '#{set[:value]}'" }.join(', ')}"
|
142
|
+
end
|
143
|
+
|
144
|
+
if @parts[:where].any?
|
145
|
+
query << " WHERE#{build_where}"
|
146
|
+
end
|
147
|
+
|
148
|
+
if @parts[:order_by].any?
|
149
|
+
query << " ORDER BY "
|
150
|
+
query << @parts[:order_by].map { |order| order[:field] + (order[:sort].nil? ? '' : " #{order[:sort]}")}.join(', ')
|
151
|
+
end
|
152
|
+
|
153
|
+
query
|
154
|
+
end
|
155
|
+
|
156
|
+
def build_query_for_delete
|
157
|
+
query = "DELETE #{@parts[:from]}"
|
158
|
+
|
159
|
+
if @parts[:where].any?
|
160
|
+
query << " WHERE#{build_where}"
|
161
|
+
end
|
162
|
+
|
163
|
+
if @parts[:order_by].any?
|
164
|
+
query << " ORDER BY "
|
165
|
+
query << @parts[:order_by].map { |order| order[:field] + (order[:sort].nil? ? '' : " #{order[:sort]}")}.join(', ')
|
166
|
+
end
|
167
|
+
|
168
|
+
query
|
169
|
+
end
|
170
|
+
|
171
|
+
def build_where
|
172
|
+
whereStatement = ''
|
173
|
+
|
174
|
+
@parts[:where].each do |where|
|
175
|
+
if !whereStatement.empty?
|
176
|
+
whereStatement << " #{where[:operator]}"
|
177
|
+
end
|
178
|
+
|
179
|
+
whereStatement << " #{where[:condition]}"
|
180
|
+
end
|
181
|
+
|
182
|
+
whereStatement
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
data/rubyq.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rubyq/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rubyq'
|
8
|
+
spec.version = Rubyq::VERSION
|
9
|
+
spec.authors = ['Indra Gunawan']
|
10
|
+
spec.email = ['guind.online@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Query Builder'
|
13
|
+
spec.description = 'Simple SQL Query Builder'
|
14
|
+
spec.homepage = 'https://github.com/IndraGunawan/rubyq'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubyq
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Indra Gunawan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Simple SQL Query Builder
|
56
|
+
email:
|
57
|
+
- guind.online@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/rubyq.rb
|
72
|
+
- lib/rubyq/version.rb
|
73
|
+
- rubyq.gemspec
|
74
|
+
homepage: https://github.com/IndraGunawan/rubyq
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.7.7
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Query Builder
|
98
|
+
test_files: []
|