dynamic_query 0.7.1 → 0.7.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.
@@ -2,71 +2,116 @@
|
|
2
2
|
|
3
3
|
<center id="combined_result">
|
4
4
|
|
5
|
-
<table id="combined_result_table" style="text-align: center">
|
6
|
-
<% if completed_row %>
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
<%= completed_row.map{ |rec|
|
13
|
-
rec.attributes.map { |k, _| k } }.flatten.map { |col| "<th>#{col}</th>" }.join.html_safe %>
|
14
|
-
</tr>
|
15
|
-
<% end %>
|
16
|
-
<% result.each do |row| %>
|
17
|
-
<tr>
|
18
|
-
<% row.map { |rec| rec.attributes.map { |k ,v| v } }.flatten.each do |col| %>
|
19
|
-
<td><%= col %></td>
|
20
|
-
<% end %>
|
21
|
-
</tr>
|
5
|
+
<table id="combined_result_table" style="text-align: center">
|
6
|
+
<% if completed_row %>
|
7
|
+
<tr>
|
8
|
+
<%= completed_row.map { |rec|
|
9
|
+
rec.attributes.map { |k, _| "<th>#{k}</th>" }
|
10
|
+
}.flatten.join.html_safe %>
|
11
|
+
</tr>
|
22
12
|
<% end %>
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
13
|
+
<% result.each do |row| %>
|
14
|
+
<tr>
|
15
|
+
<% row.map { |rec| rec.attributes.map { |k ,v| v } }.flatten.each do |col| %>
|
16
|
+
<td><%= col %></td>
|
17
|
+
<% end %>
|
18
|
+
</tr>
|
19
|
+
<% end %>
|
20
|
+
</table>
|
21
|
+
|
22
|
+
<button id="table2csv">Download CSV</button>
|
23
|
+
|
24
|
+
<script>
|
25
|
+
$( function() {
|
26
|
+
if ( sessionStorage.getItem( 'toggle_columns' ) == null ) {
|
27
|
+
sessionStorage.setItem( 'toggle_columns', '[]' )
|
28
|
+
}
|
29
|
+
console.log( sessionStorage.getItem( 'toggle_columns' ) );
|
30
|
+
var hide_ary = jQuery.parseJSON( sessionStorage.getItem( 'toggle_columns' ) );
|
31
|
+
|
32
|
+
$.each( hide_ary, function( index, value ) {
|
33
|
+
var header = $("#combined_result_table tr > th:eq(" + value + ")");
|
34
|
+
header.attr( 'title', header.text() );
|
35
|
+
header.text( '●' );
|
36
|
+
$('#combined_result_table tr').find( "td:eq(" + value +")" ).each( function() {
|
37
|
+
$( this ).attr( 'title', $( this ).text() );
|
38
|
+
$( this ).text( '' );
|
39
|
+
});
|
40
|
+
});
|
41
|
+
|
42
|
+
$( '#combined_result_table th' ).each( function( index ) {
|
43
|
+
$( this ).on( 'click', function() {
|
44
|
+
var header = $("#combined_result_table tr > th:eq(" + index + ")");
|
45
|
+
if ( header.text() == '●' ) {
|
46
|
+
var hide_ary = jQuery.parseJSON( sessionStorage.getItem( 'toggle_columns' ) );
|
47
|
+
hide_ary.splice( hide_ary.indexOf(index), 1 );
|
48
|
+
sessionStorage.setItem( 'toggle_columns', JSON.stringify( hide_ary ) )
|
49
|
+
|
50
|
+
header.text( header.attr( 'title' ) );
|
51
|
+
header.attr( 'title', '' );
|
52
|
+
$('#combined_result_table tr').find( "td:eq(" + index +")" ).each( function() {
|
53
|
+
$( this ).text( $( this ).attr( 'title' ) );
|
54
|
+
$( this ).attr( 'title', '' );
|
55
|
+
});
|
56
|
+
} else {
|
57
|
+
var hide_ary = jQuery.parseJSON( sessionStorage.getItem( 'toggle_columns' ) );
|
58
|
+
hide_ary.push( index );
|
59
|
+
sessionStorage.setItem( 'toggle_columns', JSON.stringify( hide_ary ) )
|
60
|
+
|
61
|
+
header.attr( 'title', header.text() );
|
62
|
+
header.text( '●' );
|
63
|
+
$('#combined_result_table tr').find( "td:eq(" + index +")" ).each( function() {
|
64
|
+
$( this ).attr( 'title', $( this ).text() );
|
65
|
+
$( this ).text( '' );
|
66
|
+
});
|
67
|
+
}
|
68
|
+
});
|
69
|
+
});
|
70
|
+
});
|
71
|
+
|
72
|
+
var regulateCSVCell = function( cell ) {
|
73
|
+
if ( cell.indexOf( ',' ) == -1 ) {
|
74
|
+
return cell;
|
75
|
+
}
|
76
|
+
return '"' + cell + '"';
|
77
|
+
};
|
78
|
+
|
79
|
+
var ary2CSV = function( ary ) {
|
80
|
+
csv = '';
|
81
|
+
for ( var row in ary ) {
|
82
|
+
for ( var col in ary[row] ) {
|
83
|
+
if ( col == 0 ) {
|
84
|
+
csv += regulateCSVCell( ary[row][col] )
|
85
|
+
} else {
|
86
|
+
csv += ',' + regulateCSVCell( ary[row][col] )
|
87
|
+
}
|
43
88
|
}
|
89
|
+
csv += "\n"
|
44
90
|
}
|
45
|
-
csv
|
46
|
-
}
|
47
|
-
|
48
|
-
|
91
|
+
return csv;
|
92
|
+
};
|
93
|
+
|
94
|
+
var saveCSV = function( csv ) {
|
95
|
+
var href = 'data:application/csv;charset=utf-8,'
|
96
|
+
href += encodeURIComponent( csv );
|
97
|
+
window.open( href );
|
98
|
+
};
|
49
99
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
};
|
55
|
-
|
56
|
-
$( '#table2csv' ).on( 'click', function() {
|
57
|
-
var table = [];
|
58
|
-
$( '#combined_result_table tr' ).each( function( index ) {
|
59
|
-
if ( index > 0 ) {
|
100
|
+
$( '#table2csv' ).on( 'click', function() {
|
101
|
+
var hide_ary = jQuery.parseJSON( sessionStorage.getItem( 'toggle_columns' ) );
|
102
|
+
var table = [];
|
103
|
+
$( '#combined_result_table tr' ).each( function( index ) {
|
60
104
|
var row = [];
|
61
|
-
$( this ).children().each( function() {
|
62
|
-
|
105
|
+
$( this ).children().each( function( index ) {
|
106
|
+
if ( $.inArray( index, hide_ary ) == -1 ) {
|
107
|
+
row.push( $( this ).text() );
|
108
|
+
}
|
63
109
|
});
|
64
110
|
table.push( row );
|
65
|
-
}
|
111
|
+
});
|
112
|
+
|
113
|
+
saveCSV( ary2CSV( table ) );
|
66
114
|
});
|
67
|
-
|
68
|
-
saveCSV( ary2CSV( table ) );
|
69
|
-
});
|
70
|
-
</script>
|
115
|
+
</script>
|
71
116
|
|
72
117
|
</center>
|
@@ -1,4 +1,5 @@
|
|
1
|
-
<center>
|
1
|
+
<center id="dynamic_result">
|
2
|
+
|
2
3
|
<table id="dynamic_result_table" style="text-align: center">
|
3
4
|
<% if result.first %>
|
4
5
|
<tr>
|
@@ -22,7 +23,7 @@
|
|
22
23
|
console.log( sessionStorage.getItem( 'toggle_columns' ) );
|
23
24
|
var hide_ary = jQuery.parseJSON( sessionStorage.getItem( 'toggle_columns' ) );
|
24
25
|
|
25
|
-
$.each( hide_ary, function(index, value) {
|
26
|
+
$.each( hide_ary, function( index, value ) {
|
26
27
|
var header = $("#dynamic_result_table tr > th:eq(" + value + ")");
|
27
28
|
header.attr( 'title', header.text() );
|
28
29
|
header.text( '●' );
|
@@ -32,7 +33,7 @@
|
|
32
33
|
});
|
33
34
|
});
|
34
35
|
|
35
|
-
$( '#dynamic_result_table th' ).each( function(index) {
|
36
|
+
$( '#dynamic_result_table th' ).each( function( index ) {
|
36
37
|
$( this ).on( 'click', function() {
|
37
38
|
var header = $("#dynamic_result_table tr > th:eq(" + index + ")");
|
38
39
|
if ( header.text() == '●' ) {
|
@@ -106,4 +107,5 @@
|
|
106
107
|
saveCSV( ary2CSV( table ) );
|
107
108
|
});
|
108
109
|
</script>
|
110
|
+
|
109
111
|
</center>
|
data/lib/dynamic_query.rb
CHANGED
@@ -10,6 +10,7 @@ module DynamicQuery
|
|
10
10
|
'BETWEEN', 'NOT BETWEEN', 'IS NULL', 'IS NOT NULL']
|
11
11
|
|
12
12
|
def dynamic_query(*models, opt)
|
13
|
+
models.flatten!
|
13
14
|
DynamicQueryInstance.new(*models, opt)
|
14
15
|
end
|
15
16
|
|
@@ -51,6 +52,66 @@ module DynamicQuery
|
|
51
52
|
def all_columns_in(*models)
|
52
53
|
models.map { |m| m.columns.map { |col| "#{m.table_name}.#{col.name}" } }.flatten.join ', '
|
53
54
|
end
|
55
|
+
|
56
|
+
def estimate_total(dq, query)
|
57
|
+
models = dq.models
|
58
|
+
et = models.map { |m| { :table => m.table_name, :total => m.count, :conditioned => conditioned_count(dq, query, m) } }
|
59
|
+
major = et.max_by { |i| i[:total] }
|
60
|
+
et.delete major
|
61
|
+
estimate = major[:conditioned] * et.map { |i| i[:conditioned].to_f / i[:total] }.reduce(:*)
|
62
|
+
estimate = estimate.to_i
|
63
|
+
end
|
64
|
+
|
65
|
+
def conditioned_count(dq, query, model)
|
66
|
+
conditions = dq.conditions query, model.table_name
|
67
|
+
statement = Querier.statement conditions
|
68
|
+
model.where(statement).count
|
69
|
+
end
|
70
|
+
|
71
|
+
class Querier
|
72
|
+
STMT_SUFFIX_DICT = {
|
73
|
+
nil => '= ?',
|
74
|
+
'eq' => '= ?',
|
75
|
+
'not_eq' => '!= ?',
|
76
|
+
'gt' => '> ?',
|
77
|
+
'ge' => '>= ?',
|
78
|
+
'lt' => '< ?',
|
79
|
+
'le' => '<= ?',
|
80
|
+
'in' => 'IN (?)',
|
81
|
+
'not_in' => 'NOT IN (?)',
|
82
|
+
'like' => 'LIKE ?',
|
83
|
+
'not_like' => 'NOT LIKE ?',
|
84
|
+
'btw' => 'BETWEEN ? AND ?',
|
85
|
+
'not_btw' => 'NOT BETWEEN ? AND ?',
|
86
|
+
'between' => 'BETWEEN ? AND ?',
|
87
|
+
'not_between' => 'NOT BETWEEN ? AND ?',
|
88
|
+
'is' => 'IS ?',
|
89
|
+
'is_not' => 'IS NOT ?'
|
90
|
+
}
|
91
|
+
|
92
|
+
def self.statement(conditions)
|
93
|
+
conditions ||= []
|
94
|
+
statement = []
|
95
|
+
values = []
|
96
|
+
|
97
|
+
conditions.each do |conds|
|
98
|
+
and_conds = []
|
99
|
+
conds.each do |key, val|
|
100
|
+
col, op = key.to_s.split(/\./)
|
101
|
+
op = STMT_SUFFIX_DICT[op]
|
102
|
+
and_conds << "#{col} #{op}"
|
103
|
+
if ['BETWEEN ? AND ?', 'NOT BETWEEN ? AND ?'].include? op
|
104
|
+
values << val[0] << val[1]
|
105
|
+
else
|
106
|
+
values << val
|
107
|
+
end
|
108
|
+
end
|
109
|
+
statement << and_conds.join(' AND ')
|
110
|
+
end
|
111
|
+
|
112
|
+
values.unshift statement.map { |s| "(#{s})" }.join(' OR ')
|
113
|
+
end
|
114
|
+
end
|
54
115
|
|
55
116
|
class DynamicQueryInstance
|
56
117
|
include CombinedQuery, Validator
|
@@ -71,6 +132,8 @@ module DynamicQuery
|
|
71
132
|
opt = {}
|
72
133
|
end
|
73
134
|
|
135
|
+
@origin_models = models.dup
|
136
|
+
|
74
137
|
models.map! do |m|
|
75
138
|
if [String, Symbol].include? m.class
|
76
139
|
m.to_s.split(/_/).map { |word| word.capitalize }.join.constantize
|
@@ -149,6 +212,10 @@ module DynamicQuery
|
|
149
212
|
@aliases = used_aliases
|
150
213
|
end
|
151
214
|
|
215
|
+
def models
|
216
|
+
@origin_models
|
217
|
+
end
|
218
|
+
|
152
219
|
def panel(query)
|
153
220
|
query ||= { 'or_1' => { 'and_1' => { :column => '', :operator => '', :value1 => '', :value2 => '' } } }
|
154
221
|
query = query.clone
|
@@ -8,7 +8,7 @@ module DynamicQuery
|
|
8
8
|
copy_file 'app/helpers/dynamic_query_helper.rb', 'app/helpers/dynamic_query_helper.rb'
|
9
9
|
copy_file 'app/helpers/_dynamic_query.html.erb', 'app/helpers/_dynamic_query.html.erb'
|
10
10
|
copy_file 'app/helpers/_dynamic_result.html.erb', 'app/helpers/_dynamic_result.html.erb'
|
11
|
-
copy_file 'app/helpers/_combined_result.html.erb', 'app/helpers/_combined_result.html.erb'
|
11
|
+
#copy_file 'app/helpers/_combined_result.html.erb', 'app/helpers/_combined_result.html.erb'
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -191,7 +191,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
191
191
|
version: '0'
|
192
192
|
segments:
|
193
193
|
- 0
|
194
|
-
hash: -
|
194
|
+
hash: -309352636508812369
|
195
195
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
196
196
|
none: false
|
197
197
|
requirements:
|
@@ -200,11 +200,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
200
200
|
version: '0'
|
201
201
|
segments:
|
202
202
|
- 0
|
203
|
-
hash: -
|
203
|
+
hash: -309352636508812369
|
204
204
|
requirements: []
|
205
205
|
rubyforge_project:
|
206
206
|
rubygems_version: 1.8.25
|
207
207
|
signing_key:
|
208
208
|
specification_version: 3
|
209
|
-
summary: dynamic_query-0.7.
|
209
|
+
summary: dynamic_query-0.7.2
|
210
210
|
test_files: []
|