void bgl_odbc_sql_end_tran(obj_t handletype, SQLHANDLE handle, obj_t completiontype) { SQLRETURN v; SQLSMALLINT ht; SQLSMALLINT ct; if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(handletype)), "environment")) { ht = SQL_HANDLE_ENV; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(handletype)), "connection")) { ht = SQL_HANDLE_DBC; } else { odbc_error("bgl_odbc_sql_end_tran", "Invalid handle type", handletype); } if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(completiontype)), "commit")) { ct = SQL_COMMIT; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(handletype)), "rollback")) { ct = SQL_ROLLBACK; } else { odbc_error("bgl_odbc_sql_end_tran", "Invalid completion type", completiontype); } v = SQLEndTran(ht, handle, ct); if(!SQL_SUCCEEDED(v)) { report_odbc_error("bgl_odbc_sql_end_tran", ht, handle); } }
void bgl_odbc_free_statement(SQLHANDLE stmt, obj_t option) { SQLRETURN v; SQLSMALLINT opt; if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(option)), "close")) { opt = SQL_CLOSE; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(option)), "unbind")) { opt = SQL_UNBIND; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(option)), "reset-params")) { opt = SQL_RESET_PARAMS; } else { odbc_error("bgl_odbc_free_statement", "invalid option", option); } v = SQLFreeStmt(stmt,opt); if(!SQL_SUCCEEDED(v)) { report_odbc_error("bgl_odbc_free_statement", SQL_HANDLE_STMT, stmt); } }
static void report_odbc_error(char* who, SQLSMALLINT handle_type, SQLHANDLE handle) { SQLTCHAR SQLErrorMessage[512]; SQLTCHAR SQLState[6]; SQLINTEGER NativeError = 0; SQLSMALLINT ErrMsgLength = 0; SQLGetDiagRec(handle_type, handle, 1, SQLState, &NativeError, SQLErrorMessage, sizeof(SQLErrorMessage), &ErrMsgLength); odbc_error(who,SQLErrorMessage,string_to_bstring(SQLState)); }
int odbc_command(ClipMachine* cm,SQLSTMT* s,ClipVar* ap){ ODBC_STMT* stmt = (ODBC_STMT*)s; SQLRETURN er; SDWORD count; odbc_bindpars(stmt,ap); if((er = SQLAllocStmt(stmt->conn->conn,&stmt->hstmt))) goto err; if((er = SQLPrepare(stmt->hstmt,stmt->sql,strlen(stmt->sql)))) goto err; if((er = SQLExecute(stmt->hstmt))) goto err; if((er = SQLRowCount(stmt->hstmt,&count))) goto err; if(count < 0) count = 0; return count; err: return odbc_error(cm,stmt->conn->loc,0,stmt->conn->conn,stmt->hstmt,__LINE__,er_execute); }
int odbc_refresh(ClipMachine* cm,SQLROWSET* rs,SQLSTMT* s,ClipVar* ap){ ODBC_STMT* stmt = (ODBC_STMT*)s; SQLRETURN er; ODBC_ROWSET* rowset = (ODBC_ROWSET*)rs; SQLSMALLINT cols; int i; void** rec; odbc_bindpars(stmt,ap); if((er = SQLAllocStmt(stmt->conn->conn,&stmt->hstmt))) goto err; if((er = SQLPrepare(stmt->hstmt,stmt->sql,strlen(stmt->sql)))) goto err; if((er = SQLExecute(stmt->hstmt))) goto err; if((er = SQLNumResultCols(stmt->hstmt,&cols))) goto err; rec = malloc(sizeof(void*)*rowset->nfields); for(i=0;i<rowset->nfields;i++){ rec[i] = malloc(rowset->fields[i].buflen+4+_TERM_ZERO); if((er = SQLBindCol(stmt->hstmt,i+1,SQL_DEFAULT, rec[i]+4,rowset->fields[i].buflen+_TERM_ZERO,rec[i]))) goto err; } er = SQLFetch(stmt->hstmt); if(er != SQL_NO_DATA){ for(i=0;i<rowset->nfields;i++) free(rowset->data[rowset->recno-1][i]); free(rowset->data[rowset->recno-1]); rowset->data[rowset->recno-1] = rec; } else { for(i=0;i<rowset->nfields;i++) free(rec[i]); free(rec); odbc_delete((SQLROWSET*)rowset); rowset->lastrec--; if(rowset->recno > rowset->lastrec) rowset->recno = rowset->lastrec; } return 0; err: return odbc_error(cm,stmt->conn->loc,0,stmt->conn->conn,stmt->hstmt,__LINE__,er_refresh); }
int odbc_createconn(ClipMachine * cm) { int dbl,userl,passwdl; char* db = _clip_parcl(cm,2,&dbl); char* user = _clip_parcl(cm,3,&userl); char* passwd = _clip_parcl(cm,4,&passwdl); ODBC_CONN *conn = NULL; SQLHDBC tmpconn; SQLRETURN er; SQLHENV henv; char dbms[30]; if(!user) userl = 0; if(!passwd) passwdl = 0; if((er = SQLAllocEnv(&henv))) goto err; if((er = SQLAllocConnect(henv,&tmpconn))) goto err; if((er = SQLConnect(tmpconn,db,dbl,user,userl,passwd,passwdl))) goto err; /* silly PostgreSQL's ODBC driver */ if((er = SQLGetInfo(tmpconn,SQL_DBMS_NAME,dbms,sizeof(dbms),0))) goto err; conn = calloc(sizeof(ODBC_CONN),1); conn->conn = tmpconn; conn->vtbl = &vtbl; conn->henv = henv; if(strcmp(dbms,"PostgreSQL")==0) conn->postgres = 1; return _clip_store_c_item(cm, (void *) conn, _C_ITEM_TYPE_SQL, destroy_odbc_conn); err: odbc_error(cm,(conn?conn->loc:0),henv,tmpconn,0,__LINE__,er_connect); if(conn) free(conn); return -1; }
int main(int argc, char** argv) { char buffer[BUFSIZE]; char result[BUFSIZE]; char *dsn = getenv("DSN"); char *uid = getenv("UID"); char *pwd = getenv("PWD"); if (SQL_SUCCESS != (rc = SQLAllocEnv (&henv))) { odbc_error (); return -1; } if (SQL_SUCCESS != (rc = SQLAllocConnect (henv, &hdbc))) { odbc_error (); return -1; } if (SQL_SUCCESS != (rc = SQLConnect (hdbc, dsn ? dsn : "1111", SQL_NTS, uid ? uid : "dba", SQL_NTS, pwd ? pwd : "dba", SQL_NTS))) { odbc_error (); return -1; } if (SQL_SUCCESS != (rc = SQLAllocStmt (hdbc, &hstmt))) { odbc_error (); return -1; } while (NULL != fgets (buffer, BUFSIZE, stdin)) { if (SQL_SUCCESS != (rc = SQLBindParameter (hstmt, 3, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_CHAR, sizeof (result), 0, result, sizeof (result), NULL))) { odbc_error (); break; } if (0 == strncmp ("chdir", buffer, 5)) { if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 1, SQL_C_CHAR, SQL_CHAR, 0, 0, replace_newline (buffer + 6), NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 2, SQL_C_CHAR, SQL_CHAR, 0, 0, "", NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLExecDirect (hstmt, "XSLT_CHDIR (?, ?, ?)", SQL_NTS))) { odbc_error (); break; } /*change directory:*/ printf ("%s\n", result); fflush (stdout); } else if (0 == strncmp ("stylesheet", buffer, 10)) { /*load stylesheet: */ if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 1, SQL_C_CHAR, SQL_CHAR, 0, 0, replace_newline (&buffer[11]), NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 2, SQL_C_CHAR, SQL_CHAR, 0, 0, "", NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLExecDirect (hstmt, "XSLT_STYLESHEET (?, ?, ?)", SQL_NTS))) { odbc_error (); break; } printf ("%s\n", result); fflush (stdout); } else if (0 == strncmp ("input", buffer, 5)) { if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 1, SQL_C_CHAR, SQL_CHAR, 0, 0, replace_newline (&buffer[6]), NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 2, SQL_C_CHAR, SQL_CHAR, 0, 0, "", NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLExecDirect (hstmt, "XSLT_INPUT (?, ?, ?)", SQL_NTS))) { odbc_error (); break; } printf ("%s\n", result); fflush (stdout); } else if (0 == strncmp ("transform", buffer, 9)) { short iter = 1; char * c = buffer+10; char *filename; timer_account_t ta; /* get output filename */ while (*c != '\n' && *c != '\0' && *c != ' ') { c++; } if (*c == ' ') { *c = 0; c++; filename = buffer + 10; } /* get # of iterations */ iter = atoi(c); if (iter <= 0) iter = 1; /* printf ("file: %s iter : %d buffer %s\n", filename, iter, buffer); */ ta_init (&ta, "test"); ta_enter (&ta); if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 1, SQL_C_CHAR, SQL_CHAR, 0, 0, filename, NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLSetParam (hstmt, 2, SQL_C_SHORT, SQL_INTEGER, 0, 0, &iter, NULL))) { odbc_error (); break; } if (SQL_SUCCESS != (rc = SQLExecDirect (hstmt, "XSLT_TRANSFORM (?, ?, ?)", SQL_NTS))) { odbc_error (); break; } ta_leave (&ta); printf ("OK wallclock: %06ld ms; cpuclock: %06ld'\n", ta.ta_total, ta.ta_total); fflush (stdout); } else if (0 == strncmp ("terminate", buffer, 9)) { printf ("OK\n"); break; } if (SQL_SUCCESS != (rc = SQLFreeStmt (hstmt, SQL_CLOSE))) { odbc_error (); return -1; } if (SQL_SUCCESS != (rc = SQLFreeStmt (hstmt, SQL_RESET_PARAMS))) { odbc_error (); return -1; } } if (SQL_SUCCESS != (rc = SQLFreeStmt (hstmt, SQL_DROP))) return -1; if (SQL_SUCCESS != (rc = SQLDisconnect (hdbc))) return -1; if (SQL_SUCCESS != (rc = SQLFreeConnect (hdbc))) return -1; if (SQL_SUCCESS != (rc = SQLFreeEnv (henv))) return -1; return 0; }
int odbc_createrowset(ClipMachine* cm,SQLROWSET* rs,ClipVar* ap,ClipVar* idname,const char* gen_idSQL){ ODBC_ROWSET* rowset = (ODBC_ROWSET*)rs; ODBC_STMT* stmt = rowset->stmt; SQLRETURN er; SQLSMALLINT cols; SQLINTEGER nullable; int i; odbc_bindpars(stmt,ap); if((er = SQLAllocStmt(stmt->conn->conn,&stmt->hstmt))) goto err; if((er = SQLPrepare(stmt->hstmt,stmt->sql,strlen(stmt->sql)))) goto err; rowset->rowset_item = _clip_store_c_item(cm,rowset,_C_ITEM_TYPE_SQL,destroy_odbc_rowset); if((er = SQLExecute(stmt->hstmt))) goto err; if((er = SQLNumResultCols(stmt->hstmt,&cols))) goto err; rowset->nfields = cols; rowset->fields = calloc(rowset->nfields,sizeof(SQLFIELD)); rowset->id = -1; for(i=0;i<rowset->nfields;i++){ if((er = SQLColAttributes(stmt->hstmt,i+1, SQL_COLUMN_NAME,rowset->fields[i].name,MAXFIELDNAME+1,0,0))) goto err; if((er = SQLColAttributes(stmt->hstmt,i+1, SQL_COLUMN_TYPE,0,0,0,(SQLPOINTER)&rowset->fields[i].type))) goto err; rowset->fields[i].ctype[0] = _odbc_ctype(rowset->fields[i].type); if((er = SQLColAttributes(stmt->hstmt,i+1, SQL_COLUMN_LENGTH,0,0,0,(SQLPOINTER)&rowset->fields[i].buflen))) goto err; if((er = SQLColAttributes(stmt->hstmt,i+1, SQL_COLUMN_DISPLAY_SIZE,0,0,0,(SQLPOINTER)&rowset->fields[i].len))) goto err; if((er = SQLColAttributes(stmt->hstmt,i+1, SQL_COLUMN_SCALE,0,0,0,(SQLPOINTER)&rowset->fields[i].dec))) goto err; if((er = SQLColAttributes(stmt->hstmt,i+1, SQL_COLUMN_UNSIGNED,0,0,0,(SQLPOINTER)&rowset->fields[i].unsign))) goto err; if((er = SQLColAttributes(stmt->hstmt,i+1, SQL_COLUMN_NULLABLE,0,0,0,(SQLPOINTER)&nullable))) goto err; rowset->fields[i].notnull = (nullable == SQL_NO_NULLS); /* silly PostgreSQL's ODBC driver */ if(stmt->conn->postgres && (rowset->fields[i].type == SQL_NUMERIC || rowset->fields[i].type == SQL_DECIMAL)){ rowset->fields[i].dec = LOWORD(rowset->fields[i].len); rowset->fields[i].len = HIWORD(rowset->fields[i].len); rowset->fields[i].buflen = 8; } if(rowset->fields[i].type == SQL_LONGVARBINARY) rowset->fields[i].buflen *= 2; if(idname->t.type == CHARACTER_t){ if(idname->s.str.buf && !strcasecmp(rowset->fields[i].name,idname->s.str.buf)){ rowset->id = i; rowset->nids = 1; } } else if(idname->t.type == ARRAY_t){ int j; for(j=0;j<idname->a.count;j++){ ClipVar* vp = idname->a.items+j; if(vp->t.type == CHARACTER_t && vp->s.str.buf && !strcasecmp(rowset->fields[i].name,vp->s.str.buf)){ rowset->nids++; rowset->ids = realloc(rowset->ids,rowset->nids*sizeof(int)); rowset->ids[rowset->nids-1] = i; } } } if(rowset->fields[i].type == SQL_BINARY || rowset->fields[i].type == SQL_VARBINARY || rowset->fields[i].type == SQL_LONGVARBINARY) rowset->fields[i].binary = 1; } rowset->lastrec = 0; rowset->data = malloc(sizeof(void*)*rowset->lastrec); rowset->unknownrows = 1; return 0; err: odbc_error(cm,stmt->conn->loc,0,stmt->conn->conn,stmt->hstmt,__LINE__,er_createrowset); return 1; }
void bgl_odbc_sql_set_env_attr(SQLHENV env, obj_t attribute, obj_t value) { SQLRETURN v; SQLUINTEGER uintval = 0; SQLPOINTER valueptr = 0; SQLINTEGER stringlength = 0; SQLINTEGER attr = 0; if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "connection-pooling")) { attr = SQL_ATTR_CONNECTION_POOLING; if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(value)), "off")) { uintval = SQL_CP_OFF; valueptr = (SQLPOINTER)uintval; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(value)), "one-per-driver")) { uintval = SQL_CP_ONE_PER_DRIVER; valueptr = (SQLPOINTER)uintval; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(value)), "one-per-environment")) { uintval = SQL_CP_ONE_PER_HENV; valueptr = (SQLPOINTER)uintval; } else { odbc_error("bgl_odbc_sql_set_env_attr", "Invalid attribute value", MAKE_PAIR(attribute, value)); } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "match")) { attr = SQL_ATTR_CP_MATCH; if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(value)), "strict")) { uintval = SQL_CP_STRICT_MATCH; valueptr = (SQLPOINTER)uintval; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(value)), "relaxed")) { uintval = SQL_CP_ONE_PER_DRIVER; valueptr = (SQLPOINTER)uintval; } else { odbc_error("bgl_odbc_sql_set_env_attr", "Invalid attribute value", MAKE_PAIR(attribute, value)); } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "odbc-version")) { attr = SQL_ATTR_ODBC_VERSION; if(INTEGERP(value)) { uintval = (SQLUINTEGER)CINT(value); valueptr = (SQLPOINTER)uintval; } else { odbc_error("bgl_odbc_sql_set_env_attr", "Invalid attribute value", MAKE_PAIR(attribute, value)); } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "output-nts")) { attr = SQL_ATTR_OUTPUT_NTS; if(TRUEP(value)) { uintval = SQL_TRUE; valueptr = (SQLPOINTER)uintval; } else { uintval = SQL_FALSE; valueptr = (SQLPOINTER)uintval; } } else { odbc_error("bgl_odbc_sql_set_env_attr", "Invalid or Unsupported attribute ", attribute); } v = SQLSetEnvAttr(env, attr, valueptr, stringlength); if(!SQL_SUCCEEDED(v)) { report_odbc_error("bgl_odbc_sql_set_env_attr", SQL_HANDLE_ENV, env); } }
void bgl_odbc_sql_set_connect_attr(SQLHANDLE dbc, obj_t attribute, obj_t value) { SQLRETURN v; SQLUINTEGER uintval = 0; SQLPOINTER valueptr = 0; SQLINTEGER stringlength = 0; SQLINTEGER attr = 0; if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "access-mode")) { attr = SQL_ATTR_ACCESS_MODE; if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "read-only")) { uintval = SQL_MODE_READ_ONLY; valueptr = (SQLPOINTER)uintval; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "read-write")) { uintval = SQL_MODE_READ_WRITE; valueptr = (SQLPOINTER)uintval; } else { odbc_error("bgl_odbc_sql_set_connect_attr", "Invalid attribute value", MAKE_PAIR(attribute, value)); } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "asynch-enable")) { attr = SQL_ATTR_ASYNC_ENABLE; if(TRUEP( value )) { uintval = SQL_ASYNC_ENABLE_ON; valueptr = (SQLPOINTER)uintval; } else { uintval = SQL_ASYNC_ENABLE_OFF; valueptr = (SQLPOINTER)uintval; } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "auto-ipd")) { attr = SQL_ATTR_AUTO_IPD; if(TRUEP( value )) { uintval = SQL_TRUE; valueptr = (SQLPOINTER)uintval; } else { uintval = SQL_FALSE; valueptr = (SQLPOINTER)uintval; } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "autocommit")) { attr = SQL_ATTR_AUTOCOMMIT; if(TRUEP( value )) { uintval = SQL_AUTOCOMMIT_OFF; valueptr = (SQLPOINTER)uintval; } else { uintval = SQL_AUTOCOMMIT_ON; valueptr = (SQLPOINTER)uintval; } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "connection-timeout")) { attr = SQL_ATTR_CONNECTION_TIMEOUT; if(INTEGERP( value )) { uintval = CINT(value); valueptr = (SQLPOINTER)uintval; } else { odbc_error("bgl_odbc_sql_set_connect_attr", "Invalid attribute value", MAKE_PAIR(attribute, value)); } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "login-timeout")) { attr = SQL_ATTR_LOGIN_TIMEOUT; if(INTEGERP( value )) { uintval = CINT(value); valueptr = (SQLPOINTER)uintval; } else { odbc_error("bgl_odbc_sql_set_connect_attr", "Invalid attribute value", MAKE_PAIR(attribute, value)); } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "current-catalog")) { attr = SQL_ATTR_CURRENT_CATALOG; if(STRINGP( value )) { valueptr = (SQLPOINTER)BSTRING_TO_STRING(value); stringlength = strlen(BSTRING_TO_STRING(value)); } else { odbc_error("bgl_odbc_sql_set_connect_attr", "Invalid attribute value", MAKE_PAIR(attribute, value)); } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "metadata-id")) { attr = SQL_ATTR_METADATA_ID; if(TRUEP( value )) { uintval = SQL_TRUE; valueptr = (SQLPOINTER)uintval; } else { uintval = SQL_FALSE; valueptr = (SQLPOINTER)uintval; } } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "odbc-cursor")) { attr = SQL_ATTR_ODBC_CURSORS; if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "use-if-needed")) { uintval = SQL_CUR_USE_IF_NEEDED; valueptr = (SQLPOINTER)uintval; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "use-odbc")) { uintval = SQL_CUR_USE_ODBC; valueptr = (SQLPOINTER)uintval; } else if(0 == strcmp(BSTRING_TO_STRING(SYMBOL_TO_STRING(attribute)), "use-driver")) { uintval = SQL_CUR_USE_DRIVER; valueptr = (SQLPOINTER)uintval; } else { odbc_error("bgl_odbc_sql_set_connect_attr", "Invalid attribute value", MAKE_PAIR(attribute, value)); } } else { odbc_error("bgl_odbc_sql_set_connect_attr", "Invalid Or Unsupported attribute", MAKE_PAIR(attribute, value)); } v = SQLSetConnectAttr(dbc, attr, valueptr, stringlength); if(!SQL_SUCCEEDED(v)) { report_odbc_error("bgl_odbc_sql_set_connect_attr", SQL_HANDLE_DBC, dbc); } }