where_builder 0.0.1 → 0.0.2
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.
- data/.gitignore +1 -0
- data/README.md +4 -1
- data/lib/where_builder.rb +61 -8
- data/lib/where_builder/version.rb +1 -1
- metadata +1 -1
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
|
1
2
|
# WhereBuilder
|
2
3
|
|
3
|
-
|
4
|
+
use this tool can build a where sentence for sql, it's can ignore a condition when it's para is nil or black string.
|
5
|
+
my purpose is not check nil for every condition, don't repeat so much if else.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -27,3 +29,4 @@ TODO: Write usage instructions here
|
|
27
29
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
30
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
31
|
5. Create new Pull Request
|
32
|
+
|
data/lib/where_builder.rb
CHANGED
@@ -2,38 +2,72 @@
|
|
2
2
|
require "where_builder/version"
|
3
3
|
|
4
4
|
module WhereBuilder
|
5
|
+
|
6
|
+
=begin
|
7
|
+
use this tool can build a where sentence for sql, it's can ignore a condition when it's para is nil or black string.
|
8
|
+
my purpose is not check nil for every condition, don't repeat so much if else.
|
9
|
+
=end
|
5
10
|
class WhereBuilder
|
6
11
|
|
7
|
-
|
8
|
-
|
12
|
+
=begin
|
13
|
+
add first condition on 'where ..' or '(..)', you can use AND(..) or OR(..), it's ok, just ugly
|
14
|
+
cond_str: like 'a.name=?', 'a.id=b.user_id'
|
15
|
+
para: like 'leo', 123, nil
|
16
|
+
|
17
|
+
return : fn
|
18
|
+
=end
|
19
|
+
def add(cond_str, para=nil)
|
20
|
+
if (para == nil or para.to_s.strip.size == 0) and cond_str.include?('?')
|
9
21
|
return
|
10
22
|
end
|
11
23
|
#TODO in and not in
|
12
24
|
return lambda{return cond_str, para}
|
13
25
|
end
|
14
26
|
|
27
|
+
=begin
|
28
|
+
add a 'and' condition on 'where ..' or '(..)',
|
29
|
+
cond_str: like 'a.name=?', 'a.id=b.user_id' or nil, if it's nil, just append a ' AND ' str,
|
30
|
+
para: like 'leo', 123, nil
|
31
|
+
|
32
|
+
return : fn
|
33
|
+
=end
|
15
34
|
def AND(cond_str=nil, para=nil)
|
16
35
|
if cond_str == nil
|
17
36
|
return lambda{return " AND", nil}
|
18
37
|
end
|
19
|
-
if para == nil
|
38
|
+
if (para == nil or para.to_s.strip.size == 0) and cond_str.include?('?')
|
20
39
|
return
|
21
40
|
end
|
22
41
|
return lambda{return " AND #{cond_str}", para}
|
23
42
|
end
|
24
43
|
|
44
|
+
=begin
|
45
|
+
add a 'or' condition on 'where ..' or '(..)',
|
46
|
+
cond_str: like 'a.name=?', 'a.id=b.user_id' or nil, if it's nil, just append a ' OR ' str,
|
47
|
+
para: like 'leo', 123, nil
|
48
|
+
|
49
|
+
return : fn
|
50
|
+
=end
|
25
51
|
def OR(cond_str=nil, para=nil)
|
26
52
|
if cond_str == nil
|
27
53
|
return lambda{return " OR", nil}
|
28
54
|
end
|
29
|
-
if para == nil
|
55
|
+
if (para == nil or para.to_s.strip.size == 0) and cond_str.include?('?')
|
30
56
|
return
|
31
57
|
end
|
32
58
|
return lambda{return " OR #{cond_str}", para}
|
33
59
|
end
|
34
60
|
|
35
|
-
|
36
|
-
|
61
|
+
=begin
|
62
|
+
if you want add some condition with '()', use this method.
|
63
|
+
use like this :
|
64
|
+
f = WhereBuilder.new()
|
65
|
+
f.bracket(f.add(...), f.AND(...), f.AND(...))
|
66
|
+
|
67
|
+
return : fn
|
68
|
+
=end
|
69
|
+
def bracket(*args)
|
70
|
+
fn = _build_fn(*args)
|
37
71
|
return if fn == nil
|
38
72
|
para = fn.call
|
39
73
|
if para == nil || para.size == 0
|
@@ -42,7 +76,10 @@ module WhereBuilder
|
|
42
76
|
return lambda{return " (#{para[0]})", para[1]}
|
43
77
|
end
|
44
78
|
|
45
|
-
|
79
|
+
=begin
|
80
|
+
do not use this method if you do't want to fix bug or upgrade this tool.
|
81
|
+
=end
|
82
|
+
def _build_fn(*args)
|
46
83
|
cond_str = []
|
47
84
|
para_list = []
|
48
85
|
size = args.size
|
@@ -83,8 +120,24 @@ module WhereBuilder
|
|
83
120
|
return lambda{return " #{cond_str.join('')}", para_list}
|
84
121
|
end
|
85
122
|
|
123
|
+
=begin
|
124
|
+
this is the enter for this tool,
|
125
|
+
use it like this :
|
126
|
+
f = WhereBuilder.new()
|
127
|
+
|
128
|
+
string, para_list = f.build(
|
129
|
+
f.add(...),
|
130
|
+
f.AND(...),
|
131
|
+
f.AND(...),
|
132
|
+
f.bracket(
|
133
|
+
f.add(...),
|
134
|
+
f.OR(...))
|
135
|
+
f.OR(...))
|
136
|
+
|
137
|
+
return : [string, para_list]
|
138
|
+
=end
|
86
139
|
def build(*args)
|
87
|
-
fn =
|
140
|
+
fn = _build_fn(*args)
|
88
141
|
if fn == nil
|
89
142
|
return ''
|
90
143
|
end
|