pg_data_encoder 0.0.7 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -73,6 +73,7 @@ or
73
73
  * Timestamp
74
74
  * Date
75
75
  * Array (integer and string single dimension)
76
+ * UUID (through passing column_map: {0 => :uuid} to options hash)
76
77
 
77
78
  ## Contributing
78
79
 
@@ -13,8 +13,8 @@ module PgDataEncoder
13
13
  def add(row)
14
14
  setup_io if !@io
15
15
  @io.write([row.size].pack("n"))
16
- row.each {|col|
17
- encode_field(@io, col)
16
+ row.each_with_index {|col, index|
17
+ encode_field(@io, col, index)
18
18
  }
19
19
  end
20
20
 
@@ -53,7 +53,8 @@ module PgDataEncoder
53
53
  @io.write([0,0].pack("NN"))
54
54
  end
55
55
 
56
- def encode_field(io, field, depth=0)
56
+ def encode_field(io, field, index, depth=0)
57
+
57
58
  case field
58
59
  when Integer
59
60
  buf = [field].pack("N")
@@ -74,11 +75,19 @@ module PgDataEncoder
74
75
  when nil
75
76
  io.write([-1].pack("N"))
76
77
  when String
77
- buf = field.encode("UTF-8")
78
- io.write([buf.bytesize].pack("N"))
79
- io.write(buf)
78
+ if @options[:column_types] && @options[:column_types][index] == :uuid
79
+ io.write([16].pack("N"))
80
+ c = [field.gsub(/-/, "")].pack('H*')
81
+ io.write(c)
82
+ else
83
+ buf = field.encode("UTF-8")
84
+ io.write([buf.bytesize].pack("N"))
85
+ io.write(buf)
86
+ end
80
87
  when Array
81
88
  array_io = StringIO.new
89
+ field.compact!
90
+ completed = false
82
91
  case field[0]
83
92
  when String
84
93
  array_io.write([1].pack("N")) # unknown
@@ -108,14 +117,17 @@ module PgDataEncoder
108
117
  array_io.write(buf)
109
118
 
110
119
  }
120
+ when nil
121
+ io.write([-1].pack("N"))
122
+ completed = true
111
123
  else
112
124
  raise Exception.new("Arrays support int or string only")
113
125
  end
114
126
 
115
- io.write([array_io.pos].pack("N"))
116
-
117
-
118
- io.write(array_io.string)
127
+ if !completed
128
+ io.write([array_io.pos].pack("N"))
129
+ io.write(array_io.string)
130
+ end
119
131
  when Hash
120
132
  raise Exception.new("Hash's can't contain hashes") if depth > 0
121
133
  hash_io = StringIO.new
@@ -125,7 +137,7 @@ module PgDataEncoder
125
137
  buf = key.to_s.encode("UTF-8")
126
138
  hash_io.write([buf.bytesize].pack("N"))
127
139
  hash_io.write(buf.to_s)
128
- encode_field(hash_io, val.nil? ? val : val.to_s, depth + 1)
140
+ encode_field(hash_io, val.nil? ? val : val.to_s, index, depth + 1)
129
141
  }
130
142
  io.write([hash_io.pos].pack("N")) # assumed identifier for hstore column
131
143
  io.write(hash_io.string)
@@ -1,3 +1,3 @@
1
1
  module PgDataEncoder
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.9"
3
3
  end
Binary file
@@ -211,4 +211,17 @@ describe "generating data" do
211
211
  str.should == existing_data
212
212
  end
213
213
 
214
+ it 'should encode uuid correctly from tempfile' do
215
+ encoder = PgDataEncoder::EncodeForCopy.new(:use_tempfile => true, column_types: {0 => :uuid})
216
+ encoder.add ['e876eef5-a116-4a27-b71f-bac4a1dcd20e']
217
+ encoder.close
218
+ io = encoder.get_io
219
+ existing_data = filedata("uuid.dat")
220
+ str = io.read
221
+ io.class.name.should == "Tempfile"
222
+ str.force_encoding("ASCII-8BIT")
223
+ #File.open("spec/fixtures/output.dat", "w:ASCII-8BIT") {|out| out.write(str) }
224
+ str.should == existing_data
225
+ end
226
+
214
227
  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.7
4
+ version: 0.0.9
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: 2014-01-10 00:00:00.000000000 Z
12
+ date: 2014-01-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -81,6 +81,7 @@ files:
81
81
  - spec/fixtures/output.dat
82
82
  - spec/fixtures/timestamp.dat
83
83
  - spec/fixtures/trueclass.dat
84
+ - spec/fixtures/uuid.dat
84
85
  - spec/spec_helper.rb
85
86
  - spec/verify_data_formats_spec.rb
86
87
  homepage: https://github.com/pbrumm/pg_data_encoder
@@ -129,6 +130,7 @@ test_files:
129
130
  - spec/fixtures/output.dat
130
131
  - spec/fixtures/timestamp.dat
131
132
  - spec/fixtures/trueclass.dat
133
+ - spec/fixtures/uuid.dat
132
134
  - spec/spec_helper.rb
133
135
  - spec/verify_data_formats_spec.rb
134
136
  has_rdoc: