@@ -313,6 +313,67 @@ class Record {
313
313
314
314
}
315
315
316
+ /**
317
+ * Helper function template for create getter delegates.
318
+ **/
319
+ static Variant delegate (Record )
320
+ createGetDelegate(T, string member)(ColumnInfo info) {
321
+ // Alias to target member, for type information.
322
+ alias current = Target! (__traits(getMember, T, member));
323
+
324
+ // Create the get delegate.
325
+ return delegate (Record local) {
326
+ // Check if null-assignable.
327
+ static if (isAssignable! (typeof (current), typeof (null ))) {
328
+ // Check that the value abides by null rules.
329
+ if (info.notNull && ! info.autoIncrement &&
330
+ __traits(getMember, cast (T)(local), member) is null ) {
331
+ throw new RecordException(" Non-nullable value of " ~
332
+ member ~ " was null." );
333
+ }
334
+ }
335
+
336
+ // Check for a length property.
337
+ static if (__traits(hasMember, typeof (current), " length" ) ||
338
+ isSomeString! (typeof (current)) || isArray! (typeof (current))) {
339
+ // Check that length doesn't exceed max.
340
+ if (info.maxLength != - 1 && __traits(getMember,
341
+ cast (T)(local), member).length > info.maxLength) {
342
+ throw new RecordException(" Value of " ~
343
+ member ~ " exceeds max length." );
344
+ }
345
+ }
346
+
347
+ // Convert value to variant.
348
+ static if (is (typeof (current) == Variant )) {
349
+ return __traits (getMember, cast (T)(local), member);
350
+ } else {
351
+ return Variant (__traits(getMember, cast (T)(local), member));
352
+ }
353
+ };
354
+ }
355
+
356
+ /**
357
+ * Helper function template for create setter delegates.
358
+ **/
359
+ static void delegate (Record , Variant )
360
+ createSetDelegate(T, string member)(ColumnInfo local) {
361
+ // Alias to target member, for type information.
362
+ alias current = Target! (__traits(getMember, T, member));
363
+
364
+ // Create the set delegate.
365
+ return delegate (Record local, Variant v) {
366
+ // Convert value from variant.
367
+ static if (is (typeof (current) == Variant )) {
368
+ auto value = v;
369
+ } else {
370
+ auto value = v.coerce! (typeof (current));
371
+ }
372
+
373
+ __traits(getMember, cast (T)(local), member) = value;
374
+ };
375
+ }
376
+
316
377
/**
317
378
* The ActiveRecord mixin.
318
379
**/
@@ -343,46 +404,8 @@ mixin template ActiveRecord(T : Record) {
343
404
info.name = name;
344
405
345
406
// Create delegate get and set.
346
- info.get = delegate (Record local) {
347
- // Check if null-assignable.
348
- static if (isAssignable! (typeof (current), typeof (null ))) {
349
- // Check that the value abides by null rules.
350
- if (info.notNull && ! info.autoIncrement &&
351
- __traits(getMember, cast (T)(local), member) is null ) {
352
- throw new RecordException(" Non-nullable value of " ~
353
- member ~ " was null." );
354
- }
355
- }
356
-
357
- // Check for a length property.
358
- static if (__traits(hasMember, typeof (current), " length" ) ||
359
- isSomeString! (typeof (current)) ||
360
- isArray! (typeof (current))) {
361
- // Check that length doesn't exceed max.
362
- if (info.maxLength != - 1 && __traits(getMember,
363
- cast (T)(local), member).length > info.maxLength) {
364
- throw new RecordException(" Value of " ~
365
- member ~ " exceeds max length." );
366
- }
367
- }
368
-
369
- // Convert value to variant.
370
- static if (is (typeof (current) == Variant )) {
371
- return __traits (getMember, cast (T)(local), member);
372
- } else {
373
- return Variant (__traits(getMember, cast (T)(local), member));
374
- }
375
- };
376
- info.set = delegate (Record local, Variant v) {
377
- // Convert value from variant.
378
- static if (is (typeof (current) == Variant )) {
379
- auto value = v;
380
- } else {
381
- auto value = v.coerce! (typeof (current));
382
- }
383
-
384
- __traits(getMember, cast (T)(local), member) = value;
385
- };
407
+ info.get = createGetDelegate! (T, member)(info);
408
+ info.set = createSetDelegate! (T, member)(info);
386
409
387
410
// Populate other fields.
388
411
foreach (annotation; __traits (getAttributes, current)) {
@@ -405,7 +428,7 @@ mixin template ActiveRecord(T : Record) {
405
428
// Check if @AutoIncrement is present.
406
429
static if (is (annotation == AutoIncrement)) {
407
430
// Check that this can be auto incremented.
408
- static if (is ( ! isNumeric! (typeof (current) ))) {
431
+ static if (! isNumeric! (typeof (current))) {
409
432
throw new RecordException(" Cannot increment" ~
410
433
member ~ " in " ~ T.stringof);
411
434
}
0 commit comments