testlora.c

来自「一个很好用的Linux/Unix下Oracle OCI开发接口封装库」· C语言 代码 · 共 1,757 行 · 第 1/4 页

C
1,757
字号
    {
      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)
#else
int 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, 50);
#else
  status = sqlo_init(0, MAX_LOOPS, 50);
#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 + =
减小字号Ctrl + -
显示快捷键?