1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.data.mapping.engines.mysql;
7 
8 import std..string : format;
9 import std.variant : Variant;
10 
11 import mysql;
12 
13 import diamond.data.mapping.engines.sqlshared;
14 
15 public
16 {
17   import diamond.data.mapping.engines.mysql.mysqladapter;
18   import diamond.data.mapping.engines.mysql.mysqlentityformatter;
19   import diamond.data.mapping.engines.mysql.mysqlmodel;
20 }
21 
22 /// A variant db parameter type.
23 alias DbParam = Variant;
24 
25 /// The connection string format.
26 private enum connectionStringFormat = "host=%s;port=%s;user=%s;pwd=%s;db=%s";
27 
28 /// The db connection string.
29 private static __gshared string _dbConnectionString;
30 
31 /// Collection of mysql connection pools.
32 private static __gshared MySQLPool[string] _pools;
33 
34 package(diamond.data.mapping.engines)
35 {
36   mixin CreatePool!(MySQLPool);
37 }
38 
39 /// Initializing Mysql
40 package(diamond) void initializeMySql()
41 {
42   import diamond.core.webconfig;
43 
44   if (!webConfig)
45   {
46     loadWebConfig();
47   }
48 
49   if (!webConfig.dbConnections || !webConfig.dbConnections.mysql)
50   {
51     return;
52   }
53 
54   auto dbConfig = webConfig.dbConnections.mysql.get("default", null);
55 
56   if (!dbConfig)
57   {
58     return;
59   }
60 
61   _dbConnectionString = connectionStringFormat.format(
62     dbConfig.host, dbConfig.port ? dbConfig.port : 3306,
63     dbConfig.user, dbConfig.password,
64     dbConfig.database
65   );
66 
67   _pools[_dbConnectionString] = new MySQLPool(_dbConnectionString);
68 
69   import diamond.data.mapping.engines.mysql.mysqlmodel;
70 
71   initializeMySqlAdapter(_dbConnectionString);
72 }
73 
74 @property
75 {
76   /// Gets the connection string.
77   auto dbConnectionString()
78   {
79     return _dbConnectionString;
80   }
81 }
82 
83 /// The mysql adapter.
84 private __gshared MySqlRawAdapter _adapter;
85 
86 /**
87 * Initializes the mysql adapter.
88 * Params:
89 *   connectionString = The connection string of the mysql adapter.
90 */
91 package(diamond) void initializeMySqlAdapter(string connectionString)
92 {
93   _adapter = new MySqlRawAdapter(connectionString);
94 }
95 
96 @property
97 {
98   /// Gets the mysql adapter.
99   MySqlRawAdapter mySqlAdapter() { return _adapter; }
100 }