/* System includes */ #include #include #include #include #include #include #define false 0 #define true 1 /* valid event types */ typedef enum { EVENT_NONE=0, /* zero is a not used */ EVENT_SHUTDOWN, EVENT_ERROR, EVENT_RESET, EVENT_PAUSE, EVENT_RESUME, EVENT_ALARM, EVENT_PIPE, EVENT_OTHER } event_t; sigset_t _allsigs; sigset_t _nosigs; int iUserCancelled = false; RETCODE db_odbc_test (); int db_odbc_get_lastautoincrement_id(HENV henv, HDBC hdbc, unsigned long *maxId); int db_odbc_exec_sql_stmt ( HENV henv, HDBC hdbc, char *stmtStr); void db_odbc_error(HENV henv, HDBC hdbc, HSTMT hstmt, char* stmt); void EventInit(void); int db_fetch_data(HENV henv, HDBC hdbc); int db_fetch_data_no_print(HENV henv, HDBC hdbc); static void SignalHandler(int iSigNo); int main(int argc, char **argv) { int iResult = -1; iResult = db_odbc_test(); if(iResult == -1) { printf("\n Error!\n"); } if(iResult == 0) { printf("\n Success\n"); } return 0; } RETCODE db_odbc_test () { HENV henv; SQLHDBC hdbc; unsigned long maxId; char stmt[200]; SQLCHAR outstr[1024]; SQLSMALLINT outstrlen; int i = 0; RETCODE rc; int iInsertOnce = true; EventInit(); system("date"); /* Register the Signal Handler for SIGPIPE */ rc = SQLAllocEnv (&henv); if ((rc == SQL_SUCCESS) || (rc == SQL_SUCCESS_WITH_INFO)) rc = SQLAllocConnect (henv, &hdbc); rc = SQLDriverConnect(hdbc, NULL, "DSN=mysqldb;", SQL_NTS, outstr, sizeof(outstr), &outstrlen, SQL_DRIVER_COMPLETE); if (!SQL_SUCCEEDED(rc)) { fprintf(stderr, "Failed to connect\n"); exit (1); } printf("Connected\n"); printf("Returned connection string was:\n\t%s\n", outstr); if (rc == SQL_SUCCESS_WITH_INFO) { printf("Driver reported the following diagnostics\n"); } if ( (rc == SQL_SUCCESS) || (rc == SQL_SUCCESS_WITH_INFO) ) { /* turn off auto-commit; must use SQLTransact() to commit DB mods */ rc = SQLSetConnectOption( hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF ); } else { db_odbc_error( henv, hdbc, SQL_NULL_HSTMT, "SQLConnect()" ); return (-1); } sprintf ( stmt , "USE test"); rc = db_odbc_exec_sql_stmt (henv, hdbc, stmt); if ( rc == -1) { printf ( "Error use database.\n"); return (-1); } sprintf ( stmt , "DROP TABLE if exists hpuxtest"); db_odbc_exec_sql_stmt (henv, hdbc, stmt); sprintf ( stmt , "CREATE TABLE hpuxtest (id int PRIMARY KEY AUTO_INCREMENT, name char(64) NOT NULL)"); rc = db_odbc_exec_sql_stmt (henv, hdbc, stmt); if ( rc == -1) { printf ( "Error creating table.\n"); return (-1); } sprintf ( stmt , "INSERT INTO hpuxtest (name) VALUES (\"Testing\")"); rc = db_odbc_exec_sql_stmt (henv, hdbc, stmt); if ( rc == -1) { printf ( "Error Inserting row.\n"); return (-1); } rc = db_odbc_get_lastautoincrement_id (henv, hdbc, &maxId); if ( rc == -1) { printf ( "Error Fetching last_insert_id .\n"); return (-1); } iUserCancelled = false; signal(SIGPIPE, SignalHandler); while(iUserCancelled == false) { if(iInsertOnce) { /* Delete all the rows */ sprintf ( stmt , "DELETE FROM hpuxtest"); rc = db_odbc_exec_sql_stmt (henv, hdbc, stmt); if ( rc == -1) { printf ( "Error Inserting row.\n"); return (-1); } for(i = 0; i < 10; i++) { sprintf ( stmt , "INSERT INTO hpuxtest (name) VALUES (\"Testing\")"); rc = db_odbc_exec_sql_stmt (henv, hdbc, stmt); if ( rc == -1) { printf ( "Error Inserting row.\n"); return (-1); } rc = db_odbc_get_lastautoincrement_id (henv, hdbc, &maxId); if ( rc == -1) { printf ( "Error Fetching last_insert_id .\n"); return (-1); } printf("\nMaxID = %ld", maxId); } iInsertOnce = false; } sleep(1); } printf("\n User has stopped the program...\n"); /* after the user presses ctrl+c */ if(iUserCancelled) { return db_fetch_data(henv, hdbc); } return (0); } int db_odbc_get_lastautoincrement_id(HENV henv, HDBC hdbc, unsigned long *maxId) { RETCODE rc; HSTMT hstmt; char stmtStr[100] = ""; SDWORD valMaxId; unsigned long InsertedId; /* Initialize maxId */ *maxId = 0; /* Build the SQL statement */ sprintf(stmtStr, "SELECT LAST_INSERT_ID()"); /* Allocate ODBC statement structure */ rc = SQLAllocStmt( hdbc, &hstmt ); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { db_odbc_error( henv, hdbc, hstmt, NULL ); return (rc); } /* Bind return value */ rc = SQLBindCol( hstmt, 1, SQL_C_LONG, &InsertedId, (SDWORD)sizeof(SDWORD), &valMaxId); /* Execute statement */ rc = SQLExecDirect( hstmt, stmtStr, SQL_NTS ); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { db_odbc_error (henv, hdbc, hstmt, stmtStr ); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (-1); } /* Get return value */ rc = SQLFetch( hstmt ); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) && (rc != SQL_NO_DATA_FOUND) ) { db_odbc_error( henv, hdbc, hstmt, NULL ); return ( rc ); } *maxId = InsertedId; printf("\n Last Inserted id: %ld", InsertedId); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (rc); } int db_odbc_exec_sql_stmt ( HENV henv, HDBC hdbc, char *stmtStr ) { RETCODE rc; HSTMT hstmt; /* Allocate a HSTMT to communicate with ODBC DB Driver. */ rc = SQLAllocStmt( hdbc, &hstmt ); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { db_odbc_error( henv, hdbc, hstmt, NULL ); return ( rc ); } /* Execute statement */ rc = SQLExecDirect( hstmt, stmtStr, SQL_NTS ); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { db_odbc_error( henv, hdbc, hstmt, stmtStr ); return( rc ); } return ( rc ); } void db_odbc_error(HENV henv, HDBC hdbc, HSTMT hstmt, char* stmt) { char sqlstate[10]; char errmsg[SQL_MAX_MESSAGE_LENGTH]; SQLINTEGER nativeerr; SWORD actualmsglen = 0; RETCODE rc; if ( stmt ) rc = SQLError(henv, hdbc, hstmt, sqlstate, &nativeerr, errmsg, SQL_MAX_MESSAGE_LENGTH - 1, &actualmsglen); if (rc == SQL_ERROR) { return; } if (rc != SQL_NO_DATA_FOUND) { errmsg[actualmsglen] = '\0'; } printf("[%s][%s]",sqlstate,errmsg); } static void SignalHandler(int iSigNo) { event_t event; signal(SIGPIPE, SignalHandler); switch(iSigNo) { case SIGALRM: event = EVENT_ALARM; printf("\n EVENT_ALARM \n"); break; case SIGHUP: event = EVENT_RESET; printf("\n EVENT_RESET \n"); break; case SIGINT: case SIGQUIT: case SIGKILL: /* can't catch this */ case SIGTERM: iUserCancelled = true; event = EVENT_SHUTDOWN; printf("\n EVENT_SHUTDOWN \n"); break; case SIGABRT: case SIGILL: case SIGFPE: case SIGBUS: case SIGSEGV: case SIGSYS: event = EVENT_ERROR; printf("\n EVENT_ERROR \n"); break; case SIGPIPE: /* handling sigpipe for mysql. but will not stop */ printf("\n In the SigPipe Event. SigPipe is being called."); system("date"); printf("\n"); break; case SIGSTOP: /* can't catch this */ case SIGTSTP: event = EVENT_PAUSE; printf("\n EVENT_PAUSE \n"); break; case SIGCONT: event = EVENT_RESUME; printf("\n EVENT_RESUME \n"); break; case SIGCHLD: printf("\n Received signal from child process (SIGCHLD)\n"); break; default: printf("\n Received unknown event (%d)\n", iSigNo); break; } } void EventInit(void) { int sig; struct sigaction act; sigfillset(&_allsigs); sigemptyset(&_nosigs); act.sa_handler = SignalHandler; act.sa_flags = 0; sigfillset(&act.sa_mask); /* Catch all valid signals */ for (sig = 1; sig < NSIG; sig++) { switch ( sig ) { case SIGKILL: /* can't catch this */ case SIGSTOP: /* can't catch this */ break; case SIGALRM: case SIGHUP: case SIGINT: case SIGQUIT: case SIGABRT: case SIGTERM: case SIGILL: case SIGFPE: case SIGBUS: case SIGSEGV: case SIGSYS: case SIGTSTP: case SIGCONT: case SIGCHLD: if ( sigaction(sig, &act, NULL) == -1 ) { printf("sigaction(%d) failed\n", sig); } break; case SIGPIPE: if ( sigaction(sig, &act, NULL) == -1 ) { printf("sigaction(%d) failed\n", sig); } break; } } } /* Fetch the data and publish on the screen */ int db_fetch_data(HENV henv, HDBC hdbc) { RETCODE rc; HSTMT hstmt; char stmtStr[1000] = ""; SQLCHAR name[1024]; SDWORD valMaxIdSz; unsigned long InsertedId; void *recepId[1]; SWORD cbName=0; SWORD type=0; UDWORD size=0; DWORD input[3]={0,0,0}; SQLLEN input_len; int i; printf("Fetch Data called\n"); /* Build the SQL statement */ sprintf(stmtStr, "SELECT id, name FROM hpuxtest WHERE id = 1"); /* Allocate ODBC statement structure */ rc = SQLAllocStmt( hdbc, &hstmt ); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { db_odbc_error( henv, hdbc, hstmt, NULL ); return (-1); } rc=SQLPrepare(hstmt,stmtStr,SQL_NTS); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { printf("Error in Execution of Direct SQL Smttm\n"); db_odbc_error (henv, hdbc, hstmt, stmtStr ); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (-1); } for(i=0;i<1;i++) { rc=SQLBindParameter(hstmt,1,SQL_PARAM_INPUT,SQL_INTEGER,SQL_INTEGER,0,0,(PTR)&input[i],0, &input_len); } input[0]=1; input[1]=2; input[2]=3; /* Execute statement */ rc = SQLExecute( hstmt); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { printf("Error in Execution of Direct SQL Smttm\n"); db_odbc_error (henv, hdbc, hstmt, stmtStr ); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (-1); } /* rc=SQLColAttribute(hstmt,1,SQL_DESC_TYPE,NULL,0,NULL,&type);*/ rc= SQLDescribeCol(hstmt,1,name,64,&cbName,&type,&size,NULL,NULL); printf("\tname=%s, namelen=%d, type=%d, colsz=%d\n", name, cbName, type, (int)size); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { printf("Failed with first Describe\n"); db_odbc_error (henv, hdbc, hstmt, stmtStr ); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (-1); } switch(type) { case SQL_VARCHAR: case SQL_LONGVARCHAR: case SQL_CHAR: type=SQL_C_CHAR; break; case SQL_INTEGER: case SQL_BIGINT: type=SQL_C_ULONG; break; case SQL_TINYINT:type=SQL_C_UTINYINT; break; case SQL_SMALLINT: type=SQL_C_USHORT; break; default: printf("\nIDont knw"); } /* Bind return value */ printf("\nAshwin[%d]\n",type); rc = SQLBindCol( hstmt, 1, type, &InsertedId, (SDWORD)sizeof(SDWORD), &valMaxIdSz); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { printf("Failed with first binnd\n"); db_odbc_error (henv, hdbc, hstmt, stmtStr ); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (-1); } rc= SQLDescribeCol(hstmt,2,name,1000,&cbName,&type,&size,NULL,NULL); printf("\tname=%s, namelen=%d, type=%d, colsz=%d\n", name, cbName, type, (int)size); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { printf("Failed with second Describe\n"); db_odbc_error (henv, hdbc, hstmt, stmtStr ); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (-1); } switch(type) { case SQL_VARCHAR: case SQL_LONGVARCHAR: case SQL_CHAR: type=SQL_C_CHAR; break; case SQL_INTEGER: case SQL_BIGINT: printf("\nInteger"); type=SQL_C_ULONG; break; case SQL_TINYINT:type=SQL_C_UTINYINT; break; case SQL_SMALLINT: type=SQL_C_USHORT; break; default: printf("\nIDont knw"); } printf("\nAshwin[%d]\n",type); recepId[0]=(void *)malloc(20); rc = SQLBindCol( hstmt, 2, SQL_C_DEFAULT,recepId[0] , (SDWORD)100, &valMaxIdSz); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { printf("Failed with second binnd\n"); db_odbc_error (henv, hdbc, hstmt, stmtStr ); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (-1); } name[0]='\0'; for(;;) { /* Get return value */ rc = SQLFetch( hstmt ); if(rc == SQL_NO_DATA_FOUND) break; if((rc == SQL_SUCCESS) || (rc == SQL_SUCCESS_WITH_INFO)) { printf("\n Name: %d ID: %ld", *(int*)recepId[0], InsertedId); } if((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO)) { db_odbc_error( henv, hdbc, hstmt, NULL ); break; } } rc = SQLFreeStmt( hstmt, SQL_DROP ); return (rc); } /* This function will only keep pinging the MySQL to check if the connection is broken */ int db_fetch_data_no_print(HENV henv, HDBC hdbc) { RETCODE rc; HSTMT hstmt; char stmtStr[100] = ""; char name[1024]; SDWORD nameSz; SDWORD valMaxIdSz; unsigned long InsertedId; /* Build the SQL statement */ sprintf(stmtStr, "SELECT id, name from hpuxtest"); /* Allocate ODBC statement structure */ rc = SQLAllocStmt( hdbc, &hstmt ); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { db_odbc_error( henv, hdbc, hstmt, NULL ); return (-1); } /* Bind return value */ rc = SQLBindCol( hstmt, 1, SQL_C_LONG, &InsertedId, (SDWORD)sizeof(SDWORD), &valMaxIdSz); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { db_odbc_error (henv, hdbc, hstmt, stmtStr ); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (-1); } rc = SQLBindCol( hstmt, 2, SQL_C_CHAR, name, sizeof(name), &nameSz); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { db_odbc_error (henv, hdbc, hstmt, stmtStr ); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (-1); } /* Execute statement */ rc = SQLExecDirect( hstmt, stmtStr, SQL_NTS ); if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) ) { db_odbc_error (henv, hdbc, hstmt, stmtStr ); rc = SQLFreeStmt( hstmt, SQL_DROP ); return (-1); } for(;;) { /* Get return value */ rc = SQLFetch( hstmt ); if(rc == SQL_NO_DATA_FOUND) break; if((rc == SQL_SUCCESS) || (rc == SQL_SUCCESS_WITH_INFO)) { /* Do nothing if it is working. We need to find an issue if there is one */ } if((rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO)) { db_odbc_error( henv, hdbc, hstmt, NULL ); break; } } rc = SQLFreeStmt( hstmt, SQL_DROP ); return (rc); }