-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.zig
165 lines (128 loc) · 6.66 KB
/
func.zig
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
//! This module provides routines that enable
//! user to register custom functions that can be
//! invoked from within sqlite3, as custom sql functions.
const std = @import("std");
const assert = std.debug.assert;
const errors = @import("errors.zig");
const sqlite3 = @import("sqlite3.zig");
const c = @import("c.zig").c;
pub const FuncOptions = struct {
/// Name of the function to register with sqlite3
Name: [:0]const u8,
/// Deterministic must be true if the function will always return
/// the same result given the same inputs within a single SQL statement
Deterministic: bool,
/// Args is the number of arguments that this function accepts
Args: usize,
};
/// createScalarFunction registers the custom scalar function with the provided database connection object.
pub fn createScalarFunction(
db: *sqlite3.Database,
opts: FuncOptions,
ptr: anytype,
comptime apply: fn (@TypeOf(ptr), *sqlite3.Context, []const *sqlite3.Value) void,
) !void {
const conn = @ptrCast(*c.sqlite3, db);
const Ptr = @TypeOf(ptr);
const ptr_info = @typeInfo(Ptr);
assert(ptr_info == .Pointer); // Must be a pointer
assert(ptr_info.Pointer.size == .One); // Must be a single-item pointer
const alignment = ptr_info.Pointer.alignment;
const gen = struct {
fn apply(ctx: ?*c.sqlite3_context, argc: c_int, argv: [*c]?*c.sqlite3_value) callconv(.C) void {
var self: ?Ptr = if (@sizeOf(Ptr) != 0) @ptrCast(Ptr, @alignCast(alignment, c.sqlite3_user_data(ctx.?))) else null;
var args = @ptrCast([*]*sqlite3.Value, argv)[0..@intCast(usize, argc)];
@call(.{ .modifier = .always_inline }, apply, .{ self.?, @ptrCast(*sqlite3.Context, ctx.?), args });
}
};
var flags = @as(c_int, c.SQLITE_UTF8);
if (opts.Deterministic) {
flags |= c.SQLITE_DETERMINISTIC;
}
const userData = if (@sizeOf(Ptr) != 0) @ptrCast(*anyopaque, ptr) else null;
const ret = c.sqlite3_create_function_v2(conn, opts.Name, @intCast(c_int, opts.Args), flags, userData, gen.apply, null, null, null);
if (ret != c.SQLITE_OK) {
return errors.from(ret);
}
}
/// createAggregateFunction registers the custom aggregate function with the provided database connection object.
pub fn createAggregateFunction(
db: *sqlite3.Database,
opts: FuncOptions,
ptr: anytype,
comptime step: fn (@TypeOf(ptr), *sqlite3.Context, []const *sqlite3.Value) void,
comptime final: fn (@TypeOf(ptr), *sqlite3.Context) void,
) !void {
const conn = @ptrCast(*c.sqlite3, db);
const Ptr = @TypeOf(ptr);
const ptr_info = @typeInfo(Ptr);
assert(ptr_info == .Pointer); // Must be a pointer
assert(ptr_info.Pointer.size == .One); // Must be a single-item pointer
const alignment = ptr_info.Pointer.alignment;
const gen = struct {
fn step(ctx: ?*c.sqlite3_context, argc: c_int, argv: [*c]?*c.sqlite3_value) callconv(.C) void {
var self: ?Ptr = if (@sizeOf(Ptr) != 0) @ptrCast(Ptr, @alignCast(alignment, c.sqlite3_user_data(ctx.?))) else null;
var args = @ptrCast([*]*sqlite3.Value, argv)[0..@intCast(usize, argc)];
@call(.{ .modifier = .always_inline }, step, .{ self.?, @ptrCast(*sqlite3.Context, ctx.?), args });
}
fn final(ctx: ?*c.sqlite3_context) callconv(.C) void {
var self: ?Ptr = if (@sizeOf(Ptr) != 0) @ptrCast(Ptr, @alignCast(alignment, c.sqlite3_user_data(ctx.?))) else null;
@call(.{ .modifier = .always_inline }, final, .{ self.?, @ptrCast(*sqlite3.Context, ctx.?) });
}
};
var flags = @as(c_int, c.SQLITE_UTF8);
if (opts.Deterministic) {
flags |= c.SQLITE_DETERMINISTIC;
}
const userData = if (@sizeOf(Ptr) != 0) @ptrCast(*anyopaque, ptr) else null;
const ret = c.sqlite3_create_function_v2(conn, opts.Name, @intCast(c_int, opts.Args), flags, userData, null, gen.step, gen.final, null);
if (ret != c.SQLITE_OK) {
return errors.from(ret);
}
}
/// createWindowFunction registers the custom window function with the provided database connection object.
pub fn createWindowFunction(
db: *sqlite3.Database,
opts: FuncOptions,
ptr: anytype,
comptime step: fn (@TypeOf(ptr), *sqlite3.Context, []const *sqlite3.Value) void,
comptime value: fn (@TypeOf(ptr), *sqlite3.Context) void,
comptime inverse: fn (@TypeOf(ptr), *sqlite3.Context, []const *sqlite3.Value) void,
comptime final: fn (@TypeOf(ptr), *sqlite3.Context) void,
) !void {
const conn = @ptrCast(*c.sqlite3, db);
const Ptr = @TypeOf(ptr);
const ptr_info = @typeInfo(Ptr);
assert(ptr_info == .Pointer); // Must be a pointer
assert(ptr_info.Pointer.size == .One); // Must be a single-item pointer
const alignment = ptr_info.Pointer.alignment;
const gen = struct {
fn step(ctx: ?*c.sqlite3_context, argc: c_int, argv: [*c]?*c.sqlite3_value) callconv(.C) void {
var self: ?Ptr = if (@sizeOf(Ptr) != 0) @ptrCast(Ptr, @alignCast(alignment, c.sqlite3_user_data(ctx.?))) else null;
var args = @ptrCast([*]*sqlite3.Value, argv)[0..@intCast(usize, argc)];
@call(.{ .modifier = .always_inline }, step, .{ self.?, @ptrCast(*sqlite3.Context, ctx.?), args });
}
fn value(ctx: ?*c.sqlite3_context) callconv(.C) void {
var self: ?Ptr = if (@sizeOf(Ptr) != 0) @ptrCast(Ptr, @alignCast(alignment, c.sqlite3_user_data(ctx.?))) else null;
@call(.{ .modifier = .always_inline }, value, .{ self.?, @ptrCast(*sqlite3.Context, ctx.?) });
}
fn inverse(ctx: ?*c.sqlite3_context, argc: c_int, argv: [*c]?*c.sqlite3_value) callconv(.C) void {
var self: ?Ptr = if (@sizeOf(Ptr) != 0) @ptrCast(Ptr, @alignCast(alignment, c.sqlite3_user_data(ctx.?))) else null;
var args = @ptrCast([*]*sqlite3.Value, argv)[0..@intCast(usize, argc)];
@call(.{ .modifier = .always_inline }, inverse, .{ self.?, @ptrCast(*sqlite3.Context, ctx.?), args });
}
fn final(ctx: ?*c.sqlite3_context) callconv(.C) void {
var self: ?Ptr = if (@sizeOf(Ptr) != 0) @ptrCast(Ptr, @alignCast(alignment, c.sqlite3_user_data(ctx.?))) else null;
@call(.{ .modifier = .always_inline }, final, .{ self.?, @ptrCast(*sqlite3.Context, ctx.?) });
}
};
var flags = @as(c_int, c.SQLITE_UTF8);
if (opts.Deterministic) {
flags |= c.SQLITE_DETERMINISTIC;
}
const userData = if (@sizeOf(Ptr) != 0) @ptrCast(*anyopaque, ptr) else null;
const ret = c.sqlite3_create_window_function(conn, opts.Name, @intCast(c_int, opts.Args), flags, userData, gen.step, gen.final, gen.value, gen.inverse, null);
if (ret != c.SQLITE_OK) {
return errors.from(ret);
}
}