In real-life complex scenarios, your application often needs to fetch data based on particular attributes rather than searching through the primary key. Querying your data using SQL makes it easier for you to retrieve data from your database based on your custom logic at runtime.
NCache supports an SQL querying mechanism to let you search your cache according to a given criterion and return the required result set. NCache uses a query language that is very close to the native SQL structured language, which makes it easy for you to query your indexed cached data.
NCache uses query interface similar to the ADO.NET interface to support various query methods to search for or delete data from the cache. Here are some of the most important query methods supported in NCache.
string query = "SELECT * FROM FQN.Product WHERE ProductID > ?";
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("ProductID",50000);
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand, false, 0);
if (reader.FieldCount > 0)
{
while (reader.Read())
{
string result = reader.GetValue<string>(0);
}
}
String query = "SELECT * FROM FQN.Product WHERE ProductID > ?";
QueryCommand queryCommand = new QueryCommand(query);
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("ProductID", 50000);
queryCommand.getParameters().putAll(parameters);
CacheReader cacheReader = cache.getSearchService().executeReader(queryCommand);
if (cacheReader.getFieldCount() > 0) {
while (cacheReader.read()) {
cacheReader.getString(0);
}
NCache allows you to specify columns to project against your SQL query. Mentioned below are the basic, most commonly used projections supported in NCache.
SELECT COUNT(*) FROM FQN.Product WHERE UnitsInStock > ?
Refer to SQL Search for Object for more information.
These operators are used and re-used throughout the SQL database for processing and manipulating data. NCache supports various SQL operators that allow you to get the desired result efficiently. These functions are:
string query = "SELECT OrderID FROM FQN.Order WHERE OrderDate = ?";
var queryCommand = new QueryCommand(query);
DateTime orderDate = new DateTime(2022, 01, 01);
queryCommand.Parameters.Add("OrderDate", orderDate);
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
if (reader.FieldCount > 0)
{
while (reader.Read())
{
int orderID = reader.GetValue<int>("OrderID");
Console.WriteLine($"Order: {orderID} was placed on 1st January, 2022.");
}
}
else
{
Console.WriteLine($"No orders were placed on 1st January 2022.");
}
String query = "SELECT OrderID FROM FQN.Order WHERE OrderDate = ?";
QueryCommand queryCommand = new QueryCommand(query);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date orderDate;
orderDate = sdf.parse("2022-01-01");
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("OrderDate", orderDate);
queryCommand.getParameters().putAll(parameters);
CacheReader cacheReader = cache.getSearchService().executeReader(queryCommand);
if (cacheReader.getFieldCount() > 0) {
while (cacheReader.read()) {
int orderID = cacheReader.getInt(cacheReader.getOrdinal("OrderID"));
System.out.println("Order: " + orderID + " was placed on 1st January, 2022.");
}
} else {
System.out.println("No orders were placed on 1st January 2022.");
}
SQL operators are reserved keywords in the WHERE clause to perform logical and comparison operations. NCache provides you with the support of the following SQL operators to query your data according to custom conditions.
SELECT ProductName, UnitsAvailable From FQN.Product WHERE Category IN ('Beverages', 'Confections')
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.