testlora.c

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

C
1,757
字号
      if (SQLO_SUCCESS != 
          (sqlo_bind_by_name(sth, ":ip1", SQLOT_FLT, &ip1, sizeof(ip1),0,0) ||
           sqlo_bind_by_name(sth, ":ip2", SQLOT_INT, &ip2, sizeof(ip2),0,0) ||
           sqlo_bind_by_name(sth, ":op1", SQLOT_INT, &op1, sizeof(op1),0,0) ||
           sqlo_bind_by_name(sth, ":op2", SQLOT_STR, op2, sizeof(op2),0,0)
           ))
        {
          printf("sqlo_bind_param failed failed: %s\n", sqlo_geterror(dbh) );
          return 0;
        }
      else
        {
          while (SQLO_STILL_EXECUTING == (status = sqlo_execute(sth, 1)))
            {
              /* 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
      printf ("ip1: %.16f, ip2: %d, op1: %d, op2: %s\n", ip1, ip2, op1, op2);
    }
  else
    {
      printf("sqlo_prepare failed: Status: %d, %s\n", sth, sqlo_geterror(dbh) );
      return 0;
    }
  printf("test_plsql finished\n");
  return 1;
}

/*-------------------------------------------------------------------------
 * test_insert with bind by pos
 *-----------------------------------------------------------------------*/
int test_insert( int dbh, int thread_id, int delete_flag )
{
  int nkey;
  char ckey[6];
  double nval;
  char cval[21];
  char dval[12];
  int status;
  int sth = SQLO_STH_INIT;
  char fval[255];
  short cval_ind = 0;

  char * insert_stmt = 
    "INSERT INTO T_SQLORA_TEST (THREAD_ID, NKEY, CKEY, NVAL, CVAL, DVAL, FVAL) VALUES (:THR_ID, :NKEY, :CKEY, :NVAL, :CVAL, TO_DATE(:DVAL,'DD-MM-YYYY'), :FVAL)";



  printf("Testing Insert (bind by pos)\n");

  if (!create_table(dbh))
    return 0;


  nkey = 100;
  strcpy(ckey, "ckey");
  nval = 1234567890.001;
  strcpy(fval," 98765432109876543210987654321098765432109876543210.01");
  strcpy(cval,"aaaaaaaaaaaaaaaaaaaa");
  strcpy(dval,"01-JUL-2000");

  if (0 <= (sth = sqlo_prepare(dbh, insert_stmt)))
    {
      cval_ind = cval_ind == -1 ? 0 : -1;

      if (SQLO_SUCCESS != 
          (sqlo_bind_by_pos(sth, 1, SQLOT_INT, &thread_id, sizeof(int),0,0) ||
           sqlo_bind_by_pos(sth, 2, SQLOT_INT, &nkey, sizeof(int),0,0) ||
           sqlo_bind_by_pos(sth, 3, SQLOT_STR, ckey, sizeof(ckey),0,0) ||
           sqlo_bind_by_pos(sth, 4, SQLOT_FLT, &nval, sizeof(double),0,0) ||
           sqlo_bind_by_pos(sth, 5, SQLOT_STR, cval, sizeof(cval), &cval_ind,0) ||
           sqlo_bind_by_pos(sth, 6, SQLOT_STR, dval, sizeof(dval), 0, 0) ||
           sqlo_bind_by_pos(sth, 7, SQLOT_STR, fval, sizeof(fval), 0, 0)
           ))
        {
          printf("sqlo_bind_param failed at line %d: %s\n", __LINE__, sqlo_geterror(dbh) );
          return 0;
        }
      else
        {
          printf("Inserting the values\n");
          while (SQLO_STILL_EXECUTING == (status = sqlo_execute(sth, 1)))
            {
              /* do something */
              printf(".");
              SQLO_USLEEP;
            }
          printf("\n");
          if (SQLO_SUCCESS != status)
            {
              printf("sqlo_execute failed at line %d: %s\n", 
                     __LINE__, 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);

  printf("finished test_insert\n");
  return 1;
}

/*-------------------------------------------------------------------------
 * test_array_insert
 *-----------------------------------------------------------------------*/
int test_array_insert( int dbh, int thread_id, int delete_flag )
{
  int nkey[MAX_ITERS];
  char ckey[MAX_ITERS][6];
  double nval[MAX_ITERS];
  char cval[MAX_ITERS][4001];
  char dval[MAX_ITERS][16];
  char fval[MAX_ITERS][255];

  short nind[MAX_ITERS];
  short cind[MAX_ITERS];
  short dind[MAX_ITERS];
  short find[MAX_ITERS];
  int thread_ids[MAX_ITERS];

  int sth, i, j;
  int status;

  char * insert_stmt = 
    "INSERT INTO T_SQLORA_TEST (THREAD_ID, NKEY, CKEY, NVAL, CVAL, DVAL, FVAL) VALUES (:THR_ID, :NKEY, :CKEY, :NVAL, :CVAL, TO_DATE(:DVAL,'DD-MM-YYYY'), :FVAL)";

  printf("Testing Array Insert (bind by name)\n");


  if (!create_table(dbh))
    return 0;

  /* setup bind arrays */
  for ( i = 0 ; i < MAX_ITERS; i++)
    {
      nkey[i] = i+1;
      sprintf(ckey[i], "%c", 'A' + i % 26 );
      nval[i] = 1234567890.0 + i / 1000.0;
      strcpy(fval[i],"123456789012345678901234567890123456789012345678901234567890");
      for (j = 0; j < 20; j++)
        cval[i][j] = 'a' + i % 26;
      cval[i][20] = '\0';
      sprintf(dval[i], "%02d-JUL-2000", (i % 30 ) + 1);
      
      nind[i] = 0;
      if (i == 0)
        cind[i] = 0;
      else
        cind[i] = cind[i - 1] == 0 ? -1 : 0;

      if (cind[i])
        cval[i][0] = '\0';

      dind[i] = 0;
      find[i] = 0;

      thread_ids[i] = thread_id;
    }

  if (0 <= (sth = sqlo_prepare(dbh, insert_stmt)))
    {
      if (SQLO_SUCCESS != 
          (sqlo_bind_by_name(sth, ":NKEY", SQLOT_INT, &nkey[0], sizeof(int), NULL,1) ||
           sqlo_bind_by_name(sth, ":CKEY", SQLOT_STR, &ckey[0], sizeof(ckey[0]), NULL,1) ||
           sqlo_bind_by_name(sth, ":NVAL", SQLOT_FLT, &nval[0], sizeof(double), nind,1) ||
           sqlo_bind_by_name(sth, ":CVAL", SQLOT_STR, &cval[0], sizeof(cval[0]), cind,1) ||
           sqlo_bind_by_name(sth, ":DVAL", SQLOT_STR, &dval[0], sizeof(dval[0]), dind,1) ||
           sqlo_bind_by_name(sth, ":THR_ID", SQLOT_INT, &thread_ids[0], sizeof(int), NULL,1) ||
           sqlo_bind_by_name(sth, ":FVAL", SQLOT_STR, &fval[0], 255, find,1)
           ))
        {
          printf("sqlo_bind_param 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_array_insert2 (by pos)
 *-----------------------------------------------------------------------*/
int test_array_insert2( int dbh, int thread_id, int delete_flag )
{
  int nkey[MAX_ITERS];
  char ckey[MAX_ITERS][6];
  double nval[MAX_ITERS];
  char cval[MAX_ITERS][4001];
  char dval[MAX_ITERS][16];
  double fval[MAX_ITERS];

  short nind[MAX_ITERS];
  short cind[MAX_ITERS];
  short dind[MAX_ITERS];
  short find[MAX_ITERS];

  int thread_ids[MAX_ITERS];

  int i, j;
  int status;
  int sth;

  char * insert_stmt = 
    "INSERT INTO T_SQLORA_TEST (THREAD_ID, NKEY, CKEY, NVAL, CVAL, DVAL, FVAL) VALUES (:THR_ID, :NKEY, :CKEY, :NVAL, :CVAL, TO_DATE(:DVAL,'DD-MM-YYYY'), :FVAL)";

  printf("Testing Array Insert ( bind by pos)\n");


  if (!create_table(dbh))
    return (0);

  /* setup bind arrays */
  for ( i = 0 ; i < MAX_ITERS; i++)
    {
      nkey[i] = i+1;
      sprintf(ckey[i], "%c", 'A' + i % 26 );
      nval[i] = 1234567890.0 + i / 1000.0;
      fval[i] = nval[i];
      for (j = 0; j < 20; j++)
        cval[i][j] = 'a' + i % 26;
      cval[i][20] = '\0';
      sprintf(dval[i], "%02d-JUL-2000", (i % 30) + 1);
      
      nind[i] = 0;
      if (i == 0)
        cind[i] = 0;
      else
        cind[i] = cind[ i - 1 ] ? 0 : -1;

      dind[i] = 0;
      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(thread_ids[0]), NULL,1) ||
           sqlo_bind_by_pos(sth, 2, SQLOT_INT, &nkey[0], sizeof(nkey[0]), NULL,1) ||
           sqlo_bind_by_pos(sth, 3, SQLOT_STR, &ckey[0], sizeof(ckey[0]), NULL,1) ||
           sqlo_bind_by_pos(sth, 4, SQLOT_FLT, &nval[0], sizeof(nval[0]), nind,1) ||
           sqlo_bind_by_pos(sth, 5, SQLOT_STR, &cval[0], sizeof(cval[0]), cind,1) ||
           sqlo_bind_by_pos(sth, 6, SQLOT_STR, &dval[0], sizeof(dval[0]), dind,1) ||
           sqlo_bind_by_pos(sth, 7, SQLOT_FLT, &fval[0], sizeof(fval[0]), find,1) 
           ))
        {
          printf("sqlo_bind_param 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)

⌨️ 快捷键说明

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