Skip to content

Commit 1595dae

Browse files
committed
add FunctionCallInfo, SortSupport, StringInfo
1 parent e1db075 commit 1595dae

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

src/pgzx/datum.zig

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ const varatt = @import("varatt.zig");
99
pub fn Conv(comptime T: type, comptime from: anytype, comptime to: anytype) type {
1010
return struct {
1111
pub const Type = T;
12+
1213
pub fn fromNullableDatum(d: c.NullableDatum) !Type {
1314
if (d.isnull) {
1415
return err.PGError.UnexpectedNullValue;
1516
}
1617
return try from(d.value);
1718
}
19+
1820
pub fn toNullableDatum(v: Type) !c.NullableDatum {
1921
return .{
2022
.value = try to(v),
@@ -27,12 +29,14 @@ pub fn Conv(comptime T: type, comptime from: anytype, comptime to: anytype) type
2729
pub fn ConvNoFail(comptime T: type, comptime from: anytype, comptime to: anytype) type {
2830
return struct {
2931
pub const Type = T;
32+
3033
pub fn fromNullableDatum(d: c.NullableDatum) !T {
3134
if (d.isnull) {
3235
return err.PGError.UnexpectedNullValue;
3336
}
3437
return from(d.value);
3538
}
39+
3640
pub fn toNullableDatum(v: T) !c.NullableDatum {
3741
return .{
3842
.value = to(v),
@@ -46,12 +50,14 @@ pub fn ConvNoFail(comptime T: type, comptime from: anytype, comptime to: anytype
4650
pub fn OptConv(comptime C: anytype) type {
4751
return struct {
4852
pub const Type = ?C.Type;
53+
4954
pub fn fromNullableDatum(d: c.NullableDatum) !Type {
5055
if (d.isnull) {
5156
return null;
5257
}
5358
return try C.fromNullableDatum(d);
5459
}
60+
5561
pub fn toNullableDatum(v: Type) !c.NullableDatum {
5662
if (v) |value| {
5763
return try C.toNullableDatum(value);
@@ -70,7 +76,10 @@ pub fn OptConv(comptime C: anytype) type {
7076
/// reflection only.
7177
var directMappings = .{
7278
.{ c.Datum, PGDatum },
79+
.{ c.FunctionCallInfo, PGFunctionCallInfo },
7380
.{ c.NullableDatum, PGNullableDatum },
81+
.{ c.SortSupport, PGSortSupport },
82+
.{ c.StringInfo, PGStringInfo },
7483
};
7584

7685
pub fn fromNullableDatum(comptime T: type, d: c.NullableDatum) !T {
@@ -154,7 +163,7 @@ inline fn isConv(comptime T: type) bool {
154163
return @hasDecl(T, "Type") and @hasDecl(T, "fromNullableDatum") and @hasDecl(T, "toNullableDatum");
155164
}
156165

157-
pub const Void = ConvNoFail(void, idDatum, toVoid);
166+
pub const Void = ConvNoFail(void, makeID(c.Datum), toVoid);
158167
pub const Bool = ConvNoFail(bool, c.DatumGetBool, c.BoolGetDatum);
159168
pub const Int8 = ConvNoFail(i8, datumGetInt8, c.Int8GetDatum);
160169
pub const Int16 = ConvNoFail(i16, c.DatumGetInt16, c.Int16GetDatum);
@@ -170,12 +179,18 @@ pub const Float64 = ConvNoFail(f64, c.DatumGetFloat8, c.Float8GetDatum);
170179
pub const SliceU8 = Conv([]const u8, getDatumTextSlice, sliceToDatumText);
171180
pub const SliceU8Z = Conv([:0]const u8, getDatumTextSliceZ, sliceToDatumText);
172181

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);
174187
const PGNullableDatum = struct {
175188
pub const Type = c.NullableDatum;
189+
176190
pub fn fromNullableDatum(d: c.NullableDatum) !Type {
177191
return d;
178192
}
193+
179194
pub fn toNullableDatum(v: Type) !c.NullableDatum {
180195
return v;
181196
}
@@ -185,8 +200,26 @@ const PGNullableDatum = struct {
185200

186201
// TODO: conversion decorator for jsonb decoding/encoding types
187202

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)));
190223
}
191224

192225
fn toVoid(d: void) c.Datum {

0 commit comments

Comments
 (0)