⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testlora.c

📁 Linux下的操作oracle数据库的连接库
💻 C
📖 第 1 页 / 共 3 页
字号:
      find[i] = 0;      thread_ids[i] = thread_id;    }  if (0 <= (sth = sqlo_prepare(dbh, insert_stmt)))    {      if (SQLO_SUCCESS !=           (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &thread_ids[0], sizeof(int), NULL,1) ||           sqlo_bind_by_pos(sth, 2, SQLOT_INT, &nkey[0], sizeof(int), NULL,1) ||           sqlo_bind_by_pos(sth, 3, SQLOT_STR, &ckey[0], 6, NULL,1) ||           sqlo_bind_by_pos(sth, 4, SQLOT_FLT, &nval[0], sizeof(double), nind,1) ||           sqlo_bind_by_pos(sth, 5, SQLOT_STR, &cval[0], 21, cind,1) ||           sqlo_bind_by_pos(sth, 6, SQLOT_STR, &dval[0], 16, dind,1) ||           sqlo_bind_by_pos(sth, 7, SQLOT_FLT, &fval[0], sizeof(double), find,1)            ))        {          printf("sqlo_bind_by_pos failed failed: %s\n", sqlo_geterror(dbh) );          return 0;        }      else        {          while (SQLO_STILL_EXECUTING == (status = sqlo_execute(sth, MAX_ITERS)))            {              /* do something */              printf(".");              SQLO_USLEEP;            }          printf("\n");          if (SQLO_SUCCESS != status)            {              printf("sqlo_execute failed: %s\n", sqlo_geterror(dbh) );              return 0;            }        }#ifdef CLOSE_CURSOR      if (SQLO_SUCCESS != sqlo_close(sth))        {          printf("sqlo_close failed: %s\n", sqlo_geterror(dbh) );          return 0;        }#endif      if (delete_flag)        if (!do_select(dbh, thread_id))          return 0;    }  else    {      printf("sqlo_prepare failed: Status: %d, %s\n", sth, sqlo_geterror(dbh) );      return 0;    }  if (delete_flag)    if (!delete_table(dbh, "T_SQLORA_TEST", thread_id))      return(0);  return 1;}/*------------------------------------------------------------------------- * test_exists *-----------------------------------------------------------------------*/int test_exists(int dbh, int thread_id){  int status;  char sthread_id[16];  sprintf(sthread_id,"%d", thread_id);  /* Record does not exist */  if (SQLO_SUCCESS ==       (status = sqlo_exists(dbh, "T_SQLORA_TEST", "THREAD_ID", "-1", NULL)))    printf("test_exists failed\n");  else    {      if (status != SQLO_NO_DATA)        {          printf("test_exists failed: %s\n", sqlo_geterror(dbh));          return 0;        }      else        printf("test_exists  ok\n");            }  return 1;}/*------------------------------------------------------------------------- * test_count *-----------------------------------------------------------------------*/int test_count(int dbh, int thread_id){  int count;  char sthread_id[16];  sprintf(sthread_id,"%d", thread_id);  if (!test_insert(dbh, thread_id, 0))    return(0);  if ( 0 <= (count = sqlo_count(dbh, "T_SQLORA_TEST", "THREAD_ID", sthread_id, NULL)))    printf("test_count(1) ok\n");  else    {      printf("test_count(1) failed: %s\n", sqlo_geterror(dbh));      return 0;    }  return 1;}/*------------------------------------------------------------------------- * test_exec * Tests sqlo_exec with sqlo_break  *-----------------------------------------------------------------------*/int test_exec(int dbh, int thread_id){  int status;  int retval = 1;  int i;  char * stmt = "ALTER SESSION SET SQL_TRACE FALSE";  int bf = 0;  int sth = SQLO_STH_INIT;  char * stmt2 = "SELECT TABLE_NAME FROM ALL_TABLES WHERE ROWNUM <= 3";  const char **v;  printf("test_exec ");    while (SQLO_STILL_EXECUTING == (status = sqlo_open2(&sth, dbh, stmt2, 0, NULL)))    {      printf(".");      SQLO_USLEEP;    }  if (0 > status)    {      printf("sqlo_open2 returned Error %s\n%s\n", sqlo_geterror(dbh),             stmt2);      return (0);    }  for (i = 0; i < 1000; ++i)    {      while (SQLO_STILL_EXECUTING ==  (status = sqlo_exec(dbh, stmt)))        {          int status2;          printf(".");          if ( i == 998 && !bf )            {              printf("Testing break\n");              status2 = sqlo_break(dbh);              if (0 > status2)                {                  printf("sqlo_break failed (status=%d): %s\n", status2, sqlo_geterror(dbh));                  retval = 0;                }              bf = 1;              if (retval)                printf("Testing break ok\n");              else                printf("Testing break failed\n");            }        }      if ( 0 > status)        {          printf("sqlo_exec failed at %d (status=%d): %s\n", i, status, sqlo_geterror(dbh));          return(0);        }    } /* end for */  /* Lets see if we can continue fetch from the opened cursor after   * we called sqlo_break */  while (SQLO_STILL_EXECUTING == (status = sqlo_fetch(sth, 1)))    {      printf(".");      SQLO_USLEEP;    }  if (0 > status)    {      printf("sqlo_fetch failed: %s\n", sqlo_geterror(dbh));      retval = 0;    }  else    {      do {        v = sqlo_values(sth, NULL, 1);        printf("Table %s\n", v[0]);        while (SQLO_STILL_EXECUTING == (status = sqlo_fetch(sth, 1)))          {            printf(".");            SQLO_USLEEP;          }      } while (status == SQLO_SUCCESS);            if (status != SQLO_NO_DATA)        {          printf("fetch vailed %s\n", sqlo_geterror(dbh));          retval = 0;        }    }  if (retval)    printf("Test sqlo_exec ok\n");  else    printf("Test sqlo_exec failed\n");  return retval;}/*------------------------------------------------------------------------- * create_tables(dbh) *-----------------------------------------------------------------------*/int create_tables(int dbh){  if (!create_table(dbh))    return(0);  if (!create_table_long(dbh))    return(0);  if (!create_packages(dbh))    return (0);  return(1);}/*------------------------------------------------------------------------- * int cleanup *-----------------------------------------------------------------------*/int cleanup(int dbh){  /* ignore all errors maybe they weren't created */  while (SQLO_STILL_EXECUTING == sqlo_exec(dbh, "DROP Table T_SQLORA_TEST"))    {      printf(".");      SQLO_USLEEP;    }  while (SQLO_STILL_EXECUTING == sqlo_exec(dbh, "DROP TABLE T_SQLORA_TEST_LONG"))    {      printf(".");      SQLO_USLEEP;    }  while (SQLO_STILL_EXECUTING == sqlo_exec(dbh, "DROP PACKAGE BODY SQLORA_TEST"))    {      printf(".");      SQLO_USLEEP;    }  while (SQLO_STILL_EXECUTING == sqlo_exec(dbh, "DROP PACKAGE SQLORA_TEST"))    {      printf(".");      SQLO_USLEEP;    }  return 1;}/*------------------------------------------------------------------------- * run_test(void * arg); *-----------------------------------------------------------------------*/#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)void * run_test(void * arg)#elseint run_test(int arg)#endif{  int status;  int i = (int) arg;  int thread_id;  int session_created;  int server_attached;#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)  thread_id = pthread_self();#else  thread_id = 1;#endif  server_attached = 0;  do {    if (SQLO_SUCCESS != (status = sqlo_server_attach(&g_dbh[i], g_cstr))) {      printf("sqlo_sever_attach: failed %s\n", sqlo_geterror(g_dbh[i]));      sleep(5);    } else {      server_attached = 1;    }  } while (!server_attached);  if (SQLO_SUCCESS == status)    {#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)      printf("Thread %d attached to server. g_dbh[i]=%d\n", (int) pthread_self(), g_dbh[i]);#else      printf("Attached to server. g_dbh[i]=%d\n", g_dbh[i]);#endif    }  else    {      printf("connect failed with status: %d\n%s", status, sqlo_geterror(g_dbh[i]));#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)      pthread_exit((void *) 1);#else      return 1;#endif    }    session_created = 0;  do {    if ( SQLO_SUCCESS != (status = sqlo_session_begin(g_dbh[i], g_cstr, ""))) {      printf("sqlo_session_begin: failed for dbh=%d %s\n", g_dbh[i],             sqlo_geterror(g_dbh[i]));      sleep(5);    } else {      session_created = 1;    }  } while (!session_created);  /* make sure we use the right number format */  if (SQLO_SUCCESS != sqlo_exec(g_dbh[i], "ALTER SESSION SET NLS_TERRITORY='America'"))    {      printf("SQLERROR: %s\n", sqlo_geterror(g_dbh[i]));      return 0;    }  if (g_nonblocking_mode)    {      printf("Running in non-blocking mode\n");      if (SQLO_SUCCESS != sqlo_set_blocking(g_dbh[i], 0))        {          printf("Could not switch to non-blocking mode %s\n",                 sqlo_geterror(g_dbh[i]));#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)          pthread_exit((void *) 1);#else          return 1;#endif                  }    }  else    {      printf("Running in blocking mode\n");    }#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)  pthread_mutex_lock(&g_create_tables_lock);#endif  if (!create_tables(g_dbh[i]))    {#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)      pthread_mutex_unlock(&g_create_tables_lock);      pthread_exit((void*) 1);#else      return 0;#endif    }#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)  pthread_mutex_unlock(&g_create_tables_lock);#endif  if (!test_plsql(g_dbh[i], thread_id))#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)    pthread_exit((void *) 1);#else  return 1;#endif  if (!test_insert(g_dbh[i], thread_id, 1))#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)    pthread_exit((void *) 1);#else  return 1;#endif  if (!test_array_insert(g_dbh[i], thread_id, 1)) /* bind by name */#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)    pthread_exit((void *) 1);#else  return 1;#endif  if (!test_array_insert2(g_dbh[i], thread_id, 1)) /* bind by pos */#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)    pthread_exit((void *) 1);#else  return 1;#endif  if (!test_exists(g_dbh[i], thread_id))#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)    pthread_exit((void *) 1);#else  return 1;#endif  if (!test_count(g_dbh[i], thread_id))#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)    pthread_exit((void *) 1);#else  return 1;#endif  if (!test_reopen(g_dbh[i], thread_id))#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)    pthread_exit((void *) 1);#else  return 1;#endif  if (!test_select2(g_dbh[i], thread_id))#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)    pthread_exit((void *) 1);#else  return 1;#endif  if (!test_long(g_dbh[i], thread_id))#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)    pthread_exit((void *) 1);#else  return 1;#endif  printf("Rollback ");  if (SQLO_SUCCESS != (status = sqlo_rollback(g_dbh[i])))    printf("rollback failed (%d): %s\n", status, sqlo_geterror(g_dbh[i]));  printf("ok\n");    /* Test a lot of sqlo_exec calls */  if (!test_exec(g_dbh[i], thread_id))#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)    pthread_exit((void *) 1);#else  return 1;#endif  sqlo_finish(g_dbh[i]);#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)  pthread_exit((void *) 0);#else  return 0;#endif}/*------------------------------------------------------------------------- * start_test *-----------------------------------------------------------------------*/static int start_test(void){  int i;#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)  pthread_mutex_init(&g_create_tables_lock, NULL);#endif  for (i = 0; i < MAX_LOOPS; i++)    {#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)      pthread_create(&g_thr[i], NULL, run_test, (void *) i);#else      if (run_test( i))        return 1;#endif    }  return(0);}/*------------------------------------------------------------------------- * join_threads *-----------------------------------------------------------------------*/static int join_threads(void){#if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)  int i;  int thr_status;  /* wait for the threads to complete */  for (i = 0; i < MAX_LOOPS; i++)    {      pthread_join(g_thr[i], (void **) &thr_status);      printf("Thread %ld finished with %d\n",             g_thr[i], thr_status);    }#endif  return (0);}/*========================================================================= *  main *=======================================================================*/int main (int argc, char * argv[]) {  int status;  int dbh;  /*  signal(SIGALRM, sig_handler);  alarm(20);  */  printf("-------------------------------------------------------------\n\n");  if (argc > 1)    g_cstr = argv[1];  else    g_cstr = "scott/tiger";  #if defined(ENABLE_PTHREADS)  && defined(HAVE_PTHREAD_H)  status = sqlo_init(1, MAX_LOOPS+1, 5);#else  status = sqlo_init(0, MAX_LOOPS+1, 5);#endif    if (SQLO_SUCCESS != status)    {      printf ("sqlo_init failed. Exiting\n");      exit(1);    }  status = sqlo_connect(&dbh, g_cstr);  if (SQLO_SUCCESS != status)     {      printf ("sqlo_connect failed. Exiting\n");      exit(1);    }      /* start test */  if (start_test())    {      exit(1);    }  join_threads();  cleanup(dbh);  g_nonblocking_mode=1;  printf("*** RUN ALL TESTS IN NON-BLOCKING MODE ***\n");  /* start test */  if (start_test())    {      exit (1);    }  join_threads();  cleanup(dbh);  sqlo_finish(dbh);  printf("Finished tests.\nDid the output look like expected?\n");  return (0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -