Compare commits
2 commits
28bef7ef42
...
9d0cc13b65
Author | SHA1 | Date | |
---|---|---|---|
9d0cc13b65 | |||
26c854a39c |
4 changed files with 40 additions and 6 deletions
|
@ -14,6 +14,36 @@ pub fn Insertable(comptime ValueType: type) type {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Build an insertable structure type from a normal structure.
|
||||||
|
pub fn InsertableStruct(comptime StructType: type) type {
|
||||||
|
// Get type info of the given structure.
|
||||||
|
const typeInfo = @typeInfo(StructType);
|
||||||
|
|
||||||
|
// Initialize fields of the insertable struct.
|
||||||
|
var newFields: [typeInfo.Struct.fields.len]std.builtin.Type.StructField = undefined;
|
||||||
|
for (typeInfo.Struct.fields, &newFields) |field, *newField| {
|
||||||
|
// Create a new field for each field of the given struct.
|
||||||
|
const newFieldType = Insertable(field.type);
|
||||||
|
newField.* = std.builtin.Type.StructField{
|
||||||
|
.name = field.name,
|
||||||
|
.type = newFieldType,
|
||||||
|
.default_value = null,
|
||||||
|
.is_comptime = false,
|
||||||
|
.alignment = @alignOf(newFieldType),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the insertable structure type.
|
||||||
|
return @Type(std.builtin.Type{
|
||||||
|
.Struct = .{
|
||||||
|
.layout = .auto,
|
||||||
|
.decls = &[0]std.builtin.Type.Declaration{},
|
||||||
|
.fields = &newFields,
|
||||||
|
.is_tuple = false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// Repository insert query configuration structure.
|
/// Repository insert query configuration structure.
|
||||||
pub fn RepositoryInsertConfiguration(comptime InsertShape: type) type {
|
pub fn RepositoryInsertConfiguration(comptime InsertShape: type) type {
|
||||||
return struct {
|
return struct {
|
||||||
|
|
|
@ -50,12 +50,15 @@ pub fn ModelKeyType(comptime Model: type, comptime TableShape: type, comptime co
|
||||||
var fieldName: [keyName.len:0]u8 = undefined;
|
var fieldName: [keyName.len:0]u8 = undefined;
|
||||||
@memcpy(fieldName[0..keyName.len], keyName);
|
@memcpy(fieldName[0..keyName.len], keyName);
|
||||||
|
|
||||||
|
// Get current field type.
|
||||||
|
const fieldType = std.meta.fields(TableShape)[std.meta.fieldIndex(TableShape, keyName).?].type;
|
||||||
|
|
||||||
field.* = .{
|
field.* = .{
|
||||||
.name = &fieldName,
|
.name = &fieldName,
|
||||||
.type = std.meta.fields(TableShape)[std.meta.fieldIndex(TableShape, keyName).?].type,
|
.type = fieldType,
|
||||||
.default_value = null,
|
.default_value = null,
|
||||||
.is_comptime = false,
|
.is_comptime = false,
|
||||||
.alignment = 0,
|
.alignment = @alignOf(fieldType),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ pub const RepositoryConfiguration = repository.RepositoryConfiguration;
|
||||||
pub const RepositoryResult = repository.RepositoryResult;
|
pub const RepositoryResult = repository.RepositoryResult;
|
||||||
|
|
||||||
pub const Insertable = insert.Insertable;
|
pub const Insertable = insert.Insertable;
|
||||||
|
pub const InsertableStruct = insert.InsertableStruct;
|
||||||
|
|
||||||
pub const QueryParameter = _sql.QueryParameter;
|
pub const QueryParameter = _sql.QueryParameter;
|
||||||
pub const SqlParams = _sql.SqlParams;
|
pub const SqlParams = _sql.SqlParams;
|
||||||
|
|
|
@ -59,10 +59,10 @@ const CompositeModelRepository = zrm.Repository(CompositeModel, CompositeModelTa
|
||||||
.table = "composite_models",
|
.table = "composite_models",
|
||||||
|
|
||||||
// Insert shape used by default for inserts in the repository.
|
// Insert shape used by default for inserts in the repository.
|
||||||
.insertShape = struct {
|
.insertShape = zrm.InsertableStruct(struct {
|
||||||
secondcol: zrm.Insertable([]const u8),
|
secondcol: []const u8,
|
||||||
label: zrm.Insertable([]const u8),
|
label: []const u8,
|
||||||
},
|
}),
|
||||||
|
|
||||||
.key = &[_][]const u8{"firstcol", "secondcol"},
|
.key = &[_][]const u8{"firstcol", "secondcol"},
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue