sql-builder 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/sql-builder/builder.rb +25 -7
- data/lib/sql-builder/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5bcc084c0688d944d96e0ee1714b177d32c879cac4d1ef3fae67cd05ada5a47b
|
4
|
+
data.tar.gz: 5d31a28f462ba242b1d473bda0305796c6b9862c5b2b4e4f94c1861fb03579ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3adb6c21540df083abf0c1480c314acae8362191e0389cba9318ca86d716f286eaa02f9a600b369228d341e0984e297b8cc699d5df9ded21938d596f1752863
|
7
|
+
data.tar.gz: 5cd5ad219554c76dcf07561cbf094d3ab5306a531e6b3414461bbc8b376e4f850328602b8ab0af9275fab0b87aa8dc2071127309e8ebc1ce2c4424120e8d20ea
|
data/README.md
CHANGED
@@ -47,7 +47,7 @@ Add the conditions by request params:
|
|
47
47
|
|
48
48
|
```ruby
|
49
49
|
query.where("age >= ?", params[:age]) unless params[:age].blank?
|
50
|
-
query.where(
|
50
|
+
query.where(status: params[:status]) unless params[:status].nil?
|
51
51
|
if params[:created_at_from] && params[:created_at_to]
|
52
52
|
query.where("created_at >= ? and created_at <= ?", params[:created_at_from], params[:created_at_to])
|
53
53
|
end
|
@@ -67,7 +67,7 @@ ORDER BY id desc LIMIT 100 OFFSET 0
|
|
67
67
|
|
68
68
|
```rb
|
69
69
|
query = SQLBuilder.new("select user_id, name, count(ip) as ip_count from user_visits")
|
70
|
-
query.where(
|
70
|
+
query.where(status: 1).where("created_at > ?", params[:created_at])
|
71
71
|
query.group("user_id").group("name").having("count(ip) > 2")
|
72
72
|
query.to_sql
|
73
73
|
```
|
data/lib/sql-builder/builder.rb
CHANGED
@@ -12,7 +12,7 @@ require "active_record"
|
|
12
12
|
# .to_sql
|
13
13
|
class SQLBuilder
|
14
14
|
attr_reader :sql, :conditions, :havings, :orders, :groups, :limit_options, :page_options
|
15
|
-
delegate :
|
15
|
+
delegate :sanitize_sql_array, :sanitize_sql_for_order, to: ActiveRecord::Base
|
16
16
|
|
17
17
|
# Create a new SQLBuilder
|
18
18
|
#
|
@@ -44,7 +44,7 @@ class SQLBuilder
|
|
44
44
|
query_scope = condition.first
|
45
45
|
@conditions = query_scope.conditions
|
46
46
|
else
|
47
|
-
conditions <<
|
47
|
+
conditions << sanitize_sql_for_assignment(condition)
|
48
48
|
end
|
49
49
|
|
50
50
|
self
|
@@ -108,7 +108,7 @@ class SQLBuilder
|
|
108
108
|
# # => "GROUP BY name HAVING count(name) > 5"
|
109
109
|
#
|
110
110
|
def having(*condition)
|
111
|
-
havings <<
|
111
|
+
havings << sanitize_sql_for_assignment(condition)
|
112
112
|
self
|
113
113
|
end
|
114
114
|
|
@@ -137,16 +137,16 @@ class SQLBuilder
|
|
137
137
|
def to_sql
|
138
138
|
sql_parts = [sql]
|
139
139
|
if conditions.any?
|
140
|
-
sql_parts << "WHERE " + conditions.join(" AND ")
|
140
|
+
sql_parts << "WHERE " + conditions.flatten.join(" AND ")
|
141
141
|
end
|
142
142
|
if orders.any?
|
143
|
-
sql_parts << "ORDER BY " + orders.join(", ")
|
143
|
+
sql_parts << "ORDER BY " + orders.flatten.join(", ")
|
144
144
|
end
|
145
145
|
if groups.any?
|
146
|
-
sql_parts << "GROUP BY " + groups.join(", ")
|
146
|
+
sql_parts << "GROUP BY " + groups.flatten.join(", ")
|
147
147
|
end
|
148
148
|
if havings.any?
|
149
|
-
sql_parts << "HAVING " + havings.join(" AND ")
|
149
|
+
sql_parts << "HAVING " + havings.flatten.join(" AND ")
|
150
150
|
end
|
151
151
|
if limit_options[:limit]
|
152
152
|
sql_parts << "LIMIT " + limit_options[:limit].to_s
|
@@ -156,4 +156,22 @@ class SQLBuilder
|
|
156
156
|
end
|
157
157
|
sql_parts.join(" ")
|
158
158
|
end
|
159
|
+
|
160
|
+
private
|
161
|
+
|
162
|
+
# https://api.rubyonrails.org/classes/ActiveRecord/Sanitization/ClassMethods.html#method-i-sanitize_sql_for_assignment
|
163
|
+
def sanitize_sql_for_assignment(assignments)
|
164
|
+
case assignments.first
|
165
|
+
when Hash; sanitize_sql_hash_for_assignment(assignments.first)
|
166
|
+
else
|
167
|
+
sanitize_sql_array(assignments)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
# https://api.rubyonrails.org/classes/ActiveRecord/Sanitization/ClassMethods.html#method-i-sanitize_sql_hash_for_assignment
|
172
|
+
def sanitize_sql_hash_for_assignment(attrs)
|
173
|
+
attrs.map do |attr, value|
|
174
|
+
sanitize_sql_array(["#{attr} = ?", value])
|
175
|
+
end
|
176
|
+
end
|
159
177
|
end
|
data/lib/sql-builder/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sql-builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|