Skip to content

Commit 4e529ab

Browse files
committed
ExecuteCommand - Unsupported SELECT command
1 parent e33523a commit 4e529ab

File tree

6 files changed

+100
-2
lines changed

6 files changed

+100
-2
lines changed

QueryDB.Core.Tests/MSSQLTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using QueryDB.Exceptions;
23
using System;
34
using System.Linq;
45

@@ -402,6 +403,27 @@ public void Test_MSSQL_ExecuteCommand_DML_Queries()
402403
Assert.IsTrue(data.Count == 0);
403404
}
404405

406+
[TestMethod]
407+
[TestCategory(DB_TESTS), TestCategory(MSSQL_TESTS)]
408+
public void Test_MSSQL_ExecuteCommand_DML_Unsupported_SELECT_Queries()
409+
{
410+
var selectSql = Queries.MSSQLQueries.TestDB.DML.SelectSql;
411+
412+
// Select
413+
try
414+
{
415+
var dbContext = new DBContext(DB.MSSQL, MSSQLConnectionString);
416+
var rows = dbContext.ExecuteCommand(selectSql);
417+
Assert.Fail("No Exception");
418+
}
419+
catch (QueryDBException ex)
420+
{
421+
Assert.AreEqual(ex.Message, "SELECT queries are not supported here.");
422+
Assert.AreEqual(ex.ErrorType, "UnsupportedCommand");
423+
Assert.AreEqual(ex.AdditionalInfo, "'ExecuteCommand' doesn't support SELECT queries.");
424+
}
425+
}
426+
405427
#endregion
406428

407429
#endregion

QueryDB.Core.Tests/MySQLTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using QueryDB.Exceptions;
23
using System;
34
using System.Linq;
45

@@ -382,6 +383,27 @@ public void Test_MySQL_ExecuteCommand_DML_Queries()
382383
Assert.IsTrue(data.Count == 0);
383384
}
384385

386+
[TestMethod]
387+
[TestCategory(DB_TESTS), TestCategory(MYSQL_TESTS)]
388+
public void Test_MySQL_ExecuteCommand_DML_Unsupported_SELECT_Queries()
389+
{
390+
var selectSql = Queries.MySQLQueries.TestDB.DML.SelectSql;
391+
392+
// Select
393+
try
394+
{
395+
var dbContext = new DBContext(DB.MySQL, MySQLConnectionString);
396+
var rows = dbContext.ExecuteCommand(selectSql);
397+
Assert.Fail("No Exception");
398+
}
399+
catch (QueryDBException ex)
400+
{
401+
Assert.AreEqual(ex.Message, "SELECT queries are not supported here.");
402+
Assert.AreEqual(ex.ErrorType, "UnsupportedCommand");
403+
Assert.AreEqual(ex.AdditionalInfo, "'ExecuteCommand' doesn't support SELECT queries.");
404+
}
405+
}
406+
385407
#endregion
386408

387409
#endregion

QueryDB.Core.Tests/OracleTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using QueryDB.Exceptions;
23
using System;
34
using System.Linq;
45

@@ -384,6 +385,27 @@ public void Test_Oracle_ExecuteCommand_DML_Queries()
384385
Assert.IsTrue(data.Count == 0);
385386
}
386387

388+
[TestMethod]
389+
[TestCategory(DB_TESTS), TestCategory(ORACLE_TESTS)]
390+
public void Test_Oracle_ExecuteCommand_DML_Unsupported_SELECT_Queries()
391+
{
392+
var selectSql = Queries.OracleQueries.TestDB.DML.SelectSql;
393+
394+
// Select
395+
try
396+
{
397+
var dbContext = new DBContext(DB.Oracle, OracleConnectionString);
398+
var rows = dbContext.ExecuteCommand(selectSql);
399+
Assert.Fail("No Exception");
400+
}
401+
catch (QueryDBException ex)
402+
{
403+
Assert.AreEqual(ex.Message, "SELECT queries are not supported here.");
404+
Assert.AreEqual(ex.ErrorType, "UnsupportedCommand");
405+
Assert.AreEqual(ex.AdditionalInfo, "'ExecuteCommand' doesn't support SELECT queries.");
406+
}
407+
}
408+
387409
#endregion
388410

389411
#endregion

QueryDB.Core.Tests/PostgreSQLTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using QueryDB.Exceptions;
23
using System;
34
using System.Linq;
45

@@ -376,6 +377,27 @@ public void Test_PostgreSQL_ExecuteCommand_DML_Queries()
376377
Assert.IsTrue(data.Count == 0);
377378
}
378379

380+
[TestMethod]
381+
[TestCategory(DB_TESTS), TestCategory(POSTGRESQL_TESTS)]
382+
public void Test_PostgreSQL_ExecuteCommand_DML_Unsupported_SELECT_Queries()
383+
{
384+
var selectSql = Queries.PostgreSQLQueries.TestDB.DML.SelectSql;
385+
386+
// Select
387+
try
388+
{
389+
var dbContext = new DBContext(DB.PostgreSQL, PostgreSQLConnectionString);
390+
var rows = dbContext.ExecuteCommand(selectSql);
391+
Assert.Fail("No Exception");
392+
}
393+
catch (QueryDBException ex)
394+
{
395+
Assert.AreEqual(ex.Message, "SELECT queries are not supported here.");
396+
Assert.AreEqual(ex.ErrorType, "UnsupportedCommand");
397+
Assert.AreEqual(ex.AdditionalInfo, "'ExecuteCommand' doesn't support SELECT queries.");
398+
}
399+
}
400+
379401
#endregion
380402

381403
#endregion

QueryDB.Core.Tests/Queries.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ internal static class DML
4040
internal static string UpdateSql = @"UPDATE Agents SET Commission = '0.15' WHERE Agent_Code = 'A020'";
4141
internal static string DeleteSql = @"DELETE FROM Agents WHERE Agent_Code = 'A020'";
4242
internal static string VerifyDMLExecution = @"SELECT * FROM Agents WHERE Agent_Code = 'A020'";
43+
internal static string SelectSql = @"SELECT * FROM Agents";
4344
}
4445
}
4546
}
@@ -82,6 +83,7 @@ internal static class DML
8283
internal static string UpdateSql = @"UPDATE Agents SET Commission = '0.15' WHERE Agent_Code = 'A020'";
8384
internal static string DeleteSql = @"DELETE FROM Agents WHERE Agent_Code = 'A020'";
8485
internal static string VerifyDMLExecution = @"SELECT * FROM Agents WHERE Agent_Code = 'A020'";
86+
internal static string SelectSql = @"SELECT * FROM Agents";
8587
}
8688
}
8789
}
@@ -124,6 +126,7 @@ internal static class DML
124126
internal static string UpdateSql = @"UPDATE Agents SET Commission = '0.15' WHERE Agent_Code = 'A020'";
125127
internal static string DeleteSql = @"DELETE FROM Agents WHERE Agent_Code = 'A020'";
126128
internal static string VerifyDMLExecution = @"SELECT * FROM Agents WHERE Agent_Code = 'A020'";
129+
internal static string SelectSql = @"SELECT * FROM Agents";
127130
}
128131
}
129132
}
@@ -166,6 +169,7 @@ internal static class DML
166169
internal static string UpdateSql = @"UPDATE Agents SET Commission = '0.15' WHERE Agent_Code = 'A020'";
167170
internal static string DeleteSql = @"DELETE FROM Agents WHERE Agent_Code = 'A020'";
168171
internal static string VerifyDMLExecution = @"SELECT * FROM Agents WHERE Agent_Code = 'A020'";
172+
internal static string SelectSql = @"SELECT * FROM Agents";
169173
}
170174
}
171175
}

QueryDB/DBContext.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
using QueryDB.Resources;
1+
using QueryDB.Exceptions;
2+
using QueryDB.Resources;
3+
using System;
24
using System.Collections.Generic;
5+
using System.Text.RegularExpressions;
36

47
namespace QueryDB
58
{
@@ -113,7 +116,7 @@ public List<DataDictionary> FetchData(string selectSql, bool upperCaseKeys = fal
113116
{
114117
var _systemAdapter = new MSSQL.Adapter();
115118
dataList = _systemAdapter.FetchData<T>(selectSql, msSqlDBConnection.SqlConnection, strict);
116-
}
119+
}
117120
}
118121
else if (Database.Equals(DB.MySQL))
119122
{
@@ -149,6 +152,9 @@ public List<DataDictionary> FetchData(string selectSql, bool upperCaseKeys = fal
149152
/// <returns>The number of rows affected.</returns>
150153
public int ExecuteCommand(string sqlStatement)
151154
{
155+
if (Regex.IsMatch(sqlStatement, "^\\s*SELECT\\s+.*", RegexOptions.IgnoreCase | RegexOptions.Singleline, TimeSpan.FromSeconds(5)))
156+
throw new QueryDBException(QueryDBExceptions.ErrorMessage.UnsupportedSelectExecuteCommand,
157+
QueryDBExceptions.ErrorType.UnsupportedCommand, QueryDBExceptions.AdditionalInfo.UnsupportedSelectExecuteCommand);
152158
if (Database.Equals(DB.MSSQL))
153159
{
154160
using (var msSqlDBConnection = GetSqlServerConnection())

0 commit comments

Comments
 (0)