pg_data_encoder 0.0.2 → 0.0.3

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.
@@ -66,6 +66,45 @@ module PgDataEncoder
66
66
  buf = field.encode("UTF-8")
67
67
  io.write([buf.bytesize].pack("N"))
68
68
  io.write(buf)
69
+ when Array
70
+ array_io = StringIO.new
71
+ case field[0]
72
+ when String
73
+ array_io.write([1].pack("N")) # unknown
74
+ array_io.write([0].pack("N")) # unknown
75
+
76
+ array_io.write([25].pack("N")) # I think is used to determine string data type
77
+ array_io.write([field.size].pack("N"))
78
+ array_io.write([1].pack("N")) # forcing single dimention array for now
79
+
80
+ field.each_with_index {|val, index|
81
+ buf = val.to_s.encode("UTF-8")
82
+ array_io.write([buf.bytesize].pack("N"))
83
+ array_io.write(buf)
84
+
85
+ }
86
+ when Integer
87
+ array_io.write([1].pack("N")) # unknown
88
+ array_io.write([0].pack("N")) # unknown
89
+
90
+ array_io.write([23].pack("N")) # I think is used to detemine int data type
91
+ array_io.write([field.size].pack("N"))
92
+ array_io.write([1].pack("N")) # forcing single dimention array for now
93
+
94
+ field.each_with_index {|val, index|
95
+ buf = [val.to_i].pack("N")
96
+ array_io.write([buf.bytesize].pack("N"))
97
+ array_io.write(buf)
98
+
99
+ }
100
+ else
101
+ raise Exception.new("Arrays support int or string only")
102
+ end
103
+
104
+ io.write([array_io.pos].pack("N"))
105
+
106
+
107
+ io.write(array_io.string)
69
108
  when Hash
70
109
  raise Exception.new("Hash's can't contain hashes") if depth > 0
71
110
  hash_io = StringIO.new
@@ -89,4 +128,4 @@ module PgDataEncoder
89
128
  end
90
129
 
91
130
  end
92
- end
131
+ end
@@ -1,3 +1,3 @@
1
1
  module PgDataEncoder
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1 @@
1
+ 1 hi {hi,there,rubyist}
Binary file
Binary file
Binary file
@@ -27,6 +27,41 @@ describe "generating data" do
27
27
  str.should == existing_data
28
28
  end
29
29
 
30
+ it 'should encode array data correctly' do
31
+ encoder = PgDataEncoder::EncodeForCopy.new
32
+ encoder.add [1, "hello", ["hi", "jim"]]
33
+ encoder.close
34
+ io = encoder.get_io
35
+ existing_data = filedata("array_with_two.dat")
36
+ str = io.read
37
+ io.class.name.should == "StringIO"
38
+ str.force_encoding("ASCII-8BIT")
39
+ str.should == existing_data
40
+ end
41
+
42
+ it 'should encode array data from tempfile correctly' do
43
+ encoder = PgDataEncoder::EncodeForCopy.new(:use_tempfile => true)
44
+ encoder.add [1, "hi", ["hi", "there", "rubyist"]]
45
+ encoder.close
46
+ io = encoder.get_io
47
+ existing_data = filedata("3_column_array.dat")
48
+ str = io.read
49
+ io.class.name.should == "Tempfile"
50
+ str.force_encoding("ASCII-8BIT")
51
+ str.should == existing_data
52
+ end
53
+
54
+ it 'should encode integer array data from tempfile correctly' do
55
+ encoder = PgDataEncoder::EncodeForCopy.new(:use_tempfile => true)
56
+ encoder.add [[1,2,3]]
57
+ encoder.close
58
+ io = encoder.get_io
59
+ existing_data = filedata("intarray.dat")
60
+ str = io.read
61
+ io.class.name.should == "Tempfile"
62
+ str.force_encoding("ASCII-8BIT")
63
+ str.should == existing_data
64
+ end
30
65
 
31
66
  it 'should encode timestamp data correctly' do
32
67
  encoder = PgDataEncoder::EncodeForCopy.new
@@ -80,4 +115,4 @@ describe "generating data" do
80
115
  str.should == existing_data
81
116
  end
82
117
 
83
- end
118
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_data_encoder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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-09-09 00:00:00.000000000 Z
12
+ date: 2013-10-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -61,9 +61,13 @@ files:
61
61
  - lib/pg_data_encoder/encode_for_copy.rb
62
62
  - lib/pg_data_encoder/version.rb
63
63
  - pg_data_encoder.gemspec
64
+ - spec/fixtures/3_col_array.txt
64
65
  - spec/fixtures/3_col_hstore.dat
65
66
  - spec/fixtures/3_col_hstore.txt
67
+ - spec/fixtures/3_column_array.dat
68
+ - spec/fixtures/array_with_two.dat
66
69
  - spec/fixtures/float.dat
70
+ - spec/fixtures/intarray.dat
67
71
  - spec/fixtures/output.dat
68
72
  - spec/fixtures/timestamp.dat
69
73
  - spec/spec_helper.rb
@@ -94,9 +98,13 @@ specification_version: 3
94
98
  summary: for faster input of data into postgres you can use this to generate the binary
95
99
  import and run COPY FROM
96
100
  test_files:
101
+ - spec/fixtures/3_col_array.txt
97
102
  - spec/fixtures/3_col_hstore.dat
98
103
  - spec/fixtures/3_col_hstore.txt
104
+ - spec/fixtures/3_column_array.dat
105
+ - spec/fixtures/array_with_two.dat
99
106
  - spec/fixtures/float.dat
107
+ - spec/fixtures/intarray.dat
100
108
  - spec/fixtures/output.dat
101
109
  - spec/fixtures/timestamp.dat
102
110
  - spec/spec_helper.rb