From 66ff3fe9a514ad7b92e3787408454114f5538a71 Mon Sep 17 00:00:00 2001 From: vabenil Date: Tue, 8 Nov 2022 15:06:24 +0200 Subject: [PATCH 1/3] remove redundant private --- database_generation.d | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/database_generation.d b/database_generation.d index 84e8826..56bf564 100644 --- a/database_generation.d +++ b/database_generation.d @@ -928,20 +928,18 @@ string toFieldName(T)(string s, bool isPlural = false) +/ template one_to_many(alias fk_field, string t2 = null, string t1 = null) { - private { - alias T1 = __traits(parent, fk_field); + alias T1 = __traits(parent, fk_field); - static assert( - isFieldRefInAttributes!(__traits(getAttributes, fk_field)), - T1.stringof ~ "." ~ fk_field.stringof ~ " does't have a ForeignKey"); + static assert( + isFieldRefInAttributes!(__traits(getAttributes, fk_field)), + T1.stringof ~ "." ~ fk_field.stringof ~ " does't have a ForeignKey"); - alias FieldRef = getRefToField!(fk_field); - alias T2 = FieldRef.Table; - alias ref_field = FieldRef.field; + alias FieldRef = getRefToField!(fk_field); + alias T2 = FieldRef.Table; + alias ref_field = FieldRef.field; - immutable string t2_name = toFieldName!T2(t2); - immutable string t1_name = toFieldName!T1(t1, true); - } + immutable string t2_name = toFieldName!T2(t2); + immutable string t1_name = toFieldName!T1(t1, true); static immutable string one_to_many = T2.stringof~` get_`~t2_name~`(`~T1.stringof~` row, Database db) From a0e2efc223f559294b360bdeacbe69d92aa8ca8e Mon Sep 17 00:00:00 2001 From: vabenil Date: Tue, 8 Nov 2022 15:12:46 +0200 Subject: [PATCH 2/3] one_to_many handle when t1 or t2 are "" --- database_generation.d | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/database_generation.d b/database_generation.d index 56bf564..882cf69 100644 --- a/database_generation.d +++ b/database_generation.d @@ -941,7 +941,7 @@ template one_to_many(alias fk_field, string t2 = null, string t1 = null) immutable string t2_name = toFieldName!T2(t2); immutable string t1_name = toFieldName!T1(t1, true); - static immutable string one_to_many = + static immutable string one = (t2 is "") ? "" : T2.stringof~` get_`~t2_name~`(`~T1.stringof~` row, Database db) { import std.exception; @@ -955,7 +955,8 @@ template one_to_many(alias fk_field, string t2 = null, string t1 = null) ).to_table_rows!`~T2.stringof~`; return res.front(); - } + }`; + static immutable string many = (t1 is "") ? "" : ` TabResultSet!`~T1.stringof~` get_`~t1_name~`(`~T2.stringof~` row, Database db) { import std.exception; @@ -970,4 +971,5 @@ template one_to_many(alias fk_field, string t2 = null, string t1 = null) return res; }`; + static immutable string one_to_many = one ~ many; } From f0d649300c7e537c40f0013e702017cccbc423b3 Mon Sep 17 00:00:00 2001 From: vabenil Date: Tue, 8 Nov 2022 15:50:08 +0200 Subject: [PATCH 3/3] Update one_to_many doc --- database_generation.d | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/database_generation.d b/database_generation.d index 882cf69..40ff734 100644 --- a/database_generation.d +++ b/database_generation.d @@ -920,8 +920,9 @@ string toFieldName(T)(string s, bool isPlural = false) } --- - if t2 or t1 are set as null they will be inferred from either - the `DBName` attribute or from the name of the Table + if t2 or t1 are set as "" the get function will not be generated + (the name will not be inferred), if set as null they will be inferred from + either the `DBName` attribute or from the name of the Table. History: Added November 5, 2022 (dub v10.10)