rusql 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rusql/basic_condition.rb +59 -0
- data/lib/rusql/column.rb +41 -0
- data/lib/rusql/column_selector.rb +14 -0
- data/lib/rusql/complex_condition.rb +63 -0
- data/lib/rusql/condition.rb +4 -0
- data/lib/rusql/convert_tz_function_operand.rb +17 -0
- data/lib/rusql/count_selector.rb +17 -0
- data/lib/rusql/date_function_operand.rb +7 -0
- data/lib/rusql/distinct_function_selector.rb +17 -0
- data/lib/rusql/group_concat_function_selector.rb +17 -0
- data/lib/rusql/join.rb +27 -0
- data/lib/rusql/operand.rb +47 -0
- data/lib/rusql/order.rb +23 -0
- data/lib/rusql/query.rb +108 -0
- data/lib/rusql/selector.rb +13 -0
- data/lib/rusql/table.rb +34 -0
- data/lib/rusql/type_exception.rb +7 -0
- data/lib/rusql/version.rb +3 -0
- data/lib/rusql.rb +105 -0
- data/rusql.gemspec +25 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a381919b48060b9765e153ac4ccad6cd4c76489f
|
4
|
+
data.tar.gz: b9d6180ad19a5d30796b84211f89514e3336bc48
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e60d9ca5b6a00ed5db2fe942e10d61924a48f7b308869a766f15c1db9179c0adc199de170d9ac422de430076bada47f1fde991dd4879e5c48f87780754ab9de1
|
7
|
+
data.tar.gz: 27d0646cc1ec66b7e43126e86343211753d0090acd99d1d0567a6521a301fef1fc8b2f84f6926c038690aa0a6ac3ceac546b85090cf06429686673c969f4451a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at csy0013@googlemail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Ajith Hussain
|
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,41 @@
|
|
1
|
+
# Rusql
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rusql`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'rusql'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install rusql
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rusql. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rusql"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module Rusql
|
2
|
+
class BasicCondition < Condition
|
3
|
+
attr_reader :type
|
4
|
+
typed_attr_accessor :left, Operand
|
5
|
+
attr_accessor :right
|
6
|
+
|
7
|
+
TYPES = %i(
|
8
|
+
equals
|
9
|
+
greater_than
|
10
|
+
greater_than_or_equals
|
11
|
+
in
|
12
|
+
less_than
|
13
|
+
less_than_or_equals
|
14
|
+
like
|
15
|
+
not_equals
|
16
|
+
not_in
|
17
|
+
)
|
18
|
+
|
19
|
+
def type=(t)
|
20
|
+
raise Exception.new("Expected type to be one of #{ TYPES.map(&:to_s).join(", ") }") unless TYPES.include?(t)
|
21
|
+
|
22
|
+
@type = t
|
23
|
+
end
|
24
|
+
|
25
|
+
def and(condition)
|
26
|
+
raise Exception.new(Condition, condition.class) unless condition.is_a?(Condition)
|
27
|
+
|
28
|
+
c = ComplexCondition.new
|
29
|
+
c.type = :and
|
30
|
+
c.add_condition(self)
|
31
|
+
c.add_condition(condition)
|
32
|
+
|
33
|
+
c
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s(indent: " ") # to make it compatible with complex condition to_s
|
37
|
+
case self.type
|
38
|
+
when :equals
|
39
|
+
"#{left.to_s} = #{convert_value(self.right)}"
|
40
|
+
when :greater_than
|
41
|
+
"#{left.to_s} > #{convert_value(self.right)}"
|
42
|
+
when :greater_than_or_equals
|
43
|
+
"#{left.to_s} >= #{convert_value(self.right)}"
|
44
|
+
when :in
|
45
|
+
"#{left.to_s} IN (#{ self.right.map{|v| convert_value(v) }.join(", ") })"
|
46
|
+
when :less_than
|
47
|
+
"#{left.to_s} < #{convert_value(self.right)}"
|
48
|
+
when :less_than_or_equals
|
49
|
+
"#{left.to_s} <= #{convert_value(self.right)}"
|
50
|
+
when :like
|
51
|
+
"#{left.to_s} LIKE #{convert_value(self.right)}"
|
52
|
+
when :not_equals
|
53
|
+
"#{left.to_s} != #{convert_value(self.right)}"
|
54
|
+
when :not_in
|
55
|
+
"#{left.to_s} NOT IN (#{ self.right.map{|v| convert_value(v) }.join(", ") })"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/rusql/column.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Rusql
|
2
|
+
class Column < Operand
|
3
|
+
typed_attr_accessor :table, Table
|
4
|
+
typed_attr_accessor :name, Symbol
|
5
|
+
|
6
|
+
def initialize(table, name)
|
7
|
+
@table = table
|
8
|
+
@name = name
|
9
|
+
|
10
|
+
super("")
|
11
|
+
end
|
12
|
+
|
13
|
+
def as_selector
|
14
|
+
s = ColumnSelector.new
|
15
|
+
s.table = self.table
|
16
|
+
s.name = self.name
|
17
|
+
|
18
|
+
s
|
19
|
+
end
|
20
|
+
|
21
|
+
def as(a)
|
22
|
+
self.as_selector.as(a)
|
23
|
+
end
|
24
|
+
|
25
|
+
def as_operand
|
26
|
+
Operand.new(self.to_s)
|
27
|
+
end
|
28
|
+
|
29
|
+
def desc
|
30
|
+
Order.new(:desc, self)
|
31
|
+
end
|
32
|
+
|
33
|
+
def asc
|
34
|
+
Order.new(:asc, self)
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_s
|
38
|
+
"#{self.table.to_s}.#{self.name.to_s}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Rusql
|
2
|
+
class ColumnSelector < Selector
|
3
|
+
typed_attr_accessor :table, Table
|
4
|
+
typed_attr_accessor :name, Symbol
|
5
|
+
|
6
|
+
def to_s
|
7
|
+
str = "#{self.table.to_s}.#{name}"
|
8
|
+
unless self.alias.nil?
|
9
|
+
str += " AS #{self.alias}"
|
10
|
+
end
|
11
|
+
str
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Rusql
|
2
|
+
class ComplexCondition < Condition
|
3
|
+
attr_reader :type
|
4
|
+
attr_reader :conditions
|
5
|
+
|
6
|
+
TYPES = %i(
|
7
|
+
and
|
8
|
+
or
|
9
|
+
)
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@conditions = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_condition(c)
|
16
|
+
raise TypeException.new(Condition, c.class) unless c.is_a?(Condition)
|
17
|
+
|
18
|
+
@conditions << c
|
19
|
+
end
|
20
|
+
|
21
|
+
def type=(t)
|
22
|
+
raise Exception.new("Expected type to be one of #{ TYPES.map(&:to_s).join(", ") }") unless TYPES.include?(t)
|
23
|
+
|
24
|
+
@type = t
|
25
|
+
end
|
26
|
+
|
27
|
+
def and(condition)
|
28
|
+
new_condition = ComplexCondition.new
|
29
|
+
new_condition.type = :and
|
30
|
+
|
31
|
+
if self.type == :and
|
32
|
+
self.conditions.each do |c|
|
33
|
+
new_condition.add_condition(c)
|
34
|
+
end
|
35
|
+
else
|
36
|
+
new_condition.add_condition(self)
|
37
|
+
end
|
38
|
+
new_condition.add_condition(condition)
|
39
|
+
|
40
|
+
new_condition
|
41
|
+
end
|
42
|
+
|
43
|
+
def or(condition)
|
44
|
+
new_condition = ComplexCondition.new
|
45
|
+
new_condition.type = :or
|
46
|
+
|
47
|
+
if self.type == :or
|
48
|
+
self.conditions.each do |c|
|
49
|
+
new_condition.add_condition(c)
|
50
|
+
end
|
51
|
+
else
|
52
|
+
new_condition.add_condition(self)
|
53
|
+
end
|
54
|
+
new_condition.add_condition(condition)
|
55
|
+
|
56
|
+
new_condition
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_s(indent: " ")
|
60
|
+
self.conditions.map{ |c| c.to_s(indent: indent+" ") }.join("\n#{indent}#{self.type.to_s.upcase} ")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rusql
|
2
|
+
class ConvertTzFunctionOperand < Operand
|
3
|
+
def initialize(op, from, to)
|
4
|
+
raise TypeException.new(String, from.class) unless from.is_a?(String)
|
5
|
+
raise TypeException.new(String, to.class) unless to.is_a?(String)
|
6
|
+
|
7
|
+
op_string = case op
|
8
|
+
when Column
|
9
|
+
op.as_operand.to_s
|
10
|
+
else
|
11
|
+
op.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
super("CONVERT_TZ(#{ op_string }, #{ convert_value(from) }, #{ convert_value(to) })")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rusql
|
2
|
+
class CountSelector < Selector
|
3
|
+
attr_reader :field
|
4
|
+
|
5
|
+
def initialize(field)
|
6
|
+
@field = field
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_s
|
10
|
+
str = "COUNT(#{ self.field })"
|
11
|
+
unless self.alias.nil?
|
12
|
+
str += " AS self.alias#{}"
|
13
|
+
end
|
14
|
+
str
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rusql
|
2
|
+
class DistinctFunctionSelector < Selector
|
3
|
+
attr_reader :field
|
4
|
+
|
5
|
+
def initialize(field)
|
6
|
+
@field = field
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_s
|
10
|
+
str = "DISTINCT #{ self.field }"
|
11
|
+
unless self.alias.nil?
|
12
|
+
str += " AS #{self.alias}"
|
13
|
+
end
|
14
|
+
str
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rusql
|
2
|
+
class GroupConcatFunctionSelector < Selector
|
3
|
+
attr_reader :field
|
4
|
+
|
5
|
+
def initialize(field)
|
6
|
+
@field = field
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_s
|
10
|
+
str = "GROUP_CONCAT(#{ self.field })"
|
11
|
+
unless self.alias.nil?
|
12
|
+
str += " AS #{self.alias}"
|
13
|
+
end
|
14
|
+
str
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/rusql/join.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Rusql
|
2
|
+
class Join
|
3
|
+
attr_reader :type
|
4
|
+
typed_attr_accessor :table, Table
|
5
|
+
typed_attr_accessor :condition, BasicCondition
|
6
|
+
|
7
|
+
TYPES = %i(
|
8
|
+
inner_join
|
9
|
+
left_outer_join
|
10
|
+
outer_join
|
11
|
+
)
|
12
|
+
|
13
|
+
def initialize(type, table, condition)
|
14
|
+
raise Exception.new("Expected type to be one of #{ TYPES.map(&:to_s).join(",") }") unless TYPES.include?(type)
|
15
|
+
raise TypeException.new(Table, table.class) unless table.is_a?(Table)
|
16
|
+
raise TypeException.new(BasicCondition, condition.class) unless condition.is_a?(BasicCondition)
|
17
|
+
|
18
|
+
@type = type
|
19
|
+
@table = table
|
20
|
+
@condition = condition
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
"#{ self.type.to_s.upcase.gsub("_"," ") } #{self.table.to_s_for_aliasing} ON #{self.condition.to_s}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Rusql
|
2
|
+
class Operand
|
3
|
+
def initialize(op)
|
4
|
+
raise TypeException.new(String, op.class) unless op.is_a?(String)
|
5
|
+
@op = op
|
6
|
+
end
|
7
|
+
|
8
|
+
%i(greater_than greater_than_or_equals less_than less_than_or_equals equals not_equals).each do |type|
|
9
|
+
define_method type, Proc.new { |v|
|
10
|
+
c = BasicCondition.new
|
11
|
+
c.type = type
|
12
|
+
c.left = self
|
13
|
+
c.right = v
|
14
|
+
|
15
|
+
c
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
%i(in not_in).each do |type|
|
20
|
+
define_method type, Proc.new { |v|
|
21
|
+
raise TypeException.new(Array, v.class) unless v.is_a?(Array) || v.is_a?(Range)
|
22
|
+
|
23
|
+
c = BasicCondition.new
|
24
|
+
c.type = type
|
25
|
+
c.left = self
|
26
|
+
c.right = v.to_a
|
27
|
+
|
28
|
+
c
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
def like(v)
|
33
|
+
raise TypeException.new(String, v.class) unless v.is_a?(String)
|
34
|
+
|
35
|
+
c = BasicCondition.new
|
36
|
+
c.type = :like
|
37
|
+
c.left = self
|
38
|
+
c.right = v
|
39
|
+
|
40
|
+
c
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_s
|
44
|
+
@op
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/rusql/order.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Rusql
|
2
|
+
class Order
|
3
|
+
attr_reader :type
|
4
|
+
attr_reader :column
|
5
|
+
|
6
|
+
TYPES =%i(
|
7
|
+
asc
|
8
|
+
desc
|
9
|
+
)
|
10
|
+
|
11
|
+
def initialize(type, column)
|
12
|
+
raise Exception.new("Expected type to be one of #{}") unless TYPES.include?(type)
|
13
|
+
raise TypeException.new(Column, column.class) unless column.is_a?(Column)
|
14
|
+
|
15
|
+
@type = type
|
16
|
+
@column = column
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
"#{self.column.to_s} #{type.to_s.upcase}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/rusql/query.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
module Rusql
|
2
|
+
class Query
|
3
|
+
attr_reader :selectors
|
4
|
+
attr_reader :joins
|
5
|
+
attr_reader :from_table
|
6
|
+
attr_reader :condition
|
7
|
+
attr_reader :orders
|
8
|
+
attr_reader :limit
|
9
|
+
|
10
|
+
def initialize(selectors)
|
11
|
+
selectors.each do |selector|
|
12
|
+
raise TypeException.new(Selector, selector.class) unless selector.is_a?(Selector)
|
13
|
+
end
|
14
|
+
|
15
|
+
@selectors = selectors
|
16
|
+
@joins = []
|
17
|
+
@orders = []
|
18
|
+
end
|
19
|
+
|
20
|
+
def select(*selectors)
|
21
|
+
selectors.each do |selector|
|
22
|
+
raise TypeException.new(Selector, selector.class) unless selector.is_a?(Selector)
|
23
|
+
end
|
24
|
+
|
25
|
+
@selectors = selectors
|
26
|
+
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def limit(c)
|
31
|
+
raise TypeException unless c.is_a?(Fixnum)
|
32
|
+
|
33
|
+
@limit = c
|
34
|
+
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def from(t)
|
39
|
+
raise TypeException unless t.is_a?(Table)
|
40
|
+
|
41
|
+
@from_table = t
|
42
|
+
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def inner_join(table,condition)
|
47
|
+
self.joins << Join.new(:inner_join, table, condition)
|
48
|
+
|
49
|
+
self
|
50
|
+
end
|
51
|
+
|
52
|
+
def outer_join(table,condition)
|
53
|
+
self.joins << Join.new(:outer_join, table, condition)
|
54
|
+
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
def left_outer_join(table,condition)
|
59
|
+
self.joins << Join.new(:left_outer_join, table, condition)
|
60
|
+
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
def right_outer_join(table,condition)
|
65
|
+
self.joins << Join.new(:right_outer_join, table, condition)
|
66
|
+
|
67
|
+
self
|
68
|
+
end
|
69
|
+
|
70
|
+
def where(condition)
|
71
|
+
raise TypeException.new(Condition, condition.class) unless condition.is_a?(Condition)
|
72
|
+
|
73
|
+
@condition = condition
|
74
|
+
|
75
|
+
self
|
76
|
+
end
|
77
|
+
|
78
|
+
def order_by(*orders)
|
79
|
+
orders.each do |o|
|
80
|
+
raise TypeException.new(Order, o.class) unless o.is_a?(Order)
|
81
|
+
end
|
82
|
+
|
83
|
+
@orders = orders
|
84
|
+
|
85
|
+
self
|
86
|
+
end
|
87
|
+
|
88
|
+
def to_s
|
89
|
+
join_part = self.joins.map{ |j| "\n#{j.to_s}" }.join
|
90
|
+
where_part = "\nWHERE"
|
91
|
+
order_part = "\nORDER BY #{ self.orders.map(&:to_s).join(", ") }"
|
92
|
+
|
93
|
+
if self.condition.is_a?(BasicCondition)
|
94
|
+
where_part += " "
|
95
|
+
where_part += self.condition.to_s
|
96
|
+
elsif self.condition.is_a?(ComplexCondition)
|
97
|
+
where_part += "\n "
|
98
|
+
where_part += self.condition.to_s
|
99
|
+
end
|
100
|
+
|
101
|
+
<<-EOS
|
102
|
+
SELECT
|
103
|
+
#{ self.selectors.map{ |s| " #{s.to_s}" }.join(",\n") }
|
104
|
+
FROM #{ self.from_table.to_s_for_aliasing }#{ (self.joins.length > 0) ? join_part : "" }#{ self.condition.nil? ? "" : where_part }#{ self.orders.length > 0 ? order_part : "" }
|
105
|
+
EOS
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/lib/rusql/table.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Rusql
|
2
|
+
class Table
|
3
|
+
typed_attr_accessor :name, Symbol
|
4
|
+
attr_reader :alias
|
5
|
+
|
6
|
+
def [](ind)
|
7
|
+
Column.new(self, ind)
|
8
|
+
end
|
9
|
+
|
10
|
+
def as(name)
|
11
|
+
raise TypeException.new(Symbol, name.class) unless name.is_a?(String) || name.is_a?(Symbol)
|
12
|
+
|
13
|
+
@alias = name
|
14
|
+
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
if self.alias.nil?
|
20
|
+
self.name
|
21
|
+
else
|
22
|
+
self.alias
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s_for_aliasing
|
27
|
+
if self.alias.nil?
|
28
|
+
"#{self.name}"
|
29
|
+
else
|
30
|
+
"#{self.name} AS #{self.alias}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/rusql.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require "rusql/version"
|
2
|
+
require "rusql/type_exception"
|
3
|
+
|
4
|
+
class Class
|
5
|
+
def typed_attr_accessor(name, type)
|
6
|
+
raise TypeException.new(Class, type.class) unless type.is_a?(Class)
|
7
|
+
|
8
|
+
instance_eval { attr_reader name }
|
9
|
+
|
10
|
+
define_method "#{name}=", Proc.new { |val|
|
11
|
+
instance_variable_set("@#{name}", val)
|
12
|
+
}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def convert_value(v)
|
17
|
+
case v
|
18
|
+
when String
|
19
|
+
"'#{v}'"
|
20
|
+
when Date
|
21
|
+
v.strftime("'%Y-%m-%d'")
|
22
|
+
when DateTime, Time
|
23
|
+
v.strftime("'%Y-%m-%d %H:%M:%S'")
|
24
|
+
else
|
25
|
+
v.to_s
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module Rusql
|
30
|
+
def date(value)
|
31
|
+
DateFunctionOperand.new(value)
|
32
|
+
end
|
33
|
+
|
34
|
+
def convert_tz(value, from, to)
|
35
|
+
ConvertTzFunctionOperand.new(value, from, to)
|
36
|
+
end
|
37
|
+
|
38
|
+
def distinct(sel)
|
39
|
+
raise TypeException.new(Selector, sel.class) unless sel.is_a?(Selector) || sel.is_a?(Column)
|
40
|
+
final_sel = sel.is_a?(Column) ? sel.as_selector : sel
|
41
|
+
DistinctFunctionSelector.new(final_sel.to_s)
|
42
|
+
end
|
43
|
+
|
44
|
+
def group_concat(sel)
|
45
|
+
raise TypeException.new(Selector, sel.class) unless sel.is_a?(Selector) || sel.is_a?(Column)
|
46
|
+
final_sel = sel.is_a?(Column) ? sel.as_selector : sel
|
47
|
+
GroupConcatFunctionSelector.new(final_sel.to_s)
|
48
|
+
end
|
49
|
+
|
50
|
+
def count(col)
|
51
|
+
raise TypeException.new(Column, col.class) unless col.is_a?(Column)
|
52
|
+
CountSelector.new(col.to_s)
|
53
|
+
end
|
54
|
+
|
55
|
+
def count_all
|
56
|
+
CountSelector.new("*")
|
57
|
+
end
|
58
|
+
|
59
|
+
def table(name)
|
60
|
+
t = Table.new
|
61
|
+
t.name = name
|
62
|
+
t
|
63
|
+
end
|
64
|
+
|
65
|
+
def inner_join(table,condition)
|
66
|
+
Join.new(:inner_join, table, condition)
|
67
|
+
end
|
68
|
+
|
69
|
+
def outer_join(table,condition)
|
70
|
+
Join.new(:outer_join, table, condition)
|
71
|
+
end
|
72
|
+
|
73
|
+
def left_outer_join(table,condition)
|
74
|
+
Join.new(:left_outer_join, table, condition)
|
75
|
+
end
|
76
|
+
|
77
|
+
def right_outer_join(table,condition)
|
78
|
+
Join.new(:right_outer_join, table, condition)
|
79
|
+
end
|
80
|
+
|
81
|
+
def select(*opts)
|
82
|
+
opts.each do |arg|
|
83
|
+
raise TypeException.new(Selector, arg.class) unless arg.is_a?(Selector) || arg.is_a?(Column)
|
84
|
+
end
|
85
|
+
|
86
|
+
Query.new(opts.map{|a| a.is_a?(Column) ? a.as_selector : a })
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
require "rusql/operand"
|
91
|
+
require "rusql/table"
|
92
|
+
require "rusql/column"
|
93
|
+
require "rusql/selector"
|
94
|
+
require "rusql/column_selector"
|
95
|
+
require "rusql/count_selector"
|
96
|
+
require "rusql/query"
|
97
|
+
require "rusql/convert_tz_function_operand"
|
98
|
+
require "rusql/date_function_operand"
|
99
|
+
require "rusql/condition"
|
100
|
+
require "rusql/basic_condition"
|
101
|
+
require "rusql/complex_condition"
|
102
|
+
require "rusql/join"
|
103
|
+
require "rusql/order"
|
104
|
+
require "rusql/distinct_function_selector"
|
105
|
+
require "rusql/group_concat_function_selector"
|
data/rusql.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rusql/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rusql"
|
8
|
+
spec.version = Rusql::VERSION
|
9
|
+
spec.authors = ["Ajith Hussain"]
|
10
|
+
spec.email = ["csy0013@googlemail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{rusql provides a DSL for building SQL queries}
|
13
|
+
spec.homepage = "http://github.com/sparkymat/rusql"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rusql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ajith Hussain
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-26 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
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:
|
56
|
+
email:
|
57
|
+
- csy0013@googlemail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/rusql.rb
|
73
|
+
- lib/rusql/basic_condition.rb
|
74
|
+
- lib/rusql/column.rb
|
75
|
+
- lib/rusql/column_selector.rb
|
76
|
+
- lib/rusql/complex_condition.rb
|
77
|
+
- lib/rusql/condition.rb
|
78
|
+
- lib/rusql/convert_tz_function_operand.rb
|
79
|
+
- lib/rusql/count_selector.rb
|
80
|
+
- lib/rusql/date_function_operand.rb
|
81
|
+
- lib/rusql/distinct_function_selector.rb
|
82
|
+
- lib/rusql/group_concat_function_selector.rb
|
83
|
+
- lib/rusql/join.rb
|
84
|
+
- lib/rusql/operand.rb
|
85
|
+
- lib/rusql/order.rb
|
86
|
+
- lib/rusql/query.rb
|
87
|
+
- lib/rusql/selector.rb
|
88
|
+
- lib/rusql/table.rb
|
89
|
+
- lib/rusql/type_exception.rb
|
90
|
+
- lib/rusql/version.rb
|
91
|
+
- rusql.gemspec
|
92
|
+
homepage: http://github.com/sparkymat/rusql
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.5.1
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: rusql provides a DSL for building SQL queries
|
116
|
+
test_files: []
|