widget_list 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +26 -2
- data/app/views/widget_list/_condition_fields.html.erb +0 -0
- data/app/views/widget_list/_ransack_fields.html.erb +0 -0
- data/app/views/widget_list/_ransack_widget_list_advanced_search.html.erb +0 -0
- data/app/views/widget_list/administration/_field_row.html.erb +11 -0
- data/app/views/widget_list/administration/_output.html.erb +856 -0
- data/app/views/widget_list/administration/_output_save.html.erb +2 -0
- data/app/views/widget_list/list_partials/_col.html.erb +0 -0
- data/app/views/widget_list/list_partials/_list_description.html.erb +0 -0
- data/app/views/widget_list/list_partials/_no_sort_column.html.erb +0 -0
- data/app/views/widget_list/list_partials/_pagination_jump_active.html.erb +0 -0
- data/app/views/widget_list/list_partials/_pagination_jump_unactive.html.erb +0 -0
- data/app/views/widget_list/list_partials/_pagination_next_active.html.erb +0 -0
- data/app/views/widget_list/list_partials/_pagination_next_disabled.html.erb +0 -0
- data/app/views/widget_list/list_partials/_pagination_previous_active.html.erb +0 -0
- data/app/views/widget_list/list_partials/_pagination_previous_disabled.html.erb +0 -0
- data/app/views/widget_list/list_partials/_pagination_wrapper.html.erb +0 -0
- data/app/views/widget_list/list_partials/_row.html.erb +0 -0
- data/app/views/widget_list/list_partials/_sequence.html.erb +0 -0
- data/app/views/widget_list/list_partials/_sort_column.html.erb +0 -0
- data/lib/extensions/action_controller_base.rb +33 -33
- data/lib/widget_list.rb +3664 -2783
- data/lib/widget_list/engine.rb +8 -8
- data/lib/widget_list/hash.rb +113 -113
- data/lib/widget_list/md5.rb +18 -18
- data/lib/widget_list/railtie.rb +42 -42
- data/lib/widget_list/sequel.rb +218 -218
- data/lib/widget_list/string.rb +41 -41
- data/lib/widget_list/tpl.rb +185 -185
- data/lib/widget_list/utils.rb +92 -92
- data/lib/widget_list/version.rb +3 -3
- data/lib/widget_list/widgets.rb +756 -756
- metadata +5 -2
data/lib/widget_list/engine.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
module WidgetList
|
2
|
-
class Engine < Rails::Engine
|
3
|
-
# auto wire
|
4
|
-
initializer 'widget_list.load_static_assets' do |app|
|
5
|
-
app.middleware.use ::ActionDispatch::Static, "#{root}/vendor"
|
6
|
-
end
|
7
|
-
end
|
8
|
-
end
|
1
|
+
module WidgetList
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
# auto wire
|
4
|
+
initializer 'widget_list.load_static_assets' do |app|
|
5
|
+
app.middleware.use ::ActionDispatch::Static, "#{root}/vendor"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
data/lib/widget_list/hash.rb
CHANGED
@@ -1,114 +1,114 @@
|
|
1
|
-
class Hash
|
2
|
-
|
3
|
-
def to_params
|
4
|
-
params = ''
|
5
|
-
stack = []
|
6
|
-
|
7
|
-
each do |k, v|
|
8
|
-
if v.is_a?(Hash)
|
9
|
-
stack << [k,v]
|
10
|
-
elsif v.is_a?(Array)
|
11
|
-
stack << [k,Hash.from_array(v)]
|
12
|
-
else
|
13
|
-
params << "#{k}=#{v}&"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
stack.each do |parent, hash|
|
18
|
-
hash.each do |k, v|
|
19
|
-
if v.is_a?(Hash)
|
20
|
-
stack << ["#{parent}[#{k}]", v]
|
21
|
-
else
|
22
|
-
params << "#{parent}[#{k}]=#{v}&"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
params.chop!
|
28
|
-
params
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.from_array(array = [])
|
32
|
-
h = Hash.new
|
33
|
-
array.size.times do |t|
|
34
|
-
h[t] = array[t]
|
35
|
-
end
|
36
|
-
h
|
37
|
-
end
|
38
|
-
|
39
|
-
# Merges self with another hash, recursively.
|
40
|
-
#
|
41
|
-
# This code was lovingly stolen from some random gem:
|
42
|
-
# http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
|
43
|
-
#
|
44
|
-
# Thanks to whoever made it.
|
45
|
-
|
46
|
-
def deep_merge(hash)
|
47
|
-
target = dup
|
48
|
-
|
49
|
-
hash.keys.each do |key|
|
50
|
-
if hash[key].is_a? Hash and self[key].is_a? Hash
|
51
|
-
target[key] = target[key].deep_merge(hash[key])
|
52
|
-
next
|
53
|
-
end
|
54
|
-
|
55
|
-
target[key] = hash[key]
|
56
|
-
end
|
57
|
-
|
58
|
-
target
|
59
|
-
end
|
60
|
-
|
61
|
-
|
62
|
-
# From: http://www.gemtacular.com/gemdocs/cerberus-0.2.2/doc/classes/Hash.html
|
63
|
-
# File lib/cerberus/utils.rb, line 42
|
64
|
-
|
65
|
-
def deep_merge!(second)
|
66
|
-
second.each_pair do |k,v|
|
67
|
-
if self[k].is_a?(Hash) and second[k].is_a?(Hash)
|
68
|
-
self[k].deep_merge!(second[k])
|
69
|
-
else
|
70
|
-
self[k] = second[k]
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
|
76
|
-
#-----------------
|
77
|
-
|
78
|
-
# cf. http://subtech.g.hatena.ne.jp/cho45/20061122
|
79
|
-
def deep_merge2(other)
|
80
|
-
deep_proc = Proc.new { |k, s, o|
|
81
|
-
if s.kind_of?(Hash) && o.kind_of?(Hash)
|
82
|
-
next s.merge(o, &deep_proc)
|
83
|
-
end
|
84
|
-
next o
|
85
|
-
}
|
86
|
-
merge(other, &deep_proc)
|
87
|
-
end
|
88
|
-
|
89
|
-
|
90
|
-
def deep_merge3(second)
|
91
|
-
|
92
|
-
# From: http://www.ruby-forum.com/topic/142809
|
93
|
-
# Author: Stefan Rusterholz
|
94
|
-
|
95
|
-
merger = proc { |key,v1,v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
96
|
-
self.merge(second, &merger)
|
97
|
-
|
98
|
-
end
|
99
|
-
|
100
|
-
|
101
|
-
def keep_merge(hash)
|
102
|
-
target = dup
|
103
|
-
hash.keys.each do |key|
|
104
|
-
if hash[key].is_a? Hash and self[key].is_a? Hash
|
105
|
-
target[key] = target[key].keep_merge(hash[key])
|
106
|
-
next
|
107
|
-
end
|
108
|
-
#target[key] = hash[key]
|
109
|
-
target.update(hash) { |key, *values| values.flatten.uniq }
|
110
|
-
end
|
111
|
-
target
|
112
|
-
end
|
113
|
-
|
1
|
+
class Hash
|
2
|
+
|
3
|
+
def to_params
|
4
|
+
params = ''
|
5
|
+
stack = []
|
6
|
+
|
7
|
+
each do |k, v|
|
8
|
+
if v.is_a?(Hash)
|
9
|
+
stack << [k,v]
|
10
|
+
elsif v.is_a?(Array)
|
11
|
+
stack << [k,Hash.from_array(v)]
|
12
|
+
else
|
13
|
+
params << "#{k}=#{v}&"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
stack.each do |parent, hash|
|
18
|
+
hash.each do |k, v|
|
19
|
+
if v.is_a?(Hash)
|
20
|
+
stack << ["#{parent}[#{k}]", v]
|
21
|
+
else
|
22
|
+
params << "#{parent}[#{k}]=#{v}&"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
params.chop!
|
28
|
+
params
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.from_array(array = [])
|
32
|
+
h = Hash.new
|
33
|
+
array.size.times do |t|
|
34
|
+
h[t] = array[t]
|
35
|
+
end
|
36
|
+
h
|
37
|
+
end
|
38
|
+
|
39
|
+
# Merges self with another hash, recursively.
|
40
|
+
#
|
41
|
+
# This code was lovingly stolen from some random gem:
|
42
|
+
# http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
|
43
|
+
#
|
44
|
+
# Thanks to whoever made it.
|
45
|
+
|
46
|
+
def deep_merge(hash)
|
47
|
+
target = dup
|
48
|
+
|
49
|
+
hash.keys.each do |key|
|
50
|
+
if hash[key].is_a? Hash and self[key].is_a? Hash
|
51
|
+
target[key] = target[key].deep_merge(hash[key])
|
52
|
+
next
|
53
|
+
end
|
54
|
+
|
55
|
+
target[key] = hash[key]
|
56
|
+
end
|
57
|
+
|
58
|
+
target
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
# From: http://www.gemtacular.com/gemdocs/cerberus-0.2.2/doc/classes/Hash.html
|
63
|
+
# File lib/cerberus/utils.rb, line 42
|
64
|
+
|
65
|
+
def deep_merge!(second)
|
66
|
+
second.each_pair do |k,v|
|
67
|
+
if self[k].is_a?(Hash) and second[k].is_a?(Hash)
|
68
|
+
self[k].deep_merge!(second[k])
|
69
|
+
else
|
70
|
+
self[k] = second[k]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
#-----------------
|
77
|
+
|
78
|
+
# cf. http://subtech.g.hatena.ne.jp/cho45/20061122
|
79
|
+
def deep_merge2(other)
|
80
|
+
deep_proc = Proc.new { |k, s, o|
|
81
|
+
if s.kind_of?(Hash) && o.kind_of?(Hash)
|
82
|
+
next s.merge(o, &deep_proc)
|
83
|
+
end
|
84
|
+
next o
|
85
|
+
}
|
86
|
+
merge(other, &deep_proc)
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
def deep_merge3(second)
|
91
|
+
|
92
|
+
# From: http://www.ruby-forum.com/topic/142809
|
93
|
+
# Author: Stefan Rusterholz
|
94
|
+
|
95
|
+
merger = proc { |key,v1,v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
96
|
+
self.merge(second, &merger)
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
def keep_merge(hash)
|
102
|
+
target = dup
|
103
|
+
hash.keys.each do |key|
|
104
|
+
if hash[key].is_a? Hash and self[key].is_a? Hash
|
105
|
+
target[key] = target[key].keep_merge(hash[key])
|
106
|
+
next
|
107
|
+
end
|
108
|
+
#target[key] = hash[key]
|
109
|
+
target.update(hash) { |key, *values| values.flatten.uniq }
|
110
|
+
end
|
111
|
+
target
|
112
|
+
end
|
113
|
+
|
114
114
|
end
|
data/lib/widget_list/md5.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
require 'digest/md5'
|
2
|
-
|
3
|
-
class Object
|
4
|
-
def md5key
|
5
|
-
to_s
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
class Array
|
10
|
-
def md5key
|
11
|
-
map(&:md5key).join
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
class Hash
|
16
|
-
def md5key
|
17
|
-
sort.map(&:md5key).join
|
18
|
-
end
|
1
|
+
require 'digest/md5'
|
2
|
+
|
3
|
+
class Object
|
4
|
+
def md5key
|
5
|
+
to_s
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Array
|
10
|
+
def md5key
|
11
|
+
map(&:md5key).join
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Hash
|
16
|
+
def md5key
|
17
|
+
sort.map(&:md5key).join
|
18
|
+
end
|
19
19
|
end
|
data/lib/widget_list/railtie.rb
CHANGED
@@ -1,42 +1,42 @@
|
|
1
|
-
module WidgetList
|
2
|
-
class Railtie < Rails::Railtie
|
3
|
-
|
4
|
-
config.before_configuration do
|
5
|
-
config_file = Rails.root.join("config", "widget-list.yml")
|
6
|
-
if config_file.file?
|
7
|
-
# WidgetList::List::connect
|
8
|
-
else
|
9
|
-
puts "\nWidget List config not found. Creating config/widget-list.yml. \n\nPlease configure it with the appropriate connections"
|
10
|
-
File.open(Rails.root.join("config", "widget-list.yml"), 'w') { |file|
|
11
|
-
file.write("#For connection examples see: http://sequel.rubyforge.org/rdoc/files/doc/opening_databases_rdoc.html\n\n")
|
12
|
-
file.write("development:\n")
|
13
|
-
file.write(" :primary: sqlite:/\n")
|
14
|
-
file.write(" :secondary: sqlite:/\n")
|
15
|
-
file.write("\n")
|
16
|
-
file.write("test:\n")
|
17
|
-
file.write(" :primary: sqlite:/\n")
|
18
|
-
file.write(" :secondary: sqlite:/\n")
|
19
|
-
file.write("\n")
|
20
|
-
file.write("release:\n")
|
21
|
-
file.write(" :primary: sqlite:/\n")
|
22
|
-
file.write(" :secondary: sqlite:/\n")
|
23
|
-
file.write("\n")
|
24
|
-
file.write("production:\n")
|
25
|
-
file.write(" :primary: sqlite:/\n")
|
26
|
-
file.write(" :secondary: sqlite:/\n")
|
27
|
-
}
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
initializer "Include widget_list" do
|
32
|
-
ActiveSupport.on_load(:action_controller) do
|
33
|
-
if WidgetList::List::is_sequel(true) || WidgetList::List::is_sequel(false)
|
34
|
-
require 'sequel'
|
35
|
-
end
|
36
|
-
require 'widget_list/sequel'
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
|
41
|
-
end
|
42
|
-
end
|
1
|
+
module WidgetList
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
|
4
|
+
config.before_configuration do
|
5
|
+
config_file = Rails.root.join("config", "widget-list.yml")
|
6
|
+
if config_file.file?
|
7
|
+
# WidgetList::List::connect
|
8
|
+
else
|
9
|
+
puts "\nWidget List config not found. Creating config/widget-list.yml. \n\nPlease configure it with the appropriate connections"
|
10
|
+
File.open(Rails.root.join("config", "widget-list.yml"), 'w') { |file|
|
11
|
+
file.write("#For connection examples see: http://sequel.rubyforge.org/rdoc/files/doc/opening_databases_rdoc.html\n\n")
|
12
|
+
file.write("development:\n")
|
13
|
+
file.write(" :primary: sqlite:/\n")
|
14
|
+
file.write(" :secondary: sqlite:/\n")
|
15
|
+
file.write("\n")
|
16
|
+
file.write("test:\n")
|
17
|
+
file.write(" :primary: sqlite:/\n")
|
18
|
+
file.write(" :secondary: sqlite:/\n")
|
19
|
+
file.write("\n")
|
20
|
+
file.write("release:\n")
|
21
|
+
file.write(" :primary: sqlite:/\n")
|
22
|
+
file.write(" :secondary: sqlite:/\n")
|
23
|
+
file.write("\n")
|
24
|
+
file.write("production:\n")
|
25
|
+
file.write(" :primary: sqlite:/\n")
|
26
|
+
file.write(" :secondary: sqlite:/\n")
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
initializer "Include widget_list" do
|
32
|
+
ActiveSupport.on_load(:action_controller) do
|
33
|
+
if WidgetList::List::is_sequel(true) || WidgetList::List::is_sequel(false)
|
34
|
+
require 'sequel'
|
35
|
+
end
|
36
|
+
require 'widget_list/sequel'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
data/lib/widget_list/sequel.rb
CHANGED
@@ -1,219 +1,219 @@
|
|
1
|
-
module Sequel
|
2
|
-
class Database
|
3
|
-
@final_results = {}
|
4
|
-
attr_accessor :final_results
|
5
|
-
|
6
|
-
@errors = false
|
7
|
-
attr_accessor :errors
|
8
|
-
|
9
|
-
@last_error = ''
|
10
|
-
attr_accessor :last_error
|
11
|
-
|
12
|
-
@db_type = ''
|
13
|
-
attr_accessor :db_type
|
14
|
-
|
15
|
-
@final_count = 0
|
16
|
-
attr_accessor :final_count
|
17
|
-
|
18
|
-
@last_sql = ''
|
19
|
-
attr_accessor :last_sql
|
20
|
-
|
21
|
-
def _convert_bind(bind=[])
|
22
|
-
parameters = ''
|
23
|
-
unless bind.empty?
|
24
|
-
all = []
|
25
|
-
(bind||{}).each { |v|
|
26
|
-
if v.class.name.downcase == 'string'
|
27
|
-
all << "'" + v.gsub(/'/,"\\\\'") + "'"
|
28
|
-
else
|
29
|
-
all << v
|
30
|
-
end
|
31
|
-
}
|
32
|
-
parameters = "," + all.join(' ,')
|
33
|
-
end
|
34
|
-
parameters
|
35
|
-
end
|
36
|
-
|
37
|
-
def _convert_active_record_bind(sql='',bind=[])
|
38
|
-
unless bind.empty?
|
39
|
-
(bind||{}).each { |v|
|
40
|
-
sql.sub!(/\?/,v.to_s)
|
41
|
-
}
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
# _exec, pass a block and iterate the total rows
|
46
|
-
#
|
47
|
-
# example
|
48
|
-
#
|
49
|
-
=begin
|
50
|
-
$DATABASE._exec {|i|
|
51
|
-
asdf = "#{@final_results['NAME'][i]}"
|
52
|
-
}
|
53
|
-
=end
|
54
|
-
#
|
55
|
-
# Alternatively you could
|
56
|
-
=begin
|
57
|
-
@final_results['ID'].each_with_index { |id,k|
|
58
|
-
name = @final_results['NAME'][k]
|
59
|
-
price = @final_results['PRICE'][k]
|
60
|
-
}
|
61
|
-
=end
|
62
|
-
|
63
|
-
def _exec
|
64
|
-
if block_given?
|
65
|
-
@final_count.times {|i|
|
66
|
-
yield i
|
67
|
-
}
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def _determine_type(sql_or_obj)
|
72
|
-
if sql_or_obj.class.name.downcase != 'string' && sql_or_obj.class.name.to_s.split('::').last.downcase == 'dataset'
|
73
|
-
sql = sql_or_obj.get_sql()
|
74
|
-
elsif sql_or_obj.class.name.downcase == 'string'
|
75
|
-
sql = sql_or_obj
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
# probably not needed really
|
80
|
-
def _update(sql_or_obj,bind={})
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
# @param [Object] replace_in_query
|
85
|
-
def _bind(sql='',replace_in_query={})
|
86
|
-
if !replace_in_query.empty? && replace_in_query.class.name == 'Hash'
|
87
|
-
tmp = {}
|
88
|
-
replace_in_query.each { |k, v|
|
89
|
-
new_key = ':' + k.to_s
|
90
|
-
tmp[ new_key ] = v
|
91
|
-
}
|
92
|
-
sql = WidgetList::Utils::fill(tmp, sql)
|
93
|
-
end
|
94
|
-
sql
|
95
|
-
end
|
96
|
-
|
97
|
-
def _get_row_value(row,fieldName)
|
98
|
-
row.send(fieldName).to_s
|
99
|
-
end
|
100
|
-
|
101
|
-
# @param [Object or String] sql_or_obj
|
102
|
-
# will either take raw SQL or a Sequel object
|
103
|
-
# @param [Array] bind
|
104
|
-
# will be replacements for ? ? in your query. Must be in sequence
|
105
|
-
# @param [Hash] replace_in_query
|
106
|
-
# will be a traditional php bind hash {'BIND'=>'value'}. which will replace :BIND in the query. thanks mwild
|
107
|
-
|
108
|
-
def _select(sql_or_obj, bind=[], replace_in_query={}, active_record_model=false)
|
109
|
-
# supporting either
|
110
|
-
# if get_database._select('select * from items where name = ? AND price > ?', ['abc', 37]) > 0
|
111
|
-
# or
|
112
|
-
# if get_database._select(get_database[:items].filter(:name => 'abc')) > 0
|
113
|
-
#
|
114
|
-
sql = ''
|
115
|
-
sql = _determine_type(sql_or_obj)
|
116
|
-
|
117
|
-
if self.class.name != 'WidgetListActiveRecord'
|
118
|
-
|
119
|
-
# build csv of bind to eval below (arguments need to be like this for raw SQL passed with bind in Sequel)
|
120
|
-
#
|
121
|
-
parameters = _convert_bind(bind)
|
122
|
-
|
123
|
-
# escape anything incoming in raw SQL such as bound items to create the ruby string to pass
|
124
|
-
#
|
125
|
-
sql.gsub!(/'/,"\\\\'")
|
126
|
-
else
|
127
|
-
|
128
|
-
_convert_active_record_bind(sql, bind)
|
129
|
-
|
130
|
-
end
|
131
|
-
|
132
|
-
sql = _bind(sql,replace_in_query)
|
133
|
-
|
134
|
-
# build rows array['COLUMN'][0] = 1234;
|
135
|
-
#
|
136
|
-
first = 1
|
137
|
-
cnt = 0
|
138
|
-
@final_results = {}
|
139
|
-
if Rails.env == 'development'
|
140
|
-
Rails.logger.info(sql)
|
141
|
-
end
|
142
|
-
|
143
|
-
if self.class.name == 'WidgetListActiveRecord'
|
144
|
-
begin
|
145
|
-
results = active_record_model.find_by_sql(sql)
|
146
|
-
(results||[]).each { |row|
|
147
|
-
cnt += 1
|
148
|
-
row.attributes.keys.each { |fieldName|
|
149
|
-
if first == 1
|
150
|
-
@final_results[fieldName.to_s.upcase] = []
|
151
|
-
end
|
152
|
-
@final_results[fieldName.to_s.upcase] << ((row.send(fieldName).nil? && row.attributes[fieldName].nil?) ? '' : _get_row_value(row,fieldName))
|
153
|
-
}
|
154
|
-
first = 0
|
155
|
-
}
|
156
|
-
@last_sql = sql_or_obj
|
157
|
-
|
158
|
-
rescue Exception => e
|
159
|
-
cnt = 0
|
160
|
-
Rails.logger.info(e)
|
161
|
-
@errors = true
|
162
|
-
@last_error = e.to_s
|
163
|
-
@last_sql = sql_or_obj
|
164
|
-
end
|
165
|
-
else
|
166
|
-
eval("
|
167
|
-
begin
|
168
|
-
@errors = false
|
169
|
-
self['" + sql + "' " + parameters + "].each { |row|
|
170
|
-
cnt += 1
|
171
|
-
row.each { |k,v|
|
172
|
-
if first == 1
|
173
|
-
@final_results[k.to_s.upcase] = []
|
174
|
-
end
|
175
|
-
@final_results[k.to_s.upcase] << v
|
176
|
-
}
|
177
|
-
first = 0
|
178
|
-
}
|
179
|
-
@last_sql = self['" + sql + "' " + parameters + "].get_sql
|
180
|
-
rescue Exception => e
|
181
|
-
Rails.logger.info(e)
|
182
|
-
@errors = true
|
183
|
-
@last_error = e.to_s
|
184
|
-
@last_sql = '" + sql + "' + \"\n\n\n\" + ' With Bind => ' + bind.inspect + ' And BindLegacy => ' + replace_in_query.inspect
|
185
|
-
end
|
186
|
-
")
|
187
|
-
end
|
188
|
-
@final_count = cnt
|
189
|
-
return cnt
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
class Dataset
|
194
|
-
def get_sql
|
195
|
-
select_sql()
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
|
200
|
-
|
201
|
-
class WidgetListActiveRecord < Sequel::Database
|
202
|
-
@final_results = {}
|
203
|
-
attr_accessor :final_results
|
204
|
-
|
205
|
-
@errors = false
|
206
|
-
attr_accessor :errors
|
207
|
-
|
208
|
-
@last_error = ''
|
209
|
-
attr_accessor :last_error
|
210
|
-
|
211
|
-
@db_type = ''
|
212
|
-
attr_accessor :db_type
|
213
|
-
|
214
|
-
@final_count = 0
|
215
|
-
attr_accessor :final_count
|
216
|
-
|
217
|
-
@last_sql = ''
|
218
|
-
attr_accessor :last_sql
|
1
|
+
module Sequel
|
2
|
+
class Database
|
3
|
+
@final_results = {}
|
4
|
+
attr_accessor :final_results
|
5
|
+
|
6
|
+
@errors = false
|
7
|
+
attr_accessor :errors
|
8
|
+
|
9
|
+
@last_error = ''
|
10
|
+
attr_accessor :last_error
|
11
|
+
|
12
|
+
@db_type = ''
|
13
|
+
attr_accessor :db_type
|
14
|
+
|
15
|
+
@final_count = 0
|
16
|
+
attr_accessor :final_count
|
17
|
+
|
18
|
+
@last_sql = ''
|
19
|
+
attr_accessor :last_sql
|
20
|
+
|
21
|
+
def _convert_bind(bind=[])
|
22
|
+
parameters = ''
|
23
|
+
unless bind.empty?
|
24
|
+
all = []
|
25
|
+
(bind||{}).each { |v|
|
26
|
+
if v.class.name.downcase == 'string'
|
27
|
+
all << "'" + v.gsub(/'/,"\\\\'") + "'"
|
28
|
+
else
|
29
|
+
all << v
|
30
|
+
end
|
31
|
+
}
|
32
|
+
parameters = "," + all.join(' ,')
|
33
|
+
end
|
34
|
+
parameters
|
35
|
+
end
|
36
|
+
|
37
|
+
def _convert_active_record_bind(sql='',bind=[])
|
38
|
+
unless bind.empty?
|
39
|
+
(bind||{}).each { |v|
|
40
|
+
sql.sub!(/\?/,v.to_s)
|
41
|
+
}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# _exec, pass a block and iterate the total rows
|
46
|
+
#
|
47
|
+
# example
|
48
|
+
#
|
49
|
+
=begin
|
50
|
+
$DATABASE._exec {|i|
|
51
|
+
asdf = "#{@final_results['NAME'][i]}"
|
52
|
+
}
|
53
|
+
=end
|
54
|
+
#
|
55
|
+
# Alternatively you could
|
56
|
+
=begin
|
57
|
+
@final_results['ID'].each_with_index { |id,k|
|
58
|
+
name = @final_results['NAME'][k]
|
59
|
+
price = @final_results['PRICE'][k]
|
60
|
+
}
|
61
|
+
=end
|
62
|
+
|
63
|
+
def _exec
|
64
|
+
if block_given?
|
65
|
+
@final_count.times {|i|
|
66
|
+
yield i
|
67
|
+
}
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def _determine_type(sql_or_obj)
|
72
|
+
if sql_or_obj.class.name.downcase != 'string' && sql_or_obj.class.name.to_s.split('::').last.downcase == 'dataset'
|
73
|
+
sql = sql_or_obj.get_sql()
|
74
|
+
elsif sql_or_obj.class.name.downcase == 'string'
|
75
|
+
sql = sql_or_obj
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# probably not needed really
|
80
|
+
def _update(sql_or_obj,bind={})
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
# @param [Object] replace_in_query
|
85
|
+
def _bind(sql='',replace_in_query={})
|
86
|
+
if !replace_in_query.empty? && replace_in_query.class.name == 'Hash'
|
87
|
+
tmp = {}
|
88
|
+
replace_in_query.each { |k, v|
|
89
|
+
new_key = ':' + k.to_s
|
90
|
+
tmp[ new_key ] = v
|
91
|
+
}
|
92
|
+
sql = WidgetList::Utils::fill(tmp, sql)
|
93
|
+
end
|
94
|
+
sql
|
95
|
+
end
|
96
|
+
|
97
|
+
def _get_row_value(row,fieldName)
|
98
|
+
row.send(fieldName).to_s
|
99
|
+
end
|
100
|
+
|
101
|
+
# @param [Object or String] sql_or_obj
|
102
|
+
# will either take raw SQL or a Sequel object
|
103
|
+
# @param [Array] bind
|
104
|
+
# will be replacements for ? ? in your query. Must be in sequence
|
105
|
+
# @param [Hash] replace_in_query
|
106
|
+
# will be a traditional php bind hash {'BIND'=>'value'}. which will replace :BIND in the query. thanks mwild
|
107
|
+
|
108
|
+
def _select(sql_or_obj, bind=[], replace_in_query={}, active_record_model=false)
|
109
|
+
# supporting either
|
110
|
+
# if get_database._select('select * from items where name = ? AND price > ?', ['abc', 37]) > 0
|
111
|
+
# or
|
112
|
+
# if get_database._select(get_database[:items].filter(:name => 'abc')) > 0
|
113
|
+
#
|
114
|
+
sql = ''
|
115
|
+
sql = _determine_type(sql_or_obj)
|
116
|
+
|
117
|
+
if self.class.name != 'WidgetListActiveRecord'
|
118
|
+
|
119
|
+
# build csv of bind to eval below (arguments need to be like this for raw SQL passed with bind in Sequel)
|
120
|
+
#
|
121
|
+
parameters = _convert_bind(bind)
|
122
|
+
|
123
|
+
# escape anything incoming in raw SQL such as bound items to create the ruby string to pass
|
124
|
+
#
|
125
|
+
sql.gsub!(/'/,"\\\\'")
|
126
|
+
else
|
127
|
+
|
128
|
+
_convert_active_record_bind(sql, bind)
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
sql = _bind(sql,replace_in_query)
|
133
|
+
|
134
|
+
# build rows array['COLUMN'][0] = 1234;
|
135
|
+
#
|
136
|
+
first = 1
|
137
|
+
cnt = 0
|
138
|
+
@final_results = {}
|
139
|
+
if Rails.env == 'development'
|
140
|
+
Rails.logger.info(sql)
|
141
|
+
end
|
142
|
+
|
143
|
+
if self.class.name == 'WidgetListActiveRecord'
|
144
|
+
begin
|
145
|
+
results = active_record_model.find_by_sql(sql)
|
146
|
+
(results||[]).each { |row|
|
147
|
+
cnt += 1
|
148
|
+
row.attributes.keys.each { |fieldName|
|
149
|
+
if first == 1
|
150
|
+
@final_results[fieldName.to_s.upcase] = []
|
151
|
+
end
|
152
|
+
@final_results[fieldName.to_s.upcase] << ((row.send(fieldName).nil? && row.attributes[fieldName].nil?) ? '' : _get_row_value(row,fieldName))
|
153
|
+
}
|
154
|
+
first = 0
|
155
|
+
}
|
156
|
+
@last_sql = sql_or_obj
|
157
|
+
|
158
|
+
rescue Exception => e
|
159
|
+
cnt = 0
|
160
|
+
Rails.logger.info(e)
|
161
|
+
@errors = true
|
162
|
+
@last_error = e.to_s
|
163
|
+
@last_sql = sql_or_obj
|
164
|
+
end
|
165
|
+
else
|
166
|
+
eval("
|
167
|
+
begin
|
168
|
+
@errors = false
|
169
|
+
self['" + sql + "' " + parameters + "].each { |row|
|
170
|
+
cnt += 1
|
171
|
+
row.each { |k,v|
|
172
|
+
if first == 1
|
173
|
+
@final_results[k.to_s.upcase] = []
|
174
|
+
end
|
175
|
+
@final_results[k.to_s.upcase] << v
|
176
|
+
}
|
177
|
+
first = 0
|
178
|
+
}
|
179
|
+
@last_sql = self['" + sql + "' " + parameters + "].get_sql
|
180
|
+
rescue Exception => e
|
181
|
+
Rails.logger.info(e)
|
182
|
+
@errors = true
|
183
|
+
@last_error = e.to_s
|
184
|
+
@last_sql = '" + sql + "' + \"\n\n\n\" + ' With Bind => ' + bind.inspect + ' And BindLegacy => ' + replace_in_query.inspect
|
185
|
+
end
|
186
|
+
")
|
187
|
+
end
|
188
|
+
@final_count = cnt
|
189
|
+
return cnt
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
class Dataset
|
194
|
+
def get_sql
|
195
|
+
select_sql()
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
|
201
|
+
class WidgetListActiveRecord < Sequel::Database
|
202
|
+
@final_results = {}
|
203
|
+
attr_accessor :final_results
|
204
|
+
|
205
|
+
@errors = false
|
206
|
+
attr_accessor :errors
|
207
|
+
|
208
|
+
@last_error = ''
|
209
|
+
attr_accessor :last_error
|
210
|
+
|
211
|
+
@db_type = ''
|
212
|
+
attr_accessor :db_type
|
213
|
+
|
214
|
+
@final_count = 0
|
215
|
+
attr_accessor :final_count
|
216
|
+
|
217
|
+
@last_sql = ''
|
218
|
+
attr_accessor :last_sql
|
219
219
|
end
|