An ODBC Interface for CLIPS
- Table of Contents -
- What does the ODBC Interface do?
- How do I use the ODBC Interface
- A simple example program
- Download a more complete example
- Notes and Caveats on using the interface
- Technical Details
This function call allows a developer to issue a SQL query against an ODBC data source and have the resulting rows asserted as facts into a CLIPS program. It works with the CLIPS DLL and is meant for people who would like to embed CLIPS into a C++ program using the wrapper classes.
How do I use the ODBC Interface
This is the function prototype for the ODBC interface :int CCLIPSWrap::CLIPSODBCQuery(CString& strQuery, CString& strCredentials, CString& strDeftemplateName, BOOL bImplode)
The parameters for the ODBC interface are :
- CString& strQuery - This is the SQL query
- CString& strCredentials - This is the ODBC connect string. This must contain
DSN=ODBC data source name;
. It may also contain UID and password information. - CString& strDeftemplateName - Is the name of the deftemplate where the
facts are to be asserted. All column names returned from the SQL query are
forced to lower case. Code your deftemplate accordingly.
- BOOL bImplode - If this parameter is set to TRUE, then all data fields will be imploded before the fact is asserted.
Notes and Caveats on using the interface
There are several details to keep in mind to use the CLIPS/ODBC interface properly :- All ODBC administrivia must be in working order.
(i.e. Drivers must be installed, ODBC data source must be registered etc.) - All deleted records are skipped.
- If a record contains a data field with a NULL value, that field will NOT be included in the assert statement.
- A deftemplate containing all of the desired columns has to be loaded before the call to
CCLIPSWRAP::CLIPSODBC()
in order to 'catch' the rows returned from query.
The solution was implemented by creating a new class, CVarRecordSet as an extension of CRecordset. Two functions were overridden to obtain the column names and actual data returned from a given query.
PreBindFields()
- This function is called by theCRecordset::Open()
function after the query has executed but before any data is fetched. By calling::SQLDescribeCol()
the column names are captured and stored for use as slot names in a deftemplate.Move()
-CRecordset::Move()
is called initially to handle cursor movement, EOF/BOF processing etc.::SQLGetData()
is then invoked and the actual data is stored in CString format.
CCLIPSWRAP::CLIPSAssert()
is invoked and the corresponding rules fire.Information regarding CVarRecordset was obtained from book Visual C++ 4, Masterclass (chapter 7 by Ken Ramirez) available from Wrox Press.
Note : The code provided provided in this book is compatible with VC++ 4.0. There is a version for VC++ 4.2/5.0 available in the CLIPS Wrapper Class
Last modified : 9-Aug-2003
Michael Giordano