mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 18:48:33 -04:00
feat: zig-sqlite.
This commit is contained in:
59
zig-vendor/zig-sqlite/examples/zigcrypto.zig
Normal file
59
zig-vendor/zig-sqlite/examples/zigcrypto.zig
Normal file
@@ -0,0 +1,59 @@
|
||||
const std = @import("std");
|
||||
const Blake3 = std.crypto.hash.Blake3;
|
||||
const Sha3_512 = std.crypto.hash.sha3.Sha3_512;
|
||||
|
||||
const sqlite = @import("sqlite");
|
||||
const c = sqlite.c;
|
||||
|
||||
const name = "zigcrypto";
|
||||
|
||||
pub const loadable_extension = true;
|
||||
|
||||
var module_allocator: std.heap.GeneralPurposeAllocator(.{}) = undefined;
|
||||
var module_context: sqlite.vtab.ModuleContext = undefined;
|
||||
|
||||
const logger = std.log.scoped(.zigcrypto);
|
||||
|
||||
fn createAllFunctions(db: *sqlite.Db) !void {
|
||||
try db.createScalarFunction(
|
||||
"blake3",
|
||||
struct {
|
||||
fn run(input: []const u8) [Blake3.digest_length]u8 {
|
||||
var output: [Blake3.digest_length]u8 = undefined;
|
||||
Blake3.hash(input, output[0..], .{});
|
||||
return output;
|
||||
}
|
||||
}.run,
|
||||
.{},
|
||||
);
|
||||
try db.createScalarFunction(
|
||||
"sha3_512",
|
||||
struct {
|
||||
fn run(input: []const u8) [Sha3_512.digest_length]u8 {
|
||||
var output: [Sha3_512.digest_length]u8 = undefined;
|
||||
Sha3_512.hash(input, output[0..], .{});
|
||||
return output;
|
||||
}
|
||||
}.run,
|
||||
.{},
|
||||
);
|
||||
}
|
||||
|
||||
pub export fn sqlite3_zigcrypto_init(raw_db: *c.sqlite3, err_msg: [*c][*c]u8, api: *c.sqlite3_api_routines) callconv(.c) c_int {
|
||||
_ = err_msg;
|
||||
|
||||
c.sqlite3_api = api;
|
||||
|
||||
module_allocator = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
|
||||
var db = sqlite.Db{
|
||||
.db = raw_db,
|
||||
};
|
||||
|
||||
createAllFunctions(&db) catch |err| {
|
||||
logger.err("unable to create all SQLite functions, err: {!}", .{err});
|
||||
return c.SQLITE_ERROR;
|
||||
};
|
||||
|
||||
return c.SQLITE_OK;
|
||||
}
|
||||
56
zig-vendor/zig-sqlite/examples/zigcrypto_test.zig
Normal file
56
zig-vendor/zig-sqlite/examples/zigcrypto_test.zig
Normal file
@@ -0,0 +1,56 @@
|
||||
const builtin = @import("builtin");
|
||||
const std = @import("std");
|
||||
const debug = std.debug;
|
||||
const mem = std.mem;
|
||||
|
||||
const sqlite = @import("sqlite");
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
var arena = std.heap.ArenaAllocator.init(gpa.allocator());
|
||||
defer arena.deinit();
|
||||
const allocator = arena.allocator();
|
||||
|
||||
//
|
||||
|
||||
var db = try sqlite.Db.init(.{
|
||||
.mode = sqlite.Db.Mode{ .Memory = {} },
|
||||
.open_flags = .{ .write = true },
|
||||
});
|
||||
defer db.deinit();
|
||||
|
||||
{
|
||||
const result = sqlite.c.sqlite3_enable_load_extension(db.db, 1);
|
||||
debug.assert(result == sqlite.c.SQLITE_OK);
|
||||
}
|
||||
|
||||
{
|
||||
const extension_path = if (builtin.os.tag == .windows)
|
||||
"./zig-out/bin/zigcrypto.dll"
|
||||
else
|
||||
"./zig-out/lib/libzigcrypto";
|
||||
|
||||
var pzErrMsg: [*c]u8 = undefined;
|
||||
const result = sqlite.c.sqlite3_load_extension(db.db, extension_path, null, &pzErrMsg);
|
||||
if (result != sqlite.c.SQLITE_OK) {
|
||||
const err = sqlite.c.sqlite3_errstr(result);
|
||||
std.debug.panic("unable to load extension at path {s}, err: {s}, err message: {s}\n", .{ extension_path, err, std.mem.sliceTo(pzErrMsg, 0) });
|
||||
}
|
||||
}
|
||||
|
||||
var diags = sqlite.Diagnostics{};
|
||||
|
||||
const blake3_digest = db.oneAlloc([]const u8, allocator, "SELECT hex(blake3('foobar'))", .{ .diags = &diags }, .{}) catch |err| {
|
||||
debug.print("unable to get blake3 hash, err: {}, diags: {f}\n", .{ err, diags });
|
||||
return err;
|
||||
};
|
||||
debug.assert(blake3_digest != null);
|
||||
debug.assert(mem.eql(u8, "AA51DCD43D5C6C5203EE16906FD6B35DB298B9B2E1DE3FCE81811D4806B76B7D", blake3_digest.?));
|
||||
|
||||
const sha3_digest = db.oneAlloc([]const u8, allocator, "SELECT hex(sha3_512('foobar'))", .{ .diags = &diags }, .{}) catch |err| {
|
||||
debug.print("unable to get sha3 hash, err: {}, diags: {f}\n", .{ err, diags });
|
||||
return err;
|
||||
};
|
||||
debug.assert(sha3_digest != null);
|
||||
debug.assert(mem.eql(u8, "FF32A30C3AF5012EA395827A3E99A13073C3A8D8410A708568FF7E6EB85968FCCFEBAEA039BC21411E9D43FDB9A851B529B9960FFEA8679199781B8F45CA85E2", sha3_digest.?));
|
||||
}
|
||||
Reference in New Issue
Block a user