qruby 0.1.0 → 0.2.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 +4 -4
- data/.gitignore +6 -0
- data/.travis.yml +1 -0
- data/DOCS.md +476 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +61 -0
- data/Rakefile +6 -0
- data/lib/qruby.rb +225 -0
- data/lib/qruby/version.rb +3 -0
- data/qruby.gemspec +23 -0
- metadata +45 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 07464bd91e7a1924b98ef263dd69a501af2be14c
|
4
|
+
data.tar.gz: 52977e3d2a1c2b9c4cf395a70ffad9189f2336aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b3669d7ee1a0113a1d366630ea3d8dae906c876dc6100830b723238962435d4e5654a6aa2dfd47912f8535be5231c18956381c4b65260fed56b23647a6808700
|
7
|
+
data.tar.gz: 62c0e989ea983b995dd77aaa3a1b64b203f0d64ec8cec90540dc7cfa7df4b442e065b7790b5deebad213f464050ee32012ba148366ff25f673a1c6d96bee5bba
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
language: ruby
|
data/DOCS.md
ADDED
@@ -0,0 +1,476 @@
|
|
1
|
+
# QRuby Documentation
|
2
|
+
|
3
|
+
sql query builder library for Ruby
|
4
|
+
|
5
|
+
Documentation Page
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this to your application's `Gemfile` and run `gem update` command:
|
10
|
+
|
11
|
+
```yaml
|
12
|
+
gem 'qruby'
|
13
|
+
```
|
14
|
+
|
15
|
+
OR run the following command directly on terminal:
|
16
|
+
|
17
|
+
```
|
18
|
+
gem install qruby
|
19
|
+
```
|
20
|
+
|
21
|
+
## Quick Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require "qruby"
|
25
|
+
builder = QRuby::Builder.new
|
26
|
+
|
27
|
+
p builder.table("test").where("id", 1).get
|
28
|
+
|
29
|
+
# Output:
|
30
|
+
# "SELECT * FROM test WHERE id = '1' LIMIT 1"
|
31
|
+
```
|
32
|
+
|
33
|
+
# Usage and Methods
|
34
|
+
|
35
|
+
Create a new QRuby Object
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require "qruby"
|
39
|
+
builder = QRuby::Builder.new
|
40
|
+
```
|
41
|
+
|
42
|
+
|
43
|
+
> **NOTE**: You must trigger with the **get** or **get_all** methods to execute the queries.
|
44
|
+
|
45
|
+
## escape_character
|
46
|
+
|
47
|
+
This method is used to set the escape character for your queries.
|
48
|
+
|
49
|
+
Default value: "\\\\" (for SQL syntax)
|
50
|
+
|
51
|
+
For sql syntax:
|
52
|
+
```ruby
|
53
|
+
QRuby::Builder.escape_character = "\\"
|
54
|
+
```
|
55
|
+
|
56
|
+
For PostgreSQL syntax:
|
57
|
+
```ruby
|
58
|
+
QRuby::Builder.escape_character = "'"
|
59
|
+
```
|
60
|
+
|
61
|
+
### table
|
62
|
+
```ruby
|
63
|
+
# Usage 1: String Parameter
|
64
|
+
builder.table("test")
|
65
|
+
|
66
|
+
# Output: "SELECT * FROM test"
|
67
|
+
```
|
68
|
+
```ruby
|
69
|
+
# Usage 2: Array Parameter
|
70
|
+
builder.table(["foo", "bar"])
|
71
|
+
|
72
|
+
# Output: "SELECT * FROM foo, bar"
|
73
|
+
```
|
74
|
+
|
75
|
+
### select
|
76
|
+
```ruby
|
77
|
+
# Usage 1: String Parameter
|
78
|
+
builder.table("test").select("id, title, content, tags")
|
79
|
+
|
80
|
+
# Output: "SELECT id, title, content, tags FROM test"
|
81
|
+
```
|
82
|
+
```ruby
|
83
|
+
# Usage 2: Array Parameter
|
84
|
+
builder.table("test").select(["id", "title", "content", "tags"])
|
85
|
+
|
86
|
+
# Output: "SELECT id, title, content, tags FROM test"
|
87
|
+
```
|
88
|
+
|
89
|
+
### select functions (min, max, sum, avg, count)
|
90
|
+
```ruby
|
91
|
+
# Usage 1:
|
92
|
+
builder.table("test").max("price")
|
93
|
+
|
94
|
+
# Output: "SELECT MAX(price) FROM test"
|
95
|
+
```
|
96
|
+
```ruby
|
97
|
+
# Usage 2:
|
98
|
+
builder.table("test").count("id", "total_row")
|
99
|
+
|
100
|
+
# Output: "SELECT COUNT(id) AS total_row FROM test"
|
101
|
+
```
|
102
|
+
|
103
|
+
### join
|
104
|
+
```ruby
|
105
|
+
builder.table("test as t").join("foo as f", "t.id", "f.t_id").where("t.status", 1).get_all
|
106
|
+
|
107
|
+
# Output: "SELECT * FROM test as t JOIN foo as f ON t.id = f.t_id WHERE t.status = '1'"
|
108
|
+
```
|
109
|
+
|
110
|
+
You can use this method in 7 ways. These;
|
111
|
+
|
112
|
+
- join
|
113
|
+
- left_join
|
114
|
+
- right_join
|
115
|
+
- inner_join
|
116
|
+
- full_outer_join
|
117
|
+
- left_outer_join
|
118
|
+
- right_outer_join
|
119
|
+
|
120
|
+
Examples:
|
121
|
+
```ruby
|
122
|
+
builder.table("test as t").left_join("foo as f", "t.id", "f.t_id").get_all
|
123
|
+
|
124
|
+
# Output: "SELECT * FROM test as t LEFT JOIN foo as f ON t.id = f.t_id"
|
125
|
+
```
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
builder.table("test as t").full_outer_join("foo as f", "t.id", "f.t_id").get_all
|
129
|
+
|
130
|
+
# Output: "SELECT * FROM test as t FULL OUTER JOIN foo as f ON t.id = f.t_id"
|
131
|
+
```
|
132
|
+
|
133
|
+
### where
|
134
|
+
```ruby
|
135
|
+
builder.table("test").where("active", 1).get_all
|
136
|
+
|
137
|
+
# Output: "SELECT * FROM test WHERE active = '1'"
|
138
|
+
|
139
|
+
# OR
|
140
|
+
|
141
|
+
builder.table("test").where("age", ">=", 18).get_all
|
142
|
+
|
143
|
+
# Output: "SELECT * FROM test WHERE age >= '18'"
|
144
|
+
|
145
|
+
# OR
|
146
|
+
|
147
|
+
builder.table("test").where("age = ? OR age = ?", [18, 20]).get_all
|
148
|
+
|
149
|
+
# Output: "SELECT * FROM test WHERE age = '18' OR age = '20'"
|
150
|
+
```
|
151
|
+
|
152
|
+
You can use this method in 4 ways. These;
|
153
|
+
|
154
|
+
- where
|
155
|
+
- or_where
|
156
|
+
- not_where
|
157
|
+
- or_not_where
|
158
|
+
|
159
|
+
Example:
|
160
|
+
```ruby
|
161
|
+
builder.table("test").where("active", 1).not_where("auth", 1).get_all
|
162
|
+
|
163
|
+
# Output: "SELECT * FROM test WHERE active = '1' AND NOT auth = '1'"
|
164
|
+
|
165
|
+
# OR
|
166
|
+
|
167
|
+
builder.table("test").where("age", 20).or_where("age", '>', 25).get_all
|
168
|
+
|
169
|
+
# Output: "SELECT * FROM test WHERE age = '20' OR age > '25'"
|
170
|
+
```
|
171
|
+
|
172
|
+
### in
|
173
|
+
```ruby
|
174
|
+
builder.table("test").where("active", 1).in("id", [1, 2, 3]).get_all
|
175
|
+
|
176
|
+
# Output: "SELECT * FROM test WHERE active = '1' AND id IN ('1', '2', '3')"
|
177
|
+
```
|
178
|
+
|
179
|
+
You can use this method in 4 ways. These;
|
180
|
+
|
181
|
+
- in
|
182
|
+
- or_in
|
183
|
+
- not_in
|
184
|
+
- or_not_in
|
185
|
+
|
186
|
+
Example:
|
187
|
+
```ruby
|
188
|
+
builder.table("test").where("active", 1).not_in("id", [1, 2, 3]).get_all
|
189
|
+
|
190
|
+
# Output: "SELECT * FROM test WHERE active = '1' AND id NOT IN ('1', '2', '3')"
|
191
|
+
|
192
|
+
# OR
|
193
|
+
|
194
|
+
builder.table("test").where("active", 1).or_in("id", [1, 2, 3]).get_all
|
195
|
+
|
196
|
+
# Output: "SELECT * FROM test WHERE active = '1' OR id IN ('1', '2', '3')"
|
197
|
+
```
|
198
|
+
|
199
|
+
### between
|
200
|
+
```ruby
|
201
|
+
builder.table("test").where("active", 1).between("age", 18, 25).get_all
|
202
|
+
|
203
|
+
# Output: "SELECT * FROM test WHERE active = '1' AND age BETWEEN '18' AND '25'"
|
204
|
+
```
|
205
|
+
|
206
|
+
You can use this method in 4 ways. These;
|
207
|
+
|
208
|
+
- between
|
209
|
+
- or_between
|
210
|
+
- not_between
|
211
|
+
- or_not_between
|
212
|
+
|
213
|
+
Example:
|
214
|
+
```ruby
|
215
|
+
builder.table("test").where("active", 1).not_between("age", 18, 25).get_all
|
216
|
+
|
217
|
+
# Output: "SELECT * FROM test WHERE active = '1' AND age NOT BETWEEN '18' AND '25'"
|
218
|
+
|
219
|
+
# OR
|
220
|
+
|
221
|
+
builder.table("test").where("active", 1).or_between("age", 18, 25).get_all
|
222
|
+
|
223
|
+
# Output: "SELECT * FROM test WHERE active = '1' OR age BETWEEN '18' AND '25'"
|
224
|
+
```
|
225
|
+
|
226
|
+
### like
|
227
|
+
```ruby
|
228
|
+
builder.table("test").where("active", 1).like("title", "%ruby%").get_all
|
229
|
+
|
230
|
+
# Output: "SELECT * FROM test WHERE active = '1' AND title LIKE '%ruby%'"
|
231
|
+
```
|
232
|
+
|
233
|
+
You can use this method in 4 ways. These;
|
234
|
+
|
235
|
+
- like
|
236
|
+
- or_like
|
237
|
+
- not_like
|
238
|
+
- or_not_like
|
239
|
+
|
240
|
+
Example:
|
241
|
+
```ruby
|
242
|
+
builder.table("test").where("active", 1).not_like("tags", "%dot-net%").get_all
|
243
|
+
|
244
|
+
# Output: "SELECT * FROM test WHERE active = '1' AND tags NOT LIKE '%dot-net%'"
|
245
|
+
|
246
|
+
# OR
|
247
|
+
|
248
|
+
builder.table("test").like("bio", "%ruby%").or_like("bio", "%php%").get_all
|
249
|
+
|
250
|
+
# Output: "SELECT * FROM test WHERE bio LIKE '%ruby%' OR bio LIKE '%php%'"
|
251
|
+
```
|
252
|
+
|
253
|
+
### group_by
|
254
|
+
```ruby
|
255
|
+
# Usage 1: One parameter
|
256
|
+
builder.table("test").where("status", 1).group_by("cat_id").get_all
|
257
|
+
|
258
|
+
# Output: "SELECT * FROM test WHERE status = '1' GROUP BY cat_id"
|
259
|
+
```
|
260
|
+
|
261
|
+
```ruby
|
262
|
+
# Usage 1: Array parameter
|
263
|
+
builder.table("test").where("status", 1).group_by(["cat_id", "user_id"]).get_all
|
264
|
+
|
265
|
+
# Output: "SELECT * FROM test WHERE status = '1' GROUP BY cat_id, user_id"
|
266
|
+
```
|
267
|
+
|
268
|
+
### having
|
269
|
+
```ruby
|
270
|
+
builder.table("test").where("status", 1).group_by("city").having("COUNT(person)", 100).get_all
|
271
|
+
|
272
|
+
# Output: "SELECT * FROM test WHERE status = '1' GROUP BY city HAVING COUNT(person) > '100'"
|
273
|
+
|
274
|
+
# OR
|
275
|
+
|
276
|
+
builder.table("test").where("active", 1).group_by("department_id").having("AVG(salary)", "<=", 500).get_all
|
277
|
+
|
278
|
+
# Output: "SELECT * FROM test WHERE active = '1' GROUP BY department_id HAVING AVG(salary) <= '500'"
|
279
|
+
|
280
|
+
# OR
|
281
|
+
|
282
|
+
builder.table("test").where("active", 1).group_by("department_id").having("AVG(salary) > ? AND MAX(salary) < ?", [250, 1000]).get_all
|
283
|
+
|
284
|
+
# Output: "SELECT * FROM test WHERE active = '1' GROUP BY department_id HAVING AVG(salary) > '250' AND MAX(salary) < '1000'"
|
285
|
+
```
|
286
|
+
|
287
|
+
### order_by
|
288
|
+
```ruby
|
289
|
+
# Usage 1: One parameter
|
290
|
+
builder.table("test").where("status", 1).order_by("id").get_all
|
291
|
+
|
292
|
+
# Output: "SELECT * FROM test WHERE status = '1' ORDER BY id ASC"
|
293
|
+
|
294
|
+
### OR
|
295
|
+
|
296
|
+
builder.table("test").where("status", 1).order_by("id desc").get_all
|
297
|
+
|
298
|
+
# Output: "SELECT * FROM test WHERE status = '1' ORDER BY id desc"
|
299
|
+
```
|
300
|
+
|
301
|
+
```ruby
|
302
|
+
# Usage 1: Two parameters
|
303
|
+
builder.table("test").where("status", 1).order_by("id", "desc").get_all
|
304
|
+
|
305
|
+
# Output: "SELECT * FROM test WHERE status = '1' ORDER BY id DESC"
|
306
|
+
```
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
# Usage 3: Rand()
|
310
|
+
builder.table("test").where("status", 1).order_by("rand()").limit(10).get_all
|
311
|
+
|
312
|
+
# Output: "SELECT * FROM test WHERE status = '1' ORDER BY rand() LIMIT 10"
|
313
|
+
```
|
314
|
+
|
315
|
+
### limit
|
316
|
+
```ruby
|
317
|
+
# Usage 1: One parameter
|
318
|
+
builder.table("test").limit(10).get_all
|
319
|
+
|
320
|
+
# Output: "SELECT * FROM test LIMIT 10"
|
321
|
+
```
|
322
|
+
```ruby
|
323
|
+
# Usage 2: Two parameters
|
324
|
+
builder.table("test").limit(10, 20).get_all
|
325
|
+
|
326
|
+
# Output: "SELECT * FROM test LIMIT 10, 20"
|
327
|
+
```
|
328
|
+
|
329
|
+
### get - get_all
|
330
|
+
```ruby
|
331
|
+
# 1. get
|
332
|
+
# Return 1 record.
|
333
|
+
builder.table("test").get
|
334
|
+
|
335
|
+
# Output: "SELECT * FROM test LIMIT 1"
|
336
|
+
```
|
337
|
+
```ruby
|
338
|
+
# 2. get_all
|
339
|
+
# Return many records.
|
340
|
+
builder.table("test").get_all
|
341
|
+
|
342
|
+
# Output: "SELECT * FROM test"
|
343
|
+
```
|
344
|
+
|
345
|
+
### insert
|
346
|
+
```ruby
|
347
|
+
data = {
|
348
|
+
"title" => "QRuby",
|
349
|
+
"content" => "sql query builder library for Ruby...",
|
350
|
+
"tags" => "ruby, query, builder",
|
351
|
+
"time" => Time.new(2015, 2, 4),
|
352
|
+
"status" => 1
|
353
|
+
}
|
354
|
+
|
355
|
+
builder.table("test").insert(data)
|
356
|
+
|
357
|
+
# Output:
|
358
|
+
# "INSERT INTO test (title, content, tags, time, status) VALUES ('QRuby', 'sql query builder library for Ruby...', 'ruby, query, builder', '2015-02-04 00:00:00 +0200', '1')"
|
359
|
+
```
|
360
|
+
|
361
|
+
### update
|
362
|
+
```ruby
|
363
|
+
data = {
|
364
|
+
"title" => "Ruby on Rails",
|
365
|
+
"content" => "A superb web framework for Ruby.",
|
366
|
+
"tags" => "ruby, framework, rails",
|
367
|
+
"status" => 1
|
368
|
+
}
|
369
|
+
|
370
|
+
builder.table("test").where("id", 5).update(data)
|
371
|
+
|
372
|
+
# Output:
|
373
|
+
# "UPDATE test SET title = 'Ruby on Rails', content = 'A superb web framework for Ruby.', tags = 'ruby, framework, rails', status = '1' WHERE id = '5'"
|
374
|
+
```
|
375
|
+
|
376
|
+
### delete
|
377
|
+
```ruby
|
378
|
+
builder.table("test").where("id", 5).delete
|
379
|
+
|
380
|
+
# Output: "DELETE FROM test WHERE id = '5'"
|
381
|
+
|
382
|
+
# OR
|
383
|
+
|
384
|
+
builder.table("test").delete
|
385
|
+
|
386
|
+
# Output: "TRUNCATE TABLE delete"
|
387
|
+
```
|
388
|
+
|
389
|
+
### query
|
390
|
+
```ruby
|
391
|
+
builder.query("SELECT id, title, content FROM pages WHERE id = ? AND active = ? ORDER BY updated_at DESC", [10, 1])
|
392
|
+
|
393
|
+
# Output: "SELECT id, title, content FROM pages WHERE id = '10' AND active = '1' ORDER BY updated_at DESC"
|
394
|
+
|
395
|
+
# OR
|
396
|
+
|
397
|
+
builder.query("SELECT * FROM test WHERE title LIKE ? AND status = ? LIMIT 10", ["%Ruby%", 1])
|
398
|
+
|
399
|
+
# Output: "SELECT * FROM test WHERE title LIKE '%Ruby%' AND status = '1' LIMIT 10"
|
400
|
+
```
|
401
|
+
|
402
|
+
### analyze
|
403
|
+
```ruby
|
404
|
+
builder.table("test").analyze
|
405
|
+
|
406
|
+
# Output: "ANALYZE TABLE test"
|
407
|
+
|
408
|
+
# OR
|
409
|
+
|
410
|
+
builder.table(["foo", "bar", "baz"]).analyze
|
411
|
+
|
412
|
+
# Output: "ANALYZE TABLE foo, bar, baz"
|
413
|
+
```
|
414
|
+
|
415
|
+
### check
|
416
|
+
```ruby
|
417
|
+
builder.table("test").check
|
418
|
+
|
419
|
+
# Output: "CHECK TABLE test"
|
420
|
+
|
421
|
+
# OR
|
422
|
+
|
423
|
+
builder.table(["foo", "bar", "baz"]).check
|
424
|
+
|
425
|
+
# Output: "CHECK TABLE foo, bar, baz"
|
426
|
+
```
|
427
|
+
|
428
|
+
### checksum
|
429
|
+
```ruby
|
430
|
+
builder.table("test").checksum
|
431
|
+
|
432
|
+
# Output: "CHECKSUM TABLE test"
|
433
|
+
|
434
|
+
# OR
|
435
|
+
|
436
|
+
builder.table(["foo", "bar", "baz"]).checksum
|
437
|
+
|
438
|
+
# Output: "CHECKSUM TABLE foo, bar, baz"
|
439
|
+
```
|
440
|
+
|
441
|
+
### optimize
|
442
|
+
```ruby
|
443
|
+
builder.table("test").optimize
|
444
|
+
|
445
|
+
# Output: "OPTIMIZE TABLE test"
|
446
|
+
|
447
|
+
# OR
|
448
|
+
|
449
|
+
builder.table(["foo", "bar", "baz"]).optimize
|
450
|
+
|
451
|
+
# Output: "OPTIMIZE TABLE foo, bar, baz"
|
452
|
+
```
|
453
|
+
|
454
|
+
### repair
|
455
|
+
```ruby
|
456
|
+
builder.table("test").repair
|
457
|
+
|
458
|
+
# Output: "REPAIR TABLE test"
|
459
|
+
|
460
|
+
# OR
|
461
|
+
|
462
|
+
builder.table(["foo", "bar", "baz"]).repair
|
463
|
+
|
464
|
+
# Output: "REPAIR TABLE foo, bar, baz"
|
465
|
+
```
|
466
|
+
|
467
|
+
### last_query
|
468
|
+
```ruby
|
469
|
+
builder.table("test").where("active", 1).order_by("id", "desc").limit(10).get_all
|
470
|
+
|
471
|
+
# Output: "SELECT * FROM test WHERE active = '1' ORDER BY id DESC LIMIT 10"
|
472
|
+
|
473
|
+
builder.last_query
|
474
|
+
|
475
|
+
# Output: "SELECT * FROM test WHERE active = '1' ORDER BY id DESC LIMIT 10"
|
476
|
+
```
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 İzni Burak Demirtaş
|
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,61 @@
|
|
1
|
+
# qruby
|
2
|
+
|
3
|
+
```
|
4
|
+
_
|
5
|
+
| |
|
6
|
+
__ _ _ __ _ _| |__ _ _
|
7
|
+
/ _` | '__| | | | '_ \| | | |
|
8
|
+
| (_| | | | |_| | |_) | |_| |
|
9
|
+
\__, |_| \__,_|_.__/ \__, |
|
10
|
+
| | __/ |
|
11
|
+
|_| |___/
|
12
|
+
```
|
13
|
+
|
14
|
+
[](https://travis-ci.org/izniburak/qruby)
|
15
|
+
|
16
|
+
sql query builder library for Ruby
|
17
|
+
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
```
|
22
|
+
gem install qruby
|
23
|
+
```
|
24
|
+
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require "qruby"
|
31
|
+
builder = QRuby::Builder.new
|
32
|
+
|
33
|
+
p builder.table("test").where("id", 17).or_where("language", "ruby").get
|
34
|
+
|
35
|
+
# Output:
|
36
|
+
# "SELECT * FROM test WHERE id = '17' OR language = 'ruby' LIMIT 1"
|
37
|
+
|
38
|
+
|
39
|
+
p builder.table('test').select('id, title, status').order_by('id', 'desc').limit(10).get_all
|
40
|
+
# Output:
|
41
|
+
# "SELECT id, title, status FROM test ORDER BY id DESC LIMIT 10"
|
42
|
+
```
|
43
|
+
|
44
|
+
|
45
|
+
## Docs
|
46
|
+
|
47
|
+
Documentation Page: [qruby Docs](https://github.com/izniburak/qruby/blob/master/DOCS.md)
|
48
|
+
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it ( https://github.com/izniburak/qruby/fork )
|
53
|
+
2. Create your feature branch (git checkout -b my-new-feature)
|
54
|
+
3. Commit your changes (git commit -am 'Add some feature')
|
55
|
+
4. Push to the branch (git push origin my-new-feature)
|
56
|
+
5. Create a new Pull Request
|
57
|
+
|
58
|
+
|
59
|
+
## Contributors
|
60
|
+
|
61
|
+
- [izniburak](https://github.com/izniburak) İzni Burak Demirtaş - creator, maintainer
|
data/Rakefile
ADDED
data/lib/qruby.rb
ADDED
@@ -0,0 +1,225 @@
|
|
1
|
+
require_relative "qruby/version"
|
2
|
+
|
3
|
+
module QRuby
|
4
|
+
class Builder
|
5
|
+
@@escape_character = "\\"
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@select = "*"
|
9
|
+
@table, @join, @where, @group_by, @having, @order_by, @limit, @last_query = "", "", "", "", "", "", "", ""
|
10
|
+
@operators = ["=", "!=", "<", ">", "<=", ">=", "<>"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def table(name)
|
14
|
+
@table = name.is_a?(Array) ? name.join(", ") : name.to_s
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def select(fields)
|
19
|
+
value = fields.is_a?(Array) ? fields.join(", ") : fields.to_s
|
20
|
+
@select = (@select<=>"*").eql?(0) ? value : "#{@select}, #{value}"
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
["max", "min", "sum", "count", "avg"].each do |method|
|
25
|
+
define_method "#{method}" do |field, name = nil|
|
26
|
+
value = "#{method.upcase}(#{field})"
|
27
|
+
value += " AS #{name}" if !name.nil?
|
28
|
+
@select = (@select<=>"*").eql?(0) ? value : "#{@select}, #{value}"
|
29
|
+
self
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def join(table, field1, field2 = nil, type = "")
|
34
|
+
@join += field2.nil? ? " #{type}JOIN #{table} ON #{field1}" : " #{type}JOIN #{table} ON #{field1} = #{field2}"
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
["left", "right", "inner", "full_outer", "left_outer", "right_outer"].each do |method|
|
39
|
+
define_method "#{method}_join" do |table, field1, field2 = nil|
|
40
|
+
join table, field1, field2, "#{method.upcase} "
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def where(field, operator, val = nil, type = "", and_or = "AND")
|
45
|
+
if operator.is_a?(Array)
|
46
|
+
query = ""
|
47
|
+
field.split("?").map.with_index { |val, i| query += i < operator.size ? "#{type}#{val}#{escape(operator[i])}" : "#{val}" }
|
48
|
+
where = query
|
49
|
+
elsif @operators.include?(operator.to_s)
|
50
|
+
where = "#{type}#{field} #{operator} #{escape(val)}"
|
51
|
+
else
|
52
|
+
where = "#{type}#{field} = #{escape(operator)}"
|
53
|
+
end
|
54
|
+
@where += @where.empty? ? where : " #{and_or} #{where}"
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
["or", "not", "or_not"].map.with_index do |method, i|
|
59
|
+
define_method "#{method}_where" do |field, operator, val = nil|
|
60
|
+
where field, operator, val, (i > 0 ? "NOT " : ""), (i == 1 ? "AND" : "OR")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def in(field, values, type = "", and_or = "AND")
|
65
|
+
keys = []
|
66
|
+
values.each { |val| keys << "#{escape(val)}" }
|
67
|
+
@where += @where.empty? ? "#{field} #{type}IN (#{keys.join(", ")})" : " #{and_or} #{field} #{type}IN (#{keys.join(", ")})"
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
["or", "not", "or_not"].map.with_index do |method, i|
|
72
|
+
define_method "#{method}_in" do |field, values|
|
73
|
+
self.in field, values, (i > 0 ? "NOT " : ""), (i == 1 ? "AND" : "OR")
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def between(field, value1, value2, type = "", and_or = "AND")
|
78
|
+
@where += @where.empty? ? "#{field} #{type}BETWEEN #{escape(value1)} AND #{escape(value2)}" : " #{and_or} #{field} #{type}BETWEEN #{escape(value1)} AND #{escape(value2)}"
|
79
|
+
self
|
80
|
+
end
|
81
|
+
|
82
|
+
["or", "not", "or_not"].map.with_index do |method, i|
|
83
|
+
define_method "#{method}_between" do |field, value1, value2|
|
84
|
+
between field, value1, value2, (i > 0 ? "NOT " : ""), (i == 1 ? "AND" : "OR")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def like(field, value, type = "", and_or = "AND")
|
89
|
+
@where += @where.empty? ? "#{field} #{type}LIKE #{escape(value)}" : " #{and_or} #{field} #{type}LIKE #{escape(value)}"
|
90
|
+
self
|
91
|
+
end
|
92
|
+
|
93
|
+
["or", "not", "or_not"].map.with_index do |method, i|
|
94
|
+
define_method "#{method}_like" do |field, value|
|
95
|
+
like field, value, (i > 0 ? "NOT " : ""), (i == 1 ? "AND" : "OR")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def limit(limit, limit_end = nil)
|
100
|
+
@limit = !limit_end.nil? ? "#{limit}, #{limit_end}" : "#{limit}"
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
def order_by(field, dir = nil)
|
105
|
+
if !dir.nil?
|
106
|
+
order_by = "#{field} #{dir.upcase}"
|
107
|
+
else
|
108
|
+
order_by = (field.include?(" ") || field == "rand()") ? field : "#{field} ASC"
|
109
|
+
end
|
110
|
+
@order_by += @order_by.empty? ? order_by : ", #{order_by}"
|
111
|
+
self
|
112
|
+
end
|
113
|
+
|
114
|
+
def group_by(field)
|
115
|
+
@group_by = field.is_a?(Array) ? field.join(", ") : field
|
116
|
+
self
|
117
|
+
end
|
118
|
+
|
119
|
+
def having(field, operator, val = nil)
|
120
|
+
if operator.is_a?(Array)
|
121
|
+
query = ""
|
122
|
+
field.split("?").map.with_index { |val, i| query += i < operator.size ? "#{val}#{escape(operator[i])}" : "#{val}" }
|
123
|
+
@having = query
|
124
|
+
else
|
125
|
+
@having = @operators.include?(operator.to_s) ? "#{field} #{operator} #{escape(val)}" : "#{field} > #{escape(operator)}"
|
126
|
+
end
|
127
|
+
self
|
128
|
+
end
|
129
|
+
|
130
|
+
def get
|
131
|
+
@limit = 1
|
132
|
+
get_all
|
133
|
+
end
|
134
|
+
|
135
|
+
def get_all
|
136
|
+
query = "SELECT #{@select} FROM #{@table}"
|
137
|
+
query += "#{@join}" if !@join.empty?
|
138
|
+
query += " WHERE #{@where}" if !@where.empty?
|
139
|
+
query += " GROUP BY #{@group_by}" if !@group_by.empty?
|
140
|
+
query += " HAVING #{@having}" if !@having.empty?
|
141
|
+
query += " ORDER BY #{@order_by}" if !@order_by.empty?
|
142
|
+
query += " LIMIT #{@limit}" if !@limit.to_s.empty?
|
143
|
+
end_query query
|
144
|
+
end
|
145
|
+
|
146
|
+
def insert(datas)
|
147
|
+
fields = datas.keys
|
148
|
+
values = []
|
149
|
+
datas.values.each { |val| values << "#{escape(val)}" }
|
150
|
+
query = "INSERT INTO #{@table} (#{fields.join(", ")}) VALUES (#{values.join(", ")})"
|
151
|
+
end_query query
|
152
|
+
end
|
153
|
+
|
154
|
+
def update(datas)
|
155
|
+
query = "UPDATE #{@table} SET"
|
156
|
+
fields = datas.keys
|
157
|
+
values = []
|
158
|
+
datas.values.map.with_index { |val, i| values << "#{fields[i]} = #{escape(val)}" }
|
159
|
+
query += " #{values.join(", ")}"
|
160
|
+
query += " WHERE #{@where}" if !@where.empty?
|
161
|
+
query += " ORDER BY #{@order_by}" if !@order_by.empty?
|
162
|
+
query += " LIMIT #{@limit}" if !@limit.to_s.empty?
|
163
|
+
end_query query
|
164
|
+
end
|
165
|
+
|
166
|
+
def delete
|
167
|
+
query = "DELETE FROM #{@table}"
|
168
|
+
query += " WHERE #{@where}" if !@where.empty?
|
169
|
+
query += " ORDER BY #{@order_by}" if !@order_by.empty?
|
170
|
+
query += " LIMIT #{@limit}" if !@limit.to_s.empty?
|
171
|
+
query = "TRUNCATE TABLE #{@table}" if query == "DELETE FROM #{@table}"
|
172
|
+
end_query query
|
173
|
+
end
|
174
|
+
|
175
|
+
def drop(check_exists = false)
|
176
|
+
query = "DROP TABLE#{check_exists ? " IF EXISTS" : ""} #{@table}"
|
177
|
+
end_query query
|
178
|
+
end
|
179
|
+
|
180
|
+
def alter(command, column, data_type = "")
|
181
|
+
query = "ALTER TABLE #{@table} #{command.gsub('_', ' ').upcase} #{column}"
|
182
|
+
query += " #{data_type}" if !data_type.empty?
|
183
|
+
end_query query
|
184
|
+
end
|
185
|
+
|
186
|
+
def query(sql, params)
|
187
|
+
query = ""
|
188
|
+
sql.split("?").map.with_index { |val, i| query += i < params.size ? "#{val}#{escape(params[i])}" : "#{val}" }
|
189
|
+
end_query query
|
190
|
+
end
|
191
|
+
|
192
|
+
["analyze", "check", "checksum", "optimize", "repair"].each do |method|
|
193
|
+
define_method "#{method}" do
|
194
|
+
query = "#{method.upcase} TABLE #{@table}"
|
195
|
+
end_query query
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
def last_query
|
200
|
+
@last_query
|
201
|
+
end
|
202
|
+
|
203
|
+
def self.escape_character=(character)
|
204
|
+
@@escape_character = character
|
205
|
+
end
|
206
|
+
|
207
|
+
private
|
208
|
+
|
209
|
+
def reset
|
210
|
+
@table, @join, @where, @group_by, @having, @order_by, @limit, @last_query = "", "", "", "", "", "", "", ""
|
211
|
+
@select = "*"
|
212
|
+
end
|
213
|
+
|
214
|
+
def end_query(query)
|
215
|
+
reset
|
216
|
+
@last_query = query
|
217
|
+
query
|
218
|
+
end
|
219
|
+
|
220
|
+
def escape(data)
|
221
|
+
return "NULL" if data.nil?
|
222
|
+
"'#{data.to_s.gsub(/\\|'/) { |c| @@escape_character + c }}'"
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
data/qruby.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'qruby/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'qruby'
|
8
|
+
s.version = QRuby::VERSION
|
9
|
+
s.date = '2017-10-05'
|
10
|
+
s.summary = 'simple sql query builder library for Ruby.'
|
11
|
+
s.description = 'QRuby: simple sql query builder library for Ruby.'
|
12
|
+
s.authors = ['İzni Burak Demirtaş']
|
13
|
+
s.email = ['info@burakdemirtas.org']
|
14
|
+
s.homepage = 'https://github.com/izniburak/qruby'
|
15
|
+
s.license = 'MIT'
|
16
|
+
|
17
|
+
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec)/}) }
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
|
20
|
+
s.add_development_dependency 'bundler', '~> 1.15'
|
21
|
+
s.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
s.add_development_dependency 'rspec', '~> 3.0'
|
23
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- İzni Burak Demirtaş
|
@@ -10,27 +10,65 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2017-10-05 00:00:00.000000000 Z
|
12
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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
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'
|
13
41
|
- !ruby/object:Gem::Dependency
|
14
42
|
name: rspec
|
15
43
|
requirement: !ruby/object:Gem::Requirement
|
16
44
|
requirements:
|
17
|
-
- - "
|
45
|
+
- - "~>"
|
18
46
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
47
|
+
version: '3.0'
|
20
48
|
type: :development
|
21
49
|
prerelease: false
|
22
50
|
version_requirements: !ruby/object:Gem::Requirement
|
23
51
|
requirements:
|
24
|
-
- - "
|
52
|
+
- - "~>"
|
25
53
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
description: simple sql query builder library for Ruby.
|
54
|
+
version: '3.0'
|
55
|
+
description: 'QRuby: simple sql query builder library for Ruby.'
|
28
56
|
email:
|
29
57
|
- info@burakdemirtas.org
|
30
58
|
executables: []
|
31
59
|
extensions: []
|
32
60
|
extra_rdoc_files: []
|
33
|
-
files:
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- DOCS.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/qruby.rb
|
70
|
+
- lib/qruby/version.rb
|
71
|
+
- qruby.gemspec
|
34
72
|
homepage: https://github.com/izniburak/qruby
|
35
73
|
licenses:
|
36
74
|
- MIT
|