pg_funcall 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.gitignore +3 -1
- data/Gemfile.lock +1 -1
- data/lib/pg_funcall/ipaddr_monkeys.rb +3 -0
- data/lib/pg_funcall/type_info.rb +130 -128
- data/lib/pg_funcall/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NTZmYzA2NzhiMGI5NTc1OTVhZDJmOWI3MjdmMzc1OTA1ZThmZDgzYQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZjFkN2I1ODI5ZmY3ODJhODg5ZmUyZjJlYWU3YzdiZjU2ZTNhNDRlZA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OGEyNjg1N2Y0MWNhZjY5NDJjZjc5MDEzN2FkMmRhYmY2ODYxZTY1ZDM3NzIw
|
10
|
+
ZmUyOWNjMzJmNDkwOTEzMDA0N2FmMzZhZGY4OGFmMzU5MjM1MjkyZmMzMmYy
|
11
|
+
MWIxOTI2NDdhODM5MjE0NzA0MWM5ZGFlMmE0ODA5NmJjNTE5Njc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MWRhZjY2YWVmZTRlY2NjMTJkYzZhMWMyYWYzZDA3ZmJlMzVkNzNjYWJiZTkw
|
14
|
+
ODE3ZDdiNjY1Y2FkY2Q2ZjU2ZGY4MWNjZmE0YjM1ZWIwZWY4MWYzYjM0MzI5
|
15
|
+
Zjk2YTRhYjg5MzFhZDAwODgyNTIzYzA5MTYzMmQwYmViMjY1ODg=
|
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/lib/pg_funcall/type_info.rb
CHANGED
@@ -1,149 +1,151 @@
|
|
1
|
-
class PgFuncall
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
class PgFuncall
|
2
|
+
class TypeInfo
|
3
|
+
attr_accessor :ar_type
|
4
|
+
attr_accessor :array_type
|
5
|
+
attr_accessor :element_type
|
6
|
+
|
7
|
+
def initialize(row, ar_type = nil)
|
8
|
+
@row = {}
|
9
|
+
# copy and convert int-looking things to int along the way
|
10
|
+
row.each do |key, val|
|
11
|
+
@row[key] =
|
12
|
+
(val && val.respond_to?(:match) && val.match(/^-?\d+$/)) ? val.to_i : val
|
13
|
+
end
|
14
|
+
@row.freeze
|
15
|
+
@ar_type = ar_type
|
12
16
|
end
|
13
|
-
@row.freeze
|
14
|
-
@ar_type = ar_type
|
15
|
-
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
# TODO: replace this to not use ar_type
|
19
|
+
def cast_from_database(value)
|
20
|
+
@ar_type.respond_to?(:type_cast_from_database) ?
|
21
|
+
@ar_type.type_cast_from_database(value) :
|
22
|
+
@ar_type.type_cast(value)
|
23
|
+
end
|
23
24
|
|
25
|
+
#
|
26
|
+
# Represent a Ruby object in a string form to be passed as a parameter
|
27
|
+
# within a descriptor hash, rather than substituted into a string-form
|
28
|
+
# query.
|
24
29
|
#
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
'false'
|
45
|
-
when String
|
46
|
-
if type == 'bytea' || param.encoding == Encoding::BINARY
|
47
|
-
'\x' + param.unpack('C*').map {|x| sprintf("%02X", x)}.join("")
|
48
|
-
else
|
49
|
-
param
|
50
|
-
end
|
51
|
-
when Array
|
52
|
-
"{" + param.map {|p| _format_param_for_descriptor(p)}.join(",") + "}"
|
53
|
-
when IPAddr
|
54
|
-
param.to_cidr_string
|
55
|
-
when Range
|
56
|
-
last_char = param.exclude_end? ? ')' : ']'
|
57
|
-
case type
|
58
|
-
when 'tsrange', 'tstzrange'
|
59
|
-
"[#{param.first.utc},#{param.last.utc}#{last_char}"
|
30
|
+
def _format_param_for_descriptor(param, type=nil)
|
31
|
+
return param.value if param.is_a?(PgFuncall::Literal)
|
32
|
+
|
33
|
+
case param
|
34
|
+
when PgFuncall::TypedArray
|
35
|
+
_format_param_for_descriptor(param.value, param.type + "[]")
|
36
|
+
when PgFuncall::Typed
|
37
|
+
_format_param_for_descriptor(param.value, param.type)
|
38
|
+
when PgFuncall::PGTyped
|
39
|
+
param.respond_to?(:__pg_value) ?
|
40
|
+
param.__pg_value :
|
41
|
+
_format_param_for_descriptor(param, type)
|
42
|
+
when TrueClass
|
43
|
+
'true'
|
44
|
+
when FalseClass
|
45
|
+
'false'
|
46
|
+
when String
|
47
|
+
if type == 'bytea' || param.encoding == Encoding::BINARY
|
48
|
+
'\x' + param.unpack('C*').map {|x| sprintf("%02X", x)}.join("")
|
60
49
|
else
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
50
|
+
param
|
51
|
+
end
|
52
|
+
when Array
|
53
|
+
"{" + param.map {|p| _format_param_for_descriptor(p)}.join(",") + "}"
|
54
|
+
when IPAddr
|
55
|
+
param.to_cidr_string
|
56
|
+
when Range
|
57
|
+
last_char = param.exclude_end? ? ')' : ']'
|
58
|
+
case type
|
59
|
+
when 'tsrange', 'tstzrange'
|
60
|
+
"[#{param.first.utc},#{param.last.utc}#{last_char}"
|
61
|
+
else
|
62
|
+
"[#{param.first},#{param.last}#{last_char}"
|
63
|
+
end
|
64
|
+
when Set
|
65
|
+
_format_param_for_descriptor(param.to_a)
|
66
|
+
when Hash
|
67
|
+
param.map do |k,v|
|
68
|
+
"#{k} => #{v}"
|
69
|
+
end.join(',')
|
70
|
+
else
|
71
|
+
ActiveRecord::Base.connection.quote(param)
|
72
|
+
end
|
71
73
|
end
|
72
|
-
end
|
73
74
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
75
|
+
# TODO: replace this to not use ar_type
|
76
|
+
def cast_to_database(value)
|
77
|
+
@ar_type.respond_to?(:type_cast_for_database) ?
|
78
|
+
@ar_type.type_cast_for_database(value).to_s :
|
79
|
+
_format_param_for_descriptor(value, name)
|
80
|
+
end
|
80
81
|
|
81
|
-
|
82
|
-
|
83
|
-
|
82
|
+
def name
|
83
|
+
@row['typname']
|
84
|
+
end
|
84
85
|
|
85
|
-
|
86
|
-
|
87
|
-
|
86
|
+
def namespace
|
87
|
+
@row['nspname']
|
88
|
+
end
|
88
89
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
90
|
+
#
|
91
|
+
# Don't fully qualify base types -- this is pretty, but is it wise?
|
92
|
+
#
|
93
|
+
def fqname
|
94
|
+
namespace == 'pg_catalog' ?
|
95
|
+
name :
|
96
|
+
namespace + '.' + name
|
97
|
+
end
|
97
98
|
|
98
|
-
|
99
|
-
|
100
|
-
|
99
|
+
def oid
|
100
|
+
@row['oid']
|
101
|
+
end
|
101
102
|
|
102
|
-
|
103
|
-
|
104
|
-
|
103
|
+
def category
|
104
|
+
@row['typcategory']
|
105
|
+
end
|
105
106
|
|
106
|
-
|
107
|
-
|
108
|
-
|
107
|
+
def temporal?
|
108
|
+
datetime? || timespan?
|
109
|
+
end
|
109
110
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
111
|
+
CATEGORY_MAP =
|
112
|
+
{'A' => 'array',
|
113
|
+
'B' => 'boolean',
|
114
|
+
'C' => 'composite',
|
115
|
+
'D' => 'datetime',
|
116
|
+
'E' => 'enum',
|
117
|
+
'G' => 'geometric',
|
118
|
+
'I' => 'network_address',
|
119
|
+
'N' => 'numeric',
|
120
|
+
'P' => 'pseudotype',
|
121
|
+
'S' => 'string',
|
122
|
+
'T' => 'timespan',
|
123
|
+
'U' => 'user_defined',
|
124
|
+
'V' => 'bit_string',
|
125
|
+
'X' => 'unknown'
|
126
|
+
}
|
127
|
+
|
128
|
+
CATEGORY_MAP.each do |typ, name|
|
129
|
+
define_method("#{name}?") do
|
130
|
+
category == typ
|
131
|
+
end
|
130
132
|
end
|
131
|
-
end
|
132
133
|
|
133
|
-
|
134
|
-
|
135
|
-
|
134
|
+
def category_name
|
135
|
+
CATEGORY_MAP[category]
|
136
|
+
end
|
136
137
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
138
|
+
def element_type_oid
|
139
|
+
raise "Can only call on array" unless array?
|
140
|
+
@row['typelem']
|
141
|
+
end
|
141
142
|
|
142
|
-
|
143
|
-
|
144
|
-
|
143
|
+
def array_type_oid
|
144
|
+
@row['typarray']
|
145
|
+
end
|
145
146
|
|
146
|
-
|
147
|
-
|
147
|
+
def [](element)
|
148
|
+
@row[element.to_s]
|
149
|
+
end
|
148
150
|
end
|
149
151
|
end
|
data/lib/pg_funcall/version.rb
CHANGED