TF EXPFUNC p_db_connect(ENGid eid) { HDBC hdbc; char szDataSource[80]; char szUserID[40]; char szPassword[40]; if (pVAR != lsGetParmType(eid, 1)) { lsErrRaise(eid, "db_connect instantiation error: arg 1 must be var"); return FALSE; } g_lsRC = lsGetParm(eid, 2, cSTR, szDataSource); if (g_lsRC != OK) goto LS_ERROR; g_lsRC = lsGetParm(eid, 3, cSTR, szUserID); if (g_lsRC != OK) goto LS_ERROR; g_lsRC = lsGetParm(eid, 4, cSTR, szPassword); if (g_lsRC != OK) goto LS_ERROR; g_RC = SQLAllocConnect(g_hEnv, &hdbc); /* set up error handler */ g_hDBC = hdbc; g_hSTMT = SQL_NULL_HSTMT; g_eid = eid; ERROR_CHK("SQLAllocConnect"); // Added this line to get around erroneous messages: // S1010:[Microsoft][ODBC SQL Server Driver]Function sequence error // Fix from MS Knowledge Article 179226 // SQL_PRESERVE_CURSORS - note new name in actual call // This only applies to SQL Server, so hopefully, nothing bad will // happen if it fails. g_RC = SQLSetConnectOption(hdbc, SQL_COPT_SS_PRESERVE_CURSORS, NULL); g_RC = SQLConnect(hdbc, (UCHAR*)szDataSource, SQL_NTS, (UCHAR*)szUserID, SQL_NTS, (UCHAR*)szPassword, SQL_NTS); ERROR_CHK("SQLConnect"); lsUnifyParm(eid, 1, cADDR, &hdbc); return TRUE; LS_ERROR: return(lserror()); ODBC_ERROR: return FALSE; }
// module/1 version AMZIFUNC cv_module_1 ( ENGid Engine ) { register int i ; char module_name [ 256 ] ; // Check if help file is loaded if ( ! check_help_loaded ( Engine ) ) return ( FALSE ) ; // Get module name lsGetParm ( Engine, 1, cSTR, module_name ) ; for ( i = 0 ; i < Help -> GetModuleCount ( ) ; i ++ ) { Module * module = Help -> GetModule ( i ) ; // Module has been found : print it if ( ! stricmp ( module_name, module -> Name ) ) print_module_entries ( Engine, i, module ) ; delete module ; } return ( TRUE ) ; }
TF EXPFUNC p_db_disconnect(ENGid eid) { HDBC hdbc; g_lsRC = lsGetParm(eid, 1, cADDR, &hdbc); if (g_lsRC != OK) goto LS_ERROR; /* set up error handler */ g_hDBC = hdbc; g_hSTMT = SQL_NULL_HSTMT; g_eid = eid; g_RC = SQLDisconnect(hdbc); ERROR_CHK("SQLDisconnect"); g_RC = SQLFreeConnect(hdbc); ERROR_CHK("SQLFreeConnect"); errchk("lsODBC"); return TRUE; LS_ERROR: return(lserror()); ODBC_ERROR: return FALSE; }
/* * Class: amzi_ls_ARulesLogicServer * Method: GetStrParm * Signature: (I)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_amzi_ls_ARulesLogicServer_GetStrParm (JNIEnv * jenv, jobject jobj, jint iarg) { RC rc; aCHAR* str; GET_EID(e); int len = lsStrParmLen(e, (int)iarg); if (len < 0) // jmg len = 0; // jmg str = new aCHAR[len+1]; rc = lsGetParm(e, (int)iarg, cWSTR, str); if (rc != OK) // jmg *str = 0; // jmg jCHAR *jcstr = new jCHAR[len+1]; ac_to_jc(jcstr, str, len); jstring jstr = jenv->NewString(jcstr, len); delete[] str; delete[] jcstr; if (rc != OK) amzi_error(jenv, e, "GetStrParm"); return jstr; }
TF EXPFUNC p_db_freeq(ENGid eid) { QUERY* pq; g_lsRC = lsGetParm(eid, 1, cADDR, &pq); if (g_lsRC != OK) goto LS_ERROR; query_del(pq); return TRUE; LS_ERROR: return(lserror()); }
/* * Class: amzi_ls_ARulesLogicServer * Method: GetFloatParm * Signature: (I)D */ JNIEXPORT jdouble JNICALL Java_amzi_ls_ARulesLogicServer_GetFloatParm (JNIEnv * jenv, jobject jobj, jint iarg) { RC rc; double d; GET_EID(e); rc = lsGetParm(e, (int)iarg, cDOUBLE, &d); if (rc != OK) amzi_error(jenv, e, "GetFloatParm"); return (jdouble)d; }
/* * Class: amzi_ls_ARulesLogicServer * Method: GetIntParm * Signature: (I)I */ JNIEXPORT jint JNICALL Java_amzi_ls_ARulesLogicServer_GetIntParm (JNIEnv * jenv, jobject jobj, jint iarg) { RC rc; int i; GET_EID(e); rc = lsGetParm(e, (int)iarg, cINT, &i); if (rc != OK) amzi_error(jenv, e, "GetIntParm"); return (jint)i; }
/* * Class: amzi_ls_ARulesLogicServer * Method: GetParm * Signature: (I)J */ JNIEXPORT jlong JNICALL Java_amzi_ls_ARulesLogicServer_GetParm (JNIEnv * jenv, jobject jobj, jint iarg) { TERM t; RC rc; GET_EID(e); rc = lsGetParm(e, (int)iarg, cTERM, &t); if (rc != OK) amzi_error(jenv, e, "GetParm"); return (jlong)(ajptr)t; }
AMZIFUNC cv_string_file ( ENGid Engine ) { char * String ; characters FileName [ 1024 ] ; int Length ; register FILE * fp ; // Retrieve the filename lsGetParm ( Engine, 2, cSTR, FileName ) ; // Open the file (in binary mode, otherwise file length will be greater // than bytes read, since CRLF are converted) if ( ( fp = fopen( FileName, "rb" ) ) == NULL ) return( FALSE ) ; Length = FileLength( fp ) ; String = ( char * ) malloc( Length + 1 ) ; // Check if we are running out of memory if ( String == NULL ) { fclose ( fp ) ; return ( FALSE ) ; } // Read the file then unify the result fread( String, 1, Length, fp ) ; fclose( fp ) ; String [ Length ] = '\0' ; lsUnifyParm ( Engine, 1, cSTR, String ) ; free( String ) ; return( TRUE ) ; }
AMZIFUNC cv_usage_1 ( ENGid Engine ) { register int i, j, k ; char predicate_name [ 256 ] ; char predicate_name_arity [ 256 ] ; TERM FA ; register int found = 0 ; // Check if help file is loaded if ( ! check_help_loaded ( Engine ) ) return ( FALSE ) ; // We can specify : // help(term). // help('term/arity') // help(term/arity) // In the latter case, we receive a term instead of an atom, so we must // convert it to a string if ( lsGetParm ( Engine, 1, cSTR, predicate_name ) != OK ) { lsGetParm ( Engine, 1, cTERM, & FA ) ; lsTermToStr ( Engine, FA, predicate_name_arity, sizeof ( predicate_name_arity ) ) ; } else * predicate_name_arity = 0 ; // Cycle through the modules for ( i = 0 ; i < Help -> GetModuleCount ( ) ; i ++ ) { Module * module = Help -> GetModule ( i ) ; // Cycle through the predicates for ( j = 0 ; j < Help -> GetPredicateCount ( i ) ; j ++ ) { char buffer [ 256 ] ; Predicate * pred = Help -> GetPredicate ( i, j ) ; // Make sure that it will succeed even if an arity is // provided sprintf ( buffer, "%s/%d", pred -> Name, pred -> Arity ) ; if ( ! strnicmp ( predicate_name, pred -> Name, strlen ( predicate_name ) ) || ! strnicmp ( predicate_name, buffer, strlen ( predicate_name ) ) || ! strnicmp ( predicate_name_arity, buffer, strlen ( predicate_name ) ) || strstr ( pred -> Name, predicate_name ) != NULL ) { char usage [ 256 ] ; // May need a linefeed between this predicate and the preceding one if ( found ) prolog_printf ( Engine, "\n" ) ; found = 1 ; // Build the usage string sprintf ( usage, "Usage : %s( ", pred -> Name ) ; for ( k = 0 ; pred -> Arguments [k]. Name != NULL ; k ++ ) { strcat ( usage, pred -> Arguments [k]. Name ) ; if ( pred -> Arguments [k+1]. Name != NULL ) strcat ( usage, ", " ) ; } strcat ( usage, " )." ) ; // print the usage string and short description prolog_printf ( Engine, "%s\n\t%s\n", usage, pred -> ShortDescription ) ; } delete pred ; } delete module ; } return ( TRUE ) ; }
AMZIFUNC cv_help_1 ( ENGid Engine ) { register int i, j ; Argument * ap ; char predicate_name [ 256 ] ; char predicate_name_arity [ 256 ] ; TERM FA ; // Check if help file is loaded if ( ! check_help_loaded ( Engine ) ) return ( FALSE ) ; // We can specify : // help(term). // help('term/arity') // help(term/arity) // In the latter case, we receive a term instead of an atom, so we must // convert it to a string if ( lsGetParm ( Engine, 1, cSTR, predicate_name ) != OK ) { lsGetParm ( Engine, 1, cTERM, & FA ) ; lsTermToStr ( Engine, FA, predicate_name_arity, sizeof ( predicate_name_arity ) ) ; } else * predicate_name_arity = 0 ; // Cycle through all modules for ( i = 0 ; i < Help -> GetModuleCount ( ) ; i ++ ) { Module * module = Help -> GetModule ( i ) ; // Cycle through each module predicates for ( j = 0 ; j < Help -> GetPredicateCount ( i ) ; j ++ ) { char buffer [ 256 ] ; Predicate * pred = Help -> GetPredicate ( i, j ) ; // Make sure that it will succeed even if an arity is // provided sprintf ( buffer, "%s/%d", pred -> Name, pred -> Arity ) ; // Found ? if ( ! stricmp ( predicate_name, pred -> Name ) || ! stricmp ( predicate_name, buffer ) || ! stricmp ( predicate_name_arity, buffer ) ) { // Yes : print the name prolog_printf ( Engine, "Name : %s/%d\n", pred -> Name, pred -> Arity ) ; // Print the module name prolog_printf ( Engine, "Module : %s (%s)\n", module -> Name, module -> Description ) ; // Print the usage string prolog_printf ( Engine, "Usage : %s\n", pred -> ShortDescription ) ; // Print Succeeds when and Fails when if ( pred -> SucceedsWhen != NULL ) prolog_printf ( Engine, "Succeeds when : %s\n", pred -> SucceedsWhen ) ; if ( pred -> FailsWhen != NULL ) prolog_printf ( Engine, "Fails when : %s\n", pred -> FailsWhen ) ; // Print long description if ( pred -> LongDescription != NULL ) prolog_printf ( Engine, "Description :\n\t%s\n", pred -> LongDescription ) ; // Argument header if ( pred -> Arguments [0]. Name != NULL ) prolog_printf ( Engine, "Arguments :\n" ) ; // Print argument list for ( ap = pred -> Arguments ; ap -> Name != NULL ; ap ++ ) { prolog_printf ( Engine, "\tArgument %d : %s (access = %s)\n", ap -> Position, ap -> Name, ap -> Access ) ; prolog_printf ( Engine, "\t\t%s\n", ap -> Description ) ; } prolog_printf ( Engine, "\n" ) ; } delete pred ; } delete module ; } return ( TRUE ) ; }
AMZIFUNC cv_where_1 ( ENGid Engine ) { register int i, j ; char predicate_name [ 256 ] ; char predicate_name_arity [ 256 ] ; TERM FA ; register int found = 0 ; // Check if help file is loaded if ( ! check_help_loaded ( Engine ) ) return ( FALSE ) ; // We can specify : // help(term). // help('term/arity') // help(term/arity) // In the latter case, we receive a term instead of an atom, so we must // convert it to a string if ( lsGetParm ( Engine, 1, cSTR, predicate_name ) != OK ) { lsGetParm ( Engine, 1, cTERM, & FA ) ; lsTermToStr ( Engine, FA, predicate_name_arity, sizeof ( predicate_name_arity ) ) ; } else * predicate_name_arity = 0 ; // Cycle through the modules for ( i = 0 ; i < Help -> GetModuleCount ( ) ; i ++ ) { Module * module = Help -> GetModule ( i ) ; // Cycle through the predicates for ( j = 0 ; j < Help -> GetPredicateCount ( i ) ; j ++ ) { char buffer [ 256 ] ; Predicate * pred = Help -> GetPredicate ( i, j ) ; // Make sure that it will succeed even if an arity is // provided sprintf ( buffer, "%s/%d", pred -> Name, pred -> Arity ) ; if ( ! strnicmp ( predicate_name, pred -> Name, strlen ( predicate_name ) ) || ! strnicmp ( predicate_name, buffer, strlen ( predicate_name ) ) || ! strnicmp ( predicate_name_arity, buffer, strlen ( predicate_name ) ) || strstr ( pred -> Name, predicate_name ) != NULL ) { char buffer [ 256 ] ; // Print the header before the first found predicate if ( ! found ) prolog_printf ( Engine, "predicate '%s' can be found in the following module(s) : \n", predicate_name ) ; found = 1 ; // Format functor/arity to a certain width sprintf ( buffer, "%s/%d", pred -> Name, pred -> Arity ) ; // prolog_printf ( Engine, "\t%-20s %-20s %s\n", buffer, module -> Name, module -> Description ) ; } delete pred ; } delete module ; } return ( TRUE ) ; }
TF EXPFUNC p_db_fetch(ENGid eid) { QUERY* pq; HSTMT hstmt; TERM tcols; TERM t; COL* pCol; #ifdef _DEBUG TERM xt1, xt2; char xbuf1[512], xbuf2[512]; #endif if (pVAR != lsGetParmType(eid, 2)) { lsErrRaise(eid, "db_fetch instantiation error: arg 2 must be var"); return FALSE; } g_lsRC = lsGetParm(eid, 1, cADDR, &pq); if (g_lsRC != OK) goto LS_ERROR; hstmt = pq->hstmt; /* set up error handler */ g_hSTMT = hstmt; g_eid = eid; if ( SQL_NO_DATA_FOUND == (g_RC = SQLFetch(hstmt)) ) { DUMPLOG("no data found"); query_del(pq); return FALSE; } ERROR_CHK("SQLFetch"); DUMPLOG("found data"); g_lsRC = lsMakeList(eid, &tcols); if (g_lsRC != OK) goto LS_ERROR; for (pCol = pq->clist; pCol != NULL; pCol = pCol->next) { switch(pCol->pdtype) { case pdATOM: g_lsRC = lsMakeAtom(eid, &t, pCol->s); if (g_lsRC != OK) goto LS_ERROR; break; case pdSTR: g_lsRC = lsMakeStr(eid, &t, pCol->s); if (g_lsRC != OK) goto LS_ERROR; break; case pdINT: g_lsRC = lsMakeInt(eid, &t, pCol->i); if (g_lsRC != OK) goto LS_ERROR; break; case pdFLOAT: g_lsRC = lsMakeFloat(eid, &t, pCol->f); if (g_lsRC != OK) goto LS_ERROR; break; case pdDOUBLE: g_lsRC = lsMakeFloat(eid, &t, pCol->g); if (g_lsRC != OK) goto LS_ERROR; break; case pdDATE: g_lsRC = lsMakeFA(eid, &t, "date", 3); if (g_lsRC != OK) goto LS_ERROR; lsUnifyArg(eid, &t, 1, cSHORT, &(pCol->d->year)); lsUnifyArg(eid, &t, 2, cSHORT, &(pCol->d->month)); lsUnifyArg(eid, &t, 3, cSHORT, &(pCol->d->day)); break; case pdTIME: g_lsRC = lsMakeFA(eid, &t, "time", 3); if (g_lsRC != OK) goto LS_ERROR; lsUnifyArg(eid, &t, 1, cSHORT, &(pCol->t->hour)); lsUnifyArg(eid, &t, 2, cSHORT, &(pCol->t->minute)); lsUnifyArg(eid, &t, 3, cSHORT, &(pCol->t->second)); break; default: messout("Unsupported Prolog type for ODBC fetch"); } g_lsRC = lsPushList(eid, &tcols, t); if (g_lsRC != OK) return(lserror()); } /* #ifdef _DEBUG lsGetParm(eid, 2, cTERM, &xt1); lsTermToStrQ(eid, xt1, xbuf1, 500); lsTermToStrQ(eid, xt2, xbuf2, 500); #endif */ lsUnifyParm(eid, 2, cTERM, &tcols); return TRUE; LS_ERROR: return(lserror()); ODBC_ERROR: return FALSE; }
TF EXPFUNC p_db_query(ENGid eid) /* db_query/5 ex. db_query(HConnect, PQuery, $select mother, father from person where name = ?$, ['Someones Name'], [string, string]). */ { HSTMT hstmt; HDBC hdbc; char* sQ = NULL; int iParm = 1; int iCol = 1; TERM tParms, tCols; char* sBuf = NULL; char sType[80]; TERM t; pTYPE ptyp; QUERY* pq; char* s; long len; long* pl; float* pf; double* pd; long i; //float f; double g; DATE_STRUCT* pdate; TIME_STRUCT* ptime; char sMsg[512]; #ifdef _DEBUG char xbuf1[512], xbuf2[512]; #endif if (pVAR != lsGetParmType(eid, 2)) { lsErrRaise(eid, "db_query instantiation error: arg 2 must be var"); return FALSE; } /* create stmt structure */ pq = query_new(); /* add stmt handle to structure */ g_lsRC = lsGetParm(eid, 1, cADDR, &hdbc); if (g_lsRC != OK) goto LS_ERROR; /* #ifdef _DEBUG xtest(hdbc); #endif */ g_RC = SQLAllocStmt(hdbc, &hstmt); /* set up error handler */ g_hDBC = hdbc; g_hSTMT = hstmt; g_eid = eid; ERROR_CHK("SQLAllocStmt"); pq->hstmt = hstmt; /* get query string from Prolog */ sQ = (char*)malloc(lsStrParmLen(eid, 3)+1); g_lsRC = lsGetParm(eid, 3, cSTR, sQ); if (g_lsRC != OK) goto LS_ERROR; /* prepare SQL query */ //g_RC = SQLPrepare(hstmt, (UCHAR*)sQ, SQL_NTS); //errchk("lsODBC"); /* get parameter list from Prolog */ g_lsRC = lsGetParm(eid, 4, cTERM, &tParms); if (g_lsRC != OK) goto LS_ERROR; #ifdef _DEBUG lsTermToStrQ(eid, tParms, xbuf1, 500); #endif /* walk input list */ while (OK == lsPopList(eid, &tParms, cTERM, &t)) { /* bind parameter for each */ ptyp = lsGetTermType(eid, t); switch(ptyp) { case pATOM: case pSTR: sBuf = (char*)malloc(lsStrTermLen(eid, t) + 1); g_lsRC = lsGetTerm(eid, t, cSTR, sBuf); if (g_lsRC != OK) goto LS_ERROR; len = strlen(sBuf) + 1; s = query_addstrparm(pq, sBuf); g_ParmSize = SQL_NTS; g_RC = SQLBindParameter(hstmt, (UWORD)iParm, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, (UDWORD)len, 0, s, (SDWORD)len, &g_ParmSize); ERROR_CHK("SQLBindParameter"); break; case pINT: g_lsRC = lsGetTerm(eid, t, cLONG, &i); if (g_lsRC != OK) goto LS_ERROR; pl = query_addintparm(pq, i); g_ParmSize = 0; g_RC = SQLBindParameter(hstmt, (UWORD)iParm, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 4, 0, pl, 4, &g_ParmSize); ERROR_CHK("SQLBindParameter"); break; case pFLOAT: g_lsRC = lsGetTerm(eid, t, cDOUBLE, &g); if (g_lsRC != OK) goto LS_ERROR; pd = query_adddoubleparm(pq, g); g_ParmSize = 0; g_RC = SQLBindParameter(hstmt, (UWORD)iParm, SQL_PARAM_INPUT, SQL_C_DOUBLE, SQL_DOUBLE, 8, 0, pd, 8, &g_ParmSize); ERROR_CHK("SQLBindParameter"); break; default: messout("Unsupported Prolog type for ODBC input"); } iParm++; } /* get column list from Prolog */ g_lsRC = lsGetParm(eid, 5, cTERM, &tCols); if (g_lsRC != OK) goto LS_ERROR; #ifdef _DEBUG lsTermToStrQ(eid, tCols, xbuf2, 500); #endif /* walk output list */ while (OK == lsPopList(eid, &tCols, cSTR, sType)) { /* bind col for each */ switch(sType[0]) { case 'a': len = 1 + atol(&sType[1]); s = query_addstrcol(pq, pdATOM, len); g_ColSize = SQL_NO_TOTAL; g_RC = SQLBindCol(hstmt, (UWORD)iCol, SQL_C_CHAR, s, len, &g_ColSize); ERROR_CHK("SQLBindCol"); break; case 's': len = 1 + atol(&sType[1]); s = query_addstrcol(pq, pdSTR, len); g_ColSize = SQL_NO_TOTAL; g_RC = SQLBindCol(hstmt, (UWORD)iCol, SQL_C_CHAR, s, len, &g_ColSize); ERROR_CHK("SQLBindCol"); break; case 'i': pl = query_addintcol(pq); g_ColSize = 0; g_RC = SQLBindCol(hstmt, (UWORD)iCol, SQL_C_SLONG, pl, 4, &g_ColSize); ERROR_CHK("SQLBindCol"); break; case 'f': pf = query_addfloatcol(pq); g_ColSize = 0; g_RC = SQLBindCol(hstmt, (UWORD)iCol, SQL_C_FLOAT, pf, 4, &g_ColSize); ERROR_CHK("SQLBindCol"); break; case 'g': pd = query_adddoublecol(pq); g_ColSize = 0; g_RC = SQLBindCol(hstmt, (UWORD)iCol, SQL_C_DOUBLE, pd, 8, &g_ColSize); ERROR_CHK("SQLBindCol"); break; case 'd': pdate = query_adddatecol(pq); g_ColSize = 0; // ignored for date g_RC = SQLBindCol(hstmt, (UWORD)iCol, SQL_C_DATE, pdate, 6, &g_ColSize); ERROR_CHK("SQLBindCol"); break; case 't': ptime = query_addtimecol(pq); g_RC = SQLBindCol(hstmt, (UWORD)iCol, SQL_C_TIME, ptime, 6, &g_ColSize); ERROR_CHK("SQLBindCol"); break; default: sprintf(sMsg, "Unsupported Prolog type '%c' for ODBC output", sType[0]); messout(sMsg); } iCol++; } /* execute query and return pointer to query */ //g_RC = SQLExecute(hstmt); //errchk("lsODBC"); g_RC = SQLExecDirect(hstmt, (UCHAR*)sQ, SQL_NTS); ERROR_CHK("SQLExecDirect"); lsUnifyParm(eid, 2, cADDR, &pq); if (sQ != NULL) free(sQ); if (sBuf != NULL) free(sBuf); return TRUE; LS_ERROR: if (sQ != NULL) free(sQ); if (sBuf != NULL) free(sBuf); return(lserror()); ODBC_ERROR: if (sQ != NULL) free(sQ); if (sBuf != NULL) free(sBuf); return FALSE; }