FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rust-postgres-array/src/impls.rs at master · psqlpy-python/rust-postgres-array · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
psqlpy-python
/
rust-postgres-array
Public
forked from
rust-postgres/rust-postgres-array
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rust-postgres-array
/
src
/
impls.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
243 lines (212 loc) · 6.29 KB
Breadcrumbs
rust-postgres-array
/
src
/
impls.rs
Copy path
File metadata and controls
243 lines (212 loc) · 6.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use
fallible_iterator
::
FallibleIterator
;
use
postgres_protocol
;
use
postgres_protocol
::
types
;
use
postgres_types
::
{
to_sql_checked
,
FromSql
,
IsNull
,
Kind
,
ToSql
,
Type
}
;
use
std
::
error
::
Error
;
use
crate
::
{
Array
,
Dimension
}
;
use
postgres_types
::
private
::
BytesMut
;
impl
<
'
de
,
T
>
FromSql
<
'
de
>
for
Array
<
T
>
where
T
:
FromSql
<
'
de
>
,
{
fn
from_sql
(
ty
:
&
Type
,
raw
:
&
'
de
[
u8
]
)
->
Result
<
Array
<
T
>
,
Box
<
dyn
Error
+
Sync
+
Send
>
>
{
let
element_type =
match
*
ty
.
kind
(
)
{
Kind
::
Array
(
ref
ty
)
=> ty
,
_ =>
unreachable
!
(
)
,
}
;
let
array = types
::
array_from_sql
(
raw
)
?
;
let
dimensions = array
.
dimensions
(
)
.
map
(
|d|
{
Ok
(
Dimension
{
len
:
d
.
len
,
lower_bound
:
d
.
lower_bound
,
}
)
}
)
.
collect
(
)
?
;
let
elements = array
.
values
(
)
.
map
(
|v|
FromSql
::
from_sql_nullable
(
element_type
,
v
)
)
.
collect
(
)
?
;
Ok
(
Array
::
from_parts
(
elements
,
dimensions
)
)
}
fn
accepts
(
ty
:
&
Type
)
->
bool
{
match
ty
.
kind
(
)
{
&
Kind
::
Array
(
ref
ty
)
=> <
T
as
FromSql
>
::
accepts
(
ty
)
,
_ =>
false
,
}
}
}
impl
<
T
>
ToSql
for
Array
<
T
>
where
T
:
ToSql
,
{
fn
to_sql
(
&
self
,
ty
:
&
Type
,
w
:
&
mut
BytesMut
)
->
Result
<
IsNull
,
Box
<
dyn
Error
+
Sync
+
Send
>
>
{
let
element_type =
match
ty
.
kind
(
)
{
&
Kind
::
Array
(
ref
ty
)
=> ty
,
_ =>
unreachable
!
(
)
,
}
;
let
dimensions =
self
.
dimensions
(
)
.
iter
(
)
.
map
(
|d| types
::
ArrayDimension
{
len
:
d
.
len
,
lower_bound
:
d
.
lower_bound
,
}
)
;
let
elements =
self
.
iter
(
)
;
types
::
array_to_sql
(
dimensions
,
element_type
.
oid
(
)
,
elements
,
|v
,
w|
match
v
.
to_sql
(
element_type
,
w
)
{
Ok
(
IsNull
::
Yes
)
=>
Ok
(
postgres_protocol
::
IsNull
::
Yes
)
,
Ok
(
IsNull
::
No
)
=>
Ok
(
postgres_protocol
::
IsNull
::
No
)
,
Err
(
e
)
=>
Err
(
e
)
,
}
,
w
,
)
?
;
Ok
(
IsNull
::
No
)
}
fn
accepts
(
ty
:
&
Type
)
->
bool
{
match
ty
.
kind
(
)
{
&
Kind
::
Array
(
ref
ty
)
=> <
T
as
ToSql
>
::
accepts
(
ty
)
,
_ =>
false
,
}
}
to_sql_checked
!
(
)
;
}
#
[
cfg
(
test
)
]
mod
test
{
use
std
::
fmt
;
use
crate
::
Array
;
use
postgres
::
types
::
{
FromSqlOwned
,
ToSql
}
;
use
postgres
::
{
Client
,
NoTls
}
;
fn
test_type
<
T
:
PartialEq
+
FromSqlOwned
+
ToSql
+
Sync
,
S
:
fmt
::
Display
>
(
sql_type
:
&
str
,
checks
:
&
[
(
T
,
S
)
]
,
)
{
let
mut
conn =
Client
::
connect
(
"postgres://postgres:password@localhost"
,
NoTls
)
.
unwrap
(
)
;
for
&
(
ref
val
,
ref
repr
)
in
checks
.
iter
(
)
{
let
result = conn
.
query
(
&
*
format
!
(
"SELECT {}::{}"
,
*
repr
,
sql_type
)
,
&
[
]
)
.
unwrap
(
)
[
0
]
.
get
(
0
)
;
assert
!
(
val ==
&
result
)
;
let
result = conn
.
query
(
&
*
format
!
(
"SELECT $1::{}"
,
sql_type
)
,
&
[
val
]
)
.
unwrap
(
)
[
0
]
.
get
(
0
)
;
assert
!
(
val ==
&
result
)
;
}
}
macro_rules!
test_array_params
{
(
$name
:
expr
,
$v1
:
expr
,
$s1
:
expr
,
$v2
:
expr
,
$s2
:
expr
,
$v3
:
expr
,
$s3
:
expr
)
=>
{
{
let
tests =
&
[
(
Some
(
Array
::
from_vec
(
vec!
[
Some
(
$v1
)
,
Some
(
$v2
)
,
None
]
,
1
)
)
,
format!
(
"'{{{},{},NULL}}'"
,
$s1
,
$s2
)
,
)
,
(
None
,
"NULL"
.
to_string
(
)
)
,
]
;
test_type
(
&
format!
(
"{}[]"
,
$name
)
,
tests
)
;
let
mut
a =
Array
::
from_vec
(
vec!
[
Some
(
$v1
)
,
Some
(
$v2
)
]
,
0
)
;
a
.
wrap
(
-
1
)
;
a
.
push
(
Array
::
from_vec
(
vec!
[
None
,
Some
(
$v3
)
]
,
0
)
)
;
let
tests =
&
[
(
Some
(
a
)
,
format!
(
"'[-1:0][0:1]={{{{{},{}}},{{NULL,{}}}}}'"
,
$s1
,
$s2
,
$s3
)
,
)
]
;
test_type
(
&
format!
(
"{}[][]"
,
$name
)
,
tests
)
;
}
}
;
}
#
[
test
]
fn
test_boolarray_params
(
)
{
test_array_params
!
(
"BOOL"
,
false
,
"f"
,
true
,
"t"
,
true
,
"t"
)
;
}
#
[
test
]
fn
test_byteaarray_params
(
)
{
test_array_params
!
(
"BYTEA"
,
vec!
[
0u8
,
1
]
,
r#""\\x0001""#
,
vec!
[
254u8
,
255u8
]
,
r#""\\xfeff""#
,
vec!
[
10u8
,
11u8
]
,
r#""\\x0a0b""#
)
;
}
#
[
test
]
fn
test_chararray_params
(
)
{
test_array_params
!
(
"
\"
char
\"
"
,
'a'
as
i8
,
"a"
,
'z'
as
i8
,
"z"
,
'0'
as
i8
,
"0"
)
;
}
#
[
test
]
fn
test_namearray_params
(
)
{
test_array_params
!
(
"NAME"
,
"hello"
.
to_string
(
)
,
"hello"
,
"world"
.
to_string
(
)
,
"world"
,
"!"
.
to_string
(
)
,
"!"
)
;
}
#
[
test
]
fn
test_int2array_params
(
)
{
test_array_params
!
(
"INT2"
,
0i16
,
"0"
,
1i16
,
"1"
,
2i16
,
"2"
)
;
}
#
[
test
]
fn
test_int4array_params
(
)
{
test_array_params
!
(
"INT4"
,
0i32
,
"0"
,
1i32
,
"1"
,
2i32
,
"2"
)
;
}
#
[
test
]
fn
test_textarray_params
(
)
{
test_array_params
!
(
"TEXT"
,
"hello"
.
to_string
(
)
,
"hello"
,
"world"
.
to_string
(
)
,
"world"
,
"!"
.
to_string
(
)
,
"!"
)
;
}
#
[
test
]
fn
test_charnarray_params
(
)
{
test_array_params
!
(
"CHAR(5)"
,
"hello"
.
to_string
(
)
,
"hello"
,
"world"
.
to_string
(
)
,
"world"
,
"! "
.
to_string
(
)
,
"!"
)
;
}
#
[
test
]
fn
test_varchararray_params
(
)
{
test_array_params
!
(
"VARCHAR"
,
"hello"
.
to_string
(
)
,
"hello"
,
"world"
.
to_string
(
)
,
"world"
,
"!"
.
to_string
(
)
,
"!"
)
;
}
#
[
test
]
fn
test_int8array_params
(
)
{
test_array_params
!
(
"INT8"
,
0i64
,
"0"
,
1i64
,
"1"
,
2i64
,
"2"
)
;
}
#
[
test
]
fn
test_float4array_params
(
)
{
test_array_params
!
(
"FLOAT4"
,
0f32
,
"0"
,
1.5f32
,
"1.5"
,
0.009f32
,
".009"
)
;
}
#
[
test
]
fn
test_float8array_params
(
)
{
test_array_params
!
(
"FLOAT8"
,
0f64
,
"0"
,
1.5f64
,
"1.5"
,
0.009f64
,
".009"
)
;
}
#
[
test
]
fn
test_empty_array
(
)
{
let
mut
conn =
Client
::
connect
(
"postgres://postgres@localhost"
,
NoTls
)
.
unwrap
(
)
;
conn
.
query
(
"SELECT '{}'::INT4[]"
,
&
[
]
)
.
unwrap
(
)
[
0
]
.
get
::
<
_
,
Array
<
i32
>
>
(
0
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL