pg_json 0.1

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.
Files changed (3) hide show
  1. data/bin/pg_json +84 -0
  2. data/lib/pg_json/sequel/pg_json.rb +41 -0
  3. metadata +47 -0
data/bin/pg_json ADDED
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "sequel"
4
+
5
+ DB = Sequel.connect "postgres://#{ARGV.slice!(0)}", :logger => nil, :max_connections => 1
6
+
7
+ def unquoted(type)
8
+ puts <<-EOF
9
+ create or replace function to_json(data #{type}) returns text as $$
10
+ begin
11
+ return (case when data is null then 'null' else data::text end);
12
+ end;
13
+ $$ language plpgsql;
14
+ EOF
15
+ array(type)
16
+ end
17
+
18
+ def quoted(type)
19
+ puts <<-EOF
20
+ create or replace function to_json(data #{type}) returns text as $$
21
+ begin
22
+ return (case when data is null then 'null' else '"'||data::text||'"' end);
23
+ end;
24
+ $$ language plpgsql;
25
+ EOF
26
+ array(type)
27
+ end
28
+
29
+ def composite(type)
30
+ sql = <<-SQL
31
+ select t1.name from (
32
+ select column_name as name, ordinal_position from information_schema.columns where table_name = '#{type}'
33
+ union
34
+ select attribute_name as name, ordinal_position from information_schema.attributes where udt_name = '#{type}'
35
+ ) t1
36
+ order by t1.ordinal_position asc
37
+ SQL
38
+
39
+ fields = DB[sql].all.map do |row|
40
+ "\"" + row[:name] + "\":'||to_json(data.#{row[:name]})"
41
+ end.join("||',")
42
+
43
+ puts <<-EOF
44
+ drop function if exists to_json(#{type});
45
+ create or replace function to_json(data #{type}) returns text as $$
46
+ begin
47
+ if data is null then
48
+ return 'null';
49
+ else
50
+ return '{#{fields}||'}';
51
+ end if;
52
+ end;
53
+ $$ language plpgsql;
54
+ EOF
55
+
56
+ array(type)
57
+ end
58
+
59
+ def array(type)
60
+ puts <<-EOF
61
+ create or replace function to_json(data #{type}[]) returns text as $$
62
+ declare
63
+ res text;
64
+ begin
65
+ res = '[';
66
+ if data is not null then
67
+ for i in 1..array_upper(data,1) loop
68
+ res = res || to_json(data[i]);
69
+ if i < array_upper(data,1) then
70
+ res = res || ',';
71
+ end if;
72
+ end loop;
73
+ end if;
74
+ res = res || ']';
75
+ return res;
76
+ end;
77
+ $$ language plpgsql;
78
+ EOF
79
+ end
80
+
81
+ while entry = ARGV.slice!(0)
82
+ typeFun, typeName = entry.split(":")
83
+ send(typeFun.to_sym, typeName.to_sym)
84
+ end
@@ -0,0 +1,41 @@
1
+ class Hash
2
+ def recursively_symbolize_keys!
3
+ self.symbolize_keys!
4
+ self.values.each do |v|
5
+ if v.is_a? Hash
6
+ v.recursively_symbolize_keys!
7
+ elsif v.is_a? Array
8
+ v.recursively_symbolize_keys!
9
+ end
10
+ end
11
+ self
12
+ end
13
+ end
14
+
15
+ class Array
16
+ def recursively_symbolize_keys!
17
+ self.each do |item|
18
+ if item.is_a? Hash
19
+ item.recursively_symbolize_keys!
20
+ elsif item.is_a? Array
21
+ item.recursively_symbolize_keys!
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ module Sequel
28
+ module Postgres
29
+ class Dataset
30
+ def json
31
+ if self.first
32
+ JSON.parse(self.first[:to_json]).tap do |h|
33
+ h.recursively_symbolize_keys!
34
+ end
35
+ else
36
+ nil
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_json
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jan Zimmek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: bring basic json support to postgresql
15
+ email: jan.zimmek@web.de
16
+ executables:
17
+ - pg_json
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/pg_json/sequel/pg_json.rb
22
+ - bin/pg_json
23
+ homepage:
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.10
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: bring basic json support to postgresql
47
+ test_files: []