where_builder 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .DS_Store
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
+
1
2
  # WhereBuilder
2
3
 
3
- TODO: Write a gem description
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
+
@@ -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
- def add(cond_str, para)
8
- if para == nil || para.to_s.strip.size == 0
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 || para.to_s.strip.size == 0
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 || para.to_s.strip.size == 0
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
- def bracket(*args2)
36
- fn = build_fn(*args2)
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
- def build_fn(*args)
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 = build_fn(*args)
140
+ fn = _build_fn(*args)
88
141
  if fn == nil
89
142
  return ''
90
143
  end
@@ -1,3 +1,3 @@
1
1
  module WhereBuilder
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: where_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: