sql-builder 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 16002ee092061ecac8b29f725d606e5617252dad607d138269a651dad5b21a3a
4
+ data.tar.gz: bc9b44a28f909204e86bc6b8b208b8b48746423552e649ce21002e12f902bfd3
5
+ SHA512:
6
+ metadata.gz: eb95a8dddc6c56f9073bfd997e19e184c8c9c1f87992fae67bd9df6329db8af564beb5066e0ab5ecde9495c71da12c4dda3034fdaa3e5dc213cd922302c31dee
7
+ data.tar.gz: 115738ea5dda02fe1ef57114ac3765e43b4b30d1e405b1eaee1d332192dd66e4d27a6bbf7262e0980b524dfd22c0077da95065dda009261fdcb5d194f87b138d
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jason Lee
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,64 @@
1
+ # SQLBuilder
2
+
3
+ A simple SQL builder for generate SQL for non-ActiveRecord supports databases.
4
+
5
+ [![Build Status](https://travis-ci.org/huacnlee/sql-builder.svg?branch=master)](https://travis-ci.org/huacnlee/sql-builder)
6
+
7
+ ## Features
8
+
9
+ - Simple SQL generator with DSL.
10
+ - Sanitize SQL by ActiveRecord methods, keep security.
11
+ - Simple SQL geneate logic for keep support any SQL databases (MySQL, PostgreSQL, TiDB, Amazon Redshift...)
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'sql-builder'
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```rb
24
+ SQLBuilder.new("SELECT * FROM users")
25
+ .where("name = ?", "hello world")
26
+ .where("status != ?", 1)
27
+ .order("created_at desc")
28
+ .order("id asc")
29
+ .page(1)
30
+ .per(20)
31
+ .to_sql
32
+
33
+ => "SELECT * FROM users WHERE name = 'hello world' AND status != 1 ORDER BY created_at desc, id asc LIMIT 20 OFFSET 0"
34
+ ```
35
+
36
+ More complext:
37
+
38
+ ```rb
39
+ query = SQLBuilder.new("SELECT users.name, users.age, user_profiles.bio, user_profiles.avatar FROM users INNER JOIN user_profiles ON users.id = user_profiles.user_id")
40
+
41
+ # conditions by params
42
+ query.where("age >= ?", params[:age]) unless params[:age].blank?
43
+ query.where("status = ?", params[:status]) unless params[:status].nil?
44
+ if params[:created_at_from] && params[:created_at_to]
45
+ query.where("created_at >= ? and created_at <= ?", params[:created_at_from], params[:created_at_to])
46
+ end
47
+ query.order("id desc").limit(100).to_sql
48
+
49
+ => "SELECT users.name, users.age, user_profiles.bio, user_profiles.avatar FROM users INNER JOIN user_profiles ON users.id = user_profiles.user_id WHERE age >= 18 AND status = 3 AND created_at >= '2020-01-03 10:54:08 +0800' and created_at <= '2020-01-03 10:54:08 +0800' ORDER BY id desc LIMIT 100 OFFSET 0"
50
+ ```
51
+
52
+ ## TODO
53
+
54
+ - [ ] where by or;
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ 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).
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,5 @@
1
+ require "sql-builder/version"
2
+ require "sql-builder/builder"
3
+
4
+ class SQLBuilder
5
+ end
@@ -0,0 +1,106 @@
1
+ # SQLBuilder write the complex SQL as DSL
2
+ #
3
+ # Example:
4
+ #
5
+ # query = SQLBuilder.new("SELECT * FROM users inner join ")
6
+ # .where("name = ?", "hello world")
7
+ # .where("status != ?", 1)
8
+ # .order("created_at desc")
9
+ # .order("id asc")
10
+ # .page(1).per(20)
11
+ # .to_sql
12
+ require "active_record"
13
+
14
+ class SQLBuilder
15
+ attr_reader :sql, :conditions, :orders, :limit_options, :page_options
16
+ delegate :sanitize_sql, :sanitize_sql_for_order, to: ActiveRecord::Base
17
+
18
+ def initialize(sql = "")
19
+ @sql = sql
20
+ @conditions = []
21
+ @orders = []
22
+ @limit_options = {}
23
+ @page_options = { per_page: 20 }
24
+ end
25
+
26
+ # Add `AND` condition
27
+ #
28
+ # query.where("name = ?", params[:name]).where("age >= ?", 18)
29
+ # or
30
+ # count_query.where(query)
31
+ def where(*condition)
32
+ case condition.first
33
+ when SQLBuilder
34
+ query_scope = condition.first
35
+ @conditions = query_scope.conditions
36
+ else
37
+ conditions << sanitize_sql(condition)
38
+ end
39
+
40
+ self
41
+ end
42
+
43
+ # Order By
44
+ #
45
+ # query.order("name asc").order("created_at desc").to_sql
46
+ # => "ORDER BY name asc, created_at desc"
47
+ def order(condition)
48
+ orders << sanitize_sql_for_order(condition)
49
+ self
50
+ end
51
+
52
+ # Offset
53
+ #
54
+ # query.offset(3).limit(10) => "LIMIT 10 OFFSET 3"
55
+ def offset(offset)
56
+ limit_options[:offset] = offset.to_i
57
+ self
58
+ end
59
+
60
+ # Limit
61
+ #
62
+ # query.offset(3).limit(10) => "LIMIT 10 OFFSET 3"
63
+ def limit(limit)
64
+ limit_options[:offset] ||= 0
65
+ limit_options[:limit] = limit.to_i
66
+ self
67
+ end
68
+
69
+ # Pagination
70
+ #
71
+ # query.page(1).per(12) => "LIMIT 12 OFFSET 0"
72
+ # query.page(2).per(12) => "LIMIT 12 OFFSET 12"
73
+ def page(page_no)
74
+ page_options[:page] = page_no
75
+ page_options[:per_page] ||= 10
76
+
77
+ limit_options[:offset] = page_options[:per_page].to_i * (page_options[:page].to_i - 1)
78
+ limit_options[:limit] = page_options[:per_page].to_i
79
+ self
80
+ end
81
+
82
+ # See page
83
+ def per(per_page)
84
+ page_options[:per_page] = per_page
85
+ self.page(page_options[:page])
86
+ self
87
+ end
88
+
89
+ # Generate SQL
90
+ def to_sql
91
+ sql_parts = [sql]
92
+ if conditions.any?
93
+ sql_parts << "WHERE " + conditions.join(" AND ")
94
+ end
95
+ if orders.any?
96
+ sql_parts << "ORDER BY " + orders.join(", ")
97
+ end
98
+ if limit_options[:limit]
99
+ sql_parts << "LIMIT " + limit_options[:limit].to_s
100
+ end
101
+ if limit_options[:limit] && limit_options[:offset]
102
+ sql_parts << "OFFSET " + limit_options[:offset].to_s
103
+ end
104
+ sql_parts.join(" ")
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ class SQLBuilder
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql-builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ description: A simple SQL builder for generate SQL for non-ActiveRecord supports databases.
28
+ email:
29
+ - huacnlee@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - lib/sql-builder.rb
37
+ - lib/sql-builder/builder.rb
38
+ - lib/sql-builder/version.rb
39
+ homepage: https://github.com/huacnlee/sql-builder
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/huacnlee/sql-builder
44
+ changelog_uri: https://github.com/huacnlee/sql-builder/blob/master/CHANGELOG.md
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.0.3
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: A simple SQL builder for generate SQL for non-ActiveRecord supports databases
64
+ test_files: []