Difference between revisions of "OpenDBX/C API/odbx init"

From Linuxnetworks
< OpenDBX‎ | C API
Jump to: navigation, search
(Return values:)
(Description: mysql_basic.c has socket=param->host)
 
(27 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 +
'''Allocate per connection data structures'''
  
  int odbx_init(
+
  #include <opendbx/api.h>
    odbx_t** handle,
+
    const char* backend,
+
    const char* host,
+
    const char* port )
+
  
= Description: =
+
int '''odbx_init'''(
 +
    odbx_t** '''''handle''''',
 +
    const char* '''''backend''''',
 +
    const char* '''''host''''',
 +
    const char* '''''port''''' )
  
Allocates and initializes the opaque object for connecting to the database server. The pointer to the newly created object is stored in handle and it can be used in all further OpenDBX functions not related to result processing.
+
== Description ==
  
= Parameters: =
+
[[OpenDBX/API/odbx_init|odbx_init]]() allocates and initializes an opaque object required for all further operations within the OpenDBX library which is used to identify the connection and to maintain per connection information. Depending on the backend it can open a connection to the database server but often this is done not until performing authentication through [[OpenDBX/API/odbx_bind|odbx_bind]]().
  
* handle: Pointer where the newly allocated connection object should be stored
+
The pointer of the newly allocated connection object is stored in '''''handle''''' if [[OpenDBX/API/odbx_init|odbx_init]]() completes successfully. Otherwise, the value of the '''''handle''''' variable will be undefined and must not be used as input for other functions of the library. The returned connection object must be freed by [[OpenDBX/API/odbx_finish|odbx_finish]]() to avoid memory leaks if it will be no longer used by the application.
* backend: Name of the backend you want to use (mysql, pgsql, sqlite and sqlite3 - depends on the installed libraries)
+
* host: Host name or IP address of the database server or path to database file
+
* port: Port number the database server is listening to
+
  
= Return values: =
+
The OpenDBX library provides access to several different database implementations through a single interface and therefore has to know which one of the available backend modules it should use for the operations. The '''''backend''''' parameter will be used to perform the lookup of the requested module. It has to be a zero-terminated ASCII string with all characters in in lower case. Currently, these backend modules are available:
  
* ODBX_ERR_SUCCESS on success
+
* firebird (Firebird/Interbase)
* Less than zero if an error occured
+
* mssql (MS SQL Server via FreeTDS)
 +
* mysql (MySQL)
 +
* oracle (Oracle 8i/9i/10g)
 +
* pgsql (PostgreSQL)
 +
* sqlite (SQLite v2)
 +
* sqlite3 (SQLite v3)
 +
* sybase (Sybase ASE)
  
= Errors: =
+
Connecting to a database server requires at least an identifier to know where the database is located. There are several kinds of identifiers like host names, IP addresses, named pipes, etc. which could be used. One of them can be provided via the '''''host''''' parameter and it is up to the native database library what it will accept. Most native libraries accept at least host names and IP addresses and also use the provided '''''port''''' in this case. The available methods for '''''host''''' are:
  
* -ODBX_ERR_PARAM: One of the parameters or its content is invalid
+
; host name string : supported by Firebird/Interbase, MySQL, Oracle and PostgreSQL backends
* -ODBX_ERR_NOMEM: Allocating new memory failed
+
; IP address string : supported by Firebird/Interbase, MySQL, Oracle and PostgreSQL backends
* -ODBX_ERR_TOOLONG: The length of a string exceeded the buffer size
+
; absolute path to Unix domain socket : currently supported by the PostgreSQL and MySQL backends only
* -ODBX_ERR_NOEXIST: Backend library was not found
+
; (relative) directory path with trailing slash/backslash : required by SQLite and SQLite3 backends
* -ODBX_ERR_NOOP: The function does not exist in the backend
+
; section name in configuration file : required by MS SQL Server and Sybase ASE backends
* -1: Any error retured by the backend
+
  
 +
Depending on the native database library, it's also possible to use database specific default values for the '''''host''''' and '''''port''''' parameters by supplying empty, zero-terminated strings or a NULL value.
  
----
+
== Return value ==
Back to [[OpenDBX API|Overview]]
+
 
 +
[[OpenDBX/API/odbx_init|odbx_init]]() returns ODBX_ERR_SUCCESS, or an error code whose value is less than zero if one of the operations couldn't be completed successfully. Possible error codes are listed in the error section and they can be feed to [[OpenDBX/API/odbx_error|odbx_error]]() and [[OpenDBX/API/odbx_error_type|odbx_error_type]]() to get further details.
 +
 
 +
== Errors ==
 +
 
 +
; -ODBX_ERR_BACKEND : The backend module returned an error because it couldn't setup the necessary structures
 +
; -ODBX_ERR_PARAM : '''''handle''''' is NULL and the allocated connection object can't be stored
 +
; -ODBX_ERR_NOMEM : Allocating new memory for the connection object failed
 +
; -ODBX_ERR_SIZE : The length of a string exceeded the available buffer size
 +
; -ODBX_ERR_NOTEXIST : A backend module with this name wasn't found. Either the module isn't installed, the given name was wrong or not in the correct character case
 +
; -ODBX_ERR_NOOP : The backend module doesn't provide the required function
 +
 
 +
== See also ==
 +
 
 +
* [[OpenDBX/API/odbx_bind|odbx_bind]]()
 +
* [[OpenDBX/API/odbx_capabilities|odbx_capabilities]]()
 +
* [[OpenDBX/API/odbx_error|odbx_error]]()
 +
* [[OpenDBX/API/odbx_finish|odbx_finish]]()
 +
* [[OpenDBX/API/odbx_get_option|odbx_get_option]]()
 +
* [[OpenDBX/API/odbx_set_option|odbx_set_option]]()

Latest revision as of 09:46, 24 September 2012

Allocate per connection data structures

#include <opendbx/api.h>
int odbx_init(
    odbx_t** handle,
    const char* backend,
    const char* host,
    const char* port )

Description

odbx_init() allocates and initializes an opaque object required for all further operations within the OpenDBX library which is used to identify the connection and to maintain per connection information. Depending on the backend it can open a connection to the database server but often this is done not until performing authentication through odbx_bind().

The pointer of the newly allocated connection object is stored in handle if odbx_init() completes successfully. Otherwise, the value of the handle variable will be undefined and must not be used as input for other functions of the library. The returned connection object must be freed by odbx_finish() to avoid memory leaks if it will be no longer used by the application.

The OpenDBX library provides access to several different database implementations through a single interface and therefore has to know which one of the available backend modules it should use for the operations. The backend parameter will be used to perform the lookup of the requested module. It has to be a zero-terminated ASCII string with all characters in in lower case. Currently, these backend modules are available:

  • firebird (Firebird/Interbase)
  • mssql (MS SQL Server via FreeTDS)
  • mysql (MySQL)
  • oracle (Oracle 8i/9i/10g)
  • pgsql (PostgreSQL)
  • sqlite (SQLite v2)
  • sqlite3 (SQLite v3)
  • sybase (Sybase ASE)

Connecting to a database server requires at least an identifier to know where the database is located. There are several kinds of identifiers like host names, IP addresses, named pipes, etc. which could be used. One of them can be provided via the host parameter and it is up to the native database library what it will accept. Most native libraries accept at least host names and IP addresses and also use the provided port in this case. The available methods for host are:

host name string 
supported by Firebird/Interbase, MySQL, Oracle and PostgreSQL backends
IP address string 
supported by Firebird/Interbase, MySQL, Oracle and PostgreSQL backends
absolute path to Unix domain socket 
currently supported by the PostgreSQL and MySQL backends only
(relative) directory path with trailing slash/backslash 
required by SQLite and SQLite3 backends
section name in configuration file 
required by MS SQL Server and Sybase ASE backends

Depending on the native database library, it's also possible to use database specific default values for the host and port parameters by supplying empty, zero-terminated strings or a NULL value.

Return value

odbx_init() returns ODBX_ERR_SUCCESS, or an error code whose value is less than zero if one of the operations couldn't be completed successfully. Possible error codes are listed in the error section and they can be feed to odbx_error() and odbx_error_type() to get further details.

Errors

-ODBX_ERR_BACKEND 
The backend module returned an error because it couldn't setup the necessary structures
-ODBX_ERR_PARAM 
handle is NULL and the allocated connection object can't be stored
-ODBX_ERR_NOMEM 
Allocating new memory for the connection object failed
-ODBX_ERR_SIZE 
The length of a string exceeded the available buffer size
-ODBX_ERR_NOTEXIST 
A backend module with this name wasn't found. Either the module isn't installed, the given name was wrong or not in the correct character case
-ODBX_ERR_NOOP 
The backend module doesn't provide the required function

See also