1 ///
2 module dpq.mixins;
3 
4 import dpq.relationproxy;
5 import std.typecons : Nullable;
6 
7 /**
8 	Provides static methods for easy access to RelationProxy for the type
9 	this is used in. For docs on all the methods see RelationProxy's own
10 	docs.
11 
12 	Examples:
13 	------------------
14 	struct User
15 	{
16 		mixin RelationMixin;
17 
18 		@PK @serial int id;
19 		string username;
20 		ubyte[] password;
21 		int posts;
22 	}
23 
24 	auto firstUser = User.where(...).first;
25 	auto nUpdated = User.where(...).updateAll(["posts": 123]);
26 	auto userCount = User.where(...).count;
27 	------------------
28  */
29 mixin template RelationMixin()
30 {
31 	alias Type = typeof(this);
32 	alias ProxyT = RelationProxy!Type;
33 
34 	import dpq.connection : _dpqLastConnection;
35 	import std.typecons : Nullable;
36 
37 	@property static ProxyT relationProxy()
38 	{
39 		return ProxyT(*_dpqLastConnection);
40 	}
41 
42 	static ProxyT where(U)(U[string] filters)
43 	{
44 		return relationProxy.where(filters);
45 	}
46 
47 	static ProxyT where(U...)(string filter, U params)
48 	{
49 		return relationProxy.where(filter, params);
50 	}
51 
52 	static Type find(U)(U param)
53 	{
54 		return relationProxy.find(param);
55 	}
56 
57 	static Type findBy(U)(U[string] filters)
58 	{
59 		return relationProxy.findBy(filters);
60 	}
61 
62 	static ref Type insert(ref Type record)
63 	{
64 		return relationProxy.insert(record);	
65 	}
66 
67 	@property static Nullable!Type first()
68 	{
69 		return relationProxy.first;
70 	}
71 
72 	@property static Nullable!Type last()
73 	{
74 		return relationProxy.last;
75 	}
76 
77 	@property static Type[] all()
78 	{
79 		return relationProxy.all;
80 	}
81 
82 	static auto updateAll(U)(U[string] updates)
83 	{
84 		return relationProxy.updateAll(updates);
85 	}
86 
87 	static auto updateOne(U, Tpk)(Tpk id, U[string] values)
88 	{
89 		return relationProxy.update(id, values);
90 	}
91 
92 	static auto removeOne(Tpk)(Tpk id)
93 	{
94 		return relationProxy.remove(id);
95 	}
96 	
97 	static bool saveRecord(Type record)
98 	{
99 		return relationProxy.save(record);
100 	}
101 
102 	static long count(string col = "*")
103 	{
104 		return relationProxy.count(col);
105 	}
106 }