Exemple #1
0
/*
 * Function: insert_data
 *
 * Description: This routine inserts the data into the table
 *
 */
void insert_data()
{

  colp *colsptr;
  sword colindex;

  row_count = 0;
  while (row_count < numrows)
  {

    colsptr = cols;
    numinsert = numrows - row_count < MAX_ROWS_PER_INSERT ?
                       numrows - row_count : MAX_ROWS_PER_INSERT;

    for (colindex = 0; colindex < NUMCOLS; colsptr++, colindex++)
    {
       initialize_data(colsptr->c_buf, colsptr->c_size, colsptr->c_rlen,
                       numinsert);
       colsptr->c_curlen = numinsert;
    }

                      /* using offset zero - we want all the rows right away */
    if (oexec(&cda))
    {
        err_report(&cda);
        do_exit(OCI_EXIT_FAILURE);
    }

    printf(" Number of rows processed so far is %d\n", retval);

    row_count += numinsert;
  }

}
Exemple #2
0
/* {{{ proto int ora_exec(int cursor)
   Execute a parsed statement */
void php3_Ora_Exec(INTERNAL_FUNCTION_PARAMETERS)
{								/* cursor_index */
	pval *arg;
	oraCursor *cursor = NULL;

	if (getParameters(ht, 1, &arg) == FAILURE)
		WRONG_PARAM_COUNT;

	convert_to_long(arg);

	if ((cursor = ora_get_cursor(list, arg->value.lval)) == NULL) {
		RETURN_FALSE;
	}

	if (cursor->cda.ft == FT_SELECT) {
		if (ora_describe_define(cursor) < 0) {
			/* error message is given by ora_describe_define() */
			RETURN_FALSE;
		}
	}

	if(cursor->nparams > 0){
		if(!ora_set_param_values(cursor, 0)){
			RETURN_FALSE;
		}
	}

	if (oexec(&cursor->cda)) {
		php3_error(E_WARNING, "Ora_Exec failed (%s)",
				   ora_error(&cursor->cda));
		RETURN_FALSE;
	}
	
	if(cursor->nparams > 0){
		if(!ora_set_param_values(cursor, 1)){
			RETURN_FALSE;
		}
	}
	RETURN_TRUE;
}
Cda_Def *DBConnection_ORACLE::ExecuteQuery(char *p_query, DBString *p_arguments, int p_argument_count)
{
	int t_affected_rows;
	t_affected_rows = 0;

	unsigned int t_query_length;
	t_query_length = strlen(p_query);

	Cda_Def *t_cursor;
	t_cursor = new Cda_Def;

	bool t_success;
	t_success = true;

	sword t_error;
	if (t_success)
	{
		t_error = oopen(t_cursor, getLDA(), (text *)0, -1, -1, NULL, -1);
		if (t_error != 0)
			t_success = false;
	}

	char *t_parsed_query;
	t_parsed_query = p_query;

	PlaceholderMap t_placeholder_map;

	if (p_argument_count != 0)
	{
		t_placeholder_map . length = 0;
		t_placeholder_map . elements = new int[p_argument_count + 1];

		DBBuffer t_query_buffer(t_query_length + 1);
		t_success = processQuery(p_query, t_query_buffer, queryCallback, &t_placeholder_map);

		t_query_length = t_query_buffer . getSize();
		t_parsed_query = t_query_buffer . grab();
	}

	if (t_success)
	{
		if (oparse(t_cursor, (text *)t_parsed_query, t_query_length, DEFER_PARSE, PARSE_V7_LNG) != 0)
			t_success = false;
	}

	if (t_success)
	{
		if (!BindVariables(t_cursor, p_arguments, p_argument_count, &t_placeholder_map))
			t_success = false;
	}

	if (t_success)
	{
		t_error = oexec(t_cursor);
		if (t_error != 0)
			t_success = false;
	}

	if (!t_success)
	{
		delete t_cursor;
		t_cursor = NULL;
	}

	return t_cursor;
}
Exemple #4
0
static int
perform_oracle_search(uschar *query, uschar *server, uschar **resultptr,
  uschar **errmsg, BOOL *defer_break)
{
Cda_Def *cda = NULL;
struct cda_def *oracle_handle = NULL;
Ora_Describe *desc = NULL;
Ora_Define *def = NULL;
void *hda = NULL;

int i;
int ssize = 0;
int offset = 0;
int yield = DEFER;
unsigned int num_fields = 0;
uschar *result = NULL;
oracle_connection *cn = NULL;
uschar *server_copy = NULL;
uschar *sdata[4];
uschar tmp[1024];

/* Disaggregate the parameters from the server argument. The order is host,
database, user, password. We can write to the string, since it is in a
nextinlist temporary buffer. The copy of the string that is used for caching
has the password removed. This copy is also used for debugging output. */

for (i = 3; i > 0; i--)
  {
  uschar *pp = Ustrrchr(server, '/');
  if (pp == NULL)
    {
    *errmsg = string_sprintf("incomplete ORACLE server data: %s", server);
    *defer_break = TRUE;
    return DEFER;
    }
  *pp++ = 0;
  sdata[i] = pp;
  if (i == 3) server_copy = string_copy(server);  /* sans password */
  }
sdata[0] = server;   /* What's left at the start */

/* If the database is the empty string, set it NULL - the query must then
define it. */

if (sdata[1][0] == 0) sdata[1] = NULL;

/* See if we have a cached connection to the server */

for (cn = oracle_connections; cn != NULL; cn = cn->next)
  {
  if (strcmp(cn->server, server_copy) == 0)
    {
    oracle_handle = cn->handle;
    hda = cn->hda_mem;
    break;
    }
  }

/* If no cached connection, we must set one up */

if (cn == NULL)
  {
  DEBUG(D_lookup) debug_printf("ORACLE new connection: host=%s database=%s "
    "user=%s\n", sdata[0], sdata[1], sdata[2]);

  /* Get store for a new connection, initialize it, and connect to the server */

   oracle_handle = store_get(sizeof(struct cda_def));
   hda = store_get(HDA_SIZE);
   memset(hda,'\0',HDA_SIZE);

  /*
   * Perform a default (blocking) login
   *
   * sdata[0] = tnsname (service name - typically host name)
   * sdata[1] = dbname - not used at present
   * sdata[2] = username
   * sdata[3] = passwd
   */

  if(olog(oracle_handle, hda, sdata[2], -1, sdata[3], -1, sdata[0], -1,
         (ub4)OCI_LM_DEF) != 0)
    {
    *errmsg = oracle_error(oracle_handle, oracle_handle->rc,
      US"connection failed");
    *defer_break = FALSE;
    goto ORACLE_EXIT_NO_VALS;
    }

  /* Add the connection to the cache */

  cn = store_get(sizeof(oracle_connection));
  cn->server = server_copy;
  cn->handle = oracle_handle;
  cn->next = oracle_connections;
  cn->hda_mem = hda;
  oracle_connections = cn;
  }

/* Else use a previously cached connection - we can write to the server string
to obliterate the password because it is in a nextinlist temporary buffer. */

else
  {
  DEBUG(D_lookup)
    debug_printf("ORACLE using cached connection for %s\n", server_copy);
  }

/* We have a connection. Open a cursor and run the query */

cda = store_get(sizeof(Cda_Def));

if (oopen(cda, oracle_handle, (text *)0, -1, -1, (text *)0, -1) != 0)
  {
  *errmsg = oracle_error(oracle_handle, cda->rc, "failed to open cursor");
  *defer_break = FALSE;
  goto ORACLE_EXIT_NO_VALS;
  }

if (oparse(cda, (text *)query, (sb4) -1,
      (sword)PARSE_NO_DEFER, (ub4)PARSE_V7_LNG) != 0)
  {
  *errmsg = oracle_error(oracle_handle, cda->rc, "query failed");
  *defer_break = FALSE;
  oclose(cda);
  goto ORACLE_EXIT_NO_VALS;
  }

/* Find the number of fields returned and sort out their types. If the number
is one, we don't add field names to the data. Otherwise we do. */

def = store_get(sizeof(Ora_Define)*MAX_SELECT_LIST_SIZE);
desc = store_get(sizeof(Ora_Describe)*MAX_SELECT_LIST_SIZE);

if ((num_fields = describe_define(cda,def,desc)) == -1)
  {
  *errmsg = oracle_error(oracle_handle, cda->rc, "describe_define failed");
  *defer_break = FALSE;
  goto ORACLE_EXIT;
  }

if (oexec(cda)!=0)
  {
  *errmsg = oracle_error(oracle_handle, cda->rc, "oexec failed");
  *defer_break = FALSE;
  goto ORACLE_EXIT;
  }

/* Get the fields and construct the result string. If there is more than one
row, we insert '\n' between them. */

while (cda->rc != NO_DATA_FOUND)  /* Loop for each row */
  {
  ofetch(cda);
  if(cda->rc == NO_DATA_FOUND) break;

  if (result != NULL) result = string_cat(result, &ssize, &offset, "\n", 1);

  /* Single field - just add on the data */

  if (num_fields == 1)
    result = string_cat(result, &ssize, &offset, def[0].buf, def[0].col_retlen);

  /* Multiple fields - precede by file name, removing {lead,trail}ing WS */

  else for (i = 0; i < num_fields; i++)
    {
    int slen;
    uschar *s = US desc[i].buf;

    while (*s != 0 && isspace(*s)) s++;
    slen = Ustrlen(s);
    while (slen > 0 && isspace(s[slen-1])) slen--;
    result = string_cat(result, &ssize, &offset, s, slen);
    result = string_cat(result, &ssize, &offset, US"=", 1);

    /* int and float type wont ever need escaping. Otherwise, quote the value
    if it contains spaces or is empty. */

    if (desc[i].dbtype != INT_TYPE && desc[i].dbtype != FLOAT_TYPE &&
       (def[i].buf[0] == 0 || strchr(def[i].buf, ' ') != NULL))
      {
      int j;
      result = string_cat(result, &ssize, &offset, "\"", 1);
      for (j = 0; j < def[i].col_retlen; j++)
        {
        if (def[i].buf[j] == '\"' || def[i].buf[j] == '\\')
          result = string_cat(result, &ssize, &offset, "\\", 1);
        result = string_cat(result, &ssize, &offset, def[i].buf+j, 1);
        }
      result = string_cat(result, &ssize, &offset, "\"", 1);
      }

    else switch(desc[i].dbtype)
      {
      case INT_TYPE:
      sprintf(CS tmp, "%d", def[i].int_buf);
      result = string_cat(result, &ssize, &offset, tmp, Ustrlen(tmp));
      break;

      case FLOAT_TYPE:
      sprintf(CS tmp, "%f", def[i].flt_buf);
      result = string_cat(result, &ssize, &offset, tmp, Ustrlen(tmp));
      break;

      case STRING_TYPE:
      result = string_cat(result, &ssize, &offset, def[i].buf,
        def[i].col_retlen);
      break;

      default:
      *errmsg = string_sprintf("ORACLE: unknown field type %d", desc[i].dbtype);
      *defer_break = FALSE;
      result = NULL;
      goto ORACLE_EXIT;
      }

    result = string_cat(result, &ssize, &offset, " ", 1);
    }
  }

/* If result is NULL then no data has been found and so we return FAIL.
Otherwise, we must terminate the string which has been built; string_cat()
always leaves enough room for a terminating zero. */

if (result == NULL)
  {
  yield = FAIL;
  *errmsg = "ORACLE: no data found";
  }
else
  {
  result[offset] = 0;
  store_reset(result + offset + 1);
  }

/* Get here by goto from various error checks. */

ORACLE_EXIT:

/* Close the cursor; don't close the connection, as it is cached. */

oclose(cda);

ORACLE_EXIT_NO_VALS:

/* Non-NULL result indicates a sucessful result */

if (result != NULL)
  {
  *resultptr = result;
  return OK;
  }
else
  {
  DEBUG(D_lookup) debug_printf("%s\n", *errmsg);
  return yield;      /* FAIL or DEFER */
  }
}
Exemple #5
0
/* {{{ proto int ora_do(int connection, int cursor)
   Parse and execute a statement and fetch first result row */ 
void php3_Ora_Do(INTERNAL_FUNCTION_PARAMETERS)
{
	pval *argv[2];
	oraConnection *conn = NULL;
	oraCursor *cursor = NULL;
	text *query;

	if (ARG_COUNT(ht) != 2 || getParametersArray(ht, 2, argv) == FAILURE) {
		WRONG_PARAM_COUNT;
	}

	convert_to_long(argv[0]);
	convert_to_string(argv[1]);

	conn = ora_get_conn(list,plist, argv[0]->value.lval);
	if (conn == NULL) {
		RETURN_FALSE;
	}

	if ((cursor = (oraCursor *)emalloc(sizeof(oraCursor))) == NULL){
		php3_error(E_WARNING, "Out of memory");
		RETURN_FALSE;
	}

	memset(cursor, 0, sizeof(oraCursor));

        query = (text *) estrndup(argv[1]->value.str.val,argv[1]->value.str.len);

        if (query == NULL) {
                php3_error(E_WARNING, "Invalid query in Ora_Do");
                RETURN_FALSE;
        }

        cursor->query = query;

	if (oopen(&cursor->cda, &conn->lda, (text *) 0, -1, -1, (text *) 0, -1)) {
		php3_error(E_WARNING, "Unable to open new cursor (%s)",
				   ora_error(&cursor->cda));
		efree(cursor);
		RETURN_FALSE;
	}
	cursor->open = 1;
	cursor->conn_ptr = conn;	
	cursor->conn_id = argv[0]->value.lval;	
	
	/* Prepare stmt */

	if (oparse(&cursor->cda, query, (sb4) - 1, 1, VERSION_7)){
		php3_error(E_WARNING, "Ora_Do failed (%s)",
				   ora_error(&cursor->cda));
		_close_oracur(cursor);
		RETURN_FALSE;
	}

	/* Execute stmt (and fetch 1st row for selects) */
	if (cursor->cda.ft == FT_SELECT) {
		if (ora_describe_define(cursor) < 0){
			/* error message is given by ora_describe_define() */
			_close_oracur(cursor);
			RETURN_FALSE;
		}
		if (oexfet(&cursor->cda, 1, 0, 0)) {
			php3_error(E_WARNING, "Ora_Do failed (%s)",
					   ora_error(&cursor->cda));
			_close_oracur(cursor);
			RETURN_FALSE;
		}
		cursor->fetched = 1;
	} else {
		if (oexec(&cursor->cda)) {
			php3_error(E_WARNING, "Ora_Do failed (%s)",
					   ora_error(&cursor->cda));
			_close_oracur(cursor);
			RETURN_FALSE;
		}
	}

	RETURN_RESOURCE(ora_add_cursor(list, cursor));
}