pg_array_parser 0.0.8-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ # 0.0.7
2
+ * Trying to not break the C extension now
3
+
4
+ # 0.0.6
5
+ * Excludes C extension files in JRuby
6
+
7
+ # 0.0.5
8
+ * Prevents attempt to build C extension in JRuby
9
+
10
+ # 0.0.4
11
+ * Fixes bad release
12
+
13
+ # 0.0.3
14
+ * Refactored Java to ensure thread safety - [mauricio](https://github.com/mauricio) -
15
+ Merged at [a30aba](https://github.com/dockyard/pg_array_parser/commit/a30aba4885812290f83c693e6b68c697b0dac675)
16
+
17
+ # 0.0.2
18
+ * Refactored C extension - [jeremyevans](https://github.com/jeremyevens) - Merged at [ad4987](https://github.com/dockyard/pg_array_parser/commit/ad4987dba411decca4aebd0750c990212dc81039)
19
+ * Adds JRuby support - thanks to [tychobrailleur](https://github.com/tychobrailleur) for help with the java class
20
+
21
+ # 0.0.1
22
+ * Initial release
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
@@ -0,0 +1,70 @@
1
+ # PgArrayParser
2
+ [![Build Status](http://travis-ci.org/dockyard/easy_auth.png)](http://travis-ci.org/dockyard/pg_array_parser)
3
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/dockyard/pg_array_parser)
4
+
5
+ Fast PostreSQL array parsing.
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'pg_array_parser'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install pg_array_parser
21
+
22
+ ## Usage
23
+
24
+ Include the `PgArrayParser` module, which provides the `parse_pg_array`
25
+ method.
26
+
27
+ ```ruby
28
+ class MyPostgresParser
29
+ include PgArrayParser
30
+ end
31
+
32
+ parser = MyPostgresParser.new
33
+ parser.parse_pg_array '{}'
34
+ # => []
35
+ parser.parse_pg_array '{1,2,3,4}'
36
+ # => ["1", "2", "3", "4"]
37
+ parser.parse_pg_array '{1,{2,3},4}'
38
+ # => ["1", ["2", "3"], "4"]
39
+ parser.parse_pg_array '{some,strings that,"May have some ,\'s"}'
40
+ # => ["some", "strings that", "May have some ,'s"]
41
+ ```
42
+
43
+ ## Authors
44
+
45
+ [Dan McClain](http://github.com/danmcclain) [twitter](http://twitter.com/_danmcclain)
46
+
47
+ ## Versioning ##
48
+
49
+ This gem follows [Semantic Versioning](http://semver.org)
50
+
51
+ ## Want to help? ##
52
+
53
+ Stable branches are created based upon each minor version. Please make
54
+ pull requests to specific branches rather than master.
55
+
56
+ Please make sure you include tests!
57
+
58
+ Unles Rails drops support for Ruby 1.8.7 we will continue to use the
59
+ hash-rocket syntax. Please respect this.
60
+
61
+ Don't use tabs to indent, two spaces are the standard.
62
+
63
+ ## Legal ##
64
+
65
+ [DockYard](http://dockyard.com), LLC © 2012
66
+
67
+ [@dockyard](http://twitter.com/dockyard)
68
+
69
+ [Licensed under the MIT
70
+ license](http://www.opensource.org/licenses/mit-license.php)
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ spec = Gem::Specification.load('pg_array_parser.gemspec')
7
+
8
+ if RUBY_PLATFORM =~ /java/
9
+ require 'rake/javaextensiontask'
10
+ Rake::JavaExtensionTask.new('pg_array_parser', spec)
11
+ else
12
+ require 'rake/extensiontask'
13
+ Rake::ExtensionTask.new('pg_array_parser', spec)
14
+ end
15
+
16
+ task :install => :compile
17
+ task :spec => :install
18
+
19
+ RSpec::Core::RakeTask.new(:spec)
20
+
21
+ task :default => :spec
@@ -0,0 +1,118 @@
1
+ package pgarrayparser;
2
+
3
+ import org.jruby.*;
4
+ import org.jruby.anno.JRubyClass;
5
+ import org.jruby.anno.JRubyMethod;
6
+ import org.jruby.runtime.ThreadContext;
7
+ import org.jruby.runtime.builtin.IRubyObject;
8
+
9
+ @JRubyClass(name = "PgArrayParser::PgArrayParserEngine")
10
+ public class PgArrayParserEngine extends RubyObject {
11
+
12
+ public PgArrayParserEngine(final Ruby runtime, RubyClass rubyClass) {
13
+ super(runtime, rubyClass);
14
+ }
15
+
16
+ @JRubyMethod(name = "parse_pg_array")
17
+ public RubyArray parse_pg_array( ThreadContext context, IRubyObject value) {
18
+ String content = value.asJavaString();
19
+ return parseData(context, content, 0);
20
+ }
21
+
22
+ private static RubyArray parseData( ThreadContext context, String content, int index)
23
+ {
24
+ RubyArray items = RubyArray.newArray(context.getRuntime());
25
+
26
+ for( int x = index; x < content.length(); x++ ) {
27
+
28
+ char token = content.charAt(x);
29
+
30
+ switch (token) {
31
+ case '{':
32
+ x = parseArrayContents( context, items, content, x + 1 );
33
+ break;
34
+ case '}':
35
+ return items;
36
+ }
37
+
38
+ }
39
+
40
+ return items;
41
+ }
42
+
43
+ private static int parseArrayContents( ThreadContext context, RubyArray items, String content, int index ) {
44
+
45
+ StringBuilder currentItem = new StringBuilder();
46
+ boolean isEscaping = false;
47
+ boolean isQuoted = false;
48
+ boolean wasQuoted = false;
49
+
50
+ int x = index;
51
+
52
+ for(; x < content.length(); x++ ) {
53
+
54
+ char token = content.charAt(x);
55
+
56
+ if ( isEscaping ) {
57
+ currentItem.append( token );
58
+ isEscaping = false;
59
+ } else {
60
+ if ( isQuoted ) {
61
+ switch (token) {
62
+ case '"':
63
+ isQuoted = false;
64
+ wasQuoted = true;
65
+ break;
66
+ case '\\':
67
+ isEscaping = true;
68
+ break;
69
+ default:
70
+ currentItem.append(token);
71
+ }
72
+ } else {
73
+ switch (token) {
74
+ case '\\':
75
+ isEscaping = true;
76
+ break;
77
+ case ',':
78
+ addItem(context, items, currentItem, wasQuoted);
79
+ currentItem = new StringBuilder();
80
+ wasQuoted = false;
81
+ break;
82
+ case '"':
83
+ isQuoted = true;
84
+ break;
85
+ case '{':
86
+ RubyArray internalItems = RubyArray.newArray(context.getRuntime());
87
+ x = parseArrayContents( context, internalItems, content, x + 1 );
88
+ items.add(internalItems);
89
+ break;
90
+ case '}':
91
+ addItem(context, items, currentItem, wasQuoted);
92
+ return x;
93
+ default:
94
+ currentItem.append(token);
95
+ }
96
+ }
97
+ }
98
+
99
+ }
100
+
101
+ return x;
102
+ }
103
+
104
+ private static void addItem( ThreadContext context, RubyArray items, StringBuilder builder, boolean quoted ) {
105
+ String value = builder.toString();
106
+
107
+ if ( !quoted && value.length() == 0 ) {
108
+ return;
109
+ }
110
+
111
+ if ( !quoted && "NULL".equalsIgnoreCase( value ) ) {
112
+ items.add(context.getRuntime().getNil());
113
+ } else {
114
+ items.add(RubyString.newString( context.getRuntime(), value ));
115
+ }
116
+ }
117
+
118
+ }
@@ -0,0 +1,26 @@
1
+ package pgarrayparser;
2
+
3
+ import org.jruby.Ruby;
4
+ import org.jruby.RubyClass;
5
+ import org.jruby.RubyModule;
6
+ import org.jruby.runtime.ObjectAllocator;
7
+ import org.jruby.runtime.builtin.IRubyObject;
8
+ import org.jruby.runtime.load.BasicLibraryService;
9
+
10
+ import java.io.IOException;
11
+
12
+ public class PgArrayParserEngineService implements BasicLibraryService {
13
+
14
+ public boolean basicLoad(Ruby runtime) throws IOException {
15
+
16
+ RubyModule pgArrayParser = runtime.defineModule("PgArrayParser");
17
+ RubyClass pgArrayParserEngine = pgArrayParser.defineClassUnder("PgArrayParserEngine", runtime.getObject(), new ObjectAllocator() {
18
+ public IRubyObject allocate(Ruby runtime, RubyClass rubyClass) {
19
+ return new PgArrayParserEngine(runtime, rubyClass);
20
+ }
21
+ });
22
+
23
+ pgArrayParserEngine.defineAnnotatedMethods(PgArrayParserEngine.class);
24
+ return true;
25
+ }
26
+ }
Binary file
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../pg_array_parser/version', __FILE__)
2
+
3
+ if RUBY_PLATFORM =~ /java/
4
+ module PgArrayParser
5
+ require 'jruby'
6
+ require File.expand_path('../pg_array_parser.jar', __FILE__)
7
+ require 'pgArrayParser/pg_array_parser_engine'
8
+
9
+ def parse_pg_array(value)
10
+ @parser ||= PgArrayParserEngine.new
11
+ @parser.parse_pg_array(value)
12
+ end
13
+ end
14
+ else
15
+ begin
16
+ require 'pg_array_parser/pg_array_parser'
17
+ rescue LoadError
18
+ begin
19
+ require "pg_array_parser/pg_array_parser.#{RbConfig::CONFIG['DLEXT']}"
20
+ rescue LoadError
21
+ require "pg_array_parser.#{RbConfig::CONFIG['DLEXT']}"
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,3 @@
1
+ module PgArrayParser
2
+ VERSION = '0.0.8'
3
+ end
@@ -0,0 +1,40 @@
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@danmcclain.net"]
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 = "https://github.com/dockyard/pg_array_parser"
10
+
11
+ gem.files = [ 'CHANGELOG.md',
12
+ 'Gemfile',
13
+ 'README.md',
14
+ 'Rakefile',
15
+ 'lib/pg_array_parser.rb',
16
+ 'lib/pg_array_parser/version.rb',
17
+ 'pg_array_parser.gemspec',
18
+ 'spec/parser_spec.rb',
19
+ 'spec/spec_helper.rb'
20
+ ]
21
+
22
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
23
+ if RUBY_PLATFORM =~ /java/
24
+ gem.platform = 'java'
25
+ gem.files << 'ext/pg_array_parser/PgArrayParserEngine.java'
26
+ gem.files << 'ext/pg_array_parser/PgArrayParserEngineService.java'
27
+ gem.files << 'lib/pg_array_parser.jar'
28
+ else
29
+ gem.files << 'ext/pg_array_parser/pg_array_parser.c'
30
+ gem.extensions = ['ext/pg_array_parser/extconf.rb']
31
+ end
32
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
33
+ gem.name = "pg_array_parser"
34
+ gem.require_paths = ["lib"]
35
+ gem.version = PgArrayParser::VERSION
36
+
37
+ gem.add_development_dependency 'rspec', '~> 2.11.0'
38
+ gem.add_development_dependency 'rake', '~> 0.9.2.2'
39
+ gem.add_development_dependency 'rake-compiler'
40
+ end
@@ -0,0 +1,99 @@
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 'empty' do
13
+ it 'returns an empty array' do
14
+ parser.parse_pg_array(%[{}]).should eq []
15
+ end
16
+ end
17
+
18
+ context 'no strings' do
19
+ it 'returns an array of strings' do
20
+ parser.parse_pg_array(%[{1,2,3}]).should eq ['1','2','3']
21
+ end
22
+ end
23
+
24
+ context 'NULL values' do
25
+ it 'returns an array of strings, with nils replacing NULL characters' do
26
+ parser.parse_pg_array(%[{1,NULL,NULL}]).should eq ['1',nil,nil]
27
+ end
28
+ end
29
+
30
+ context 'quoted NULL' do
31
+ it 'returns an array with the word NULL' do
32
+ parser.parse_pg_array(%[{1,"NULL",3}]).should eq ['1','NULL','3']
33
+ end
34
+ end
35
+
36
+ context 'strings' do
37
+ it 'returns an array of strings when containing commas in a quoted string' do
38
+ parser.parse_pg_array(%[{1,"2,3",4}]).should eq ['1','2,3','4']
39
+ end
40
+
41
+ it 'returns an array of strings when containing an escaped quote' do
42
+ parser.parse_pg_array(%[{1,"2\\",3",4}]).should eq ['1','2",3','4']
43
+ end
44
+
45
+ it 'returns an array of strings when containing an escaped backslash' do
46
+ parser.parse_pg_array(%[{1,"2\\\\",3,4}]).should eq ['1','2\\','3','4']
47
+ parser.parse_pg_array(%[{1,"2\\\\\\",3",4}]).should eq ['1','2\\",3','4']
48
+ end
49
+
50
+ it 'returns an array containing empty strings' do
51
+ parser.parse_pg_array(%[{1,"",3,""}]).should eq ['1', '', '3', '']
52
+ end
53
+
54
+ end
55
+ end
56
+
57
+ context 'two dimensional arrays' do
58
+ context 'empty' do
59
+ it 'returns an empty array' do
60
+ parser.parse_pg_array(%[{{}}]).should eq [[]]
61
+ parser.parse_pg_array(%[{{},{}}]).should eq [[],[]]
62
+ end
63
+ end
64
+ context 'no strings' do
65
+ it 'returns an array of strings with a sub array' do
66
+ parser.parse_pg_array(%[{1,{2,3},4}]).should eq ['1',['2','3'],'4']
67
+ end
68
+ end
69
+ context 'strings' do
70
+ it 'returns an array of strings with a sub array' do
71
+ parser.parse_pg_array(%[{1,{"2,3"},4}]).should eq ['1',['2,3'],'4']
72
+ end
73
+ it 'returns an array of strings with a sub array and a quoted }' do
74
+ parser.parse_pg_array(%[{1,{"2,}3",NULL},4}]).should eq ['1',['2,}3',nil],'4']
75
+ end
76
+ it 'returns an array of strings with a sub array and a quoted {' do
77
+ parser.parse_pg_array(%[{1,{"2,{3"},4}]).should eq ['1',['2,{3'],'4']
78
+ end
79
+ it 'returns an array of strings with a sub array and a quoted { and escaped quote' do
80
+ parser.parse_pg_array(%[{1,{"2\\",{3"},4}]).should eq ['1',['2",{3'],'4']
81
+ end
82
+ it 'returns an array of strings with a sub array with empty strings' do
83
+ parser.parse_pg_array(%[{1,{""},4,{""}}]).should eq ['1',[''],'4',['']]
84
+ end
85
+ end
86
+ end
87
+ context 'three dimensional arrays' do
88
+ context 'empty' do
89
+ it 'returns an empty array' do
90
+ parser.parse_pg_array(%[{{{}}}]).should eq [[[]]]
91
+ parser.parse_pg_array(%[{{{},{}},{{},{}}}]).should eq [[[],[]],[[],[]]]
92
+ end
93
+ end
94
+ it 'returns an array of strings with sub arrays' do
95
+ parser.parse_pg_array(%[{1,{2,{3,4}},{NULL,6},7}]).should eq ['1',['2',['3','4']],[nil,'6'],'7']
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1 @@
1
+ require 'pg_array_parser'
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_array_parser
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.8
6
+ platform: java
7
+ authors:
8
+ - Dan McClain
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: 2.11.0
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.11.0
27
+ none: false
28
+ prerelease: false
29
+ type: :development
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ~>
35
+ - !ruby/object:Gem::Version
36
+ version: 0.9.2.2
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: 0.9.2.2
43
+ none: false
44
+ prerelease: false
45
+ type: :development
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake-compiler
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ none: false
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ none: false
60
+ prerelease: false
61
+ type: :development
62
+ description: Simple library to parse PostgreSQL arrays into a array of strings
63
+ email:
64
+ - git@danmcclain.net
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - CHANGELOG.md
70
+ - Gemfile
71
+ - README.md
72
+ - Rakefile
73
+ - lib/pg_array_parser.rb
74
+ - lib/pg_array_parser/version.rb
75
+ - pg_array_parser.gemspec
76
+ - spec/parser_spec.rb
77
+ - spec/spec_helper.rb
78
+ - ext/pg_array_parser/PgArrayParserEngine.java
79
+ - ext/pg_array_parser/PgArrayParserEngineService.java
80
+ - lib/pg_array_parser.jar
81
+ homepage: https://github.com/dockyard/pg_array_parser
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ none: false
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ none: false
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Converts PostgreSQL array strings into arrays of strings
105
+ test_files:
106
+ - spec/parser_spec.rb
107
+ - spec/spec_helper.rb
108
+ has_rdoc:
109
+ ...