Skip to content

Commit b2f0701

Browse files
committed
CQ - Define a constant instead of using literal multiple times
1 parent 30a2265 commit b2f0701

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

QueryDB/DBContext.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public List<DataDictionary> FetchData(string selectSql, bool upperCaseKeys = fal
153153
/// <returns>A string representing the result of the query. If the result is DBNull, an empty string is returned.</returns>
154154
public string ExecuteScalar(string sqlStatement)
155155
{
156-
if (!Regex.IsMatch(sqlStatement, @"^\s*SELECT\s+.*", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(5)))
156+
if (!Regex.IsMatch(sqlStatement, Utils.SelectQueryPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(5)))
157157
throw new QueryDBException(QueryDBExceptions.ErrorMessage.UnsupportedExecuteScalarCommand,
158158
QueryDBExceptions.ErrorType.UnsupportedCommand, QueryDBExceptions.AdditionalInfo.UnsupportedExecuteScalarCommand);
159159
var value = string.Empty;
@@ -200,7 +200,7 @@ public string ExecuteScalar(string sqlStatement)
200200
/// <returns>The result of the query, converted to the specified type. If the result is DBNull, the default value for the type is returned.</returns>
201201
public T ExecuteScalar<T>(string sqlStatement)
202202
{
203-
if (!Regex.IsMatch(sqlStatement, @"^\s*SELECT\s+.*", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(5)))
203+
if (!Regex.IsMatch(sqlStatement, Utils.SelectQueryPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(5)))
204204
throw new QueryDBException(QueryDBExceptions.ErrorMessage.UnsupportedExecuteScalarCommand,
205205
QueryDBExceptions.ErrorType.UnsupportedCommand, QueryDBExceptions.AdditionalInfo.UnsupportedExecuteScalarCommand);
206206
var value = default(T);
@@ -246,7 +246,7 @@ public T ExecuteScalar<T>(string sqlStatement)
246246
/// <returns>The number of rows affected.</returns>
247247
public int ExecuteCommand(string sqlStatement)
248248
{
249-
if (Regex.IsMatch(sqlStatement, "^\\s*SELECT\\s+.*", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(5)))
249+
if (Regex.IsMatch(sqlStatement, Utils.SelectQueryPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(5)))
250250
throw new QueryDBException(QueryDBExceptions.ErrorMessage.UnsupportedSelectExecuteCommand,
251251
QueryDBExceptions.ErrorType.UnsupportedCommand, QueryDBExceptions.AdditionalInfo.UnsupportedSelectExecuteCommand);
252252
if (Database.Equals(DB.MSSQL))
@@ -294,7 +294,7 @@ public int ExecuteCommand(string sqlStatement)
294294
/// </returns>
295295
public bool ExecuteTransaction(List<string> sqlStatements)
296296
{
297-
var selectExists = sqlStatements.Any(sqlStatement => Regex.IsMatch(sqlStatement, "^\\s*SELECT\\s+.*", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(5)));
297+
var selectExists = sqlStatements.Any(sqlStatement => Regex.IsMatch(sqlStatement, Utils.SelectQueryPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(5)));
298298
if (selectExists)
299299
throw new QueryDBException(QueryDBExceptions.ErrorMessage.UnsupportedSelectExecuteTransaction,
300300
QueryDBExceptions.ErrorType.UnsupportedCommand, QueryDBExceptions.AdditionalInfo.UnsupportedSelectExecuteTransaction);

QueryDB/Resources/Utils.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ namespace QueryDB.Resources
99
/// </summary>
1010
internal static class Utils
1111
{
12+
/// <summary>
13+
/// Regex pattern to validate that a SQL query starts with 'SELECT'.
14+
/// </summary>
15+
internal static readonly string SelectQueryPattern = @"^\s*SELECT\s+.*";
16+
1217
/// <summary>
1318
/// Checks if a specified column exists in the given data reader.
1419
/// </summary>

0 commit comments

Comments
 (0)