Main Page | Modules | File List | Globals | Related Pages | Examples

Functions to do login/logout to/from a database


Functions

int sqlo_connect (sqlo_db_handle_t *dbhp, const char *cstr)
 Connect to a database.
int sqlo_finish (sqlo_db_handle_t dbh)
 Finish the session.
int sqlo_split_cstring (const char *cstr, char *uid, char *pwd, char *tnsname, unsigned int bufsize)
 Split an Oracle connect string.
int sqlo_server_attach (sqlo_db_handle_t *dbhp, const char *tnsname)
 Attach to a database server.
int sqlo_session_begin (sqlo_db_handle_t dbh, const char *username, const char *password)
 Begin a session.
int sqlo_server_detach (sqlo_db_handle_t dbh)
 Detach from server.
int sqlo_server_free (sqlo_db_handle_t dbh)
 Free a server connection This is your emergency exit when a connection to a database gets lost (end of file on communication channel).
int sqlo_session_end (sqlo_db_handle_t dbh)
 End a session.
const char * sqlo_getdatabase (sqlo_db_handle_t dbh)
 Returns the tnsname.

Function Documentation

int sqlo_connect sqlo_db_handle_t dbhp,
const char *  cstr
 

Connect to a database.

This is the short form of sqlo_server_attach followed by sqlo_session_begin

Parameters:
dbhp O - The database handle
cstr I - A Oracle connect string.
Returns:
  • SQLO_SUCCESS
  • < 0 on error
Example:
/* $Id: examples.c,v 1.7 2004/07/30 16:28:21 kpoitschke Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include "sqlora.h"
#include "examples.h"

static CONST char * _defuser = "scott/tiger";
static int _abort_flag = 0;

void sigint_handler(void);

void sigint_handler(void) {
  printf("Catched SIGINT\n");
  _abort_flag++;
}

#define RETURN_ON_ABORT if (_abort_flag) { sqlo_rollback(dbh); return EXIT_FAILURE; }

int main(int argc, char **argv)
{
  CONST char * cstr = _defuser;
  sqlo_db_handle_t dbh;
  int stat;
  sqlo_stmt_handle_t sth;
  double min_income;
  char server_version[1024];
  int handle;                   /* handle of the interrupt handler */

  if (argc > 1)
    cstr = argv[1];
  /* init */
  if (SQLO_SUCCESS != sqlo_init(SQLO_OFF, 1, 100)) {
    printf("Failed to init libsqlora8\n");
    return EXIT_FAILURE;
  }

  /* register the interrupt handler */
  sqlo_register_int_handler(&handle, sigint_handler);

  /* login */
  if (SQLO_SUCCESS != sqlo_connect(&dbh, cstr)) {
    printf("Cannot login with %s\n", cstr);
    return EXIT_FAILURE;
  }


  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  if (SQLO_SUCCESS != sqlo_server_version(dbh, server_version, sizeof(server_version))) {
    printf("Failed to get the server version: %s\n", sqlo_geterror(dbh));
    return EXIT_FAILURE;
  }
  printf("Connected to:\n%s\n\n", server_version);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */
  /* ex1.c */
  if (!table_exists(dbh, "EMP")) {
    printf("Table EMP does not exist. Please install Oracle demo tables\n");
    return EXIT_FAILURE;
  }

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex2.c */
  /* EMP should have 8 columns */
  if ( 8 != (stat = col_count(dbh, "EMP")))
    printf("Expected 8 columns in EMP, but counted %d\n", stat);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex3.c */
  stat = update_manager(dbh);
  printf("Reduced the salary of %d managers\n", stat);

  sqlo_rollback(dbh);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex4.c */
  sth = open_cursor(dbh);
  sqlo_close(sth);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex5.c */
  sth = open_cursor2(dbh, 2500.0);
  sqlo_close(sth);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */


  /* ex6.c */
  sth = reopen_cursor(dbh, 2500.0);
  if (sth != (stat = reopen_cursor(dbh, 5000.0))) {
    printf("reopen_cursor returned a new handle %d, expected %d\n", stat, sth);
  }
  /* we don't close this cursor */

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex7.c */
  do_select(dbh, 1000); /* note: we reuse the cursor opened by ex6.c */


  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex8.c */
  stat = update_emp(dbh, 2.0, "MANAGER");
  printf("Doubled the salary of %d managers.\n", stat);

  sqlo_rollback(dbh);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex9.c */
  call_plsql(dbh);


  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex10.c */
  min_income = 2500;
  sth = prepare_cursor(dbh, &min_income);
  sqlo_close(sth);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex11.c */
  printf("Employees with salary >= 3000:\n");
  do_select2(dbh, 3000); /* note: we reuse the cursor opened by ex6.c */
  
  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex12.c */
  printf("Employees with salary >= 1200:\n");
  do_array_select(dbh, 1200);


  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex13.c */
  insert_into_blob_table(dbh, 1);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex13b.c */
  update_blob_table(dbh, 1);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex14.c */
  insert_file_into_blob_table(dbh, 2, "examples");

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex15.c */
  select_from_blob_table(dbh, 1);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */

  /* ex16.c */
  select_file_from_blob_table(dbh, 2, "examples.cmp");
  if ( 0 != system("diff -q examples examples.cmp")) {
    printf("ERROR: examples is different from examples.cmp!!!\n");
  }
  sqlo_rollback(dbh);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */
  /* ex17.c */
  printf("Employees with salary (via refcursor) >= 1200:\n");
  select_refcursor(dbh, 1200);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */
  /* ex18.c */
  printf("Employees with salary (via refcursor with bind vars) >= 1200:\n");
  select_refcursor2(dbh, 1200);

  RETURN_ON_ABORT; /* finish if SIGINT was catched */
  /* ex19.c */
  printf("Employees, departements and locations via ntable\n");
  select_ntable(dbh);


  /* ex20.c */
  printf("Example20:\n");

  ex20(dbh, 1000.0); /* note: we reuse the cursor opened by ex6.c*/

  /* rollback */
  sqlo_rollback(dbh);

  /* logout */
  sqlo_finish(dbh);

  return EXIT_SUCCESS;
}
/* $Id: examples.c,v 1.7 2004/07/30 16:28:21 kpoitschke Exp $ */
See also:
sqlo_finish, sqlo_server_attach, sqlo_session_begin

int sqlo_finish sqlo_db_handle_t  dbh  ) 
 

Finish the session.

Finish the session with implicit commit. This is the short form of sqlo_session_end followed by sqlo_server_detach.

Parameters:
dbh I - A database handle
Returns:
  • SQLO_SUCCESS
  • < 0 on error
See also:
sqlo_connect, sqlo_session_end, sqlo_server_detach, sqlo_server_attach

const char* sqlo_getdatabase sqlo_db_handle_t  dbh  ) 
 

Returns the tnsname.

Returns the tnsname (or service name) of the given dbh.

Attention:
The function returns the database name passed to sqlo_connect or sqlo_server_attach, not the real SID, which can be different!
Parameters:
dbh I - A database handle
Returns:
The tnsname or NULL in case of an invalid dbh

int sqlo_server_attach sqlo_db_handle_t dbhp,
const char *  tnsname
 

Attach to a database server.

Attaches to a database without creating a session. tnsname can be a database name or a connect string. The function extracts the database name. If no database name is supplied, the function attaches to the database given in the env. variable ORACLE_SID.

Parameters:
dbhp O - The database handle
tnsname O - The tnsname or the complete Oracle connect string.
Returns:
  • SQLO_SUCCESS
  • < 0 on error
See also:
sqlo_session_begin, sqlo_server_detach
Since:
Version 2.2

int sqlo_server_detach sqlo_db_handle_t  dbh  ) 
 

Detach from server.

Closes all open sessions and detaches from the server.

Parameters:
dbh I - A database handle
Returns:
  • SQLO_SUCCESS
  • < 0 on error
See also:
sqlo_server_attach
Since:
Version 2.2

int sqlo_server_free sqlo_db_handle_t  dbh  ) 
 

Free a server connection This is your emergency exit when a connection to a database gets lost (end of file on communication channel).

You cannot free the libsqlora8 resources in such a case by sqlo_session_end or sqlo_server_detach, because the OCI statement OCISessionEnd crashes :-( So, if you detect that a connection is broken and you want to clean up the situation and reconnect, call sqlo_server_free to detach from the server and savely free the resources allocated by libsqlora8

Parameters:
dbh I - A database handle
Returns:
  • SQLO_SUCCESS
  • < 0 on error
Since:
Version 2.3

int sqlo_session_begin sqlo_db_handle_t  dbh,
const char *  username,
const char *  password
 

Begin a session.

Do the login to an attached server. You can either pass username and password seperatly, or pass the complete connect string in username.

Parameters:
dbh I - A database handle
username I - The username for authentication, or a complete Oracle connect string.
password I - The password for authentication
Returns:
  • SQLO_SUCCESS
  • < 0 on error
See also:
sqlo_server_attach, sqlo_session_end
Since:
Version 2.2

int sqlo_session_end sqlo_db_handle_t  dbh  ) 
 

End a session.

Does a logout, but does not detach from the server. It is possible to create a new session via sqlo_session_begin.

Attention:
Closing a session this way, means also to close all the cursors. Oracle is doing an implicit commit. This is maybe not be what you want.
Parameters:
dbh I - A database handle
Returns:
  • SQLO_SUCCESS
  • < 0 on error
See also:
sqlo_session_begin
Since:
Version 2.2

int sqlo_split_cstring const char *  cstr,
char *  uid,
char *  pwd,
char *  tnsname,
unsigned int  bufsize
 

Split an Oracle connect string.

Splits an Oracle connect string of the form uid[[/pwd]@tnsname] into its components. If no tnsname is found in the cstr, we copy the value of the env. variable ORACLE_SID into tnsname.

Parameters:
cstr I - A connect string to split
uid O - The returned uid part.
pwd O - The returned pwd part.
tnsname O - The returned tnsname.
bufsize I - The capacity of the output buffers.
Returns:
  • SQLO_SUCCESS
  • SQLO_ERROR (buffer too small)
Since:
Version 2.2


Generated on Thu Aug 25 12:33:50 2005 for libsqlora8 by  doxygen 1.3.9.1