Improve relations QueryType definition and access.

This commit is contained in:
Madeorsk 2024-11-25 15:32:52 +01:00
parent b3007a1b5d
commit ce5aeffd2c
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
2 changed files with 5 additions and 17 deletions

View file

@ -90,10 +90,6 @@ pub fn typedMany(
return _genSelect(table, prefix); return _genSelect(table, prefix);
} }
fn getQueryType() type {
return QueryType;
}
fn buildQuery(_: *anyopaque, prefix: []const u8, opaqueModels: []const *anyopaque, allocator: std.mem.Allocator, connector: _database.Connector) !*anyopaque { fn buildQuery(_: *anyopaque, prefix: []const u8, opaqueModels: []const *anyopaque, allocator: std.mem.Allocator, connector: _database.Connector) !*anyopaque {
const models: []const *FromModel = @ptrCast(@alignCast(opaqueModels)); const models: []const *FromModel = @ptrCast(@alignCast(opaqueModels));
@ -153,8 +149,8 @@ pub fn typedMany(
.inlineMapping = inlineMapping, .inlineMapping = inlineMapping,
.genJoin = genJoin, .genJoin = genJoin,
.genSelect = genSelect, .genSelect = genSelect,
.getQueryType = getQueryType,
}, },
.QueryType = QueryType,
}; };
} }
@ -283,10 +279,6 @@ fn typedOne(
return _genSelect(table, prefix); return _genSelect(table, prefix);
} }
fn getQueryType() type {
return QueryType;
}
fn buildQuery(_: *anyopaque, prefix: []const u8, opaqueModels: []const *anyopaque, allocator: std.mem.Allocator, connector: _database.Connector) !*anyopaque { fn buildQuery(_: *anyopaque, prefix: []const u8, opaqueModels: []const *anyopaque, allocator: std.mem.Allocator, connector: _database.Connector) !*anyopaque {
const models: []const *FromModel = @ptrCast(@alignCast(opaqueModels)); const models: []const *FromModel = @ptrCast(@alignCast(opaqueModels));
@ -363,8 +355,8 @@ fn typedOne(
.inlineMapping = inlineMapping, .inlineMapping = inlineMapping,
.genJoin = genJoin, .genJoin = genJoin,
.genSelect = genSelect, .genSelect = genSelect,
.getQueryType = getQueryType,
}, },
.QueryType = QueryType,
}; };
} }
@ -394,9 +386,10 @@ pub fn Relation(comptime ToModel: type, comptime ToTable: type) type {
inlineMapping: *const fn (self: *anyopaque) bool, inlineMapping: *const fn (self: *anyopaque) bool,
genJoin: *const fn (self: *anyopaque, comptime alias: []const u8) []const u8, genJoin: *const fn (self: *anyopaque, comptime alias: []const u8) []const u8,
genSelect: *const fn (self: *anyopaque, comptime table: []const u8, comptime prefix: []const u8) []const u8, genSelect: *const fn (self: *anyopaque, comptime table: []const u8, comptime prefix: []const u8) []const u8,
getQueryType: *const fn () type,
}, },
QueryType: type,
/// Read the related model repository configuration. /// Read the related model repository configuration.
pub fn getRepositoryConfiguration(self: Self) repository.RepositoryConfiguration(ToModel, ToTable) { pub fn getRepositoryConfiguration(self: Self) repository.RepositoryConfiguration(ToModel, ToTable) {
return self._interface.getRepositoryConfiguration(self._interface.instance); return self._interface.getRepositoryConfiguration(self._interface.instance);
@ -417,11 +410,6 @@ pub fn Relation(comptime ToModel: type, comptime ToTable: type) type {
pub fn genSelect(self: Self, comptime table: []const u8, comptime prefix: []const u8) []const u8 { pub fn genSelect(self: Self, comptime table: []const u8, comptime prefix: []const u8) []const u8 {
return self._interface.genSelect(self._interface.instance, table, prefix); return self._interface.genSelect(self._interface.instance, table, prefix);
} }
/// Get relation query type.
pub fn getQueryType(self: Self) type {
return self._interface.getQueryType();
}
}; };
} }

View file

@ -177,7 +177,7 @@ pub fn ResultMapper(comptime Model: type, comptime TableShape: type, comptime Me
const relationInstance = relationImpl.runtimeRelation(); const relationInstance = relationImpl.runtimeRelation();
// Build query for the relation to get. // Build query for the relation to get.
const query: *comptimeRelation.getQueryType() = @ptrCast(@alignCast( const query: *comptimeRelation.QueryType = @ptrCast(@alignCast(
try relationInstance.buildQuery("relations." ++ relation.field ++ ".", @ptrCast(models.items), allocator, connector) try relationInstance.buildQuery("relations." ++ relation.field ++ ".", @ptrCast(models.items), allocator, connector)
)); ));
defer { defer {