Skip to content

Commit c410ed2

Browse files
author
“llt”
committed
Better connection parameterization
1 parent e2d86b4 commit c410ed2

2 files changed

Lines changed: 40 additions & 40 deletions

File tree

frameworks/CSharp/appmpower/src/appMpower.Orm/Data/DbConnection.cs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,28 @@ namespace appMpower.Orm.Data
88
public class DbConnection : IDbConnection
99
{
1010
private string _connectionString;
11+
private System.Data.Common.DbConnection _dbConnection;
12+
#if ODBC
1113
private bool _keyed = false;
1214
private int _number;
13-
private System.Data.Common.DbConnection _dbConnection;
1415
private ConcurrentStack<System.Data.Common.DbCommand> _dbCommands = new();
1516
private Dictionary<string, System.Data.Common.DbCommand> _keyedDbCommands;
17+
#endif
1618

1719
public DbConnection()
1820
{
1921
}
2022

2123
public DbConnection(string connectionString, bool keyed = false)
2224
{
23-
_keyed = keyed;
2425
_connectionString = connectionString;
2526

26-
if (Constants.DbProvider == DbProvider.ODBC)
27-
{
28-
GetConnection();
29-
}
30-
else
31-
{
32-
_dbConnection = DbFactory.GetConnection(_connectionString);
33-
}
27+
#if ODBC
28+
_keyed = keyed;
29+
GetConnection();
30+
#else
31+
_dbConnection = DbFactory.GetConnection(_connectionString);
32+
#endif
3433
}
3534

3635
public IDbConnection Connection
@@ -54,10 +53,13 @@ public string ConnectionString
5453
set
5554
{
5655
_connectionString = value;
56+
#if ODBC
5757
GetConnection();
58+
#endif
5859
}
5960
}
6061

62+
#if ODBC
6163
private void GetConnection()
6264
{
6365
if (_keyed)
@@ -71,6 +73,7 @@ private void GetConnection()
7173
DbConnections.GetConnectionBase();
7274
}
7375
}
76+
#endif
7477

7578
public int ConnectionTimeout
7679
{
@@ -140,58 +143,55 @@ public async Task OpenAsync()
140143

141144
public void Dispose()
142145
{
143-
if (Constants.DbProvider == DbProvider.ODBC)
146+
#if ODBC
147+
if (_keyed)
144148
{
145-
if (_keyed)
146-
{
147-
DbConnectionsKeyed.Release((Number: _number, DbConnection: _dbConnection, KeyedDbCommands: _keyedDbCommands));
148-
}
149-
else
150-
{
151-
DbConnections.Release((Number: _number, DbConnection: _dbConnection, DbCommands: _dbCommands));
152-
}
149+
DbConnectionsKeyed.Release((Number: _number, DbConnection: _dbConnection, KeyedDbCommands: _keyedDbCommands));
153150
}
154151
else
155152
{
156-
_dbConnection.Dispose();
153+
DbConnections.Release((Number: _number, DbConnection: _dbConnection, DbCommands: _dbCommands));
157154
}
155+
#else
156+
_dbConnection.Dispose();
157+
#endif
158158
}
159159

160160
internal System.Data.Common.DbCommand GetCommand(string commandText, CommandType commandType)
161161
{
162162
System.Data.Common.DbCommand dbCommand;
163163

164-
if (Constants.DbProvider == DbProvider.ODBC)
164+
#if ODBC
165+
if (_dbCommands.TryPop(out dbCommand))
165166
{
166-
if (_dbCommands.TryPop(out dbCommand))
167+
if (commandText != dbCommand.CommandText)
167168
{
168-
if (commandText != dbCommand.CommandText)
169-
{
170-
dbCommand.CommandText = commandText;
171-
dbCommand.Parameters.Clear();
172-
}
173-
174-
return dbCommand;
169+
dbCommand.CommandText = commandText;
170+
dbCommand.Parameters.Clear();
175171
}
176-
else if (_keyed && _keyedDbCommands.TryGetValue(commandText, out dbCommand)) return dbCommand;
172+
173+
return dbCommand;
177174
}
175+
else if (_keyed && _keyedDbCommands.TryGetValue(commandText, out dbCommand)) return dbCommand;
176+
#endif
178177

179178
dbCommand = _dbConnection.CreateCommand();
180179
dbCommand.CommandText = commandText;
181180
dbCommand.CommandType = commandType;
182181

183-
if (!(Constants.Dbms == Dbms.PostgreSQL && Constants.DbProvider == DbProvider.ADO)) dbCommand.Prepare();
182+
#if !(ADO && POSTGRESQL)
183+
dbCommand.Prepare();
184+
#endif
184185

185186
return dbCommand;
186187
}
187188

188189
internal void Release(System.Data.Common.DbCommand dbCommand)
189190
{
190-
if (Constants.DbProvider == DbProvider.ODBC)
191-
{
192-
if (_keyed) _keyedDbCommands.TryAdd(dbCommand.CommandText, dbCommand);
193-
else _dbCommands.Push(dbCommand);
194-
}
191+
#if ODBC
192+
if (_keyed) _keyedDbCommands.TryAdd(dbCommand.CommandText, dbCommand);
193+
else _dbCommands.Push(dbCommand);
194+
#endif
195195
}
196196
}
197197
}

frameworks/CSharp/appmpower/src/appMpower.Orm/RawDb.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ public static async Task<List<Fortune>> LoadFortunesRowsAsync()
186186
using (dbCommand)
187187
{
188188
#if AOTDLL
189-
IDataReader dataReader = dbCommand.ExecuteReader(CommandBehavior.SingleResult & CommandBehavior.SequentialAccess);
189+
var dataReader = dbCommand.ExecuteReader(CommandBehavior.SingleResult & CommandBehavior.SequentialAccess);
190+
while (dataReader.Read())
190191
#else
191-
IDataReader dataReader = await dbCommand.ExecuteReaderAsync(CommandBehavior.SingleResult & CommandBehavior.SequentialAccess);
192+
var dataReader = await dbCommand.ExecuteReaderAsync(CommandBehavior.SingleResult & CommandBehavior.SequentialAccess);
193+
while (await dataReader.ReadAsync())
192194
#endif
193-
194-
while (dataReader.Read())
195195
{
196196
fortunes.Add(new Fortune
197197
(

0 commit comments

Comments
 (0)