pg_json 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/pg_json +41 -14
- metadata +1 -1
data/bin/pg_json
CHANGED
@@ -4,27 +4,55 @@ require "sequel"
|
|
4
4
|
|
5
5
|
DB = Sequel.connect "postgres://#{ARGV.slice!(0)}", :logger => nil, :max_connections => 1
|
6
6
|
|
7
|
-
def
|
7
|
+
def simple(type, &block)
|
8
|
+
|
9
|
+
block = lambda do
|
10
|
+
"data::text"
|
11
|
+
end unless block
|
12
|
+
|
8
13
|
puts <<-EOF
|
9
14
|
create or replace function to_json(data #{type}) returns text as $$
|
10
15
|
begin
|
11
|
-
return (case when data is null then 'null' else
|
16
|
+
return (case when data is null then 'null' else #{block.call} end);
|
12
17
|
end;
|
13
18
|
$$ language plpgsql;
|
14
19
|
EOF
|
15
20
|
array(type)
|
16
21
|
end
|
17
22
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
return (case when data is null then 'null' else '"'||data::text||'"' end);
|
23
|
-
end;
|
24
|
-
$$ language plpgsql;
|
25
|
-
EOF
|
26
|
-
array(type)
|
23
|
+
simple :integer
|
24
|
+
simple :float
|
25
|
+
simple :text do
|
26
|
+
"'"'||data::text||'"'"
|
27
27
|
end
|
28
|
+
simple :date do
|
29
|
+
"'"'||to_char(data, 'YYYY-MM-DD')::text||'"'"
|
30
|
+
end
|
31
|
+
simple :time do
|
32
|
+
"'"'||to_char(data, 'HH24:MI:SS')::text||'"'"
|
33
|
+
end
|
34
|
+
|
35
|
+
# def unquoted(type)
|
36
|
+
# puts <<-EOF
|
37
|
+
# create or replace function to_json(data #{type}) returns text as $$
|
38
|
+
# begin
|
39
|
+
# return (case when data is null then 'null' else data::text end);
|
40
|
+
# end;
|
41
|
+
# $$ language plpgsql;
|
42
|
+
# EOF
|
43
|
+
# array(type)
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# def quoted(type)
|
47
|
+
# puts <<-EOF
|
48
|
+
# create or replace function to_json(data #{type}) returns text as $$
|
49
|
+
# begin
|
50
|
+
# return (case when data is null then 'null' else '"'||data::text||'"' end);
|
51
|
+
# end;
|
52
|
+
# $$ language plpgsql;
|
53
|
+
# EOF
|
54
|
+
# array(type)
|
55
|
+
# end
|
28
56
|
|
29
57
|
def composite(type)
|
30
58
|
sql = <<-SQL
|
@@ -80,7 +108,6 @@ $$ language plpgsql;
|
|
80
108
|
EOF
|
81
109
|
end
|
82
110
|
|
83
|
-
while
|
84
|
-
|
85
|
-
send(typeFun.to_sym, typeName.to_sym)
|
111
|
+
while composite = ARGV.slice!(0)
|
112
|
+
send :composite, composite.to_sym
|
86
113
|
end
|