Merge branch 'master' of yap.dcc.fc.up.pt:yap-6

This commit is contained in:
Vítor Santos Costa 2010-05-23 18:29:49 +01:00
commit 79649439ee
3 changed files with 330 additions and 221 deletions

View File

@ -39,119 +39,157 @@ STATIC_PROTO(Int c_db_odbc_row_cut,(void));
STATIC_PROTO(Int c_db_odbc_get_fields_properties,(void)); STATIC_PROTO(Int c_db_odbc_get_fields_properties,(void));
STATIC_PROTO(Int c_db_odbc_number_of_fields_in_query,(void)); STATIC_PROTO(Int c_db_odbc_number_of_fields_in_query,(void));
static int
odbc_error(SQLSMALLINT type, SQLHANDLE hdbc, char *msg, char *print)
{
SQLCHAR SqlState[6], Msg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER NativeError;
SQLSMALLINT i=1, MsgLen;
#define SQLALLOCHANDLE(A,B,C,print) \ SQLGetDiagRec(type, hdbc,i,SqlState,&NativeError,Msg, sizeof(Msg), &MsgLen);
{ \ fprintf(stderr,"%% error in SQLConnect: %s got error code %s\n%% SQL Message: %s\n", print, SqlState, Msg);
SQLRETURN retcode; \ return FALSE;
retcode = SQLAllocHandle(A,B,C); \
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \
{ \
printf("erro no SQLAllocHandle(ENV) %s\n",print); \
return FALSE; \
} \
} }
#define SQLSETENVATTR(A,B,C,D,print) \ static int
{ \ SQLALLOCHANDLE(SQLSMALLINT HandleType, SQLHANDLE hdbc, SQLHANDLE *outHandle, char *print)
SQLRETURN retcode; \ {
retcode = SQLSetEnvAttr(A,B,C,D); \ SQLRETURN retcode;
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \
{ \ retcode = SQLAllocHandle(HandleType,hdbc,outHandle);
printf("erro no SQLSetEnvAttr %s\n",print); \ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return FALSE; \ {
} \ return odbc_error(HandleType, hdbc, "SQLAllocHandle(ENV)", print);
}
return TRUE;
} }
#define SQLCONNECT(A,B,C,D,E,F,G,print) \ static int
{ \ SQLSETENVATTR(SQLHENV henv, SQLINTEGER att, SQLPOINTER p, SQLINTEGER len, char *print)
SQLRETURN retcode; \ {
retcode = SQLConnect(A,B,C,D,E,F,G); \ SQLRETURN retcode;
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \
{ \ retcode = SQLSetEnvAttr(henv,att,p,len);
printf("erro no SQLConnect %s\n",print); \ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return FALSE; \ {
} \ return odbc_error(SQL_HANDLE_ENV, henv, "SQLSetEnvAttr", print);
}
return TRUE;
} }
#define SQLEXECDIRECT(A,B,C,print) \ static int SQLCONNECT(SQLHDBC hdbc,
{ \ SQLCHAR *driver,
SQLRETURN retcode; \ SQLCHAR *user,
retcode = SQLExecDirect(A,B,C); \ SQLCHAR *password,
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ char *print)
{ \ {
printf("erro no SQLExecDirect %s \n",print); \ SQLRETURN retcode;
return FALSE; \
} \ retcode = SQLConnect(hdbc,driver,SQL_NTS,user,SQL_NTS,password,SQL_NTS);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return odbc_error(SQL_HANDLE_DBC, hdbc, "SQLConnect", print);
return TRUE;
} }
#define SQLDESCRIBECOL(A,B,C,D,E,F,G,H,I,print) \ static int SQLEXECDIRECT(SQLHSTMT StatementHandle,
{ \ SQLCHAR * StatementText,
SQLRETURN retcode; \ char *print)
retcode = SQLDescribeCol(A,B,C,D,E,F,G,H,I); \ {
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ SQLRETURN retcode;
{ \ retcode = SQLExecDirect(StatementHandle,StatementText,SQL_NTS);
printf("erro no SQLDescribeCol %s\n",print); \
return FALSE; \ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
} \ return odbc_error(SQL_HANDLE_STMT, StatementHandle, "SQLExecDirect", print);
return TRUE;
} }
#define SQLSETCONNECTATTR(A,B,C,D,print) \ static int SQLDESCRIBECOL(SQLHSTMT sth,
{ \ SQLSMALLINT colno,
SQLRETURN retcode; \ SQLCHAR * colname,
retcode = SQLSetConnectAttr(A,B,C,D); \ SQLSMALLINT bflength,
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ SQLSMALLINT * nmlengthp,
{ \ SQLSMALLINT * dtptr,
printf("erro no SQLSetConnectAttr %s\n",print); \ SQLULEN * colszptr,
return FALSE; \ SQLSMALLINT * ddptr,
} \ SQLSMALLINT * nullableptr,
char * print)
{
SQLRETURN retcode;
retcode = SQLDescribeCol(sth, colno, colname, bflength,
nmlengthp, dtptr, colszptr, ddptr,
nullableptr);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return odbc_error(SQL_HANDLE_STMT, sth, "SQLDescribeCol", print);
return TRUE;
} }
#define SQLBINDCOL(A,B,C,D,E,F,print) \ static int SQLSETCONNECTATTR(SQLHDBC hdbc,
{ \ SQLINTEGER attr,
SQLRETURN retcode; \ SQLPOINTER vptr,
retcode = SQLBindCol(A,B,C,D,E,F); \ SQLINTEGER slen,
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ char * print)
{ \ {
printf("erro no SQLbindCol %s\n",print); \ SQLRETURN retcode;
return FALSE; \ retcode = SQLSetConnectAttr(hdbc, attr, vptr, slen);
} \
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return odbc_error(SQL_HANDLE_STMT, hdbc, "SQLSetConnectAttr", print);
return TRUE;
} }
#define SQLFREESTMT(A,B,print) \ static int SQLBINDCOL(SQLHSTMT sthandle,
{ \ SQLUSMALLINT colno,
SQLRETURN retcode; \ SQLSMALLINT tt,
retcode = SQLFreeStmt(A,B); \ SQLPOINTER tvptr,
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ SQLLEN blen,
{ \ SQLLEN * strl,
printf("erro no SQLFreeStmt %s\n",print); \ char * print)
return FALSE; \ {
} \ SQLRETURN retcode;
retcode = SQLBindCol(sthandle,colno,tt,tvptr,blen,strl);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return odbc_error(SQL_HANDLE_STMT, sthandle, "SQLBindCol", print);
return TRUE;
} }
#define SQLNUMRESULTCOLS(A,B,print) \ static int SQLFREESTMT(SQLHSTMT sthandle,
{ \ SQLUSMALLINT opt,
SQLRETURN retcode; \ char * print)
retcode = SQLNumResultCols(A,B); \ {
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ SQLRETURN retcode;
{ \ retcode = SQLFreeStmt(sthandle,opt);
printf("erro no SQLNumResultCols %s\n",print); \
return FALSE; \ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
} \ return odbc_error(SQL_HANDLE_STMT, sthandle, "SQLFreeStmt", print);
return TRUE;
}
static int SQLNUMRESULTCOLS(SQLHSTMT sthandle,
SQLSMALLINT * ncols,
char * print)
{
SQLRETURN retcode;
retcode = SQLNumResultCols(sthandle,ncols);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return odbc_error(SQL_HANDLE_STMT, sthandle, "SQLNumResultCols", print);
return TRUE;
}
static int SQLCLOSECURSOR(SQLHSTMT sthandle,
char * print)
{
SQLRETURN retcode;
retcode = SQLCloseCursor(sthandle);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return odbc_error(SQL_HANDLE_STMT, sthandle, "SQLCloseCursor", print);
return TRUE;
} }
#define SQLCLOSECURSOR(A,print) \
{ \
SQLRETURN retcode; \
retcode = SQLCloseCursor(A); \
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \
{ \
printf("erro no SQLCloseCursor %s\n",print); \
return FALSE; \
} \
}
/* no db_odbc_row não é utilizada esta macro*/
#define SQLFETCH(A,print) \ #define SQLFETCH(A,print) \
{ \ { \
SQLRETURN retcode; \ SQLRETURN retcode; \
@ -160,77 +198,111 @@ STATIC_PROTO(Int c_db_odbc_number_of_fields_in_query,(void));
break; \ break; \
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \
{ \ { \
printf("erro no SQLFETCH %s\n",print); \ printf("Error in SQLFETCH: %s\n",print); \
return FALSE; \ return FALSE; \
} \ } \
} }
#define SQLGETDATA(A,B,C,D,E,F,print) \ static int SQLGETDATA(SQLHSTMT sthandle,
{ \ SQLUSMALLINT Col_or_Param_Num,
SQLRETURN retcode; \ SQLSMALLINT TargetType,
retcode = SQLGetData(A,B,C,D,E,F); \ SQLPOINTER TargetValuePtr,
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ SQLLEN BufferLength,
{ \ SQLLEN * StrLen_or_IndPtr,
printf("erro no SQLgetdata %s\n",print); \ char * print)
return FALSE; \ {
} \ SQLRETURN retcode;
retcode = SQLGetData(sthandle, Col_or_Param_Num,
TargetType, TargetValuePtr,
BufferLength, StrLen_or_IndPtr);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return odbc_error(SQL_HANDLE_STMT, sthandle, "SQLGetData", print);
return TRUE;
} }
#define SQLDISCONNECT(A,print) \ static int SQLDISCONNECT(SQLHSTMT sthandle,
{ \ char * print)
SQLRETURN retcode; \ {
retcode = SQLDisconnect(A); \ SQLRETURN retcode;
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ retcode = SQLDisconnect(sthandle);
{ \
printf("erro no SQLDisconnect %s\n",print); \ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return FALSE; \ return odbc_error(SQL_HANDLE_DBC, sthandle, "SQLDisconnect", print);
} \ return TRUE;
} }
#define SQLFREEHANDLE(A,B,print) \ static int SQLFREEHANDLE(SQLSMALLINT HandleType,
{ \ SQLHANDLE Handle,
SQLRETURN retcode; \ char * print)
retcode = SQLFreeHandle(A,B); \ {
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ SQLRETURN retcode;
{ \ retcode = SQLFreeHandle(HandleType, Handle);
printf("erro no SQLFreeHandle %s\n",print); \
return FALSE; \ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
} \ return odbc_error(HandleType, Handle, "SQLDisconnect", print);
return TRUE;
} }
#define SQLPRIMARYKEYS(A,B,C,D,E,F,G,print) \ static int SQLPRIMARYKEYS(SQLHSTMT StatementHandle,
{ \ SQLCHAR * CatalogName,
SQLRETURN retcode; \ SQLSMALLINT NameLength1,
retcode = SQLPrimaryKeys(A,B,C,D,E,F,G); \ SQLCHAR * SchemaName,
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ SQLSMALLINT NameLength2,
{ \ SQLCHAR * TableName,
printf("erro no SQLPrimaryKeys %s\n",print); \ SQLSMALLINT NameLength3,
return FALSE; \ char * print)
} \ {
SQLRETURN retcode;
retcode = SQLPrimaryKeys(StatementHandle,
CatalogName, NameLength1,
SchemaName, NameLength2,
TableName, NameLength3
);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return odbc_error(SQL_HANDLE_STMT, StatementHandle, "SQLPrimaryKeys", print);
return TRUE;
} }
#define SQLGETTYPEINFO(A,B,print) \ /********************************************
{ \ NOT IN USE
SQLRETURN retcode; \ static int SQLGETTYPEINFO(SQLHSTMT StatementHandle,
retcode = SQLGetTypeInfo(A,B); \ SQLSMALLINT DataType,
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \ char * print)
{ \ {
printf("erro no SQLGetTypeInfo %s\n",print); \ SQLRETURN retcode;
return FALSE; \ retcode = SQLGetTypeInfo(StatementHandle, DataType);
} \
}
#define SQLCOLATTRIBUTE(A,B,C,D,E,F,G,print) \ if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
{ \ return odbc_error(SQL_HANDLE_STMT, StatementHandle, "SQLGetTypeInfo", print);
SQLRETURN retcode; \ return TRUE;
retcode = SQLColAttribute(A,B,C,D,E,F,G); \
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO) \
{ \
printf("erro no SQLColAttribute %s\n",print); \
return FALSE; \
} \
} }
********************************************/
static int SQLCOLATTRIBUTE( SQLHSTMT StatementHandle,
SQLUSMALLINT ColumnNumber,
SQLUSMALLINT FieldIdentifier,
SQLPOINTER CharacterAttributePtr,
SQLSMALLINT BufferLength,
SQLSMALLINT * StringLengthPtr,
SQLLEN * NumericAttributePtr,
char * print)
{
SQLRETURN retcode;
retcode = SQLColAttribute(StatementHandle,
ColumnNumber,
FieldIdentifier,
CharacterAttributePtr,
BufferLength,
StringLengthPtr,
NumericAttributePtr
);
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
return odbc_error(SQL_HANDLE_STMT, StatementHandle, "SQLColAttribute", print);
return TRUE;
}
/* Verificar tipo de dados*/ /* Verificar tipo de dados*/
@ -265,19 +337,23 @@ c_db_odbc_connect(void) {
SQLHDBC hdbc; SQLHDBC hdbc;
/*Allocate environment handle */ /*Allocate environment handle */
SQLALLOCHANDLE(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv, "connect"); if (!SQLALLOCHANDLE(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv, "connect"))
return FALSE;
/* Set the ODBC version environment attribute */ /* Set the ODBC version environment attribute */
SQLSETENVATTR(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0, "connect"); if (!SQLSETENVATTR(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0, "connect"))
return FALSE;
/* Allocate connection handle */ /* Allocate connection handle */
SQLALLOCHANDLE(SQL_HANDLE_DBC, henv, &hdbc, "connect"); if (!SQLALLOCHANDLE(SQL_HANDLE_DBC, henv, &hdbc, "connect"))
return FALSE;
/* Set login timeout to 6 seconds. */ /* Set login timeout to 6 seconds. */
SQLSETCONNECTATTR(hdbc, SQL_LOGIN_TIMEOUT,(SQLPOINTER) 6, 0, "connect"); if (!SQLSETCONNECTATTR(hdbc, SQL_LOGIN_TIMEOUT,(SQLPOINTER) 6, 0, "connect"))
return FALSE;
/* Connect to data source */ /* Connect to data source */
SQLCONNECT(hdbc, if (!SQLCONNECT(hdbc,
(SQLCHAR*) driver, SQL_NTS, (SQLCHAR*) driver,
(SQLCHAR*) user, SQL_NTS, (SQLCHAR*) user,
(SQLCHAR*) passwd, SQL_NTS, "connect"); (SQLCHAR*) passwd, "connect"))
return FALSE;
if (!Yap_unify(arg_conn, MkIntegerTerm((Int)(hdbc)))) if (!Yap_unify(arg_conn, MkIntegerTerm((Int)(hdbc))))
return FALSE; return FALSE;
else else
@ -286,7 +362,7 @@ c_db_odbc_connect(void) {
//new = add_connection(&TOP,hdbc,henv); //new = add_connection(&TOP,hdbc,henv);
new = myddas_util_add_connection(hdbc,henv); new = myddas_util_add_connection(hdbc,henv);
if (new == NULL){ if (new == NULL){
printf("Erro ao alocar memoria para lista\n"); fprintf(stderr,"Error: could not allocate list memory\n");
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
@ -302,21 +378,22 @@ c_db_odbc_query(void) {
Term arg_bind_list = Deref(ARG4); Term arg_bind_list = Deref(ARG4);
Term arg_conn = Deref(ARG5); Term arg_conn = Deref(ARG5);
SQLCHAR *sql = AtomName(AtomOfTerm(arg_sql_query)); SQLCHAR *sql = (SQLCHAR *)AtomName(AtomOfTerm(arg_sql_query));
SQLHDBC hdbc =(SQLHDBC) (IntegerOfTerm(arg_conn)); SQLHDBC hdbc =(SQLHDBC) (IntegerOfTerm(arg_conn));
SQLHSTMT hstmt; SQLHSTMT hstmt;
SQLSMALLINT type; SQLSMALLINT type;
/*Allocate an handle for the query*/
SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt, "db_query");
/* Executes the query*/
SQLEXECDIRECT(hstmt,sql,SQL_NTS, "db_query");
Int arity; Int arity;
Int i; Int i;
/*Allocate an handle for the query*/
if (!SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt, "db_query"))
return FALSE;
/* Executes the query*/
if (!SQLEXECDIRECT(hstmt, sql, "db_query"))
return FALSE;
if (IsNonVarTerm(arg_arity)){ if (IsNonVarTerm(arg_arity)){
arity = IntegerOfTerm(arg_arity); arity = IntegerOfTerm(arg_arity);
@ -330,20 +407,22 @@ c_db_odbc_query(void) {
Term head,list=arg_bind_list; Term head,list=arg_bind_list;
SQLUINTEGER ColumnSizePtr; SQLULEN ColumnSizePtr;
SQLINTEGER *data_info=NULL; SQLLEN *data_info=NULL;
for (i=1;i<=arity;i++) for (i=1;i<=arity;i++)
{ {
head = HeadOfTerm(list); head = HeadOfTerm(list);
list = TailOfTerm(list); list = TailOfTerm(list);
SQLDESCRIBECOL(hstmt,i,NULL,0,NULL,&type,&ColumnSizePtr,NULL,NULL,"db_query"); if (!SQLDESCRIBECOL(hstmt,i,NULL,0,NULL,&type,&ColumnSizePtr,NULL,NULL,"db_query"))
return FALSE;
/* +1 because of '\0' */ /* +1 because of '\0' */
bind_space = malloc(sizeof(char)*(ColumnSizePtr+1)); bind_space = malloc(sizeof(char)*(ColumnSizePtr+1));
data_info = malloc(sizeof(SQLINTEGER)); data_info = malloc(sizeof(SQLINTEGER));
SQLBINDCOL(hstmt,i,SQL_C_CHAR,bind_space,(ColumnSizePtr+1),data_info,"db_query"); if (!SQLBINDCOL(hstmt,i,SQL_C_CHAR,bind_space,(ColumnSizePtr+1),data_info,"db_query"))
return FALSE;
properties[0] = MkIntegerTerm((Int)bind_space); properties[0] = MkIntegerTerm((Int)bind_space);
properties[2] = MkIntegerTerm((Int)data_info); properties[2] = MkIntegerTerm((Int)data_info);
@ -363,8 +442,10 @@ c_db_odbc_query(void) {
if (!Yap_unify(arg_result_set, MkIntegerTerm((Int) hstmt))) if (!Yap_unify(arg_result_set, MkIntegerTerm((Int) hstmt)))
{ {
SQLCLOSECURSOR(hstmt,"db_query"); if (!SQLCLOSECURSOR(hstmt,"db_query"))
SQLFREESTMT(hstmt,SQL_CLOSE,"db_query"); return FALSE;
if (!SQLFREESTMT(hstmt,SQL_CLOSE,"db_query"))
return FALSE;
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
@ -387,8 +468,10 @@ c_db_odbc_number_of_fields(void) {
sprintf(sql,"DESCRIBE %s",relation); sprintf(sql,"DESCRIBE %s",relation);
SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt, "db_number_of_fields"); if (!SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt, "db_number_of_fields"))
SQLEXECDIRECT(hstmt,sql,SQL_NTS, "db_number_of_fields"); return FALSE;
if (!SQLEXECDIRECT(hstmt, (SQLCHAR *)sql, "db_number_of_fields"))
return FALSE;
/* Calcula o numero de campos*/ /* Calcula o numero de campos*/
number_fields=0; number_fields=0;
@ -397,8 +480,10 @@ c_db_odbc_number_of_fields(void) {
number_fields++; number_fields++;
} }
SQLCLOSECURSOR(hstmt,"db_number_of_fields"); if (!SQLCLOSECURSOR(hstmt,"db_number_of_fields"))
SQLFREESTMT(hstmt,SQL_CLOSE,"db_number_of_fields"); return FALSE;
if (!SQLFREESTMT(hstmt,SQL_CLOSE,"db_number_of_fields"))
return FALSE;
if (!Yap_unify(arg_fields, MkIntegerTerm(number_fields))) if (!Yap_unify(arg_fields, MkIntegerTerm(number_fields)))
return FALSE; return FALSE;
@ -423,8 +508,10 @@ c_db_odbc_get_attributes_types(void) {
sprintf(sql,"DESCRIBE %s",relation); sprintf(sql,"DESCRIBE %s",relation);
SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt, "db_get_attributes_types"); if (!SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt, "db_get_attributes_types"))
SQLEXECDIRECT(hstmt,sql,SQL_NTS, "db_get_attributes_types"); return FALSE;
if (!SQLEXECDIRECT(hstmt, (SQLCHAR *)sql, "db_get_attributes_types"))
return FALSE;
while (TRUE) while (TRUE)
{ {
@ -433,7 +520,8 @@ c_db_odbc_get_attributes_types(void) {
/* Tentar fazer de uma maneira que a gente consiga calcular o tamanho que o /* Tentar fazer de uma maneira que a gente consiga calcular o tamanho que o
nome do campo vai ocupar, assim podemos alocar memoria dinamicamente*/ nome do campo vai ocupar, assim podemos alocar memoria dinamicamente*/
sql[0]='\0'; sql[0]='\0';
SQLGETDATA(hstmt, 1, SQL_C_CHAR, sql, 256, NULL, "db_get_attributes_types"); if (!SQLGETDATA(hstmt, 1, SQL_C_CHAR, sql, 256, NULL, "db_get_attributes_types"))
return FALSE;
head = HeadOfTerm(list); head = HeadOfTerm(list);
Yap_unify(head, MkAtomTerm(Yap_LookupAtom(sql))); Yap_unify(head, MkAtomTerm(Yap_LookupAtom(sql)));
@ -442,7 +530,8 @@ c_db_odbc_get_attributes_types(void) {
list = TailOfTerm(list); list = TailOfTerm(list);
sql[0]='\0'; sql[0]='\0';
SQLGETDATA(hstmt, 2, SQL_C_CHAR, sql, 256, NULL, "db_get_attributes_types"); if (!SQLGETDATA(hstmt, 2, SQL_C_CHAR, sql, 256, NULL, "db_get_attributes_types"))
return FALSE;
if (strncmp(sql, "smallint",8) == 0 || strncmp(sql,"int",3) == 0 || if (strncmp(sql, "smallint",8) == 0 || strncmp(sql,"int",3) == 0 ||
strncmp(sql, "mediumint",9) == 0 || strncmp(sql, "tinyint",7) == 0 || strncmp(sql, "mediumint",9) == 0 || strncmp(sql, "tinyint",7) == 0 ||
@ -456,8 +545,10 @@ c_db_odbc_get_attributes_types(void) {
Yap_unify(head, MkAtomTerm(Yap_LookupAtom("string"))); Yap_unify(head, MkAtomTerm(Yap_LookupAtom("string")));
} }
SQLCLOSECURSOR(hstmt,"db_get_attributes_types"); if (!SQLCLOSECURSOR(hstmt,"db_get_attributes_types"))
SQLFREESTMT(hstmt,SQL_CLOSE, "db_get_attributes_types"); return FALSE;
if (!SQLFREESTMT(hstmt,SQL_CLOSE, "db_get_attributes_types"))
return FALSE;
return TRUE; return TRUE;
} }
@ -474,9 +565,12 @@ c_db_odbc_disconnect(void) {
myddas_util_delete_connection(conn); myddas_util_delete_connection(conn);
/* More information about this process on /* More information about this process on
msdn.microsoft.com*/ msdn.microsoft.com*/
SQLDISCONNECT(conn,"db_disconnect"); if (!SQLDISCONNECT(conn,"db_disconnect"))
SQLFREEHANDLE(SQL_HANDLE_DBC,conn,"db_disconnect"); return FALSE;
SQLFREEHANDLE(SQL_HANDLE_ENV,henv,"db_disconnect"); if (!SQLFREEHANDLE(SQL_HANDLE_DBC,conn,"db_disconnect"))
return FALSE;
if (!SQLFREEHANDLE(SQL_HANDLE_ENV,henv,"db_disconnect"))
return FALSE;
return TRUE; return TRUE;
} }
@ -489,8 +583,10 @@ c_db_odbc_row_cut(void) {
SQLHSTMT hstmt = (SQLHSTMT) IntegerOfTerm(EXTRA_CBACK_CUT_ARG(Term,1)); SQLHSTMT hstmt = (SQLHSTMT) IntegerOfTerm(EXTRA_CBACK_CUT_ARG(Term,1));
SQLCLOSECURSOR(hstmt,"db_row_cut"); if (!SQLCLOSECURSOR(hstmt,"db_row_cut"))
SQLFREESTMT(hstmt,SQL_CLOSE,"db_row_cut"); return FALSE;
if (!SQLFREESTMT(hstmt,SQL_CLOSE,"db_row_cut"))
return FALSE;
return TRUE; return TRUE;
} }
@ -513,8 +609,10 @@ c_db_odbc_row(void) {
SQLRETURN retcode = SQLFetch(hstmt); SQLRETURN retcode = SQLFetch(hstmt);
if (retcode == SQL_NO_DATA) if (retcode == SQL_NO_DATA)
{ {
SQLCLOSECURSOR(hstmt,"db_row"); if (!SQLCLOSECURSOR(hstmt,"db_row"))
SQLFREESTMT(hstmt,SQL_CLOSE,"db_row"); return FALSE;
if (!SQLFREESTMT(hstmt,SQL_CLOSE,"db_row"))
return FALSE;
cut_fail(); cut_fail();
return FALSE; return FALSE;
@ -587,20 +685,25 @@ c_db_odbc_number_of_fields_in_query(void) {
SQLHSTMT hstmt; SQLHSTMT hstmt;
SQLSMALLINT number_cols=0; SQLSMALLINT number_cols=0;
SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt, if (!SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt,
"db_number_of_fields_in_query"); "db_number_of_fields_in_query"))
SQLEXECDIRECT(hstmt,sql,SQL_NTS, return FALSE;
"db_number_of_fields_in_query"); if (!SQLEXECDIRECT(hstmt ,(SQLCHAR *)sql,
"db_number_of_fields_in_query"))
return FALSE;
SQLNUMRESULTCOLS(hstmt,&number_cols, if (!SQLNUMRESULTCOLS(hstmt,&number_cols,
"db_number_of_fields_in_query"); "db_number_of_fields_in_query"))
return FALSE;
if (!Yap_unify(arg_fields, MkIntegerTerm(number_cols))){ if (!Yap_unify(arg_fields, MkIntegerTerm(number_cols))){
return FALSE; return FALSE;
} }
SQLCLOSECURSOR(hstmt,"db_number_of_fields_in_query"); if (!SQLCLOSECURSOR(hstmt,"db_number_of_fields_in_query"))
SQLFREESTMT(hstmt,SQL_CLOSE, "db_number_of_fields_in_query"); return FALSE;
if (!SQLFREESTMT(hstmt,SQL_CLOSE, "db_number_of_fields_in_query"))
return FALSE;
return TRUE; return TRUE;
} }
@ -612,7 +715,7 @@ c_db_odbc_get_fields_properties(void) {
Term fields_properties_list = Deref(ARG3); Term fields_properties_list = Deref(ARG3);
Term head, list; Term head, list;
char *relacao = AtomName(AtomOfTerm(nome_relacao)); SQLCHAR *relacao = (SQLCHAR *)AtomName(AtomOfTerm(nome_relacao));
char sql[256]; char sql[256];
char name[200]; char name[200];
Int i; Int i;
@ -620,7 +723,7 @@ c_db_odbc_get_fields_properties(void) {
SQLSMALLINT num_fields=0; SQLSMALLINT num_fields=0;
SQLSMALLINT NullablePtr=0; SQLSMALLINT NullablePtr=0;
SQLSMALLINT AutoIncrementPointer=0; SQLLEN AutoIncrementPointer=0;
SQLHSTMT hstmt,hstmt2; SQLHSTMT hstmt,hstmt2;
SQLHDBC hdbc =(SQLHDBC) (IntegerOfTerm(arg_conn)); SQLHDBC hdbc =(SQLHDBC) (IntegerOfTerm(arg_conn));
@ -630,28 +733,34 @@ c_db_odbc_get_fields_properties(void) {
sprintf (sql,"SELECT * FROM `%s` LIMIT 0",relacao); sprintf (sql,"SELECT * FROM `%s` LIMIT 0",relacao);
/*Allocate an handle for the query*/ /*Allocate an handle for the query*/
SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt, "db_get_fields_properties"); if (!SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt, "db_get_fields_properties"))
return FALSE;
/* Executes the query*/ /* Executes the query*/
SQLEXECDIRECT(hstmt,sql,SQL_NTS, "db_get_fields_properties"); if (!SQLEXECDIRECT(hstmt ,(SQLCHAR *)sql, "db_get_fields_properties"))
return FALSE;
Functor functor = Yap_MkFunctor(Yap_LookupAtom("property"),4); Functor functor = Yap_MkFunctor(Yap_LookupAtom("property"),4);
Term properties[4]; Term properties[4];
SQLNUMRESULTCOLS(hstmt,&num_fields, if (!SQLNUMRESULTCOLS(hstmt,&num_fields,
"db_get_fields_properties"); "db_get_fields_properties"))
return FALSE;
list = fields_properties_list; list = fields_properties_list;
SQLSMALLINT bind_prim_key; SQLSMALLINT bind_prim_key;
//por causa de as rows em odbc começam em 1 :) //por causa de as rows em odbc começam em 1 :)
Short *null=malloc(sizeof(Short)*(1+num_fields)); Short *null=(Short *)malloc(sizeof(Short)*(1+num_fields));
SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt2, "db_get_fields_properties"); if (!SQLALLOCHANDLE(SQL_HANDLE_STMT, hdbc, &hstmt2, "db_get_fields_properties"))
return FALSE;
/* Executes the query*/ /* Executes the query*/
SQLPRIMARYKEYS(hstmt2,NULL,0,NULL,0,relacao,SQL_NTS, "db_get_fields_properties"); if (!SQLPRIMARYKEYS(hstmt2,NULL,0,NULL,0,relacao,SQL_NTS, "db_get_fields_properties"))
return FALSE;
/* Associates bind value for the 5 column*/ /* Associates bind value for the 5 column*/
SQLBINDCOL(hstmt2,5,SQL_C_SSHORT,&bind_prim_key,sizeof(SQLSMALLINT),NULL, if (!SQLBINDCOL(hstmt2,5,SQL_C_SSHORT,&bind_prim_key,sizeof(SQLSMALLINT),NULL,
"db_get_fields_properties"); "db_get_fields_properties"))
return FALSE;
while(1) while(1)
{ {
@ -659,18 +768,21 @@ c_db_odbc_get_fields_properties(void) {
null[bind_prim_key]=1; null[bind_prim_key]=1;
} }
SQLCLOSECURSOR(hstmt2,"db_get_fields_properties"); if (!SQLCLOSECURSOR(hstmt2,"db_get_fields_properties"))
SQLFREESTMT(hstmt2,SQL_CLOSE,"db_get_fields_properties"); return FALSE;
if (!SQLFREESTMT(hstmt2,SQL_CLOSE,"db_get_fields_properties"))
return FALSE;
for (i=1;i<=num_fields;i++) for (i=1;i<=num_fields;i++)
{ {
head = HeadOfTerm(list); head = HeadOfTerm(list);
name[0]='\0'; name[0]='\0';
SQLDESCRIBECOL(hstmt,i,name,200,NULL,NULL,NULL,NULL,&NullablePtr, SQLDESCRIBECOL(hstmt,i,(SQLCHAR *)name,200,NULL,NULL,NULL,NULL,&NullablePtr,
"db_get_fields_properties"); "db_get_fields_properties");
SQLCOLATTRIBUTE(hstmt,i,SQL_DESC_AUTO_UNIQUE_VALUE,NULL,0,NULL,&AutoIncrementPointer, if (!SQLCOLATTRIBUTE(hstmt,i,SQL_DESC_AUTO_UNIQUE_VALUE,NULL,0,NULL,&AutoIncrementPointer,
"db_get_fields_properties"); "db_get_fields_properties"))
return FALSE;
properties[0] = MkAtomTerm(Yap_LookupAtom(name)); properties[0] = MkAtomTerm(Yap_LookupAtom(name));
@ -697,8 +809,10 @@ c_db_odbc_get_fields_properties(void) {
} }
} }
SQLCLOSECURSOR(hstmt,"db_get_fields_properties"); if (!SQLCLOSECURSOR(hstmt,"db_get_fields_properties"))
SQLFREESTMT(hstmt,SQL_CLOSE,"db_get_fields_properties"); return FALSE;
if (!SQLFREESTMT(hstmt,SQL_CLOSE,"db_get_fields_properties"))
return FALSE;
return TRUE; return TRUE;
} }

View File

@ -113,7 +113,7 @@ db_import(Connection,RelationName,PredName) :-
Assert =..[':-',P,','(M:'$copy_term_nv'(P,[],G,_), Assert =..[':-',P,','(M:'$copy_term_nv'(P,[],G,_),
','(M:translate(G,G,Code), ','(M:translate(G,G,Code),
','(M:queries_atom(Code,FinalSQL), ','(M:queries_atom(Code,FinalSQL),
','(M:c_db_odbc_query(FinalSQL,ResultSet,Arity,BindList,Connection), ','(M:c_db_odbc_query(FinalSQL,ResultSet,Arity,BindList,Con),
','(M:'$write_or_not'(FinalSQL), ','(M:'$write_or_not'(FinalSQL),
','(!,M:c_db_odbc_row(ResultSet,BindList,LA)))))))] ','(!,M:c_db_odbc_row(ResultSet,BindList,LA)))))))]
), ),

View File

@ -25,11 +25,6 @@
'$check_list_on_list'/2 '$check_list_on_list'/2
]). ]).
:- use_module(lists,[
is_list/1
]).
'$error_checks'(db_abolish(ModulePredName,Arity)):-!, '$error_checks'(db_abolish(ModulePredName,Arity)):-!,
(ModulePredName = Module:PredName -> (ModulePredName = Module:PredName ->
atom(Module), atom(Module),