ar_pg_array 0.9.7 → 0.9.9
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/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/ar_pg_array/schema.rb +26 -14
- data/spec/fixtures/bulks.yml +2 -2
- data/spec/fixtures/schema.rb +2 -2
- data/spec/pg_array_spec.rb +13 -3
- metadata +11 -15
data/Rakefile
CHANGED
@@ -30,7 +30,7 @@ begin
|
|
30
30
|
gemspec.email = "funny.falcon@gmail.com"
|
31
31
|
gemspec.homepage = "http://github.com/funny-falcon/activerecord-postgresql-arrays"
|
32
32
|
gemspec.authors = ["Sokolov Yura aka funny_falcon"]
|
33
|
-
gemspec.add_dependency('
|
33
|
+
gemspec.add_dependency('active_record', '>= 2.3.5', '<3.1')
|
34
34
|
gemspec.rubyforge_project = 'ar-pg-array'
|
35
35
|
end
|
36
36
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.9
|
data/lib/ar_pg_array/schema.rb
CHANGED
@@ -2,7 +2,7 @@ module ActiveRecord
|
|
2
2
|
module ConnectionAdapters
|
3
3
|
class PostgreSQLColumn < Column #:nodoc:
|
4
4
|
BASE_TYPE_COLUMNS = Hash.new{|h, base_type|
|
5
|
-
base_column= new(nil, nil, base_type, true)
|
5
|
+
base_column= new(nil, nil, base_type.to_s, true)
|
6
6
|
h[base_type] = h[base_column.type]= base_column
|
7
7
|
}
|
8
8
|
attr_reader :base_column
|
@@ -98,9 +98,8 @@ module ActiveRecord
|
|
98
98
|
eval(string.tr('{}','[]'))
|
99
99
|
end
|
100
100
|
|
101
|
-
SARRAY_QUOTED = /^"(
|
101
|
+
SARRAY_QUOTED = /^"((?:\\.|[^\\])*)"$/m
|
102
102
|
SARRAY_PARTIAL = /^".*(\\"|[^"])$/m
|
103
|
-
ESCAPE_ARRAY = Hash.new{|h,k| h[k] = eval("\"#{k}\"") }
|
104
103
|
def self.string_to_text_array(value)
|
105
104
|
return value unless value.is_a? String
|
106
105
|
return nil if value.empty?
|
@@ -114,7 +113,7 @@ module ActiveRecord
|
|
114
113
|
partial = true
|
115
114
|
end
|
116
115
|
if s =~ SARRAY_QUOTED
|
117
|
-
s = $1.gsub(/\\(
|
116
|
+
s = $1.gsub(/\\(.)/,'\1')
|
118
117
|
partial = false
|
119
118
|
elsif s == 'NULL'
|
120
119
|
s = nil
|
@@ -140,7 +139,7 @@ module ActiveRecord
|
|
140
139
|
:string, :text, :other, :datetime, :timestamp, :time
|
141
140
|
quote_array_for_arel_by_base_type( value, base_type )
|
142
141
|
else
|
143
|
-
"
|
142
|
+
"'#{ prepare_pg_string_array(value, base_type, column) }'"
|
144
143
|
end
|
145
144
|
end
|
146
145
|
|
@@ -150,7 +149,8 @@ module ActiveRecord
|
|
150
149
|
"'#{ prepare_array_for_arel_by_base_type(value, base_type) }'"
|
151
150
|
when :string, :text, :other
|
152
151
|
pa = prepare_array_for_arel_by_base_type(value, base_type)
|
153
|
-
"
|
152
|
+
"'#{ quote_string( pa ) }'"
|
153
|
+
#"'#{ pa.gsub("'","''") }'"
|
154
154
|
else
|
155
155
|
raise "Unsupported array base type #{base_type} for arel"
|
156
156
|
end
|
@@ -164,6 +164,7 @@ module ActiveRecord
|
|
164
164
|
prepare_pg_float_array(value)
|
165
165
|
when :string, :text, :other
|
166
166
|
prepare_pg_text_array(value)
|
167
|
+
#prepare_pg_string_array(value, base_type).gsub("''","'")
|
167
168
|
when :datetime, :timestamp, :time
|
168
169
|
prepare_pg_string_array(value, base_type)
|
169
170
|
when :decimal, :boolean, :date, :safe
|
@@ -185,6 +186,7 @@ module ActiveRecord
|
|
185
186
|
"{#{ value.map{|v| v.nil? ? 'NULL' : v.to_s}.join(',')}}"
|
186
187
|
end
|
187
188
|
|
189
|
+
ESCAPE_HASH={'\\'=>'\\\\', '"'=>'\\"'}
|
188
190
|
def prepare_pg_string_array(value, base_type, column=nil)
|
189
191
|
base_column= if column
|
190
192
|
column.base_column
|
@@ -192,20 +194,30 @@ module ActiveRecord
|
|
192
194
|
PostgreSQLColumn::BASE_TYPE_COLUMNS[base_type.to_sym]
|
193
195
|
end
|
194
196
|
value = value.map do|v|
|
195
|
-
|
196
|
-
|
197
|
-
|
197
|
+
unless v.nil?
|
198
|
+
v = quote_without_postgresql_arrays(v, base_column)
|
199
|
+
if v=~/^'(.+)'$/m then
|
200
|
+
"\"#{$1.gsub(/\\|"/){|s| ESCAPE_HASH[s]}}\""
|
201
|
+
else
|
202
|
+
v
|
203
|
+
end
|
204
|
+
else
|
205
|
+
'NULL'
|
206
|
+
end
|
198
207
|
end
|
199
208
|
"{#{ value.join(',')}}"
|
200
209
|
end
|
201
|
-
|
210
|
+
|
211
|
+
class CNULL; def inspect; 'NULL'; end; alias to_s inspect end
|
212
|
+
NULL = CNULL.new
|
213
|
+
|
214
|
+
TESCAPE_HASH={'\\'=>'\\\\', '"'=>'\\"'}
|
202
215
|
def prepare_pg_text_array(value)
|
203
216
|
value = value.map{|v|
|
204
|
-
v ? v.to_s.gsub(
|
205
|
-
|
206
|
-
value
|
217
|
+
v ? "\"#{v.to_s.gsub(/\\|"/){|s| TESCAPE_HASH[s]}}\"" : NULL
|
218
|
+
}.join(',')
|
219
|
+
"{#{value}}"
|
207
220
|
end
|
208
|
-
|
209
221
|
|
210
222
|
NATIVE_DATABASE_TYPES.keys.each do |key|
|
211
223
|
unless key==:primary_key
|
data/spec/fixtures/bulks.yml
CHANGED
@@ -2,7 +2,7 @@ first:
|
|
2
2
|
id: 1
|
3
3
|
ints: [ 1 ]
|
4
4
|
strings: [ one ]
|
5
|
-
times: [
|
5
|
+
times: [2011-03-01, 2011-05-05]
|
6
6
|
floats: [1.0, 2.3]
|
7
7
|
decimals: [1.0, 2.3]
|
8
8
|
second:
|
@@ -29,7 +29,7 @@ fourth:
|
|
29
29
|
fifth:
|
30
30
|
id: 5
|
31
31
|
ints: [ 10 ]
|
32
|
-
strings: [ "#{1+1}", "\\#{1+1}\"'\\z", "\t\n" ]
|
32
|
+
strings: [ "#{1+1}", "\\#{1+1}\"'\\z\x01", "\t\n" ]
|
33
33
|
times: [ ]
|
34
34
|
floats: [ ]
|
35
35
|
decimals: [ ]
|
data/spec/fixtures/schema.rb
CHANGED
@@ -11,12 +11,12 @@ ActiveRecord::Schema.define do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
create_table "bulks", :force => true do |t|
|
14
|
-
t.string :value
|
14
|
+
t.string :value, :default => "'"
|
15
15
|
t.integer_array :ints, :default => [1, 2]
|
16
16
|
t.string_array :strings, :default => %w{as so}
|
17
17
|
t.timestamp_array :times, :default => %w{2010-01-01 2010-02-01}
|
18
18
|
t.float_array :floats, :default => [1.0, 1.2]
|
19
19
|
t.decimal_array :decimals, :default => [1.0, 1.2]
|
20
|
-
t.text_array :texts, :default => [nil, 'Text', 'NULL', 'Text with nil', 'Text with , nil, !', 'nil']
|
20
|
+
t.text_array :texts, :default => [nil, 'Text', 'NULL', 'Text with nil', 'Text with , nil, !"\\', 'nil']
|
21
21
|
end
|
22
22
|
end
|
data/spec/pg_array_spec.rb
CHANGED
@@ -67,7 +67,8 @@ describe "PgArray" do
|
|
67
67
|
bulk = Bulk.find(1)
|
68
68
|
bulk.ints.should == [ 1 ]
|
69
69
|
bulk.strings.should == %w{one}
|
70
|
-
bulk.times.should ==
|
70
|
+
map_times(bulk.times).should ==
|
71
|
+
map_times(parse_times(%w{2011-03-01 2011-05-05}))
|
71
72
|
bulk.floats.should == [1.0, 2.3]
|
72
73
|
bulk.decimals.should == [1.0, 2.3]
|
73
74
|
end
|
@@ -78,7 +79,7 @@ describe "PgArray" do
|
|
78
79
|
bulk.strings.should == %w{as so}
|
79
80
|
bulk.floats.should == [1.0, 1.2]
|
80
81
|
bulk.decimals.should == [1.0, 1.2]
|
81
|
-
bulk.texts.should == [nil, 'Text', 'NULL', 'Text with nil', 'Text with , nil, !', 'nil']
|
82
|
+
bulk.texts.should == [nil, 'Text', 'NULL', 'Text with nil', 'Text with , nil, !"\\', 'nil']
|
82
83
|
map_times(bulk.times).should ==
|
83
84
|
map_times(parse_times(%w{2010-01-01 2010-02-01}))
|
84
85
|
end
|
@@ -96,10 +97,19 @@ describe "PgArray" do
|
|
96
97
|
bulk.decimals.should == [2.5, 2]
|
97
98
|
map_times(bulk.times).should == map_times(parse_times(%w{2010-04-01 2010-03-01}))
|
98
99
|
end
|
100
|
+
|
101
|
+
it "should save right text" do
|
102
|
+
bulk = Bulk.find(5)
|
103
|
+
bulk.texts = ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\\\'\""]
|
104
|
+
bulk.save!
|
105
|
+
bulk.texts = []
|
106
|
+
bulk = Bulk.find(:first, :conditions=>'5 = id')
|
107
|
+
bulk.texts.should == ['Text with , nil, !\x01\\\'"',"Text with , nil, !\x01\\\'\""]
|
108
|
+
end
|
99
109
|
|
100
110
|
it "should be safe for eval" do
|
101
111
|
bulk = Bulk.find(5)
|
102
|
-
bulk.strings.should == [
|
112
|
+
bulk.strings.should == ["\#{1+1}", "\\\#{1+1}\"'\\z\x01", "\t\n"]
|
103
113
|
#$stderr.puts ActiveRecord::ConnectionAdapters::PostgreSQLColumn::ESCAPE_ARRAY.inspect
|
104
114
|
end
|
105
115
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar_pg_array
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.9
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,19 +9,22 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2011-12-04 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement: &
|
15
|
+
name: active_record
|
16
|
+
requirement: &83197980 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
+
- - <
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
19
22
|
- - ! '>='
|
20
23
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
24
|
+
version: 2.3.5
|
22
25
|
type: :runtime
|
23
26
|
prerelease: false
|
24
|
-
version_requirements: *
|
27
|
+
version_requirements: *83197980
|
25
28
|
description: ar_pg_array includes support of PostgreSQL's int[], float[], text[],
|
26
29
|
timestamptz[] etc. into ActiveRecord. You could define migrations for array columns,
|
27
30
|
query on array columns.
|
@@ -55,8 +58,7 @@ files:
|
|
55
58
|
homepage: http://github.com/funny-falcon/activerecord-postgresql-arrays
|
56
59
|
licenses: []
|
57
60
|
post_install_message:
|
58
|
-
rdoc_options:
|
59
|
-
- --charset=UTF-8
|
61
|
+
rdoc_options: []
|
60
62
|
require_paths:
|
61
63
|
- lib
|
62
64
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -77,10 +79,4 @@ rubygems_version: 1.8.10
|
|
77
79
|
signing_key:
|
78
80
|
specification_version: 3
|
79
81
|
summary: Use power of PostgreSQL Arrays in ActiveRecord
|
80
|
-
test_files:
|
81
|
-
- spec/fixtures/schema.rb
|
82
|
-
- spec/fixtures/item.rb
|
83
|
-
- spec/fixtures/bulk.rb
|
84
|
-
- spec/fixtures/tag.rb
|
85
|
-
- spec/pg_array_spec.rb
|
86
|
-
- spec/spec_helper.rb
|
82
|
+
test_files: []
|