Something went wrong. Try again.
Dev fork of jetzig jetzig.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289const std = @import("std");
const jetquery = @import("../root.zig");
pub const PostgresqlAdapter = @import("adapters/PostgresqlAdapter.zig");pub const NullAdapter = @import("adapters/NullAdapter.zig");
pub const Name = enum { postgresql, null };
pub fn Type(adapter: Name) type { return switch (adapter) { .postgresql => PostgresqlAdapter, .null => NullAdapter, };}
pub const ConnectionOptions = struct { context: jetquery.Context };
pub fn Adapter(comptime adapter_name: Name, AdaptedRepo: type) type { const Union = union(enum) { postgresql: PostgresqlAdapter, null: NullAdapter,
const Self = @This();
pub const name = adapter_name;
pub fn connect(self: *Self, options: ConnectionOptions) !jetquery.Connection { return switch (comptime adapter_name) { inline else => |tag| try @field(self, @tagName(tag)).connect(options), }; }
pub fn release(self: *Self, connection: jetquery.Connection) void { return switch (comptime adapter_name) { inline else => |tag| @field(self, @tagName(tag)).release(connection), }; }
/// Convert a column type to a database type suitable for the active adapter. pub fn columnTypeSql(self: Self, comptime column: jetquery.schema.Column) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).columnTypeSql(column), }; }
/// Quote an identifier (e.g. a table name) suitable for the active adapter. pub fn identifier(self: Self, comptime value: []const u8) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).identifier(value), }; }
pub fn identifierAlloc( self: Self, allocator: std.mem.Allocator, value: []const u8, ) ![]const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).identifierAlloc(allocator, value), }; }
/// Quote a column bound to a table suitable for the active adapter. pub fn columnSql( self: Self, comptime column: jetquery.columns.Column, ) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).columnSql(column), }; }
/// SQL fragment used to indicate a primary key. pub fn primaryKeySql(self: Self, comptime column: jetquery.schema.Column) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).primaryKeySql(column), }; }
/// SQL fragment used to indicate a column whose value cannot be `NULL`. pub fn notNullSql(self: Self) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).notNullSql(), }; }
/// SQL fragment used to set a default value for a column. pub fn defaultValueSql(self: Self, comptime default_value: []const u8) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).defaultValueSql(default_value), }; }
/// SQL representing a bind parameter, e.g. `$1`. pub fn paramSql(self: Self, comptime index: usize) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).paramSql(index), }; }
/// Same as `paramSql` but writes to a buffer at runtime. pub fn paramSqlBuf(self: Self, buf: []u8, index: usize) ![]const u8 { return switch (self) { inline else => |adapter| try @TypeOf(adapter).paramSqlBuf(buf, index), }; }
/// SQL representing an array bind parameter with an `ANY` call, e.g. `ANY ($1)`. pub fn anyParamSql(self: Self, comptime index: usize) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).anyParamSql(index), }; }
/// SQL representing an `ORDER BY` directive, e.g. `"foo" DESC` pub fn orderSql(self: Self, comptime order_clause: jetquery.OrderClause) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).orderSql(order_clause), }; }
/// SQL fragment used when generating a `COUNT` column, e.g. `COUNT(*)` pub fn countSql( self: Self, comptime distinct: ?[]const jetquery.columns.Column, ) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).countSql(distinct), }; }
/// SQL representing an inner join, e.g. `INNER JOIN "foo" ON "bar"."baz" = "foo"."baz"` pub fn innerJoinSql( self: Self, Model: type, JoinTable: type, comptime relation_name: []const u8, comptime options: JoinOptions, ) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).innerJoinSql( Model, JoinTable, relation_name, options, ), }; }
/// SQL representing an outer join, e.g. `LEFT OUTER JOIN "foo" ON "bar"."baz" = "foo"."baz"` pub fn outerJoinSql( self: Self, Model: type, JoinTable: type, comptime relation_name: []const u8, comptime options: JoinOptions, ) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).outerJoinSql( Model, JoinTable, relation_name, options, ), }; }
/// SQL fragment used as a `WHERE` clause when no clause has been applied by the user. pub fn emptyWhereSQL(self: Self) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).emptyWhereSQL(), }; }
/// Automatically generate an index name from the given table name and columns. Fails if /// generated name is too long for adapter's identifier length limit. pub fn indexName( self: Self, comptime table_name: []const u8, comptime column_names: []const []const u8, ) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).indexName(table_name, column_names), }; }
/// Generate SQL for creating an index with the active adapter. pub fn createIndexSql( self: Self, comptime index_name: []const u8, comptime table_name: []const u8, comptime column_names: []const []const u8, comptime options: AdaptedRepo.CreateIndexOptions, ) []const u8 { return switch (self) { inline else => |adapter| &@TypeOf(adapter).createIndexSql( index_name, table_name, column_names, options, ), }; }
/// SQL fragment used when specifying a unique constraint. pub fn uniqueColumnSql(self: Self) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).uniqueColumnSql(), }; }
/// SQL fragment used to denote a foreign key. pub fn referenceSql( self: Self, comptime reference: jetquery.schema.Column.Reference, ) []const u8 { return switch (self) { inline else => |adapter| @TypeOf(adapter).referenceSql(reference), }; }
/// Resolve an appropriate type for a given aggregate function (e.g. COUNT, MIN, MAX, etc.). pub fn Aggregate(self: Self, context: jetquery.sql.FunctionContext) type { return switch (self) { inline else => |adapter| @TypeOf(adapter).Aggregate(context), }; }
/// Return all metadata from the database needed to generate a schema file. pub fn reflect( self: *Self, allocator: std.mem.Allocator, repo: *AdaptedRepo, ) !jetquery.Reflection { return switch (comptime adapter_name) { inline else => |tag| try @field(self, @tagName(tag)).reflect(allocator, repo), }; }
pub fn writeAddColumnSql( self: Self, comptime column: jetquery.schema.Column, writer: anytype, ) !void { if (column.timestamps) |timestamps| { try timestamps.toSql(writer, self); } else { try writer.print( \\{s}{s}{s}{s}{s}{s}{s} , .{ self.identifier(column.name), if (column.primary_key) "" else self.columnTypeSql(column), if (!column.primary_key and !column.options.optional) self.notNullSql() else "", if (column.options.default) |default_value| self.defaultValueSql(default_value) else "", if (column.primary_key) self.primaryKeySql(column) else "", if (column.options.unique) self.uniqueColumnSql() else "", if (column.options.reference) |reference| self.referenceSql(reference) else "", }); } } }; return Union;}
pub const JoinOptions = struct { foreign_key: ?[]const u8 = null, primary_key: ?[]const u8 = null,};
pub const test_adapter = Adapter(.postgresql){ .postgresql = .{ .options = undefined, .pool = undefined, .allocator = undefined, .connected = undefined,} };