Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Uses pg_array_parser to handle array parsing

  • Loading branch information...
commit 0f77cad8eca7a3ac77cc953bf4fa06a2f30c944c 1 parent c09b5d9
@danmcclain danmcclain authored
View
2  lib/postgres_ext.rb
@@ -1,5 +1,3 @@
-require 'netaddr'
-
require 'postgres_ext/version'
require 'postgres_ext/active_record'
require 'postgres_ext/arel'
View
52 lib/postgres_ext/active_record/connection_adapters/postgres_adapter.rb
@@ -1,10 +1,11 @@
require 'active_record/connection_adapters/postgresql_adapter'
require 'ipaddr'
-require 'csv'
+require 'pg_array_parser'
module ActiveRecord
module ConnectionAdapters
class PostgreSQLColumn
+ include PgArrayParser
attr_accessor :array
def initialize(name, default, sql_type = nil, null = true)
if sql_type =~ /\[\]$/
@@ -28,37 +29,38 @@ def type_cast_with_extended_types(value)
return coder.load(value) if encoded?
klass = self.class
- if self.array
- if array_match = value.match(/\{(.*)\}/)
- values = []
- array_match = array_match[1].gsub %r{\\"}, '""'
- array_match = array_match.gsub %r{({.*?})},'"\1"'
- array_match = array_match.gsub %r{(,|{)NULL(,|})}, '\1$$NULL$$\1'
- array_values = CSV.new(array_match).first
- array_values.each do |array_value|
- if array_value == '$$NULL$$'
- values << nil
- else
- if quoted_string = array_value.match(/^"(.*)"$/)
- values << type_cast(quoted_string[1]) unless quoted_string[1].empty?
- else
- values << type_cast(array_value) unless array_value.empty?
- end
- end
- end
- return values
+ if self.array && value.start_with?('{') && value.end_with?('}')
+ string_to_array value
+ else
+ case type
+ when :inet, :cidr then klass.string_to_cidr_address(value)
+ else
+ type_cast_without_extended_types(value)
end
end
- case type
- when :inet, :cidr then klass.string_to_cidr_address(value)
- else
- type_cast_without_extended_types(value)
- end
end
alias_method_chain :type_cast, :extended_types
def string_to_array(value)
+ string_array = parse_pg_array value
+ if type == :string
+ string_array
+ else
+ type_cast_array(string_array)
+ end
+ end
+ def type_cast_array(array)
+ casted_array = []
+ array.each do |value|
+ if Array === value
+ casted_array.push type_cast_array(value)
+ else
+ casted_array.push type_cast value
+ end
+ end
+ casted_array
end
+
def type_cast_code_with_extended_types(var_name)
klass = self.class.name
View
2  postgres_ext.gemspec
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
gem.version = PostgresExt::VERSION
gem.add_dependency 'activerecord', '~> 3.2.0'
- gem.add_dependency 'netaddr', '~> 1.5.0'
+ gem.add_dependency 'pg_array_parser', '~> 0.0.1'
gem.add_development_dependency 'rails', '~> 3.2.0'
gem.add_development_dependency 'rspec-rails', '~> 2.9.0'
View
17 spec/columns/array_spec.rb
@@ -37,17 +37,6 @@
integer_array_column.type_cast('{1,2,3,4}').should eq [1,2,3,4]
end
end
-
- context 'multi dimensional array' do
- it 'converts a PostgreSQL array value to an array of integer arrays' do
- integer_array_column.type_cast('{1,{2,3},4}').should eq [1,[2,3],4]
- end
- end
- context 'multi dimensional array with multiple arrays' do
- it 'converts a PostgreSQL array value to an array of integer arrays' do
- integer_array_column.type_cast('{1,{2,3},{4}}').should eq [1,[2,3],[4]]
- end
- end
end
describe 'integer array to SQL statment conversion' do
@@ -63,12 +52,6 @@
adapter.type_cast(value, integer_array_column).should eq '{1,2,3,4}'
end
end
- context 'multi dimensional array' do
- it 'returns a PostgreSQL array' do
- value = integer_array_column.type_cast('{1,{2,3},4}')
- adapter.type_cast(value, integer_array_column).should eq '{1,{2,3},4}'
- end
- end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.