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
- 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.
- 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.
Modern Syntax
SELECT "*"
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
}
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
}
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())
{
// reader.GetValue(0) = Cache key
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
}
Legacy Syntax
Note
Legacy API is only available in NCache Enterprise Edition.
The following example creates a query which searches the cache according to the given criteria, executes it using ExecuteReader and then returns the values of the result set. The criteria being used is the UnitsInStock being greater than 10. For Java, the searchEntries method is used to search for keys and values in the cache.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Create a query which will be executed on the data set
// Use the Fully Qualified Name (FQN) of your own custom class
string query = "SELECT FQN.Product WHERE this.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.ExecuteReader(queryCommand);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
Product result = reader.GetValue<"ProductID">(1);
// 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
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Additional Resources
NCache provides sample application for SQL Searching at:
- GitHub
- Shipped with NCache: %NCHOME%\samples\dotnet\SearchUsingSQL
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