Rename relations to relationships.

Closes #11
This commit is contained in:
Madeorsk 2024-11-27 15:26:20 +01:00
parent 66cf93d6f7
commit b897ce70ab
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
11 changed files with 205 additions and 205 deletions

View file

@ -5,7 +5,7 @@ const global = @import("global.zig");
const errors = @import("errors.zig"); const errors = @import("errors.zig");
const database = @import("database.zig"); const database = @import("database.zig");
const _sql = @import("sql.zig"); const _sql = @import("sql.zig");
const _relations = @import("relations.zig"); const _relationships = @import("relationships.zig");
const repository = @import("repository.zig"); const repository = @import("repository.zig");
const _result = @import("result.zig"); const _result = @import("result.zig");
@ -213,29 +213,29 @@ pub fn makeMapper(comptime T: type, result: *pg.Result, allocator: std.mem.Alloc
} }
/// PostgreSQL implementation of the query result reader. /// PostgreSQL implementation of the query result reader.
pub fn QueryResultReader(comptime TableShape: type, comptime MetadataShape: ?type, comptime inlineRelations: ?[]const _relations.Relation) type { pub fn QueryResultReader(comptime TableShape: type, comptime MetadataShape: ?type, comptime inlineRelationships: ?[]const _relationships.Relationship) type {
const InstanceInterface = _result.QueryResultReader(TableShape, MetadataShape, inlineRelations).Instance; const InstanceInterface = _result.QueryResultReader(TableShape, MetadataShape, inlineRelationships).Instance;
// Build relations mappers container type. // Build relationships mappers container type.
const RelationsMappersType = comptime typeBuilder: { const RelationshipsMappersType = comptime typeBuilder: {
if (inlineRelations) |_inlineRelations| { if (inlineRelationships) |_inlineRelationships| {
// Make a field for each relation. // Make a field for each relationship.
var fields: [_inlineRelations.len]std.builtin.Type.StructField = undefined; var fields: [_inlineRelationships.len]std.builtin.Type.StructField = undefined;
for (_inlineRelations, &fields) |relation, *field| { for (_inlineRelationships, &fields) |relationship, *field| {
// Get relation field type (TableShape of the related value). // Get relationship field type (TableShape of the related value).
const relationFieldType = PgMapper(relation.TableShape); const relationshipFieldType = PgMapper(relationship.TableShape);
field.* = .{ field.* = .{
.name = relation.field ++ [0:0]u8{}, .name = relationship.field ++ [0:0]u8{},
.type = relationFieldType, .type = relationshipFieldType,
.default_value = null, .default_value = null,
.is_comptime = false, .is_comptime = false,
.alignment = @alignOf(relationFieldType), .alignment = @alignOf(relationshipFieldType),
}; };
} }
// Build type with one field for each relation. // Build type with one field for each relationship.
break :typeBuilder @Type(std.builtin.Type{ break :typeBuilder @Type(std.builtin.Type{
.Struct = .{ .Struct = .{
.layout = std.builtin.Type.ContainerLayout.auto, .layout = std.builtin.Type.ContainerLayout.auto,
@ -265,9 +265,9 @@ pub fn QueryResultReader(comptime TableShape: type, comptime MetadataShape: ?typ
/// Main object mapper. /// Main object mapper.
mainMapper: PgMapper(TableShape) = undefined, mainMapper: PgMapper(TableShape) = undefined,
metadataMapper: PgMapper(MetadataShape orelse struct {}) = undefined, metadataMapper: PgMapper(MetadataShape orelse struct {}) = undefined,
relationsMappers: RelationsMappersType = undefined, relationshipsMappers: RelationshipsMappersType = undefined,
fn next(opaqueSelf: *anyopaque) !?_result.TableWithRelations(TableShape, MetadataShape, inlineRelations) { fn next(opaqueSelf: *anyopaque) !?_result.TableWithRelationships(TableShape, MetadataShape, inlineRelationships) {
const self: *Instance = @ptrCast(@alignCast(opaqueSelf)); const self: *Instance = @ptrCast(@alignCast(opaqueSelf));
// Try to get the next row. // Try to get the next row.
@ -277,17 +277,17 @@ pub fn QueryResultReader(comptime TableShape: type, comptime MetadataShape: ?typ
const mainTable = try self.mainMapper.next(&row) orelse return null; const mainTable = try self.mainMapper.next(&row) orelse return null;
// Initialize the result. // Initialize the result.
var result: _result.TableWithRelations(TableShape, MetadataShape, inlineRelations) = undefined; var result: _result.TableWithRelationships(TableShape, MetadataShape, inlineRelationships) = undefined;
// Copy each basic table field. // Copy each basic table field.
inline for (std.meta.fields(TableShape)) |field| { inline for (std.meta.fields(TableShape)) |field| {
@field(result, field.name) = @field(mainTable, field.name); @field(result, field.name) = @field(mainTable, field.name);
} }
if (inlineRelations) |_inlineRelations| { if (inlineRelationships) |_inlineRelationships| {
// For each relation, retrieve its value and put it in the result. // For each relationship, retrieve its value and put it in the result.
inline for (_inlineRelations) |relation| { inline for (_inlineRelationships) |relationship| {
@field(result, relation.field) = @field(self.relationsMappers, relation.field).next(&row) catch null; @field(result, relationship.field) = @field(self.relationshipsMappers, relationship.field).next(&row) catch null;
} }
} }
@ -323,11 +323,11 @@ pub fn QueryResultReader(comptime TableShape: type, comptime MetadataShape: ?typ
self.instance.metadataMapper = try makeMapper(MetadataType, self.result, allocator, null); self.instance.metadataMapper = try makeMapper(MetadataType, self.result, allocator, null);
} }
if (inlineRelations) |_inlineRelations| { if (inlineRelationships) |_inlineRelationships| {
// Initialize mapper for each relation. // Initialize mapper for each relationship.
inline for (_inlineRelations) |relation| { inline for (_inlineRelationships) |relationship| {
@field(self.instance.relationsMappers, relation.field) = @field(self.instance.relationshipsMappers, relationship.field) =
try makeMapper(relation.TableShape, self.result, allocator, "relations." ++ relation.field ++ "."); try makeMapper(relationship.TableShape, self.result, allocator, "relationships." ++ relationship.field ++ ".");
} }
} }
@ -335,7 +335,7 @@ pub fn QueryResultReader(comptime TableShape: type, comptime MetadataShape: ?typ
} }
/// Get the generic reader instance. /// Get the generic reader instance.
pub fn reader(self: *Self) _result.QueryResultReader(TableShape, MetadataShape, inlineRelations) { pub fn reader(self: *Self) _result.QueryResultReader(TableShape, MetadataShape, inlineRelationships) {
return .{ return .{
._interface = .{ ._interface = .{
.instance = self, .instance = self,

View file

@ -6,7 +6,7 @@ const database = @import("database.zig");
const postgresql = @import("postgresql.zig"); const postgresql = @import("postgresql.zig");
const _sql = @import("sql.zig"); const _sql = @import("sql.zig");
const _conditions = @import("conditions.zig"); const _conditions = @import("conditions.zig");
const relations = @import("relations.zig"); const _relationships = @import("relationships.zig");
const repository = @import("repository.zig"); const repository = @import("repository.zig");
const _comptime = @import("comptime.zig"); const _comptime = @import("comptime.zig");
const _result = @import("result.zig"); const _result = @import("result.zig");
@ -18,57 +18,57 @@ pub const RepositoryQueryConfiguration = struct {
where: ?_sql.RawQuery = null, where: ?_sql.RawQuery = null,
}; };
/// Compiled relations structure. /// Compiled relationships structure.
const CompiledRelations = struct { const CompiledRelationships = struct {
inlineRelations: []relations.Relation, inlineRelationships: []_relationships.Relationship,
otherRelations: []relations.Relation, otherRelationships: []_relationships.Relationship,
inlineSelect: []const u8, inlineSelect: []const u8,
inlineJoins: []const u8, inlineJoins: []const u8,
}; };
/// Repository models query manager. /// Repository models query manager.
/// Manage query string build and its execution. /// Manage query string build and its execution.
pub fn RepositoryQuery(comptime Model: type, comptime TableShape: type, comptime repositoryConfig: repository.RepositoryConfiguration(Model, TableShape), comptime with: ?[]const relations.Relation, comptime MetadataShape: ?type) type { pub fn RepositoryQuery(comptime Model: type, comptime TableShape: type, comptime repositoryConfig: repository.RepositoryConfiguration(Model, TableShape), comptime with: ?[]const _relationships.Relationship, comptime MetadataShape: ?type) type {
const compiledRelations = comptime compile: { const compiledRelationships = comptime compile: {
// Inline relations list. // Inline relationships list.
var inlineRelations: []relations.Relation = &[0]relations.Relation{}; var inlineRelationships: []_relationships.Relationship = &[0]_relationships.Relationship{};
// Other relations list. // Other relationships list.
var otherRelations: []relations.Relation = &[0]relations.Relation{}; var otherRelationships: []_relationships.Relationship = &[0]_relationships.Relationship{};
if (with) |_with| { if (with) |_with| {
// If there are relations to eager load, prepare their query. // If there are relationships to eager load, prepare their query.
// Initialize inline select array. // Initialize inline select array.
var inlineSelect: [][]const u8 = &[0][]const u8{}; var inlineSelect: [][]const u8 = &[0][]const u8{};
// Initialize inline joins array. // Initialize inline joins array.
var inlineJoins: [][]const u8 = &[0][]const u8{}; var inlineJoins: [][]const u8 = &[0][]const u8{};
for (_with) |relation| { for (_with) |relationship| {
// For each relation, determine if it's inline or not. // For each relationship, determine if it's inline or not.
if (relation.inlineMapping) { if (relationship.inlineMapping) {
// Add the current relation to inline relations. // Add the current relationship to inline relationships.
inlineRelations = @ptrCast(@constCast(_comptime.append(inlineRelations, relation))); inlineRelationships = @ptrCast(@constCast(_comptime.append(inlineRelationships, relationship)));
// Generate selected columns for the relation. // Generate selected columns for the relationship.
inlineSelect = @ptrCast(@constCast(_comptime.append(inlineSelect, relation.select))); inlineSelect = @ptrCast(@constCast(_comptime.append(inlineSelect, relationship.select)));
// Generate joined table for the relation. // Generate joined table for the relationship.
inlineJoins = @ptrCast(@constCast(_comptime.append(inlineJoins, relation.join))); inlineJoins = @ptrCast(@constCast(_comptime.append(inlineJoins, relationship.join)));
} else { } else {
// Add the current relation to other relations. // Add the current relationship to other relationships.
otherRelations = @ptrCast(@constCast(_comptime.append(otherRelations, relation))); otherRelationships = @ptrCast(@constCast(_comptime.append(otherRelationships, relationship)));
} }
} }
break :compile CompiledRelations{ break :compile CompiledRelationships{
.inlineRelations = inlineRelations, .inlineRelationships = inlineRelationships,
.otherRelations = otherRelations, .otherRelationships = otherRelationships,
.inlineSelect = if (inlineSelect.len > 0) ", " ++ _comptime.join(", ", inlineSelect) else "", .inlineSelect = if (inlineSelect.len > 0) ", " ++ _comptime.join(", ", inlineSelect) else "",
.inlineJoins = if (inlineJoins.len > 0) " " ++ _comptime.join(" ", inlineJoins) else "", .inlineJoins = if (inlineJoins.len > 0) " " ++ _comptime.join(" ", inlineJoins) else "",
}; };
} else { } else {
break :compile CompiledRelations{ break :compile CompiledRelationships{
.inlineRelations = &[0]relations.Relation{}, .inlineRelationships = &[0]_relationships.Relationship{},
.otherRelations = &[0]relations.Relation{}, .otherRelationships = &[0]_relationships.Relationship{},
.inlineSelect = "", .inlineSelect = "",
.inlineJoins = "", .inlineJoins = "",
}; };
@ -77,8 +77,8 @@ pub fn RepositoryQuery(comptime Model: type, comptime TableShape: type, comptime
// Pre-compute SQL buffer. // Pre-compute SQL buffer.
const fromClause = " FROM \"" ++ repositoryConfig.table ++ "\""; const fromClause = " FROM \"" ++ repositoryConfig.table ++ "\"";
const defaultSelectSql = "\"" ++ repositoryConfig.table ++ "\".*" ++ compiledRelations.inlineSelect; const defaultSelectSql = "\"" ++ repositoryConfig.table ++ "\".*" ++ compiledRelationships.inlineSelect;
const defaultJoin = compiledRelations.inlineJoins; const defaultJoin = compiledRelationships.inlineJoins;
// Model key type. // Model key type.
const KeyType = repository.ModelKeyType(Model, TableShape, repositoryConfig); const KeyType = repository.ModelKeyType(Model, TableShape, repositoryConfig);
@ -87,7 +87,7 @@ pub fn RepositoryQuery(comptime Model: type, comptime TableShape: type, comptime
const Self = @This(); const Self = @This();
/// Result mapper type. /// Result mapper type.
pub const ResultMapper = _result.ResultMapper(Model, TableShape, MetadataShape, repositoryConfig, compiledRelations.inlineRelations, compiledRelations.otherRelations); pub const ResultMapper = _result.ResultMapper(Model, TableShape, MetadataShape, repositoryConfig, compiledRelationships.inlineRelationships, compiledRelationships.otherRelationships);
arena: std.heap.ArenaAllocator, arena: std.heap.ArenaAllocator,
connector: database.Connector, connector: database.Connector,
@ -296,7 +296,7 @@ pub fn RepositoryQuery(comptime Model: type, comptime TableShape: type, comptime
defer queryResult.deinit(); defer queryResult.deinit();
// Map query results. // Map query results.
var postgresqlReader = postgresql.QueryResultReader(TableShape, MetadataShape, compiledRelations.inlineRelations).init(queryResult); var postgresqlReader = postgresql.QueryResultReader(TableShape, MetadataShape, compiledRelationships.inlineRelationships).init(queryResult);
return try ResultMapper.map(withMetadata, allocator, self.connector, postgresqlReader.reader()); return try ResultMapper.map(withMetadata, allocator, self.connector, postgresqlReader.reader());
} }

View file

@ -5,9 +5,9 @@ const _sql = @import("sql.zig");
const repository = @import("repository.zig"); const repository = @import("repository.zig");
const _query = @import("query.zig"); const _query = @import("query.zig");
/// Configure a "one to many" or "many to many" relation. /// Configure a "one to many" or "many to many" relationship.
pub const ManyConfiguration = union(enum) { pub const ManyConfiguration = union(enum) {
/// Direct one-to-many relation using a distant foreign key. /// Direct one-to-many relationship using a distant foreign key.
direct: struct { direct: struct {
/// The distant foreign key name pointing to the current model. /// The distant foreign key name pointing to the current model.
foreignKey: []const u8, foreignKey: []const u8,
@ -16,7 +16,7 @@ pub const ManyConfiguration = union(enum) {
modelKey: ?[]const u8 = null, modelKey: ?[]const u8 = null,
}, },
/// Used when performing a many-to-many relation through an association table. /// Used when performing a many-to-many relationship through an association table.
through: struct { through: struct {
/// Name of the join table. /// Name of the join table.
table: []const u8, table: []const u8,
@ -33,7 +33,7 @@ pub const ManyConfiguration = union(enum) {
}, },
}; };
/// Make a "one to many" or "many to many" relation. /// Make a "one to many" or "many to many" relationship.
pub fn many(comptime fromRepo: anytype, comptime toRepo: anytype, comptime config: ManyConfiguration) type { pub fn many(comptime fromRepo: anytype, comptime toRepo: anytype, comptime config: ManyConfiguration) type {
return typedMany( return typedMany(
fromRepo.ModelType, fromRepo.TableType, fromRepo.config, fromRepo.ModelType, fromRepo.TableType, fromRepo.config,
@ -42,7 +42,7 @@ pub fn many(comptime fromRepo: anytype, comptime toRepo: anytype, comptime confi
); );
} }
/// Internal implementation of a new "one to many" or "many to many" relation. /// Internal implementation of a new "one to many" or "many to many" relationship.
pub fn typedMany( pub fn typedMany(
comptime FromModel: type, comptime FromTable: type, comptime FromModel: type, comptime FromTable: type,
comptime fromRepositoryConfig: repository.RepositoryConfiguration(FromModel, FromTable), comptime fromRepositoryConfig: repository.RepositoryConfiguration(FromModel, FromTable),
@ -51,15 +51,15 @@ pub fn typedMany(
comptime config: ManyConfiguration) type { comptime config: ManyConfiguration) type {
return struct { return struct {
/// Relation implementation. /// Relationship implementation.
pub fn Implementation(field: []const u8) type { pub fn Implementation(field: []const u8) type {
// Get foreign key from relation config or repository config. // Get foreign key from relationship config or repository config.
const foreignKey = switch (config) { const foreignKey = switch (config) {
.direct => |direct| direct.foreignKey, .direct => |direct| direct.foreignKey,
.through => |through| if (through.foreignKey) |_foreignKey| _foreignKey else toRepositoryConfig.key[0], .through => |through| if (through.foreignKey) |_foreignKey| _foreignKey else toRepositoryConfig.key[0],
}; };
// Get model key from relation config or repository config. // Get model key from relationship config or repository config.
const modelKey = switch (config) { const modelKey = switch (config) {
.direct => |direct| if (direct.modelKey) |_modelKey| _modelKey else fromRepositoryConfig.key[0], .direct => |direct| if (direct.modelKey) |_modelKey| _modelKey else fromRepositoryConfig.key[0],
.through => |through| if (through.modelKey) |_modelKey| _modelKey else fromRepositoryConfig.key[0], .through => |through| if (through.modelKey) |_modelKey| _modelKey else fromRepositoryConfig.key[0],
@ -68,10 +68,10 @@ pub fn typedMany(
const FromKeyType = std.meta.fields(FromModel)[std.meta.fieldIndex(FromModel, fromRepositoryConfig.key[0]).?].type; const FromKeyType = std.meta.fields(FromModel)[std.meta.fieldIndex(FromModel, fromRepositoryConfig.key[0]).?].type;
const QueryType = _query.RepositoryQuery(ToModel, ToTable, toRepositoryConfig, null, struct { const QueryType = _query.RepositoryQuery(ToModel, ToTable, toRepositoryConfig, null, struct {
__zrm_relation_key: FromKeyType, __zrm_relationship_key: FromKeyType,
}); });
const alias = "relations." ++ field; const alias = "relationships." ++ field;
const prefix = alias ++ "."; const prefix = alias ++ ".";
return struct { return struct {
@ -103,7 +103,7 @@ pub fn typedMany(
.direct => { .direct => {
// Add SELECT. // Add SELECT.
query.select(.{ query.select(.{
.sql = baseSelect ++ ", \"" ++ toRepositoryConfig.table ++ "\".\"" ++ foreignKey ++ "\" AS \"__zrm_relation_key\"", .sql = baseSelect ++ ", \"" ++ toRepositoryConfig.table ++ "\".\"" ++ foreignKey ++ "\" AS \"__zrm_relationship_key\"",
.params = &[0]_sql.RawQueryParameter{}, .params = &[0]_sql.RawQueryParameter{},
}); });
@ -113,7 +113,7 @@ pub fn typedMany(
.through => |through| { .through => |through| {
// Add SELECT. // Add SELECT.
query.select(.{ query.select(.{
.sql = baseSelect ++ ", \"" ++ prefix ++ "pivot" ++ "\".\"" ++ through.joinModelKey ++ "\" AS \"__zrm_relation_key\"", .sql = baseSelect ++ ", \"" ++ prefix ++ "pivot" ++ "\".\"" ++ through.joinModelKey ++ "\" AS \"__zrm_relationship_key\"",
.params = &[0]_sql.RawQueryParameter{}, .params = &[0]_sql.RawQueryParameter{},
}); });
@ -131,8 +131,8 @@ pub fn typedMany(
return query; // Return built query. return query; // Return built query.
} }
/// Build the "many" generic relation. /// Build the "many" generic relationship.
pub fn relation(_: Self) Relation { pub fn relationship(_: Self) Relationship {
return .{ return .{
._interface = .{ ._interface = .{
.repositoryConfiguration = &toRepositoryConfig, .repositoryConfiguration = &toRepositoryConfig,
@ -157,9 +157,9 @@ pub fn typedMany(
} }
/// Configure a "one to one" relation. /// Configure a "one to one" relationship.
pub const OneConfiguration = union(enum) { pub const OneConfiguration = union(enum) {
/// Direct one-to-one relation using a local foreign key. /// Direct one-to-one relationship using a local foreign key.
direct: struct { direct: struct {
/// The local foreign key name. /// The local foreign key name.
foreignKey: []const u8, foreignKey: []const u8,
@ -168,7 +168,7 @@ pub const OneConfiguration = union(enum) {
modelKey: ?[]const u8 = null, modelKey: ?[]const u8 = null,
}, },
/// Reverse one-to-one relation using distant foreign key. /// Reverse one-to-one relationship using distant foreign key.
reverse: struct { reverse: struct {
/// The distant foreign key name. /// The distant foreign key name.
/// Use the default key name of the related model. /// Use the default key name of the related model.
@ -178,7 +178,7 @@ pub const OneConfiguration = union(enum) {
modelKey: ?[]const u8 = null, modelKey: ?[]const u8 = null,
}, },
/// Used when performing a one-to-one relation through an association table. /// Used when performing a one-to-one relationship through an association table.
through: struct { through: struct {
/// Name of the join table. /// Name of the join table.
table: []const u8, table: []const u8,
@ -195,7 +195,7 @@ pub const OneConfiguration = union(enum) {
}, },
}; };
/// Make a "one to one" relation. /// Make a "one to one" relationship.
pub fn one(comptime fromRepo: anytype, comptime toRepo: anytype, comptime config: OneConfiguration) type { pub fn one(comptime fromRepo: anytype, comptime toRepo: anytype, comptime config: OneConfiguration) type {
return typedOne( return typedOne(
fromRepo.ModelType, fromRepo.TableType, fromRepo.config, fromRepo.ModelType, fromRepo.TableType, fromRepo.config,
@ -204,7 +204,7 @@ pub fn one(comptime fromRepo: anytype, comptime toRepo: anytype, comptime config
); );
} }
/// Internal implementation of a new "one to one" relation. /// Internal implementation of a new "one to one" relationship.
fn typedOne( fn typedOne(
comptime FromModel: type, comptime FromTable: type, comptime FromModel: type, comptime FromTable: type,
comptime fromRepositoryConfig: repository.RepositoryConfiguration(FromModel, FromTable), comptime fromRepositoryConfig: repository.RepositoryConfiguration(FromModel, FromTable),
@ -216,24 +216,24 @@ fn typedOne(
pub fn Implementation(field: []const u8) type { pub fn Implementation(field: []const u8) type {
const FromKeyType = std.meta.fields(FromModel)[std.meta.fieldIndex(FromModel, fromRepositoryConfig.key[0]).?].type; const FromKeyType = std.meta.fields(FromModel)[std.meta.fieldIndex(FromModel, fromRepositoryConfig.key[0]).?].type;
const QueryType = _query.RepositoryQuery(ToModel, ToTable, toRepositoryConfig, null, struct { const QueryType = _query.RepositoryQuery(ToModel, ToTable, toRepositoryConfig, null, struct {
__zrm_relation_key: FromKeyType, __zrm_relationship_key: FromKeyType,
}); });
// Get foreign key from relation config or repository config. // Get foreign key from relationship config or repository config.
const foreignKey = switch (config) { const foreignKey = switch (config) {
.direct => |direct| direct.foreignKey, .direct => |direct| direct.foreignKey,
.reverse => |reverse| if (reverse.foreignKey) |_foreignKey| _foreignKey else toRepositoryConfig.key[0], .reverse => |reverse| if (reverse.foreignKey) |_foreignKey| _foreignKey else toRepositoryConfig.key[0],
.through => |through| if (through.foreignKey) |_foreignKey| _foreignKey else fromRepositoryConfig.key[0], .through => |through| if (through.foreignKey) |_foreignKey| _foreignKey else fromRepositoryConfig.key[0],
}; };
// Get model key from relation config or repository config. // Get model key from relationship config or repository config.
const modelKey = switch (config) { const modelKey = switch (config) {
.direct => |direct| if (direct.modelKey) |_modelKey| _modelKey else toRepositoryConfig.key[0], .direct => |direct| if (direct.modelKey) |_modelKey| _modelKey else toRepositoryConfig.key[0],
.reverse => |reverse| if (reverse.modelKey) |_modelKey| _modelKey else fromRepositoryConfig.key[0], .reverse => |reverse| if (reverse.modelKey) |_modelKey| _modelKey else fromRepositoryConfig.key[0],
.through => |through| if (through.modelKey) |_modelKey| _modelKey else toRepositoryConfig.key[0], .through => |through| if (through.modelKey) |_modelKey| _modelKey else toRepositoryConfig.key[0],
}; };
const alias = "relations." ++ field; const alias = "relationships." ++ field;
const prefix = alias ++ "."; const prefix = alias ++ ".";
return struct { return struct {
@ -286,7 +286,7 @@ fn typedOne(
.direct => { .direct => {
// Add SELECT. // Add SELECT.
query.select(.{ query.select(.{
.sql = baseSelect ++ ", \"" ++ fromRepositoryConfig.table ++ "\".\"" ++ fromRepositoryConfig.key[0] ++ "\" AS \"__zrm_relation_key\"", .sql = baseSelect ++ ", \"" ++ fromRepositoryConfig.table ++ "\".\"" ++ fromRepositoryConfig.key[0] ++ "\" AS \"__zrm_relationship_key\"",
.params = &[0]_sql.RawQueryParameter{}, .params = &[0]_sql.RawQueryParameter{},
}); });
@ -302,7 +302,7 @@ fn typedOne(
.reverse => { .reverse => {
// Add SELECT. // Add SELECT.
query.select(.{ query.select(.{
.sql = baseSelect ++ ", \"" ++ toRepositoryConfig.table ++ "\".\"" ++ foreignKey ++ "\" AS \"__zrm_relation_key\"", .sql = baseSelect ++ ", \"" ++ toRepositoryConfig.table ++ "\".\"" ++ foreignKey ++ "\" AS \"__zrm_relationship_key\"",
.params = &[0]_sql.RawQueryParameter{}, .params = &[0]_sql.RawQueryParameter{},
}); });
@ -312,7 +312,7 @@ fn typedOne(
.through => |through| { .through => |through| {
// Add SELECT. // Add SELECT.
query.select(.{ query.select(.{
.sql = baseSelect ++ ", \"" ++ prefix ++ "pivot" ++ "\".\"" ++ through.joinForeignKey ++ "\" AS \"__zrm_relation_key\"", .sql = baseSelect ++ ", \"" ++ prefix ++ "pivot" ++ "\".\"" ++ through.joinForeignKey ++ "\" AS \"__zrm_relationship_key\"",
.params = &[0]_sql.RawQueryParameter{}, .params = &[0]_sql.RawQueryParameter{},
}); });
@ -331,8 +331,8 @@ fn typedOne(
return query; return query;
} }
/// Build the "one" generic relation. /// Build the "one" generic relationship.
pub fn relation(_: Self) Relation { pub fn relationship(_: Self) Relationship {
return .{ return .{
._interface = .{ ._interface = .{
.repositoryConfiguration = &toRepositoryConfig, .repositoryConfiguration = &toRepositoryConfig,
@ -356,8 +356,8 @@ fn typedOne(
}; };
} }
/// Generic model relation interface. /// Generic model relationship interface.
pub const Relation = struct { pub const Relationship = struct {
const Self = @This(); const Self = @This();
_interface: struct { _interface: struct {
@ -372,14 +372,14 @@ pub const Relation = struct {
TableShape: type, TableShape: type,
/// Field where to put the related model(s). /// Field where to put the related model(s).
field: []const u8, field: []const u8,
/// Table alias of the relation. /// Table alias of the relationship.
alias: []const u8, alias: []const u8,
/// Prefix of fields of the relation. /// Prefix of fields of the relationship.
prefix: []const u8, prefix: []const u8,
/// Type of a query of the related models. /// Type of a query of the related models.
QueryType: type, QueryType: type,
/// Set if relation mapping is done inline: this means that it's done at the same time the model is mapped, /// Set if relationship mapping is done inline: this means that it's done at the same time the model is mapped,
/// and that the associated data will be retrieved in the main query. /// and that the associated data will be retrieved in the main query.
inlineMapping: bool, inlineMapping: bool,
/// In case of inline mapping, the JOIN clause to retrieve the associated data. /// In case of inline mapping, the JOIN clause to retrieve the associated data.
@ -387,8 +387,8 @@ pub const Relation = struct {
/// The SELECT clause to retrieve the associated data. /// The SELECT clause to retrieve the associated data.
select: []const u8, select: []const u8,
/// Build the query to retrieve relation data. /// Build the query to retrieve relationship data.
/// Is always used when inline mapping is not possible, but also when loading relations lazily. /// Is always used when inline mapping is not possible, but also when loading relationships lazily.
pub fn buildQuery(self: Self, models: []const *anyopaque, allocator: std.mem.Allocator, connector: _database.Connector) !*anyopaque { pub fn buildQuery(self: Self, models: []const *anyopaque, allocator: std.mem.Allocator, connector: _database.Connector) !*anyopaque {
return self._interface.buildQuery(models, allocator, connector); return self._interface.buildQuery(models, allocator, connector);
} }
@ -402,10 +402,10 @@ pub const Relation = struct {
}; };
/// Structure of an eager loaded relation. /// Structure of an eager loaded relationship.
pub const Eager = struct { pub const Eager = struct {
/// The relation to eager load. /// The relationship to eager load.
relation: Relation, relationship: Relationship,
/// Subrelations to eager load. /// Subrelationships to eager load.
with: []const Eager, with: []const Eager,
}; };

View file

@ -3,7 +3,7 @@ const zollections = @import("zollections");
const database = @import("database.zig"); const database = @import("database.zig");
const _sql = @import("sql.zig"); const _sql = @import("sql.zig");
const _conditions = @import("conditions.zig"); const _conditions = @import("conditions.zig");
const _relations = @import("relations.zig"); const _relationships = @import("relationships.zig");
const query = @import("query.zig"); const query = @import("query.zig");
const insert = @import("insert.zig"); const insert = @import("insert.zig");
const update = @import("update.zig"); const update = @import("update.zig");
@ -74,18 +74,18 @@ pub fn ModelKeyType(comptime Model: type, comptime TableShape: type, comptime co
} }
} }
/// Model relations definition type. /// Model relationships definition type.
pub fn RelationsDefinitionType(comptime rawDefinition: anytype) type { pub fn RelationshipsDefinitionType(comptime rawDefinition: anytype) type {
const rawDefinitionType = @typeInfo(@TypeOf(rawDefinition)); const rawDefinitionType = @typeInfo(@TypeOf(rawDefinition));
// Build relations fields and implementations fields. // Build relationships fields and implementations fields.
var fields: [1 + rawDefinitionType.Struct.fields.len]std.builtin.Type.StructField = undefined; var fields: [1 + rawDefinitionType.Struct.fields.len]std.builtin.Type.StructField = undefined;
var implementationsFields: [rawDefinitionType.Struct.fields.len]std.builtin.Type.StructField = undefined; var implementationsFields: [rawDefinitionType.Struct.fields.len]std.builtin.Type.StructField = undefined;
inline for (rawDefinitionType.Struct.fields, fields[1..], &implementationsFields) |originalField, *field, *implementationField| { inline for (rawDefinitionType.Struct.fields, fields[1..], &implementationsFields) |originalField, *field, *implementationField| {
field.* = std.builtin.Type.StructField{ field.* = std.builtin.Type.StructField{
.name = originalField.name, .name = originalField.name,
.type = _relations.Relation, .type = _relationships.Relationship,
.default_value = null, .default_value = null,
.is_comptime = false, .is_comptime = false,
.alignment = @alignOf(type), .alignment = @alignOf(type),
@ -144,34 +144,34 @@ pub fn Repository(comptime Model: type, comptime TableShape: type, comptime repo
/// Type of one model key. /// Type of one model key.
pub const KeyType = ModelKeyType(Model, TableShape, config); pub const KeyType = ModelKeyType(Model, TableShape, config);
pub const relations = struct { pub const relationships = struct {
/// Make a "one to one" relation. /// Make a "one to one" relationship.
pub fn one(comptime toRepo: anytype, comptime oneConfig: _relations.OneConfiguration) type { pub fn one(comptime toRepo: anytype, comptime oneConfig: _relationships.OneConfiguration) type {
return _relations.one(Self, toRepo, oneConfig); return _relationships.one(Self, toRepo, oneConfig);
} }
/// Make a "one to many" or "many to many" relation. /// Make a "one to many" or "many to many" relationship.
pub fn many(comptime toRepo: anytype, comptime manyConfig: _relations.ManyConfiguration) type { pub fn many(comptime toRepo: anytype, comptime manyConfig: _relationships.ManyConfiguration) type {
return _relations.many(Self, toRepo, manyConfig); return _relationships.many(Self, toRepo, manyConfig);
} }
/// Define a relations object for a repository. /// Define a relationships object for a repository.
pub fn define(rawDefinition: anytype) RelationsDefinitionType(rawDefinition) { pub fn define(rawDefinition: anytype) RelationshipsDefinitionType(rawDefinition) {
const rawDefinitionType = @TypeOf(rawDefinition); const rawDefinitionType = @TypeOf(rawDefinition);
// Initialize final relations definition. // Initialize final relationships definition.
var definition: RelationsDefinitionType(rawDefinition) = undefined; var definition: RelationshipsDefinitionType(rawDefinition) = undefined;
definition._implementations = .{}; definition._implementations = .{};
// Check that the definition structure only include known fields. // Check that the definition structure only include known fields.
inline for (std.meta.fieldNames(rawDefinitionType)) |fieldName| { inline for (std.meta.fieldNames(rawDefinitionType)) |fieldName| {
if (!@hasField(Model, fieldName)) { if (!@hasField(Model, fieldName)) {
@compileError("No corresponding field for relation " ++ fieldName); @compileError("No corresponding field for relationship " ++ fieldName);
} }
// Alter definition structure to set the relation instance. // Alter definition structure to set the relationship instance.
@field(definition, fieldName) = @field(definition._implementations, fieldName).relation(); @field(definition, fieldName) = @field(definition._implementations, fieldName).relationship();
} }
// Return altered definition structure. // Return altered definition structure.
@ -179,7 +179,7 @@ pub fn Repository(comptime Model: type, comptime TableShape: type, comptime repo
} }
}; };
pub fn QueryWith(comptime with: []const _relations.Relation) type { pub fn QueryWith(comptime with: []const _relationships.Relationship) type {
return query.RepositoryQuery(Model, TableShape, config, with, null); return query.RepositoryQuery(Model, TableShape, config, with, null);
} }

View file

@ -2,7 +2,7 @@ const std = @import("std");
const zollections = @import("zollections"); const zollections = @import("zollections");
const _database = @import("database.zig"); const _database = @import("database.zig");
const _repository = @import("repository.zig"); const _repository = @import("repository.zig");
const _relations = @import("relations.zig"); const _relationships = @import("relationships.zig");
/// Structure of a model with its metadata. /// Structure of a model with its metadata.
pub fn ModelWithMetadata(comptime Model: type, comptime MetadataShape: ?type) type { pub fn ModelWithMetadata(comptime Model: type, comptime MetadataShape: ?type) type {
@ -16,38 +16,38 @@ pub fn ModelWithMetadata(comptime Model: type, comptime MetadataShape: ?type) ty
} }
} }
/// Type of a retrieved table data, with its retrieved relations. /// Type of a retrieved table data, with its retrieved relationships.
pub fn TableWithRelations(comptime TableShape: type, comptime MetadataShape: ?type, comptime optionalRelations: ?[]const _relations.Relation) type { pub fn TableWithRelationships(comptime TableShape: type, comptime MetadataShape: ?type, comptime optionalRelationships: ?[]const _relationships.Relationship) type {
if (optionalRelations) |relations| { if (optionalRelationships) |relationships| {
const tableType = @typeInfo(TableShape); const tableType = @typeInfo(TableShape);
// Build fields list: copy the existing table type fields and add those for relations. // Build fields list: copy the existing table type fields and add those for relationships.
var fields: [tableType.Struct.fields.len + relations.len + (if (MetadataShape) |_| 1 else 0)]std.builtin.Type.StructField = undefined; var fields: [tableType.Struct.fields.len + relationships.len + (if (MetadataShape) |_| 1 else 0)]std.builtin.Type.StructField = undefined;
// Copy base table fields. // Copy base table fields.
@memcpy(fields[0..tableType.Struct.fields.len], tableType.Struct.fields); @memcpy(fields[0..tableType.Struct.fields.len], tableType.Struct.fields);
// For each relation, create a new struct field in the table shape. // For each relationship, create a new struct field in the table shape.
for (relations, fields[tableType.Struct.fields.len..(tableType.Struct.fields.len+relations.len)]) |relation, *field| { for (relationships, fields[tableType.Struct.fields.len..(tableType.Struct.fields.len+relationships.len)]) |relationship, *field| {
// Get relation field type (optional TableShape of the related value). // Get relationship field type (optional TableShape of the related value).
const relationFieldType = @Type(std.builtin.Type{ const relationshipFieldType = @Type(std.builtin.Type{
.Optional = .{ .Optional = .{
.child = relation.TableShape .child = relationship.TableShape
}, },
}); });
// Create the new field from relation data. // Create the new field from relationship data.
field.* = std.builtin.Type.StructField{ field.* = std.builtin.Type.StructField{
.name = relation.field ++ [0:0]u8{}, .name = relationship.field ++ [0:0]u8{},
.type = relationFieldType, .type = relationshipFieldType,
.default_value = null, .default_value = null,
.is_comptime = false, .is_comptime = false,
.alignment = @alignOf(relationFieldType), .alignment = @alignOf(relationshipFieldType),
}; };
} }
if (MetadataShape) |MetadataType| { if (MetadataShape) |MetadataType| {
// Add metadata field. // Add metadata field.
fields[tableType.Struct.fields.len + relations.len] = std.builtin.Type.StructField{ fields[tableType.Struct.fields.len + relationships.len] = std.builtin.Type.StructField{
.name = "_zrm_metadata", .name = "_zrm_metadata",
.type = MetadataType, .type = MetadataType,
.default_value = null, .default_value = null,
@ -72,8 +72,8 @@ pub fn TableWithRelations(comptime TableShape: type, comptime MetadataShape: ?ty
} }
/// Convert a value of the fully retrieved type to the TableShape type. /// Convert a value of the fully retrieved type to the TableShape type.
pub fn toTableShape(comptime TableShape: type, comptime MetadataShape: ?type, comptime optionalRelations: ?[]const _relations.Relation, value: TableWithRelations(TableShape, MetadataShape, optionalRelations)) TableShape { pub fn toTableShape(comptime TableShape: type, comptime MetadataShape: ?type, comptime optionalRelationships: ?[]const _relationships.Relationship, value: TableWithRelationships(TableShape, MetadataShape, optionalRelationships)) TableShape {
if (optionalRelations) |_| { if (optionalRelationships) |_| {
// Make a structure of TableShape type. // Make a structure of TableShape type.
var tableValue: TableShape = undefined; var tableValue: TableShape = undefined;
@ -85,13 +85,13 @@ pub fn toTableShape(comptime TableShape: type, comptime MetadataShape: ?type, co
// Return the simplified structure. // Return the simplified structure.
return tableValue; return tableValue;
} else { } else {
// No relations, it should already be of type TableShape. // No relationships, it should already be of type TableShape.
return value; return value;
} }
} }
/// Generic interface of a query result reader. /// Generic interface of a query result reader.
pub fn QueryResultReader(comptime TableShape: type, comptime MetadataShape: ?type, comptime inlineRelations: ?[]const _relations.Relation) type { pub fn QueryResultReader(comptime TableShape: type, comptime MetadataShape: ?type, comptime inlineRelationships: ?[]const _relationships.Relationship) type {
return struct { return struct {
const Self = @This(); const Self = @This();
@ -99,12 +99,12 @@ pub fn QueryResultReader(comptime TableShape: type, comptime MetadataShape: ?typ
pub const Instance = struct { pub const Instance = struct {
__interface: struct { __interface: struct {
instance: *anyopaque, instance: *anyopaque,
next: *const fn (self: *anyopaque) anyerror!?TableWithRelations(TableShape, MetadataShape, inlineRelations), next: *const fn (self: *anyopaque) anyerror!?TableWithRelationships(TableShape, MetadataShape, inlineRelationships),
}, },
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
pub fn next(self: Instance) !?TableWithRelations(TableShape, MetadataShape, inlineRelations) { pub fn next(self: Instance) !?TableWithRelationships(TableShape, MetadataShape, inlineRelationships) {
return self.__interface.next(self.__interface.instance); return self.__interface.next(self.__interface.instance);
} }
}; };
@ -121,11 +121,11 @@ pub fn QueryResultReader(comptime TableShape: type, comptime MetadataShape: ?typ
}; };
} }
/// Map query result to repository model structures, and load the given relations. /// Map query result to repository model structures, and load the given relationships.
pub fn ResultMapper(comptime Model: type, comptime TableShape: type, comptime MetadataShape: ?type, comptime repositoryConfig: _repository.RepositoryConfiguration(Model, TableShape), comptime inlineRelations: ?[]const _relations.Relation, comptime relations: ?[]const _relations.Relation) type { pub fn ResultMapper(comptime Model: type, comptime TableShape: type, comptime MetadataShape: ?type, comptime repositoryConfig: _repository.RepositoryConfiguration(Model, TableShape), comptime inlineRelationships: ?[]const _relationships.Relationship, comptime relationships: ?[]const _relationships.Relationship) type {
return struct { return struct {
/// Map the query result to a repository result, with all the required relations. /// Map the query result to a repository result, with all the required relationships.
pub fn map(comptime withMetadata: bool, allocator: std.mem.Allocator, connector: _database.Connector, queryReader: QueryResultReader(TableShape, MetadataShape, inlineRelations)) !_repository.RepositoryResult(if (withMetadata) ModelWithMetadata(Model, MetadataShape) else Model) { pub fn map(comptime withMetadata: bool, allocator: std.mem.Allocator, connector: _database.Connector, queryReader: QueryResultReader(TableShape, MetadataShape, inlineRelationships)) !_repository.RepositoryResult(if (withMetadata) ModelWithMetadata(Model, MetadataShape) else Model) {
// Get result type depending on metadata // Get result type depending on metadata
const ResultType = if (withMetadata) ModelWithMetadata(Model, MetadataShape) else Model; const ResultType = if (withMetadata) ModelWithMetadata(Model, MetadataShape) else Model;
@ -144,32 +144,32 @@ pub fn ResultMapper(comptime Model: type, comptime TableShape: type, comptime Me
while (try reader.next()) |rawModel| { while (try reader.next()) |rawModel| {
// Parse each raw model from the reader. // Parse each raw model from the reader.
const model = try allocator.create(ResultType); const model = try allocator.create(ResultType);
(if (withMetadata) model.model else model.*) = try repositoryConfig.fromSql(toTableShape(TableShape, MetadataShape, inlineRelations, rawModel)); (if (withMetadata) model.model else model.*) = try repositoryConfig.fromSql(toTableShape(TableShape, MetadataShape, inlineRelationships, rawModel));
// Map inline relations. // Map inline relationships.
if (inlineRelations) |_inlineRelations| { if (inlineRelationships) |_inlineRelationships| {
// If there are loaded inline relations, map them to the result. // If there are loaded inline relationships, map them to the result.
inline for (_inlineRelations) |relation| { inline for (_inlineRelationships) |relationship| {
// Read the inline related value. // Read the inline related value.
const relatedValue = ( const relatedValue = (
if (@field(rawModel, relation.field)) |relationVal| if (@field(rawModel, relationship.field)) |relationshipVal|
try relation.repositoryConfiguration().fromSql(relationVal) try relationship.repositoryConfiguration().fromSql(relationshipVal)
else null else null
); );
if (pointedType(@TypeOf(@field(model.*, relation.field)))) |childType| { if (pointedType(@TypeOf(@field(model.*, relationship.field)))) |childType| {
if (relatedValue) |val| { if (relatedValue) |val| {
// Allocate pointer value. // Allocate pointer value.
@field(model.*, relation.field) = try mapperArena.allocator().create(childType); @field(model.*, relationship.field) = try mapperArena.allocator().create(childType);
// Set pointer value. // Set pointer value.
@field(model.*, relation.field).?.* = val; @field(model.*, relationship.field).?.* = val;
} else { } else {
// Set NULL value. // Set NULL value.
@field(model.*, relation.field) = null; @field(model.*, relationship.field) = null;
} }
} else { } else {
// Set simple value. // Set simple value.
@field(model.*, relation.field) = relatedValue; @field(model.*, relationship.field) = relatedValue;
} }
} }
} }
@ -189,11 +189,11 @@ pub fn ResultMapper(comptime Model: type, comptime TableShape: type, comptime Me
} }
} }
if (relations) |relationsToLoad| { if (relationships) |relationshipsToLoad| {
inline for (relationsToLoad) |relation| { inline for (relationshipsToLoad) |relationship| {
// Build query for the relation to get. // Build query for the relationship to get.
const query: *relation.QueryType = @ptrCast(@alignCast( const query: *relationship.QueryType = @ptrCast(@alignCast(
try relation.buildQuery(@ptrCast(models.items), allocator, connector) try relationship.buildQuery(@ptrCast(models.items), allocator, connector)
)); ));
defer { defer {
query.deinit(); query.deinit();
@ -205,14 +205,14 @@ pub fn ResultMapper(comptime Model: type, comptime TableShape: type, comptime Me
// Create a map with related models. // Create a map with related models.
const RelatedModelsListType = std.ArrayList(@TypeOf(relatedModels.models[0].model)); const RelatedModelsListType = std.ArrayList(@TypeOf(relatedModels.models[0].model));
const RelatedModelsMapType = std.AutoHashMap(std.meta.FieldType(@TypeOf(relatedModels.models[0].metadata), .__zrm_relation_key), RelatedModelsListType); const RelatedModelsMapType = std.AutoHashMap(std.meta.FieldType(@TypeOf(relatedModels.models[0].metadata), .__zrm_relationship_key), RelatedModelsListType);
var relatedModelsMap = RelatedModelsMapType.init(allocator); var relatedModelsMap = RelatedModelsMapType.init(allocator);
defer relatedModelsMap.deinit(); defer relatedModelsMap.deinit();
// Fill the map of related models, indexing them by the relation key. // Fill the map of related models, indexing them by the relationship key.
for (relatedModels.models) |relatedModel| { for (relatedModels.models) |relatedModel| {
// For each related model, put it in the map at the relation key. // For each related model, put it in the map at the relationship key.
var modelsList = try relatedModelsMap.getOrPut(relatedModel.metadata.__zrm_relation_key); var modelsList = try relatedModelsMap.getOrPut(relatedModel.metadata.__zrm_relationship_key);
if (!modelsList.found_existing) { if (!modelsList.found_existing) {
// Initialize the related models list. // Initialize the related models list.
@ -225,7 +225,7 @@ pub fn ResultMapper(comptime Model: type, comptime TableShape: type, comptime Me
// For each model, at the grouped related models if there are some. // For each model, at the grouped related models if there are some.
for (models.items) |model| { for (models.items) |model| {
@field(model, relation.field) = ( @field(model, relationship.field) = (
if (relatedModelsMap.getPtr(@field(model, repositoryConfig.key[0]))) |relatedModelsList| if (relatedModelsMap.getPtr(@field(model, repositoryConfig.key[0]))) |relatedModelsList|
// There are related models, set them. // There are related models, set them.
try relatedModelsList.toOwnedSlice() try relatedModelsList.toOwnedSlice()

View file

@ -12,7 +12,7 @@ pub const RepositoryResult = repository.RepositoryResult;
pub const Insertable = insert.Insertable; pub const Insertable = insert.Insertable;
pub const relations = @import("relations.zig"); pub const relationships = @import("relationships.zig");
pub const RawQueryParameter = _sql.RawQueryParameter; pub const RawQueryParameter = _sql.RawQueryParameter;
pub const RawQuery = _sql.RawQuery; pub const RawQuery = _sql.RawQuery;

View file

@ -76,18 +76,18 @@ pub const UserRepository = zrm.Repository(User, User.Table, .{
.fromSql = zrm.helpers.TableModel(User, User.Table).copyTableToModel, .fromSql = zrm.helpers.TableModel(User, User.Table).copyTableToModel,
.toSql = zrm.helpers.TableModel(User, User.Table).copyModelToTable, .toSql = zrm.helpers.TableModel(User, User.Table).copyModelToTable,
}); });
pub const UserRelations = UserRepository.relations.define(.{ pub const UserRelationships = UserRepository.relationships.define(.{
.picture = UserRepository.relations.one(MediaRepository, .{ .picture = UserRepository.relationships.one(MediaRepository, .{
.direct = .{ .direct = .{
.foreignKey = "picture_id", .foreignKey = "picture_id",
} }
}), }),
.info = UserRepository.relations.one(UserInfoRepository, .{ .info = UserRepository.relationships.one(UserInfoRepository, .{
.reverse = .{}, .reverse = .{},
}), }),
.messages = UserRepository.relations.many(MessageRepository, .{ .messages = UserRepository.relationships.many(MessageRepository, .{
.direct = .{ .direct = .{
.foreignKey = "user_id", .foreignKey = "user_id",
}, },
@ -121,8 +121,8 @@ pub const UserInfoRepository = zrm.Repository(UserInfo, UserInfo.Table, .{
.fromSql = zrm.helpers.TableModel(UserInfo, UserInfo.Table).copyTableToModel, .fromSql = zrm.helpers.TableModel(UserInfo, UserInfo.Table).copyTableToModel,
.toSql = zrm.helpers.TableModel(UserInfo, UserInfo.Table).copyModelToTable, .toSql = zrm.helpers.TableModel(UserInfo, UserInfo.Table).copyModelToTable,
}); });
pub const UserInfoRelations = UserInfoRepository.relations.define(.{ pub const UserInfoRelationships = UserInfoRepository.relationships.define(.{
.user = UserInfoRepository.relations.one(UserRepository, .{ .user = UserInfoRepository.relationships.one(UserRepository, .{
.direct = .{ .direct = .{
.foreignKey = "user_id", .foreignKey = "user_id",
}, },
@ -157,14 +157,14 @@ pub const MessageRepository = zrm.Repository(Message, Message.Table, .{
.fromSql = zrm.helpers.TableModel(Message, Message.Table).copyTableToModel, .fromSql = zrm.helpers.TableModel(Message, Message.Table).copyTableToModel,
.toSql = zrm.helpers.TableModel(Message, Message.Table).copyModelToTable, .toSql = zrm.helpers.TableModel(Message, Message.Table).copyModelToTable,
}); });
pub const MessageRelations = MessageRepository.relations.define(.{ pub const MessageRelationships = MessageRepository.relationships.define(.{
.user = MessageRepository.relations.one(UserRepository, .{ .user = MessageRepository.relationships.one(UserRepository, .{
.direct = .{ .direct = .{
.foreignKey = "user_id", .foreignKey = "user_id",
} }
}), }),
.user_picture = MessageRepository.relations.one(MediaRepository, .{ .user_picture = MessageRepository.relationships.one(MediaRepository, .{
.through = .{ .through = .{
.table = "example_users", .table = "example_users",
.foreignKey = "user_id", .foreignKey = "user_id",
@ -173,7 +173,7 @@ pub const MessageRelations = MessageRepository.relations.define(.{
}, },
}), }),
.medias = MessageRepository.relations.many(MediaRepository, .{ .medias = MessageRepository.relationships.many(MediaRepository, .{
.through = .{ .through = .{
.table = "example_messages_medias", .table = "example_messages_medias",
.joinModelKey = "message_id", .joinModelKey = "message_id",
@ -194,7 +194,7 @@ test "user picture media" {
var firstQuery = UserRepository.QueryWith( var firstQuery = UserRepository.QueryWith(
// Retrieve picture of users. // Retrieve picture of users.
&[_]zrm.relations.Relation{UserRelations.picture} &[_]zrm.relationships.Relationship{UserRelationships.picture}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try firstQuery.whereKey(1); try firstQuery.whereKey(1);
defer firstQuery.deinit(); defer firstQuery.deinit();
@ -211,7 +211,7 @@ test "user picture media" {
var secondQuery = UserRepository.QueryWith( var secondQuery = UserRepository.QueryWith(
// Retrieve picture of users. // Retrieve picture of users.
&[_]zrm.relations.Relation{UserRelations.picture} &[_]zrm.relationships.Relationship{UserRelationships.picture}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try secondQuery.whereKey(3); try secondQuery.whereKey(3);
defer secondQuery.deinit(); defer secondQuery.deinit();
@ -228,7 +228,7 @@ test "user picture media" {
var thirdQuery = UserRepository.QueryWith( var thirdQuery = UserRepository.QueryWith(
// Retrieve picture of users. // Retrieve picture of users.
&[_]zrm.relations.Relation{UserRelations.picture} &[_]zrm.relationships.Relationship{UserRelationships.picture}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try thirdQuery.whereKey(5); try thirdQuery.whereKey(5);
defer thirdQuery.deinit(); defer thirdQuery.deinit();
@ -244,7 +244,7 @@ test "user picture media" {
var fourthQuery = UserRepository.QueryWith( var fourthQuery = UserRepository.QueryWith(
// Retrieve picture of users. // Retrieve picture of users.
&[_]zrm.relations.Relation{UserRelations.picture} &[_]zrm.relationships.Relationship{UserRelationships.picture}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try fourthQuery.whereKey(19); try fourthQuery.whereKey(19);
defer fourthQuery.deinit(); defer fourthQuery.deinit();
@ -267,7 +267,7 @@ test "user has info" {
var firstQuery = UserRepository.QueryWith( var firstQuery = UserRepository.QueryWith(
// Retrieve info of users. // Retrieve info of users.
&[_]zrm.relations.Relation{UserRelations.info} &[_]zrm.relationships.Relationship{UserRelationships.info}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try firstQuery.whereKey(2); try firstQuery.whereKey(2);
defer firstQuery.deinit(); defer firstQuery.deinit();
@ -284,7 +284,7 @@ test "user has info" {
var secondQuery = UserRepository.QueryWith( var secondQuery = UserRepository.QueryWith(
// Retrieve info of users. // Retrieve info of users.
&[_]zrm.relations.Relation{UserRelations.info} &[_]zrm.relationships.Relationship{UserRelationships.info}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try secondQuery.whereKey(1); try secondQuery.whereKey(1);
defer secondQuery.deinit(); defer secondQuery.deinit();
@ -299,7 +299,7 @@ test "user has info" {
var thirdQuery = UserInfoRepository.QueryWith( var thirdQuery = UserInfoRepository.QueryWith(
// Retrieve info of users. // Retrieve info of users.
&[_]zrm.relations.Relation{UserInfoRelations.user} &[_]zrm.relationships.Relationship{UserInfoRelationships.user}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try thirdQuery.whereKey(2); try thirdQuery.whereKey(2);
defer thirdQuery.deinit(); defer thirdQuery.deinit();
@ -326,7 +326,7 @@ test "user has many messages" {
var firstQuery = UserRepository.QueryWith( var firstQuery = UserRepository.QueryWith(
// Retrieve messages of users. // Retrieve messages of users.
&[_]zrm.relations.Relation{UserRelations.messages} &[_]zrm.relationships.Relationship{UserRelationships.messages}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try firstQuery.whereKey(1); try firstQuery.whereKey(1);
defer firstQuery.deinit(); defer firstQuery.deinit();
@ -354,7 +354,7 @@ test "user has many messages" {
var secondQuery = UserRepository.QueryWith( var secondQuery = UserRepository.QueryWith(
// Retrieve messages of users. // Retrieve messages of users.
&[_]zrm.relations.Relation{UserRelations.messages} &[_]zrm.relationships.Relationship{UserRelationships.messages}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try secondQuery.whereKey(5); try secondQuery.whereKey(5);
defer secondQuery.deinit(); defer secondQuery.deinit();
@ -380,7 +380,7 @@ test "message has many medias through pivot table" {
var firstQuery = MessageRepository.QueryWith( var firstQuery = MessageRepository.QueryWith(
// Retrieve medias of messages. // Retrieve medias of messages.
&[_]zrm.relations.Relation{MessageRelations.medias} &[_]zrm.relationships.Relationship{MessageRelationships.medias}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try firstQuery.whereKey(1); try firstQuery.whereKey(1);
defer firstQuery.deinit(); defer firstQuery.deinit();
@ -397,7 +397,7 @@ test "message has many medias through pivot table" {
var secondQuery = MessageRepository.QueryWith( var secondQuery = MessageRepository.QueryWith(
// Retrieve medias of messages. // Retrieve medias of messages.
&[_]zrm.relations.Relation{MessageRelations.medias} &[_]zrm.relationships.Relationship{MessageRelationships.medias}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try secondQuery.whereKey(6); try secondQuery.whereKey(6);
defer secondQuery.deinit(); defer secondQuery.deinit();
@ -423,7 +423,7 @@ test "message has many medias through pivot table" {
var thirdQuery = MessageRepository.QueryWith( var thirdQuery = MessageRepository.QueryWith(
// Retrieve medias of messages. // Retrieve medias of messages.
&[_]zrm.relations.Relation{MessageRelations.medias} &[_]zrm.relationships.Relationship{MessageRelationships.medias}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try thirdQuery.whereKey(4); try thirdQuery.whereKey(4);
defer thirdQuery.deinit(); defer thirdQuery.deinit();
@ -448,7 +448,7 @@ test "message has one user picture URL through users table" {
var firstQuery = MessageRepository.QueryWith( var firstQuery = MessageRepository.QueryWith(
// Retrieve user pictures of messages. // Retrieve user pictures of messages.
&[_]zrm.relations.Relation{MessageRelations.user_picture} &[_]zrm.relationships.Relationship{MessageRelationships.user_picture}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try firstQuery.whereKey(1); try firstQuery.whereKey(1);
defer firstQuery.deinit(); defer firstQuery.deinit();
@ -464,7 +464,7 @@ test "message has one user picture URL through users table" {
var secondQuery = MessageRepository.QueryWith( var secondQuery = MessageRepository.QueryWith(
// Retrieve user pictures of messages. // Retrieve user pictures of messages.
&[_]zrm.relations.Relation{MessageRelations.user_picture} &[_]zrm.relationships.Relationship{MessageRelationships.user_picture}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
try secondQuery.whereKey(4); try secondQuery.whereKey(4);
defer secondQuery.deinit(); defer secondQuery.deinit();
@ -476,4 +476,4 @@ test "message has one user picture URL through users table" {
try std.testing.expect(secondResult.models[0].user_picture == null); try std.testing.expect(secondResult.models[0].user_picture == null);
} }
//TODO try to load all one relations types in another query (with buildQuery). //TODO try to load all one relationships types in another query (with buildQuery).

View file

@ -33,8 +33,8 @@ test "belongsTo" {
// Build a query of submodels. // Build a query of submodels.
var myQuery = repository.MySubmodelRepository.QueryWith( var myQuery = repository.MySubmodelRepository.QueryWith(
// Retrieve parents of submodels from relation. // Retrieve parents of submodels from relationship.
&[_]zrm.relations.Relation{repository.MySubmodelRelations.parent} &[_]zrm.relationships.Relationship{repository.MySubmodelRelationships.parent}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
defer myQuery.deinit(); defer myQuery.deinit();
@ -64,8 +64,8 @@ test "hasMany" {
// Build a query of submodels. // Build a query of submodels.
var myQuery = repository.MyModelRepository.QueryWith( var myQuery = repository.MyModelRepository.QueryWith(
// Retrieve parents of submodels from relation. // Retrieve parents of submodels from relationship.
&[_]zrm.relations.Relation{repository.MyModelRelations.submodels} &[_]zrm.relationships.Relationship{repository.MyModelRelationships.submodels}
).init(std.testing.allocator, poolConnector.connector(), .{}); ).init(std.testing.allocator, poolConnector.connector(), .{});
defer myQuery.deinit(); defer myQuery.deinit();

View file

@ -68,8 +68,8 @@ pub const MySubmodelRepository = zrm.Repository(MySubmodel, MySubmodelTable, .{
.toSql = &submodelToSql, .toSql = &submodelToSql,
}); });
pub const MySubmodelRelations = MySubmodelRepository.relations.define(.{ pub const MySubmodelRelationships = MySubmodelRepository.relationships.define(.{
.parent = MySubmodelRepository.relations.one(MyModelRepository, .{ .parent = MySubmodelRepository.relationships.one(MyModelRepository, .{
.direct = .{ .direct = .{
.foreignKey = "parent_id", .foreignKey = "parent_id",
}, },
@ -126,8 +126,8 @@ pub const MyModelRepository = zrm.Repository(MyModel, MyModelTable, .{
.toSql = &modelToSql, .toSql = &modelToSql,
}); });
pub const MyModelRelations = MyModelRepository.relations.define(.{ pub const MyModelRelationships = MyModelRepository.relationships.define(.{
.submodels = MyModelRepository.relations.many(MySubmodelRepository, .{ .submodels = MyModelRepository.relationships.many(MySubmodelRepository, .{
.direct = .{ .direct = .{
.foreignKey = "parent_id", .foreignKey = "parent_id",
} }

View file

@ -5,6 +5,6 @@ comptime {
_ = @import("repository.zig"); _ = @import("repository.zig");
_ = @import("composite.zig"); _ = @import("composite.zig");
_ = @import("sessions.zig"); _ = @import("sessions.zig");
_ = @import("relations.zig"); _ = @import("relationships.zig");
_ = @import("example.zig"); _ = @import("example.zig");
} }

View file

@ -36,7 +36,7 @@ test "session with rolled back transaction and savepoint" {
try session.beginTransaction(); try session.beginTransaction();
// First UPDATE in the transaction. // First UPDATE in the transaction.
{ {
var firstUpdate = repository.MyModelRepository.Update(struct { var firstUpdate = repository.MyModelRepository.Update(struct {