mirror of
https://github.com/ziex-dev/ziex.git
synced 2026-07-19 10:09:36 -06:00
feat: setup adaptable Postgres stub
This commit is contained in:
parent
a7f47a4137
commit
cb3c666df5
11
build.zig
11
build.zig
@ -14,6 +14,7 @@ pub fn build(b: *std.Build) !void {
|
||||
|
||||
const enable_lsp = b.option(bool, "lsp", "Enabled zx lsp") orelse false;
|
||||
const enable_sqlite = b.option(bool, "feature-sqlite", "Enabled sqlite support") orelse false;
|
||||
const enable_postgres = b.option(bool, "feature-postgres", "Enabled postgres support") orelse false;
|
||||
const exclude_core_lang = b.option(bool, "exclude-core-lang", "Exclude core language tools (Ast/Parse/sourcemap) - only needed by CLI") orelse false;
|
||||
const version = b.option([]const u8, "version", "Version to embed in the binary") orelse build_zon.version;
|
||||
|
||||
@ -54,8 +55,12 @@ pub fn build(b: *std.Build) !void {
|
||||
{
|
||||
if (!is_client) {
|
||||
if (enable_sqlite) {
|
||||
const adapters_dep = b.lazyDependency("adapters", .{ .target = target, .optimize = optimize });
|
||||
if (adapters_dep) |a| mod.addImport("zqlite", a.module("zqlite"));
|
||||
const db_sqlite_dep = b.lazyDependency("db_sqlite", .{ .target = target, .optimize = optimize });
|
||||
if (db_sqlite_dep) |a| mod.addImport("zqlite", a.module("zqlite"));
|
||||
}
|
||||
if (enable_postgres) {
|
||||
const db_postgres_dp = b.lazyDependency("db_postgres", .{ .target = target, .optimize = optimize });
|
||||
if (db_postgres_dp) |a| mod.addImport("db_postgres", a.module("postgres"));
|
||||
}
|
||||
|
||||
mod.addImport("httpz", httpz_dep.module("httpz"));
|
||||
@ -111,7 +116,7 @@ pub fn build(b: *std.Build) !void {
|
||||
// --- Steps: Test --- //
|
||||
{
|
||||
const mode_test = b.createModule(.{ .root_source_file = b.path("src/root.zig") });
|
||||
if (b.lazyDependency("adapters", .{})) |ad| mode_test.addImport("zqlite", ad.module("zqlite"));
|
||||
if (b.lazyDependency("db_sqlite", .{})) |ad| mode_test.addImport("zqlite", ad.module("zqlite"));
|
||||
mode_test.addOptions("zx_info", options);
|
||||
mode_test.addOptions("zx_module_options", zx_module_options);
|
||||
mode_test.addImport("zx_core_lang", zx_core_lang_mod);
|
||||
|
||||
@ -24,22 +24,23 @@
|
||||
// .hash = "zls-0.17.0-dev-rmm5fhsnJgACAm2dSuoMNDK6QziDk419sy5-ORvIiL0y",
|
||||
// .lazy = true,
|
||||
// },
|
||||
.adapters = .{ .path = "pkg/adapters", .lazy = true },
|
||||
.zli = .{ .path = "vendor/cliz" }, // Get rid of this in favor of copying from Zig's new builtin args parser once 0.16.0 is released
|
||||
.db_sqlite = .{ .path = "pkg/db-sqlite", .lazy = true },
|
||||
.db_postgres = .{ .path = "pkg/db-postgres", .lazy = true },
|
||||
//TODO: Get rid of zli in favor of copying from Zig's new builtin args parser once 0.16.0 is released
|
||||
.zli = .{ .path = "vendor/cliz" },
|
||||
.zig_js = .{ .path = "vendor/jsz" },
|
||||
.tree_sitter = .{ .path = "vendor/zig-tree-sitter" },
|
||||
.tree_sitter_zx = .{ .path = "pkg/tree-sitter-zx" },
|
||||
.tree_sitter_mdzx = .{ .path = "pkg/tree-sitter-mdzx" },
|
||||
.cachez = .{ .path = "vendor/cachez" },
|
||||
},
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
"src",
|
||||
"pkg/adapters",
|
||||
"pkg/db-sqlite",
|
||||
"pkg/db-postgres",
|
||||
"pkg/tree-sitter-zx",
|
||||
"pkg/tree-sitter-mdzx",
|
||||
"vendor/cachez",
|
||||
"vendor/cliz",
|
||||
"vendor/jsz",
|
||||
"vendor/zig-tree-sitter",
|
||||
|
||||
12
pkg/db-postgres/build.zig
Normal file
12
pkg/db-postgres/build.zig
Normal file
@ -0,0 +1,12 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
_ = b.addModule("postgres", .{
|
||||
.root_source_file = b.path("src/postgres.zig"),
|
||||
.optimize = optimize,
|
||||
.target = target,
|
||||
});
|
||||
}
|
||||
7
pkg/db-postgres/build.zig.zon
Normal file
7
pkg/db-postgres/build.zig.zon
Normal file
@ -0,0 +1,7 @@
|
||||
.{
|
||||
.name = .db_postgres,
|
||||
.version = "0.1.0",
|
||||
.fingerprint = 0x833b56a066777e92,
|
||||
.minimum_zig_version = "0.17.0-dev.892+54537285c",
|
||||
.paths = .{""},
|
||||
}
|
||||
87
pkg/db-postgres/src/postgres.zig
Normal file
87
pkg/db-postgres/src/postgres.zig
Normal file
@ -0,0 +1,87 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn Create(Db: type) type {
|
||||
return struct {
|
||||
const Postgres = @This();
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
pub const OpenOptions = struct {
|
||||
uri: std.Uri,
|
||||
};
|
||||
|
||||
pub fn open(allocator: std.mem.Allocator, io: std.Io, options: OpenOptions) !Db {
|
||||
_ = allocator;
|
||||
_ = io;
|
||||
_ = options;
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
|
||||
pub fn db(self: *Postgres) Db {
|
||||
return .{ .userdata = self, .vtable = &database_vtable };
|
||||
}
|
||||
|
||||
pub fn from(database: Db) !*Postgres {
|
||||
if (database.vtable != &database_vtable) return Db.DbError.WrongBackend;
|
||||
return @ptrCast(@alignCast(database.userdata orelse return Db.DbError.InvalidState));
|
||||
}
|
||||
|
||||
fn prepare(_: ?*anyopaque, _: []const u8) !Db.Statement {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn run(_: ?*anyopaque, _: []const u8, _: Db.Bindings) !Db.RunResult {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn transaction(_: ?*anyopaque, _: Db.TransactionMode, _: *anyopaque, _: Db.TransactionCallback) !void {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn close(_: ?*anyopaque, _: bool) !void {
|
||||
return;
|
||||
}
|
||||
fn stmtAll(_: ?*anyopaque, _: std.mem.Allocator, _: Db.Bindings) ![]const Db.Row {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtGet(_: ?*anyopaque, _: std.mem.Allocator, _: Db.Bindings) !?Db.Row {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtRun(_: ?*anyopaque, _: Db.Bindings) !Db.RunResult {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtValues(_: ?*anyopaque, _: std.mem.Allocator, _: Db.Bindings) ![]const []const Db.Value {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtIterate(_: ?*anyopaque, _: Db.Bindings) !Db.Statement.Iterator {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtFinalize(_: ?*anyopaque) void {}
|
||||
fn stmtToString(_: ?*anyopaque, _: std.mem.Allocator) ![]u8 {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtColumnNames(_: ?*anyopaque) []const []const u8 {
|
||||
return &.{};
|
||||
}
|
||||
fn stmtColumnTypes(_: ?*anyopaque) []const Db.ColumnType {
|
||||
return &.{};
|
||||
}
|
||||
fn stmtParamsCount(_: ?*anyopaque) usize {
|
||||
return 0;
|
||||
}
|
||||
|
||||
pub const database_vtable = .{
|
||||
.prepare = &prepare,
|
||||
.run = &run,
|
||||
.transaction = &transaction,
|
||||
.close = &close,
|
||||
.stmtAll = &stmtAll,
|
||||
.stmtGet = &stmtGet,
|
||||
.stmtRun = &stmtRun,
|
||||
.stmtValues = &stmtValues,
|
||||
.stmtIterate = &stmtIterate,
|
||||
.stmtFinalize = &stmtFinalize,
|
||||
.stmtToString = &stmtToString,
|
||||
.stmtColumnNames = &stmtColumnNames,
|
||||
.stmtColumnTypes = &stmtColumnTypes,
|
||||
.stmtParamsCount = &stmtParamsCount,
|
||||
};
|
||||
};
|
||||
}
|
||||
@ -1,13 +1,12 @@
|
||||
.{
|
||||
.name = .ziex_adapters,
|
||||
.version = "0.0.1",
|
||||
.fingerprint = 0x74548a19a807c4e8,
|
||||
.name = .db_sqlite,
|
||||
.version = "0.1.0",
|
||||
.fingerprint = 0xa9acddbf8d97456d,
|
||||
.minimum_zig_version = "0.16.0",
|
||||
.dependencies = .{},
|
||||
.paths = .{
|
||||
"build.zig",
|
||||
"build.zig.zon",
|
||||
"src",
|
||||
"vendor/sqlite",
|
||||
"vendor/zqlite/src",
|
||||
},
|
||||
@ -121,6 +121,7 @@ pub fn build(b: *std.Build) !void {
|
||||
// .copy_embedded_sources = true,
|
||||
.features = .{
|
||||
.sqlite = .enabled,
|
||||
// .postgres = .enabled,
|
||||
.kv = .enabled,
|
||||
.cache = .enabled,
|
||||
},
|
||||
|
||||
@ -18,6 +18,7 @@ pub fn init(b: *std.Build, exe: *std.Build.Step.Compile, options: InitOptions) !
|
||||
.target = target,
|
||||
.@"exclude-core-lang" = true, // Users don't need parser/transpiler
|
||||
.@"feature-sqlite" = if (options.app != null and options.app.?.features.sqlite != null) true else null,
|
||||
.@"feature-postgres" = if (options.app != null and options.app.?.features.postgres != null) true else null,
|
||||
});
|
||||
|
||||
const zx_host_dep = b.dependencyFromBuildZig(build_zig, .{
|
||||
@ -283,6 +284,7 @@ pub fn initInner(
|
||||
zx_options.addOption([]const u8, "cli_command", cli_command_opt orelse "--");
|
||||
zx_options.addOption(bool, "introspect", b.option(bool, "introspect", "Print Ziex app metadata and exit") orelse false);
|
||||
zx_options.addOption(bool, "feat_sqlite_server", if (opts.features.sqlite) |s| s.server != null else false);
|
||||
zx_options.addOption(bool, "feat_pg_server", if (opts.features.postgres) |s| s.server != null else false);
|
||||
zx_options.addOption(bool, "feat_kv_server", if (opts.features.kv) |k| k.server != null else false);
|
||||
zx_options.addOption(bool, "feat_kv_client", if (opts.features.kv) |k| k.client != null else false);
|
||||
zx_options.addOption(bool, "feat_cache_server", if (opts.features.cache) |c| c.server != null else false);
|
||||
|
||||
@ -68,6 +68,16 @@ pub const AppOptions = struct {
|
||||
server: ?SqliteServerOptions = null,
|
||||
};
|
||||
|
||||
/// Enable Postgres database client, available through `zx.Db.Postgres`.
|
||||
pub const PostgresOptions = struct {
|
||||
pub const PostgresServerOptions = struct {
|
||||
pub const enabled: PostgresServerOptions = .{};
|
||||
};
|
||||
pub const enabled: PostgresOptions = .{ .server = .enabled };
|
||||
|
||||
server: ?PostgresServerOptions = null,
|
||||
};
|
||||
|
||||
/// Filesystem-backed key/value store, exposed at runtime as `zx.kv`.
|
||||
pub const KvOptions = struct {
|
||||
pub const KvServerOptions = struct {
|
||||
@ -96,6 +106,7 @@ pub const AppOptions = struct {
|
||||
pub const default = FeatureOptions{};
|
||||
|
||||
sqlite: ?SqliteOptions = null,
|
||||
postgres: ?PostgresOptions = null,
|
||||
kv: ?KvOptions = null,
|
||||
cache: ?CacheOptions = null,
|
||||
};
|
||||
|
||||
@ -1,113 +1,10 @@
|
||||
const Postgres = @This();
|
||||
const Postgres = postgres.Create(Db);
|
||||
|
||||
const std = @import("std");
|
||||
const postgres = @import("db_postgres");
|
||||
const Db = @import("../Db.zig");
|
||||
|
||||
allocator: std.mem.Allocator,
|
||||
|
||||
pub const OpenOptions = struct {
|
||||
readonly: bool = false,
|
||||
create: bool = true,
|
||||
max_pool_size: usize = 5,
|
||||
|
||||
host: ?[]const u8 = null,
|
||||
port: u16 = 5432,
|
||||
user: ?[]const u8 = null,
|
||||
password: ?[]const u8 = null,
|
||||
database: ?[]const u8 = null,
|
||||
sslmode: SslMode = .prefer,
|
||||
connect_timeout_ms: u32 = 10_000,
|
||||
|
||||
pub const SslMode = enum { disable, prefer, require, verify_full };
|
||||
|
||||
pub fn fromNeutral(neutral: Db.OpenOptions) OpenOptions {
|
||||
return .{
|
||||
.readonly = neutral.readonly,
|
||||
.create = neutral.create,
|
||||
.max_pool_size = neutral.max_pool_size,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub fn open(allocator: std.mem.Allocator, io: std.Io, url: ?[]const u8, options: anytype) !Db {
|
||||
_ = allocator;
|
||||
_ = io;
|
||||
_ = url;
|
||||
_ = normalizeOptions(options);
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
|
||||
fn normalizeOptions(options: anytype) OpenOptions {
|
||||
const T = @TypeOf(options);
|
||||
if (T == OpenOptions) return options;
|
||||
if (T == Db.OpenOptions) return OpenOptions.fromNeutral(options);
|
||||
if (@typeInfo(T) == .@"struct") return options;
|
||||
@compileError("Postgres.open expects Postgres.OpenOptions or Db.OpenOptions, got " ++ @typeName(T));
|
||||
}
|
||||
|
||||
pub fn db(self: *Postgres) Db {
|
||||
return .{ .userdata = self, .vtable = &database_vtable };
|
||||
}
|
||||
|
||||
pub fn from(database: Db) !*Postgres {
|
||||
if (database.vtable != &database_vtable) return Db.DbError.WrongBackend;
|
||||
return @ptrCast(@alignCast(database.userdata orelse return Db.DbError.InvalidState));
|
||||
}
|
||||
|
||||
fn prepare(_: ?*anyopaque, _: []const u8) !Db.Statement {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn run(_: ?*anyopaque, _: []const u8, _: Db.Bindings) !Db.RunResult {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn transaction(_: ?*anyopaque, _: Db.TransactionMode, _: *anyopaque, _: Db.TransactionCallback) !void {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn close(_: ?*anyopaque, _: bool) !void {
|
||||
return;
|
||||
}
|
||||
fn stmtAll(_: ?*anyopaque, _: std.mem.Allocator, _: Db.Bindings) ![]const Db.Row {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtGet(_: ?*anyopaque, _: std.mem.Allocator, _: Db.Bindings) !?Db.Row {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtRun(_: ?*anyopaque, _: Db.Bindings) !Db.RunResult {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtValues(_: ?*anyopaque, _: std.mem.Allocator, _: Db.Bindings) ![]const []const Db.Value {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtIterate(_: ?*anyopaque, _: Db.Bindings) !Db.Statement.Iterator {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtFinalize(_: ?*anyopaque) void {}
|
||||
fn stmtToString(_: ?*anyopaque, _: std.mem.Allocator) ![]u8 {
|
||||
return Db.DbError.Unsupported;
|
||||
}
|
||||
fn stmtColumnNames(_: ?*anyopaque) []const []const u8 {
|
||||
return &.{};
|
||||
}
|
||||
fn stmtColumnTypes(_: ?*anyopaque) []const Db.ColumnType {
|
||||
return &.{};
|
||||
}
|
||||
fn stmtParamsCount(_: ?*anyopaque) usize {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const database_vtable = Db.VTable{
|
||||
.prepare = &prepare,
|
||||
.run = &run,
|
||||
.transaction = &transaction,
|
||||
.close = &close,
|
||||
.stmtAll = &stmtAll,
|
||||
.stmtGet = &stmtGet,
|
||||
.stmtRun = &stmtRun,
|
||||
.stmtValues = &stmtValues,
|
||||
.stmtIterate = &stmtIterate,
|
||||
.stmtFinalize = &stmtFinalize,
|
||||
.stmtToString = &stmtToString,
|
||||
.stmtColumnNames = &stmtColumnNames,
|
||||
.stmtColumnTypes = &stmtColumnTypes,
|
||||
.stmtParamsCount = &stmtParamsCount,
|
||||
};
|
||||
pub const OpenOptions = postgres.OpenOptions;
|
||||
pub const open = postgres.open;
|
||||
pub const db = postgres.db;
|
||||
pub const from = postgres.from;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user