This repository was archived by the owner on Oct 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.cs
More file actions
149 lines (125 loc) · 4.58 KB
/
Database.cs
File metadata and controls
149 lines (125 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using MySql.Data.MySqlClient;
using SqlKata.Compilers;
using SqlKata.Execution;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ZeyjaFramework.Config;
using ZeyjaFramework.Interfaces;
namespace ZeyjaFramework
{
/// <summary>
/// Handles everyday database interactions.
/// </summary>
public class Database : IDatabase
{
private readonly DatabaseConfig _config;
private readonly List<QueryFactory> _availableConnections = new();
private readonly List<QueryFactory> _reservedConnections = new();
/// <summary>
/// Initializes a new instance of the <see cref="Database"/> class.
/// </summary>
///
/// <param name="config">The <see cref="DatabaseConfig"/>.</param>
public Database(DatabaseConfig config)
{
_config = config;
for (int i = 0; i < 10; i++)
{
InitializeConnection();
}
}
/// <summary>
/// Reserves a database connection <see cref="ReserveConnection"/>
/// or creates a new database connection <see cref="InitializeConnection"/>.
/// </summary>
///
/// <returns>A query factory connection.</returns>
public QueryFactory Db()
{
return ReserveConnection();
}
/// <summary>
/// Initializes a new connection.
/// </summary>
///
/// <param name="andReserve">if true the connection is created in <see cref="_reservedConnections"/>
/// opposed to <see cref="_availableConnections"/>.</param>
///
/// <returns>A QueryFactory.</returns>
private QueryFactory InitializeConnection(bool andReserve = false)
{
if ((_availableConnections.Count + _reservedConnections.Count) >= _config.MaxConnections)
{
throw new Exception("Reached max database connections.");
}
QueryFactory newConn = null;
try
{
newConn = new QueryFactory(new MySqlConnection(_config.GetConnectionString()), new MySqlCompiler());
}
catch (Exception ex)
{
throw new Exception("Unable to connect to database.", ex);
}
if (andReserve) { _reservedConnections.Add(newConn); }
else { _availableConnections.Add(newConn); }
return newConn;
}
/// <summary>
/// Reserves an existing connection.
/// </summary>
///
/// <returns>A QueryFactory.</returns>
private QueryFactory ReserveConnection()
{
if (!_availableConnections.Any())
{
CleanupConnections(); // TODO: Move to scheduled task, or better implementation
return InitializeConnection(true);
}
QueryFactory newConn = _availableConnections.First();
if (newConn.Connection.State == ConnectionState.Closed)
{
newConn.Connection.Open();
}
_availableConnections.Remove(newConn);
_reservedConnections.Add(newConn);
return newConn;
}
/// <summary>
/// Releases unused connections in <see cref="_reservedConnections"/> to <see cref="_availableConnections"/> and
/// reopens any closed connections.
/// </summary>
private void CleanupConnections()
{
foreach (QueryFactory conn in _reservedConnections)
{
switch (conn.Connection.State)
{
case ConnectionState.Closed:
conn.Connection.Open();
_reservedConnections.Remove(conn);
_availableConnections.Add(conn);
break;
case ConnectionState.Open:
_reservedConnections.Remove(conn);
_availableConnections.Add(conn);
break;
case ConnectionState.Connecting:
break;
case ConnectionState.Executing:
break;
case ConnectionState.Fetching:
break;
case ConnectionState.Broken:
conn.Connection.Open();
_reservedConnections.Remove(conn);
_availableConnections.Add(conn);
break;
}
}
}
}
}