@@ -9,12 +9,14 @@ const varatt = @import("varatt.zig");
9
9
pub fn Conv (comptime T : type , comptime from : anytype , comptime to : anytype ) type {
10
10
return struct {
11
11
pub const Type = T ;
12
+
12
13
pub fn fromNullableDatum (d : c.NullableDatum ) ! Type {
13
14
if (d .isnull ) {
14
15
return err .PGError .UnexpectedNullValue ;
15
16
}
16
17
return try from (d .value );
17
18
}
19
+
18
20
pub fn toNullableDatum (v : Type ) ! c.NullableDatum {
19
21
return .{
20
22
.value = try to (v ),
@@ -27,12 +29,14 @@ pub fn Conv(comptime T: type, comptime from: anytype, comptime to: anytype) type
27
29
pub fn ConvNoFail (comptime T : type , comptime from : anytype , comptime to : anytype ) type {
28
30
return struct {
29
31
pub const Type = T ;
32
+
30
33
pub fn fromNullableDatum (d : c.NullableDatum ) ! T {
31
34
if (d .isnull ) {
32
35
return err .PGError .UnexpectedNullValue ;
33
36
}
34
37
return from (d .value );
35
38
}
39
+
36
40
pub fn toNullableDatum (v : T ) ! c.NullableDatum {
37
41
return .{
38
42
.value = to (v ),
@@ -46,12 +50,14 @@ pub fn ConvNoFail(comptime T: type, comptime from: anytype, comptime to: anytype
46
50
pub fn OptConv (comptime C : anytype ) type {
47
51
return struct {
48
52
pub const Type = ? C .Type ;
53
+
49
54
pub fn fromNullableDatum (d : c.NullableDatum ) ! Type {
50
55
if (d .isnull ) {
51
56
return null ;
52
57
}
53
58
return try C .fromNullableDatum (d );
54
59
}
60
+
55
61
pub fn toNullableDatum (v : Type ) ! c.NullableDatum {
56
62
if (v ) | value | {
57
63
return try C .toNullableDatum (value );
@@ -70,7 +76,10 @@ pub fn OptConv(comptime C: anytype) type {
70
76
/// reflection only.
71
77
var directMappings = .{
72
78
.{ c .Datum , PGDatum },
79
+ .{ c .FunctionCallInfo , PGFunctionCallInfo },
73
80
.{ c .NullableDatum , PGNullableDatum },
81
+ .{ c .SortSupport , PGSortSupport },
82
+ .{ c .StringInfo , PGStringInfo },
74
83
};
75
84
76
85
pub fn fromNullableDatum (comptime T : type , d : c.NullableDatum ) ! T {
@@ -154,7 +163,7 @@ inline fn isConv(comptime T: type) bool {
154
163
return @hasDecl (T , "Type" ) and @hasDecl (T , "fromNullableDatum" ) and @hasDecl (T , "toNullableDatum" );
155
164
}
156
165
157
- pub const Void = ConvNoFail (void , idDatum , toVoid );
166
+ pub const Void = ConvNoFail (void , makeID ( c . Datum ) , toVoid );
158
167
pub const Bool = ConvNoFail (bool , c .DatumGetBool , c .BoolGetDatum );
159
168
pub const Int8 = ConvNoFail (i8 , datumGetInt8 , c .Int8GetDatum );
160
169
pub const Int16 = ConvNoFail (i16 , c .DatumGetInt16 , c .Int16GetDatum );
@@ -170,12 +179,18 @@ pub const Float64 = ConvNoFail(f64, c.DatumGetFloat8, c.Float8GetDatum);
170
179
pub const SliceU8 = Conv ([]const u8 , getDatumTextSlice , sliceToDatumText );
171
180
pub const SliceU8Z = Conv ([:0 ]const u8 , getDatumTextSliceZ , sliceToDatumText );
172
181
173
- pub const PGDatum = ConvNoFail (c .Datum , idDatum , idDatum );
182
+ pub const PGFunctionCallInfo = ConvID (c .FunctionCallInfo );
183
+ pub const PGSortSupport = ConvID (c .SortSupport );
184
+ pub const PGStringInfo = Conv (c .StringInfo , datumGetStringInfo , c .PointerGetDatum );
185
+
186
+ pub const PGDatum = ConvID (c .Datum );
174
187
const PGNullableDatum = struct {
175
188
pub const Type = c .NullableDatum ;
189
+
176
190
pub fn fromNullableDatum (d : c.NullableDatum ) ! Type {
177
191
return d ;
178
192
}
193
+
179
194
pub fn toNullableDatum (v : Type ) ! c.NullableDatum {
180
195
return v ;
181
196
}
@@ -185,8 +200,26 @@ const PGNullableDatum = struct {
185
200
186
201
// TODO: conversion decorator for jsonb decoding/encoding types
187
202
188
- fn idDatum (d : c.Datum ) c.Datum {
189
- return d ;
203
+ fn ConvID (comptime T : type ) type {
204
+ const idFn = makeID (T );
205
+
206
+ return ConvNoFail (T , idFn , idFn );
207
+ }
208
+
209
+ fn makeID (comptime T : type ) fn (T ) T {
210
+ return struct {
211
+ fn id (t : T ) T {
212
+ return t ;
213
+ }
214
+ }.id ;
215
+ }
216
+
217
+ fn datumGetStringInfo (datum : c.Datum ) ! c.StringInfo {
218
+ return datumGetPointer (c .StringInfo , datum );
219
+ }
220
+
221
+ inline fn datumGetPointer (comptime T : type , datum : c.Datum ) T {
222
+ return @ptrCast (@alignCast (c .DatumGetPointer (datum )));
190
223
}
191
224
192
225
fn toVoid (d : void ) c.Datum {
0 commit comments