pg_array_parser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ benchmark.rb
@@ -0,0 +1,7 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+
6
+ notifications:
7
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg_array_parser.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Dan McClain
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ # PgArrayParser
2
+
3
+ Fast PostreSQL array parsing.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pg_array_parser'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pg_array_parser
18
+
19
+ ## Usage
20
+
21
+ Include the `PgArrayParser` module. Call `PgArrayParser#parse_pg_array`
22
+ method to return a multi-dimensional array of strings from a PostgreSQL
23
+ array value
24
+
25
+ ## Authors
26
+
27
+ Dan McClain [twitter](http://twitter.com/_danmcclain) [github](http://github.com/danmcclain)
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ task :spec => :install
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile('pg_array_parser/pg_array_parser')
@@ -0,0 +1,101 @@
1
+ #include <ruby.h>
2
+
3
+ VALUE PgArrayParser = Qnil;
4
+
5
+ //Prototypes
6
+ VALUE read_array(int *index, char *string, int *length, char *word);
7
+ VALUE parse_pg_array(VALUE self, VALUE pg_array_string);
8
+
9
+ VALUE parse_pg_array(VALUE self, VALUE pg_array_string) {
10
+
11
+ //convert to c-string, create a buffer of the same length, as that will be the worst case
12
+ char *c_pg_array_string = StringValueCStr(pg_array_string);
13
+ int array_string_length = RSTRING_LEN(pg_array_string);
14
+ char *word = malloc(sizeof(char) * (array_string_length + 1));
15
+
16
+ int index = 1;
17
+
18
+ VALUE return_value = read_array(&index, c_pg_array_string, &array_string_length, word);
19
+ free(word);
20
+ return return_value;
21
+ }
22
+
23
+ VALUE read_array(int *index, char *c_pg_array_string, int *array_string_length, char *word)
24
+ {
25
+ // Return value: array
26
+ VALUE array;
27
+ array = rb_ary_new();
28
+ int word_index = 0;
29
+ int openQuote = 0;
30
+ for(;(*index) < (*array_string_length); ++(*index))
31
+ {
32
+ if(!openQuote && (c_pg_array_string[*index] == ','))
33
+ {
34
+ if(c_pg_array_string[(*index) - 1] != '"' && c_pg_array_string[(*index) - 1] != '}')
35
+ {
36
+ word[word_index] = '\0';
37
+ if (word_index == 4 && !strcmp(word,"NULL"))
38
+ {
39
+ rb_ary_push(array, Qnil);
40
+ }
41
+ else
42
+ {
43
+ rb_ary_push(array, rb_str_new2(word));
44
+ }
45
+ word_index = 0;
46
+ }
47
+ }
48
+ else if(!openQuote && c_pg_array_string[*index] == '}')
49
+ {
50
+ if(word_index > 0 && c_pg_array_string[(*index) - 1] != '"')
51
+ {
52
+ word[word_index] = '\0';
53
+ if (word_index == 4 && !strcmp(word,"NULL"))
54
+ {
55
+ rb_ary_push(array, Qnil);
56
+ }
57
+ else
58
+ {
59
+ rb_ary_push(array, rb_str_new2(word));
60
+ }
61
+ word_index = 0;
62
+ }
63
+ return array;
64
+ }
65
+ else if (openQuote && c_pg_array_string[*index] == '"' && c_pg_array_string[(*index) - 1] == '\\')
66
+ {
67
+ word[word_index - 1] = '"';
68
+ }
69
+ else if (openQuote && c_pg_array_string[*index] == '"' && c_pg_array_string[(*index) - 1] != '\\')
70
+ {
71
+ word[word_index] = '\0';
72
+ word_index = 0;
73
+ openQuote = 0;
74
+ rb_ary_push(array, rb_str_new2(word));
75
+ }
76
+ else if(c_pg_array_string[*index] == '"')
77
+ {
78
+ openQuote = 1;
79
+ }
80
+ else if(!openQuote && c_pg_array_string[*index] == '{')
81
+ {
82
+ (*index)++;
83
+ rb_ary_push(array, read_array(index, c_pg_array_string, array_string_length, word));
84
+ }
85
+ else
86
+ {
87
+ word[word_index] = c_pg_array_string[*index];
88
+ word_index++;
89
+ }
90
+ }
91
+
92
+ return array;
93
+ }
94
+
95
+ void Init_pg_array_parser(void) {
96
+ PgArrayParser = rb_define_module("PgArrayParser");
97
+ rb_define_method(PgArrayParser, "parse_pg_array", parse_pg_array, 1);
98
+
99
+ }
100
+
101
+
@@ -0,0 +1,2 @@
1
+ require "pg_array_parser/version"
2
+ require 'pg_array_parser/pg_array_parser'
@@ -0,0 +1,3 @@
1
+ module PgArrayParser
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/pg_array_parser/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Dan McClain"]
6
+ gem.email = ["git@danseaver.com"]
7
+ gem.description = %q{Simple library to parse PostgreSQL arrays into a array of strings}
8
+ gem.summary = %q{Converts PostgreSQL array strings into arrays of strings}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.extensions = ['ext/pg_array_parser/extconf.rb']
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "pg_array_parser"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = PgArrayParser::VERSION
18
+
19
+ gem.add_development_dependency 'rspec', '~> 2.10.0'
20
+ gem.add_development_dependency 'rake', '~> 0.9.2.2'
21
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ class Parser
4
+ include PgArrayParser
5
+ end
6
+
7
+ describe 'PgArrayParser' do
8
+ let!(:parser) { Parser.new }
9
+
10
+ describe '#parse_pg_array' do
11
+ context 'one dimensional arrays' do
12
+ context 'no strings' do
13
+ it 'returns an array of strings' do
14
+ parser.parse_pg_array(%[{1,2,3}]).should eq ['1','2','3']
15
+ end
16
+ end
17
+
18
+ context 'NULL values' do
19
+ it 'returns an array of strings, with nils replacing NULL characters' do
20
+ parser.parse_pg_array(%[{1,NULL,NULL}]).should eq ['1',nil,nil]
21
+ end
22
+ end
23
+
24
+ context 'quoted NULL' do
25
+ it 'returns an array with the word NULL' do
26
+ parser.parse_pg_array(%[{1,"NULL",3}]).should eq ['1','NULL','3']
27
+ end
28
+ end
29
+
30
+ context 'strings' do
31
+ it 'returns an array of strings when containing commas in a quoted string' do
32
+ parser.parse_pg_array(%[{1,"2,3",4}]).should eq ['1','2,3','4']
33
+ end
34
+
35
+ it 'returns an array of strings when containing an escaped quote' do
36
+ parser.parse_pg_array(%[{1,"2\\",3",4}]).should eq ['1','2",3','4']
37
+ end
38
+ end
39
+ end
40
+
41
+ context 'two dimensional arrays' do
42
+ context 'no strings' do
43
+ it 'returns an array of strings with a sub array' do
44
+ parser.parse_pg_array(%[{1,{2,3},4}]).should eq ['1',['2','3'],'4']
45
+ end
46
+ end
47
+ context 'strings' do
48
+ it 'returns an array of strings with a sub array' do
49
+ parser.parse_pg_array(%[{1,{"2,3"},4}]).should eq ['1',['2,3'],'4']
50
+ end
51
+ it 'returns an array of strings with a sub array and a quoted }' do
52
+ parser.parse_pg_array(%[{1,{"2,}3",NULL},4}]).should eq ['1',['2,}3',nil],'4']
53
+ end
54
+ it 'returns an array of strings with a sub array and a quoted {' do
55
+ parser.parse_pg_array(%[{1,{"2,{3"},4}]).should eq ['1',['2,{3'],'4']
56
+ end
57
+ it 'returns an array of strings with a sub array and a quoted { and escaped quote' do
58
+ parser.parse_pg_array(%[{1,{"2\\",{3"},4}]).should eq ['1',['2",{3'],'4']
59
+ end
60
+ end
61
+ end
62
+ context 'three dimensional arrays' do
63
+ it 'returns an array of strings with sub arrays' do
64
+ parser.parse_pg_array(%[{1,{2,{3,4}},{NULL,6},7}]).should eq ['1',['2',['3','4']],[nil,'6'],'7']
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1 @@
1
+ require 'pg_array_parser'
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_array_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dan McClain
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.10.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.10.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.2.2
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.2.2
46
+ description: Simple library to parse PostgreSQL arrays into a array of strings
47
+ email:
48
+ - git@danseaver.com
49
+ executables: []
50
+ extensions:
51
+ - ext/pg_array_parser/extconf.rb
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .travis.yml
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - ext/pg_array_parser/extconf.rb
61
+ - ext/pg_array_parser/pg_array_parser.c
62
+ - lib/pg_array_parser.bundle
63
+ - lib/pg_array_parser.rb
64
+ - lib/pg_array_parser/version.rb
65
+ - pg_array_parser.gemspec
66
+ - spec/parser_spec.rb
67
+ - spec/spec_helper.rb
68
+ homepage: ''
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.23
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Converts PostgreSQL array strings into arrays of strings
92
+ test_files:
93
+ - spec/parser_spec.rb
94
+ - spec/spec_helper.rb