SQL Search for Objects Syntax and Usage
Note
This feature is only available in NCache Enterprise Edition.
NCache provides you a list of data retrieval options including selected projections as well as whole cache items along with the keys. Using an SQL-like query syntax, you can easily retrieve selected projections, all projections(using *
), Groups, Named Tags etc. This is done through ExecuteReader()
which returns the result set in a tabular form to ICacheReader
type of instance. Column count and attribute values can be fetched through their names or through index number from ICacheReader
. Make sure the instance of ICacheReader
is always closed after execution, as it is necessary to clean the resources.
The new query syntax allows you to retrieve specific projections from the cache store. Also, you can now specify *
to retrieve the whole data of a type from the cache.
Pre-Requisites
- Install the following NuGet packages:
- Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
- Include the following namespaces in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Caching
Alachisoft.NCache.Client.Services
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- For API details, refer to: ICache, ICacheReader, ExecuteReader, SearchService, QueryCommand.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
- Add the following Maven dependencies in your
pom.xml
file:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-client</artifactId>
<version>5.2.0</version>
</dependency>
- Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
- Import the following packages in your application:
import com.alachisoft.ncache.client.*;
import com.alachisoft.ncache.runtime.exceptions.*;
- The application must be connected to cache before performing the operation.
- Cache must be running.
- For API details, refer to: Cache, CacheReader, executeReader, getSearchService(), QueryCommand.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
Syntax
SELECT All
Below-mentioned is an example which retrieves all the types from the cache through the *
operator.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Provide Fully Qualified Name (FQN) of your custom class
string query = "SELECT * FROM FQN.Product WHERE ProductID > ?";
/// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.Parameters.Add("ProductID",50000);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
string result = reader.GetValue<string>("ProductID");
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure that the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Provide Fully Qualified Name (FQN) of your custom class
String query = "SELECT * FROM FQN.Product WHERE ProductID > ?";
/// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.getParameters().put("ProductID", 50000);
// Executing QueryCommand through CacheReader
CacheReader reader = cache.getSearchService().executeReader(queryCommand);
// Check if the result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
String result = reader.getValue("ProductID", String.class);
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.getErrorCode() == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like IllegalArgumentException or NullPointerException
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Provide Fully Qualified Name (FQN) of your custom class
var query = "SELECT * FROM FQN.Product WHERE ProductID > ?";
/// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
// Providing parameters for query
let parameter = new Map();
parameter.set("ProductID", 50000);
queryCommand.setParameters(parameter);
// Executing QueryCommand through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if the result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
var result = reader.getValue(1, Number());
// Perform operations
}
}
else
{
// Null query result set retrieved
}
} catch (error)
{
// Handle errors
}
SELECT Column Name
Below mentioned is an example which retrieves ProductName
column from the cache.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Provide Fully Qualified Name (FQN) of your custom class
string query = "SELECT ProductName FROM FQN.Product WHERE ProductID > ?";
/// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.Parameters.Add("ProductID",50000);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
string result = reader.GetValue<string>("ProductName");
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure that the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Provide Fully Qualified Name (FQN) of your custom class
String query = "SELECT ProductName FROM FQN.Product WHERE ProductID > ?";
// Use QueryCommand for query execution
QueryCommand queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.getParameters().put("ProductID", 50000);
// Executing QueryCommand through CacheReader
CacheReader reader = cache.getSearchService().executeReader(queryCommand);
// Check if the result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
String result = reader.getValue("ProductName", String.class);
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.getErrorCode() == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like IllegalArgumentException or NullPointerException
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Provide Fully Qualified Name (FQN) of your custom class
var query = "SELECT ProductName FROM FQN.Product WHERE ProductID > ?";
/// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
// Providing parameters for query
let parameter = new Map();
parameter.set("ProductID", 50000);
queryCommand.setParameters(parameter);
// Executing QueryCommand through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if the result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
var result = reader.getValue(1, Number());
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (error)
{
// Handle errors
}
SELECT Multiple Columns
In addition to specific column projections, you can also retrieve multiple projections. Below-mentioned is an example which projects $GROUP$
and $Value$
in a single query.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Provide Fully Qualified Name (FQN) of your custom class
// Assume that the Product entered is associated to a group
string query = "SELECT $Value$, $Group$ FROM FQN.Product WHERE ProductID > ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.Parameters.Add("ProductID",50000);
// Executing QueryCommand through ICacheReader
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
Product value = reader.GetValue<Product>(1);
string groupName = reader.GetValue<string>(2);
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure that the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentException, ArgumentNullException
}
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Provide Fully Qualified Name (FQN) of your custom class
// Assume that the Product entered is associated to a group
String query = "SELECT $Value$, $Group$ FROM FQN.Product WHERE ProductID > ?";
// Use QueryCommand for query execution
QUeryCommand queryCommand = new QueryCommand(query);
// Providing parameters for query
queryCommand.getParameters().put("ProductID", 50000);
// Executing QueryCommand through CacheReader
CacheReader reader = cache.getSearchService().executeReader(queryCommand);
// Check if the result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
Product value = reader.getValue(1, Product.class);
String groupName = reader.getValue(2, String.class);
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.getErrorCode() == NCacheErrorCodes.INCORRECT_FORMAT)
{
// Make sure the query format is correct
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like IllegalArgumentException or NullPointerException
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Provide Fully Qualified Name (FQN) of your custom class
// Assume that the Product entered is associated to a group
var query = "SELECT $Value$, $Group$ FROM FQN.Product WHERE ProductID > ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
// Providing parameters for query
let parameter = new Map();
parameter.set("ProductID", 50000);
queryCommand.setParameters(parameter);
// Executing QueryCommand through CacheReader
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if the result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
var value = reader.getValue(1, ncache.JsonDataType.Object);
var groupName = reader.getValue(2, Number());
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (error)
{
// Handle errors
}
Additional Resources
NCache provides sample application for SQL Searching on
GitHub.
See Also
Locking Data For Concurrency Control
SQL Search for Keys Syntax and Usage
SQL IN Operator Syntax and Usage
SQL Like Operator Syntax and Usage
SQL GROUP BY Syntax and Usage
Query Operators
Search Cache with LINQ