Something went wrong. Try again.
Dev fork of jetzig jetzig.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675const std = @import("std");const Writer = std.Io.Writer;
const jetquery = @import("../../root.zig");
pub fn render( Adapter: type, query_context: jetquery.sql.QueryContext, Table: type, relations: []const type, comptime field_infos: []const jetquery.fields.FieldInfo, comptime columns: []const jetquery.columns.Column, comptime order_clauses: []const jetquery.sql.OrderClause, comptime distinct: ?[]const jetquery.columns.Column, comptime where_clauses: []const jetquery.sql.Where.Tree, comptime group_by: ?[]const jetquery.columns.Column, comptime having_clauses: []const jetquery.sql.Where.Tree,) []const u8 { return switch (query_context) { .select => renderSelect( Adapter, Table, relations, columns, field_infos, order_clauses, where_clauses, group_by, having_clauses, ), .update, .update_all => renderUpdate( Adapter, Table, where_clauses, field_infos, ), .insert => renderInsert( Adapter, Table, field_infos, ), .delete, .delete_all => renderDelete( Adapter, Table, field_infos, where_clauses, query_context, ), .count => renderCount( Adapter, Table, relations, field_infos, where_clauses, distinct, ), .returning => renderReturning( Adapter, Table, relations, field_infos, columns, ), .none => "", };}
fn renderSelect( Adapter: type, Table: type, relations: []const type, comptime columns: []const jetquery.columns.Column, comptime field_infos: []const jetquery.fields.FieldInfo, comptime order_clauses: []const jetquery.sql.OrderClause, comptime where_clauses: []const jetquery.sql.Where.Tree, comptime group_by: ?[]const jetquery.columns.Column, comptime having_clauses: []const jetquery.sql.Where.Tree,) []const u8 { comptime { const select_columns = renderSelectColumns(Adapter, relations, columns); const from = std.fmt.comptimePrint(" FROM {s}", .{Adapter.identifier(Table.name)}); const joins = renderJoins(Adapter, Table, relations);
return std.fmt.comptimePrint( "SELECT{s}{s}{s}{s}{s}{s}{s}", .{ select_columns, from, joins, renderWhere(Adapter, where_clauses), renderGroupBy(Adapter, group_by, having_clauses), renderOrder(Adapter, order_clauses), renderLimit(Adapter, field_infos), }, ); }}
fn renderUpdate( Adapter: type, Table: type, where_clauses: []const jetquery.sql.Where.Tree, comptime field_infos: []const jetquery.fields.FieldInfo,) []const u8 { var buf: [paramsBufSize(Adapter, field_infos, .update, .assign)]u8 = undefined; return std.fmt.comptimePrint( "UPDATE {s} SET {s}{s}", .{ Adapter.identifier(Table.name), renderParams(&buf, Adapter, field_infos, .update, .assign), renderWhere(Adapter, where_clauses), }, );}
pub fn renderUpdateRuntime( allocator: std.mem.Allocator, comptime Adapter: type, comptime Table: type, comptime where_clauses: []const jetquery.sql.Where.Tree, comptime Args: type, field_states: []const jetquery.sql.FieldState, first_value_index: usize,) ![]const u8 { var value_index = first_value_index;
var params_buf: Writer.Allocating = .init(allocator); defer params_buf.deinit(); const params_writer = ¶ms_buf.writer;
inline for (jetquery.fields.structFields(Args), 0..) |field, index| { if (field_states[index].modified) { var param_buf: [8]u8 = undefined; try params_writer.print( "{s}{s} = {s}", .{ if (value_index > first_value_index) ", " else "", Adapter.identifier(field.name), try Adapter.paramSqlBuf(¶m_buf, value_index), }, ); value_index += 1; } }
return try std.fmt.allocPrint( allocator, "UPDATE {s} SET {s}{s}", .{ Adapter.identifier(Table.name), params_buf.writer.buffered(), comptime renderWhere(Adapter, where_clauses), }, );}
fn renderInsert( Adapter: type, Table: type, comptime field_infos: []const jetquery.fields.FieldInfo,) []const u8 { var params_buf: [paramsBufSize(Adapter, field_infos, .insert, .column)]u8 = undefined; var values_buf: [paramsBufSize(Adapter, field_infos, .insert, .value)]u8 = undefined; return std.fmt.comptimePrint( "INSERT INTO {s} ({s}) VALUES ({s})", .{ Adapter.identifier(Table.name), renderParams(¶ms_buf, Adapter, field_infos, .insert, .column), renderParams(&values_buf, Adapter, field_infos, .insert, .value), }, );}
fn renderReturning( Adapter: type, Table: type, relations: []const type, comptime field_infos: []const jetquery.fields.FieldInfo, comptime columns: []const jetquery.columns.Column,) []const u8 { comptime { const insert_slice = renderInsert(Adapter, Table, field_infos); const select_columns = renderSelectColumns(Adapter, relations, columns); return std.fmt.comptimePrint("{s} RETURNING{s}", .{ insert_slice, select_columns, }); }}
fn renderDelete( Adapter: type, Table: type, comptime field_infos: []const jetquery.fields.FieldInfo, comptime where_clauses: []const jetquery.sql.Where.Tree, comptime query_context: jetquery.sql.QueryContext,) []const u8 { const statement = std.fmt.comptimePrint("DELETE FROM {s}", .{Adapter.identifier(Table.name)}); return switch (query_context) { .delete, .delete_all => std.fmt.comptimePrint("{s}{s}{s}", .{ statement, renderWhere(Adapter, where_clauses), renderLimit(Adapter, field_infos), }), else => |tag| @compileError( "Inconsistent query for DELETE: `" ++ @tagName(tag) ++ "` (this is a bug)", ), };}
fn renderCount( Adapter: type, Table: type, comptime relations: []const type, comptime field_infos: []const jetquery.fields.FieldInfo, comptime where_clauses: []const jetquery.sql.Where.Tree, comptime distinct: ?[]const jetquery.columns.Column,) []const u8 { comptime { const count_column = " " ++ Adapter.countSql(distinct); const from = std.fmt.comptimePrint(" FROM {s}", .{Adapter.identifier(Table.name)}); const joins = renderJoins(Adapter, Table, relations);
return std.fmt.comptimePrint( "SELECT{s}{s}{s}{s}{s}", .{ count_column, from, joins, renderWhere(Adapter, where_clauses), renderLimit(Adapter, field_infos), }, ); }}
fn renderLimit( Adapter: type, comptime field_infos: []const jetquery.fields.FieldInfo,) []const u8 { if (!hasParam(field_infos, .limit)) return "";
const offset = renderOffset(Adapter, field_infos);
return std.fmt.comptimePrint( " LIMIT {s}{s}", .{ Adapter.paramSql(lastParamIndex(field_infos, .limit)), offset }, );}
fn renderOffset( Adapter: type, comptime field_infos: []const jetquery.fields.FieldInfo,) []const u8 { if (!hasParam(field_infos, .offset)) return "";
return std.fmt.comptimePrint( " OFFSET {s}", .{Adapter.paramSql(lastParamIndex(field_infos, .offset))}, );}fn renderOrder( Adapter: type, comptime order_clauses: []const jetquery.sql.OrderClause,) []const u8 { if (order_clauses.len == 0) return "";
var size: usize = 0; for (order_clauses, 0..) |order_clause, index| { const separator = if (index + 1 < order_clauses.len) ", " else ""; size += (Adapter.orderSql(order_clause) ++ separator).len; } var order_buf: [size]u8 = undefined; var cursor: usize = 0; for (order_clauses, 0..) |order_clause, index| { const separator = if (index + 1 < order_clauses.len) ", " else ""; const order_sql = Adapter.orderSql(order_clause) ++ separator; @memcpy(order_buf[cursor .. cursor + order_sql.len], order_sql); cursor += order_sql.len; } return std.fmt.comptimePrint( " ORDER BY {s}", .{order_buf}, );}
fn renderGroupBy( Adapter: type, comptime maybe_group_by: ?[]const jetquery.columns.Column, comptime having_clauses: []const jetquery.sql.Where.Tree,) []const u8 { const group_by = maybe_group_by orelse return "";
var size: usize = 0;
for (group_by, 0..) |column, index| { const separator = if (index + 1 < group_by.len) ", " else ""; const column_sql = Adapter.columnSql(column) ++ separator; size += column_sql.len; }
const and_operator = " AND "; const having = " HAVING "; if (having_clauses.len > 0) size += having.len; for (having_clauses, 0..) |clause, index| { if (index > 0) size += and_operator.len; size += clause.render(Adapter).len; }
var buf: [size]u8 = undefined; var cursor: usize = 0;
for (group_by, 0..) |column, index| { const separator = if (index + 1 < group_by.len) ", " else ""; const column_sql = Adapter.columnSql(column) ++ separator; @memcpy(buf[cursor .. cursor + column_sql.len], column_sql); cursor += column_sql.len; }
if (having_clauses.len > 0) { @memcpy(buf[cursor .. cursor + having.len], having); cursor += having.len; }
for (having_clauses, 0..) |clause, index| { if (index > 0) size += and_operator.len; const operator = if (index > 0) and_operator else ""; const sql = operator ++ clause.render(Adapter); @memcpy(buf[cursor .. cursor + sql.len], sql); cursor += sql.len; }
return std.fmt.comptimePrint(" GROUP BY {s}", .{buf});}
fn renderWhere( Adapter: type, comptime where_clauses: []const jetquery.sql.Where.Tree,) []const u8 { if (where_clauses.len == 0) return " WHERE " ++ Adapter.emptyWhereSql();
const and_operator = " AND "; var size: usize = 0; for (where_clauses, 0..) |clause, index| { if (index > 0) size += and_operator.len; size += clause.render(Adapter).len; } var buf: [size]u8 = undefined; var cursor: usize = 0; for (where_clauses, 0..) |clause, index| { const operator = if (index > 0) and_operator else ""; const sql = operator ++ clause.render(Adapter); @memcpy(buf[cursor .. cursor + sql.len], sql); cursor += sql.len; }
return std.fmt.comptimePrint(" WHERE {s}", .{&buf});}
fn renderJoins(Adapter: type, Table: type, relations: []const type) []const u8 { comptime { if (relations.len == 0) return "";
var buf_len: usize = 0; for (relations) |Relation| { buf_len += switch (Relation.context) { .inner => renderInnerJoin(Adapter, Table, Relation).len, .outer => renderOuterJoin(Adapter, Table, Relation).len, .include => switch (Relation.relation_type) { .belongs_to => renderInnerJoin(Adapter, Table, Relation).len, .has_many => "".len, }, }; } var buf: [buf_len]u8 = undefined; var cursor: usize = 0; for (relations) |Relation| { const sql = switch (Relation.context) { .inner => renderInnerJoin(Adapter, Table, Relation), .outer => renderOuterJoin(Adapter, Table, Relation), .include => switch (Relation.relation_type) { .belongs_to => renderInnerJoin(Adapter, Table, Relation), .has_many => "", // We issue separate queries for has_many }, }; @memcpy(buf[cursor .. cursor + sql.len], sql); cursor += sql.len; }
return &buf; }}
fn renderInnerJoin(Adapter: type, Table: type, Relation: type) []const u8 { const PrimaryKey = std.meta.FieldEnum(Relation.Source.Definition); const ForeignKey = std.meta.FieldEnum(Table.Definition); const primary_key: PrimaryKey = std.meta.stringToEnum(PrimaryKey, switch (Relation.relation_type) { .belongs_to => Relation.primary_key, .has_many => Table.defaultForeignKey(), }) orelse @compileError("Invalid primary key field"); const foreign_key: ForeignKey = std.meta.stringToEnum(ForeignKey, Relation.foreign_key orelse switch (Relation.relation_type) { .belongs_to => Table.primary_key, .has_many => Relation.Source.primary_key, }) orelse @compileError("Invalid foreign key field");
return Adapter.innerJoinSql( Table, Relation.Source, Relation.relation_name, .{ .primary_key = @tagName(primary_key), .foreign_key = @tagName(foreign_key) }, );}
fn renderOuterJoin(Adapter: type, Table: type, Relation: type) []const u8 { const PrimaryKey = std.meta.FieldEnum(Relation.Source.Definition); const ForeignKey = std.meta.FieldEnum(Table.Definition); const primary_key: PrimaryKey = std.meta.stringToEnum(PrimaryKey, switch (Relation.relation_type) { .belongs_to => Relation.primary_key, .has_many => Table.defaultForeignKey(), }) orelse @compileError("Invalid primary key field"); const foreign_key: ForeignKey = std.meta.stringToEnum(ForeignKey, Relation.foreign_key orelse switch (Relation.relation_type) { .belongs_to => Table.primary_key, .has_many => Relation.Source.primary_key, }) orelse @compileError("Invalid foreign key field");
return Adapter.outerJoinSql( Table, Relation.Source, Relation.relation_name, .{ .primary_key = @tagName(primary_key), .foreign_key = @tagName(foreign_key) }, );}
fn renderSelectColumns( Adapter: type, relations: []const type, comptime columns: []const jetquery.columns.Column,) []const u8 { comptime { var total_columns: usize = columns.len; for (relations) |Relation| { // has_many relations issue a separate query so we don't select columns here. // Only belongs_to uses an inner join. if (Relation.relation_type != .belongs_to) continue; total_columns += Relation.select_columns.len; }
var columns_buf_len: usize = 0; for (columns, 0..) |column, index| { columns_buf_len += renderSelectColumn( Adapter, column, index, total_columns, ).len; }
var start = columns.len; for (relations) |Relation| { // has_many relations issue a separate query so we don't select columns here. // Only belongs_to uses an inner join. if (Relation.relation_type != .belongs_to) continue;
for (Relation.select_columns, start..) |column, index| { columns_buf_len += renderSelectColumn( Adapter, column, index, total_columns, ).len; } start += Relation.select_columns.len; }
var columns_buf: [columns_buf_len]u8 = undefined; var cursor: usize = 0; for (columns, 0..) |column, index| { const column_tag = renderSelectColumn( Adapter, column, index, total_columns, ); @memcpy(columns_buf[cursor .. cursor + column_tag.len], column_tag); cursor += column_tag.len; }
start = columns.len; for (relations) |Relation| { // has_many relations issue a separate query so we don't select columns here. // Only belongs_to uses an inner join. if (Relation.relation_type != .belongs_to) continue;
for (Relation.select_columns, start..) |column, index| { const column_tag = renderSelectColumn( Adapter, column, index, total_columns, ); @memcpy(columns_buf[cursor .. cursor + column_tag.len], column_tag); cursor += column_tag.len; } start += Relation.select_columns.len; } return &columns_buf; }}
fn renderSelectColumn( Adapter: type, comptime column: jetquery.columns.Column, comptime index: usize, comptime total: usize,) []const u8 { comptime { @setEvalBranchQuota(100000); return std.fmt.comptimePrint( " {s}{s}", .{ Adapter.columnSql(column), if (index + 1 < total) "," else "" }, ); }}
// TODO: Find a nicer way of counting so we don't have to keep this sync'ed with `renderParams`fn paramsBufSize( Adapter: type, comptime field_infos: []const jetquery.fields.FieldInfo, comptime context: jetquery.fields.FieldContext, comptime format: enum { column, value, assign },) usize { var buf_len: usize = 0;
const separator = switch (context) { .where => " AND ", .update, .insert => ", ", else => @compileError("Unsupported param type: `" ++ @tagName(context) ++ "`"), };
const last_param_index = lastParamIndex(field_infos, context);
const template = switch (format) { .column, .value => "{s}{s}", .assign => "{s} = {s}{s}", };
for (field_infos, 0..) |field, index| { if (!fieldContext(field_infos, index, context)) continue;
const args = switch (format) { .column => .{ switch (context) { .insert => Adapter.identifier(field.name), else => Adapter.columnSql(field), }, if (index < last_param_index) separator else "", }, .value => .{ Adapter.paramSql(index), if (index < last_param_index) separator else "", }, .assign => switch (context) { .update => .{ Adapter.identifier(field.name), Adapter.paramSql(index), if (index < last_param_index) separator else "", }, else => .{ Adapter.columnSql(field), Adapter.paramSql(index), if (index < last_param_index) separator else "", }, }, };
buf_len += std.fmt.count(template, args); } return buf_len;}
fn renderParams( buf: []u8, Adapter: type, comptime field_infos: []const jetquery.fields.FieldInfo, comptime context: jetquery.fields.FieldContext, comptime format: enum { column, value, assign },) []const u8 { if (!hasParam(field_infos, context)) @compileError("Failed compiling UPDATE query with empty params.");
const separator = switch (context) { .where => " AND ", .update, .insert => ", ", else => @compileError("Unsupported param type: `" ++ @tagName(context) ++ "`"), };
const last_param_index = lastParamIndex(field_infos, context); var cursor: usize = 0;
const template = switch (format) { .column, .value => "{s}{s}", .assign => "{s} = {s}{s}", };
for (field_infos, 0..) |field, index| { if (!fieldContext(field_infos, index, context)) continue;
const args = switch (format) { .column => .{ switch (context) { .insert => Adapter.identifier(field.name), else => Adapter.columnSql(field), }, if (index < last_param_index) separator else "", }, .value => .{ Adapter.paramSql(index), if (index < last_param_index) separator else "", }, .assign => switch (context) { .update => .{ Adapter.identifier(field.name), Adapter.paramSql(index), if (index < last_param_index) separator else "", }, else => .{ Adapter.columnSql(field), Adapter.paramSql(index), if (index < last_param_index) separator else "", }, }, }; @setEvalBranchQuota(100000); const param = std.fmt.comptimePrint(template, args); @memcpy(buf[cursor .. cursor + param.len], param); cursor += param.len; } return buf;}
fn fieldContext( comptime field_infos: []const jetquery.fields.FieldInfo, comptime index: usize, comptime context: jetquery.fields.FieldContext,) bool { return if (field_infos.len > index) field_infos[index].context == context else false;}
fn lastParamIndex( comptime field_infos: []const jetquery.fields.FieldInfo, comptime context: jetquery.fields.FieldContext,) usize { var maybe_index: ?usize = null;
for (field_infos, 0..) |field, index| { if (field.context == context) maybe_index = index; } if (maybe_index) |index| return index else @compileError("No param matched for `" ++ @tagName(context) ++ "` query.");}
fn hasParam( comptime field_infos: []const jetquery.fields.FieldInfo, comptime context: jetquery.fields.FieldContext,) bool { for (field_infos) |field| { if (field.context == context) return true; } return false;}