Something went wrong. Try again.
Dev fork of jetzig jetzig.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221const std = @import("std");const datetime = @import("datetime");const Where = @import("sql/Where.zig");
const Type = std.lang.Type;
pub const FieldContext = enum { where, update, insert, limit, offset, order, none, returning,};
/// Compat layer for `std.lang.Type.StructField`, which was removed in Zig 0.17./// Field information is now built from parallel arrays (`field_names`, `field_types`,/// `field_attrs`) on `@typeInfo(T).@"struct"`, with field properties moved to/// `Type.Struct.FieldAttributes`. We keep a unified `StructField` so that all type-building/// helpers in JetQuery continue to operate on single values.pub const StructField = struct { name: [:0]const u8, type: type, default_value_ptr: ?*const anyopaque = null, is_comptime: bool = false, alignment: ?usize = null,
pub inline fn defaultValue(comptime sf: StructField) ?sf.type { const dp: *const sf.type = @ptrCast(@alignCast(sf.default_value_ptr orelse return null)); return dp.*; }};
/// Rebuild `StructField`s for a struct type from the new parallel-array layout.pub inline fn structFields(comptime T: type) []const StructField { comptime { const info = @typeInfo(T).@"struct"; var result: [info.field_names.len]StructField = undefined; for ( info.field_names, info.field_types, info.field_attrs, &result, ) |name, FT, attrs, *field| { field.* = .{ .name = name, .type = FT, .default_value_ptr = attrs.default_value_ptr, .is_comptime = attrs.@"comptime", .alignment = attrs.@"align", }; } const final = result; return &final; }}
pub const FieldInfo = struct { info: StructField, name: []const u8, Table: type, context: FieldContext,};
pub fn fieldInfos( Adapter: type, Table: type, relations: []const type, T: type, comptime context: FieldContext,) [Where.tree(Adapter, Table, relations, T, context, 0).values_count]FieldInfo { comptime { const tree = Where.tree(Adapter, Table, relations, T, context, 0); var value_fields: [tree.values_count]FieldInfo = undefined; for (structFields(tree.ValuesTuple), tree.values_fields, 0..) |tuple_field, value_field, index| { value_fields[index] = fieldInfo(tuple_field, value_field.Table, value_field.name, context); } return value_fields; }}
pub fn fieldInfo( comptime field: StructField, Table: type, comptime name: []const u8, comptime context: FieldContext,) FieldInfo { return .{ .info = field, .context = context, .name = name, .Table = Table };}
pub fn FieldValues(Table: type, relations: []const type, comptime fields: []const FieldInfo) type { _ = Table; _ = relations; var new_types: [fields.len]type = undefined; for (fields, 0..) |field, index| { @setEvalBranchQuota(100000); new_types[index] = field.info.type; } return @Tuple(&new_types);}
pub fn ColumnType(Adapter: type, Table: type, comptime field_info: FieldInfo) type { switch (field_info.context) { .limit, .offset => return usize, .where, .update, .insert, .order, .none, .returning => {}, }
if (comptime @hasField(Table.Definition, field_info.name)) { const FT = fieldType(Table.Definition, field_info.name); return if (FT == datetime.DateTime) Adapter.DateTimePrimitive else if (FT == ?datetime.DateTime) ?Adapter.DateTimePrimitive else FT; } else { // We only arrive here when we process triplets, e.g. // `.{ .foo, .lt_eql, 100 }` // But we coerce to the other side of the triplet and only use this type as a fallback in // the specific case that two values (i.e. not a column or SQL function) are used on both // sides of the triplet, e.g.: // `.{ 1, .lt, 100 }` // Without a known coercion target the only thing we can do here is use the value's type // and assume the database adapter will know what to do with it, otherwise we get a // compile error and the user has to do an explicit cast. This is all an edge case of an // edge case. return ComptimeErasedType(field_info.info.type); }}
pub fn structField(comptime name: []const u8, T: type) StructField { comptime { return .{ .name = name ++ "", .type = T, .default_value_ptr = null, .is_comptime = false, .alignment = @alignOf(T), }; }}
pub fn structFieldDefault(comptime name: []const u8, default: anytype) StructField { comptime { return .{ .name = name ++ "", .type = @TypeOf(default), .default_value_ptr = &default, .is_comptime = false, .alignment = @alignOf(@TypeOf(default)), }; }}
pub fn structFieldComptime( comptime name: []const u8, comptime default: anytype,) StructField { comptime { return .{ .name = name ++ "", .type = @TypeOf(default), .default_value_ptr = &default, .is_comptime = true, .alignment = @alignOf(@TypeOf(default)), }; }}
pub fn structType(comptime fields: []const StructField) type { var names: [fields.len][:0]const u8 = undefined; var types: [fields.len]type = undefined; var attrs: [fields.len]Type.Struct.FieldAttributes = undefined; for (fields, &names, &types, &attrs) |f, *n, *t, *a| { n.* = f.name; t.* = f.type; a.* = .{ .@"comptime" = f.is_comptime, .@"align" = f.alignment, .default_value_ptr = f.default_value_ptr, }; } return @Struct(.auto, null, &names, &types, &attrs);}
pub fn fieldType(T: type, comptime name: []const u8) type { const tag = @field(std.meta.FieldEnum(T), name); const FT = std.meta.fieldInfo(T, tag).type; return FT;}
// We need to ensure that we don't store a comptime value otherwise our// entire data structure needs to be comptime-known. This only occurs when a// user does one of the following://// Triplet where both sides are values:// `.where(.{ 1, .eql, 1 })`// SQL string with mixed runtime and comp-time args:// `.where(.{ "foo = ?", .{ 1, a_runtime_value } })`//// Otherwise we have a target type to coerce to, either from the other side of the triplet, or// whatever the column type is defined in the schema for column values.pub fn ComptimeErasedType(T: type) type { const type_info: Type = @typeInfo(T); return switch (type_info) { .comptime_int => isize, .comptime_float => f64, else => T, };}
pub fn ComptimeErasedStructField(field: StructField) StructField { var modified = field; modified.type = ComptimeErasedType(field.type); modified.is_comptime = false; modified.alignment = @alignOf(modified.type); return modified;}