1 /++
2 Connect to a MySQL/MariaDB database using vibe.d's
3 $(LINK2 http://vibed.org/api/vibe.core.connectionpool/ConnectionPool, ConnectionPool).
4 
5 You have to include vibe.d in your project to be able to use this class.
6 If you don't want to, refer to `mysql.connection.Connection`.
7 
8 This provides various benefits over creating a new Connection manually,
9 such as automatically reusing old connections, and automatic cleanup (no need to close
10 the connection when done).
11 +/
12 module mysql.pool;
13 
14 import std.conv;
15 import mysql.connection;
16 import mysql.protocol.constants;
17 
18 version(Have_vibe_d_core) version = IncludeMySQLPool;
19 version(MySQLDocs)        version = IncludeMySQLPool;
20 
21 version(IncludeMySQLPool)
22 {
23 	version(Have_vibe_d_core)
24 		import vibe.core.connectionpool;
25 	else
26 	{
27 		/++
28 		Vibe.d's
29 		$(LINK2 http://vibed.org/api/vibe.core.connectionpool/ConnectionPool, ConnectionPool)
30 		class.
31 
32 		Not actually included in module mysql.pool. Only listed here for
33 		documentation purposes. For ConnectionPool and it's documentation, see:
34 		$(LINK http://vibed.org/api/vibe.core.connectionpool/ConnectionPool)
35 		+/
36 		class ConnectionPool(T)
37 		{
38 			/// See: http://vibed.org/api/vibe.core.connectionpool/ConnectionPool.this
39 			this(Connection delegate() connection_factory, uint max_concurrent = (uint).max)
40 			{}
41 
42 			/// See: http://vibed.org/api/vibe.core.connectionpool/ConnectionPool.lockConnection
43 			LockedConnection!T lockConnection() { return LockedConnection!T(); }
44 
45 			/// See: http://vibed.org/api/vibe.core.connectionpool/ConnectionPool.maxConcurrency
46 			uint maxConcurrency;
47 		}
48 
49 		/++
50 		Vibe.d's
51 		$(LINK2 http://vibed.org/api/vibe.core.connectionpool/LockedConnection, LockedConnection)
52 		struct.
53 
54 		Not actually included in module mysql.pool. Only listed here for
55 		documentation purposes. For LockedConnection and it's documentation, see:
56 		$(LINK http://vibed.org/api/vibe.core.connectionpool/LockedConnection)
57 		+/
58 		struct LockedConnection(Connection) {}
59 	}
60 
61 	/++
62 	A lightweight interface to a MySQL/MariaDB  database using vibe.d's
63 	$(LINK2 http://vibed.org/api/vibe.core.connectionpool/ConnectionPool, ConnectionPool).
64 
65 	You have to include vibe.d in your project to be able to use this class.
66 	If you don't want to, refer to `mysql.connection.Connection`.
67 	+/
68 	class MySQLPool {
69 		private {
70 			string m_host;
71 			string m_user;
72 			string m_password;
73 			string m_database;
74 			ushort m_port;
75 			SvrCapFlags m_capFlags;
76 			ConnectionPool!Connection m_pool;
77 		}
78 
79 		/// Sets up a connection pool with the provided connection settings.
80 		this(string host, string user, string password, string database,
81 			ushort port = 3306, uint maxConcurrent = (uint).max,
82 			SvrCapFlags capFlags = defaultClientFlags)
83 		{
84 			m_host = host;
85 			m_user = user;
86 			m_password = password;
87 			m_database = database;
88 			m_port = port;
89 			m_capFlags = capFlags;
90 			m_pool = new ConnectionPool!Connection(&createConnection);
91 		}
92 
93 		///ditto
94 		this(string host, string user, string password, string database,
95 			ushort port, SvrCapFlags capFlags)
96 		{
97 			this(host, user, password, database, port, (uint).max, capFlags);
98 		}
99 
100 		///ditto
101 		this(string connStr, uint maxConcurrent = (uint).max, SvrCapFlags capFlags = defaultClientFlags)
102 		{
103 			auto parts = Connection.parseConnectionString(connStr);
104 			this(parts[0], parts[1], parts[2], parts[3], to!ushort(parts[4]), capFlags);
105 		}
106 
107 		///ditto
108 		this(string connStr, SvrCapFlags capFlags)
109 		{
110 			this(connStr, (uint).max, capFlags);
111 		}
112 
113 		/++
114 		Obtain a connection. If one isn't available, a new one will be created.
115 
116 		The connection returned is actually a `LockedConnection!Connection`,
117 		but it uses `alias this`, and so can be used just like a Connection.
118 		(See vibe.d's
119 		$(LINK2 http://vibed.org/api/vibe.core.connectionpool/LockedConnection, LockedConnection documentation).)
120 
121 		No other fiber will be given this Connection as long as your fiber still holds it.
122 
123 		There is no need to close, release or "unlock" this connection. It is
124 		reference-counted and will automatically be returned to the pool once
125 		your fiber is done with it.
126 		+/
127 		auto lockConnection() { return m_pool.lockConnection(); }
128 
129 		private Connection createConnection()
130 		{
131 			return new Connection(m_host, m_user, m_password, m_database, m_port, m_capFlags);
132 		}
133 
134 		/++
135 		Forwards to vibe.d's
136 		$(LINK2 http://vibed.org/api/vibe.core.connectionpool/ConnectionPool.maxConcurrency, ConnectionPool.maxConcurrency)
137 		+/
138 		@property uint maxConcurrency()
139 		{
140 			return m_pool.maxConcurrency;
141 		}
142 
143 		///ditto
144 		@property void maxConcurrency(uint maxConcurrent)
145 		{
146 			m_pool.maxConcurrency = maxConcurrent;
147 		}
148 	}
149 }