1 module dorm.annotations;
2 
3 import std.datetime;
4 import std.traits;
5 import std.meta;
6 
7 enum autoCreateTime;
8 enum autoUpdateTime;
9 enum autoIncrement;
10 enum timestamp;
11 enum notNull;
12 
13 alias Id = AliasSeq!(primaryKey, autoIncrement);
14 
15 struct constructValue(alias fn) {}
16 struct validator(alias fn) {}
17 
18 struct maxLength { int maxLength; }
19 
20 alias AllowedDefaultValueTypes = AliasSeq!(
21 	string, ubyte[], byte, short, int, long, ubyte, ushort, uint, ulong, float,
22 	double, bool, Date, DateTime, TimeOfDay, SysTime
23 );
24 enum isAllowedDefaultValueType(T) = staticIndexOf!(T, AllowedDefaultValueTypes) != -1;
25 struct DefaultValue(T) { T value; }
26 auto defaultValue(T)(T value) if (isAllowedDefaultValueType!T)
27 {
28 	return DefaultValue!T(value);
29 }
30 alias PossibleDefaultValueTs = staticMap!(DefaultValue, AllowedDefaultValueTypes);
31 
32 enum defaultFromInit;
33 enum primaryKey;
34 enum unique;
35 
36 struct Choices { string[] choices; }
37 Choices choices(string[] choices...) { return Choices(choices.dup); }
38 
39 struct columnName { string name; }
40 
41 struct index
42 {
43 	// part of ctor
44 	static struct priority { int priority = 10; }
45 	static struct composite { string name; }
46 
47 	// careful: never duplicate types here, otherwise the automatic ctor doesn't work
48 	priority _priority;
49 	composite _composite;
50 
51 	this(T...)(T args)
52 	{
53 		foreach (ref field; this.tupleof)
54 		{
55 			static foreach (arg; args)
56 			{
57 				static if (is(typeof(field) == typeof(arg)))
58 					field = arg;
59 			}
60 		}
61 	}
62 }
63 
64 enum embedded;
65 enum ignored;
66 
67 /// Checks if the given attribute is part of this dorm.annotations module.
68 template isDormAttribute(alias attr)
69 {
70 	static if (is(typeof(attr) == DefaultValue!T, T))
71 		enum isDormAttribute = true;
72 	else static if (is(typeof(attr)))
73 		enum isDormAttribute = __traits(isSame, __traits(parent, typeof(attr)), dorm.annotations);
74 	else static if (is(attr == constructValue!fn, alias fn))
75 		enum isDormAttribute = true;
76 	else static if (is(attr == validator!fn, alias fn))
77 		enum isDormAttribute = true;
78 	else
79 		enum isDormAttribute = __traits(isSame, __traits(parent, attr), dorm.annotations);
80 }
81 
82 /// Checks if the given attribute affects DORM Fields
83 enum isDormFieldAttribute(alias attr) = isDormAttribute!attr;
84 
85 /// Checks if the given attribute affects DORM Models (classes)
86 enum isDormModelAttribute(alias attr) = isDormAttribute!attr;
87 
88 /// Automatically generated foreign key attribute for one-to-many and many-to-many
89 /// relations. Does not need to be assigned onto any variables.
90 struct ForeignKeyImpl
91 {
92 	string table, column;
93 	ReferentialAction onUpdate = restrict;
94 	ReferentialAction onDelete = restrict;
95 }
96 
97 enum ReferentialAction
98 {
99 	restrict,
100 	cascade,
101 	setNull,
102 	setDefault
103 }
104 
105 enum restrict = ReferentialAction.restrict;
106 enum cascade = ReferentialAction.cascade;
107 enum setNull = ReferentialAction.setNull;
108 enum setDefault = ReferentialAction.setDefault;
109 
110 struct onUpdate
111 {
112 	ReferentialAction type;
113 }
114 
115 struct onDelete
116 {
117 	ReferentialAction type;
118 }