dev541
dev541

Reputation: 53

How to run SQL Expression Query without any underlying database

I have a situation where the user enters some validation input in SQL format. This SQL query is just using basic sql date functions but not using any databse column. Sample query:

Select UNIX_TIMESTAMP(NOW()) < UNIX_TIMESTAMP(CASE WHEN DAYOFMONTH(NOW()) <= 10
THEN add_MONTHS(DATE_ADD(NOW(), (-1*DAYOFMONTH(NOW()))+1),-1)
ELSE DATE_ADD(NOW(), (-1*DAYOFMONTH(NOW()))+1) END)

I am able to run this when i have a connection object configured to some real database. But i do not find it worth to use a database and manage it just for this purpose. Is there a way where i can execute these type of queries or queries like select date() from dual etc in my code.

Upvotes: 0

Views: 5906

Answers (2)

paulsm4
paulsm4

Reputation: 121881

Frankly, if you're going to make SQL database queries ... it's generally best to have a database to query against :)

Furthermore, if your purpose is to "test" your queries ... since many things are DB-specific - it's generally best to have an instance of the actual DB you wish to test against.

Having said that, there are many options for exercising SQL without an underlying database:

  • You can use an on-line "fiddle" to test SQL queries, such as SQLFiddle.com

  • For unit tests, you'd use a mock framework. For example, for Java/JUnit, I'd use Mockito.

  • If I were testing Spring/Hibernate data access, I would use an in-memory, lightweight H2 database.

There are really many, many alternatives. It all just depends on your particular "use case"; what you're actually trying to accomplish.

PS:

I gave a number of Java examples. Here's an article about mocking Microsoft/EF data calls:

https://entityframework.net/how-to-mock-data

Upvotes: 0

Trong Hiep Le
Trong Hiep Le

Reputation: 41

no, a sql query needs to run against a database engine.

For example select date() from dual is an oracle query; you can't run it against mssql or mysql.

Upvotes: -1

Related Questions