OpenDBX::Conn(3) | opendbx | OpenDBX::Conn(3) |
OpenDBX::Conn - Encapsulates a connection to a database.
void bind (const char *database, const char *who='', const
char *cred='', odbxbind method=ODBX_BIND_SIMPLE) throw ( std::exception )
Authenticates the user and selects the database using C style string
parameters. void bind (const string &database, const string
&who='', const string &cred='', odbxbind method=ODBX_BIND_SIMPLE)
throw ( std::exception )
Authenticates the user and selects the database using C++ style string
parameters. Conn () throw ()
The default constructor for newly created connection objects without
parameters. Conn (const char *backend, const char *host='', const
char *port='') throw ( std::exception )
Creates a connection object using C style string parameters. Conn
(const string &backend, const string &host='', const string
&port='') throw ( std::exception )
Creates a connection object using C++ style string parameters. Conn
(const Conn &ref) throw ()
Copy constructor. Stmt create (const char *sql, unsigned long
size=0, Stmt::Type type=Stmt::Simple) throw ( std::exception )
Creates a statement object from a SQL text string using a C style buffer.
Stmt create (const string &sql, Stmt::Type
type=Stmt::Simple) throw ( std::exception )
Creates a statement object from a SQL text string using a C++ string. string
& escape (const char *from, unsigned long fromlen, string
&to) throw ( std::exception )
Escapes potentially dangerous characters in user input using a C style buffer.
string & escape (const string &from, string &to) throw (
std::exception )
Escapes potentially dangerous characters in user input using a C++ style
string parameter. void finish () throw ( std::exception )
Cleans up the connection object. bool getCapability (odbxcap cap) throw
( std::exception )
Tests if the database driver module does understand certain extensions. void
getOption (odbxopt option, void *value) throw ( std::exception )
Gets the value of a certain option provided by the database driver module.
Conn & operator= (const Conn &ref) throw ()
Assigns a connection instance to another one. void setOption (odbxopt
option, void *value) throw ( std::exception )
Sets a certain option provided by the database driver module. void
unbind () throw ( std::exception )
Releases the connection to the database and resets the authentication status.
~Conn () throw ()
Destroys the connection instance if no other references exist.
Encapsulates a connection to a database.
Author:
Version:
The default constructor for newly created connection objects without parameters. This is method is provided to enable programmers to use connection objects as member variables of other classes. They are initialized at construction time of the encapsulating object when no values for backend, host and port are available yet.
It's necessary to replace the created object later on by a connection instance where the necessary parameters have been given via one of the other constructors. Calling one of the member functions of an instance created by the default constructor isn't possible and will throw an exception.
Returns:
See also:
Conn( const string&, const string&, const string& )
Creates a connection object using C style string parameters. Initializes a new connection using the parameters backend, host and port, but doesn't open the connection to the database yet. This method allows C style strings as values for all parameters. Another constructor for C++ style strings is also available. It returns a new connection instance, which can be used to query options implemented by the driver or to create a statement. In case of an error, it throws an OpenDBX exception with error message, code and severity.
The parameter backend is the name of the driver the OpenDBX library should use to connect to a database. The name must be one of the implemented and available drivers on the system and is case sensitive. All driver names are in lower case, e.g. 'mysql'.
Depending on the database driver, host can have different meanings. Normally, it's the name or IP address of the server hosting the database server application. In case of serverless database implementations like SQLite it's the directory path where the database file is located. The path must contain the platform specific path separators like slash ('/') on Unix-like systems and backslash ('\') on Windows systems. Also the path must end with the path separator like '/path/to/file/'.
Furthermore port can be the number or name the database server application is listening to. If a name instead of the number is allowed depends on the database client library but the number as string does always work. If an empty string is given, the default port of the database server application is used by the database client library.
Parameters:
Returns:
Exceptions:
See also:
Creates a connection object using C++ style string parameters. Initializes a new connection using the parameters backend, host and port, but doesn't open the connection to the database yet. This method allows C++ style strings as values for all parameters. Another construtor for C style strings is also available. It returns a new connection instance, which can be used to query options implemented by the driver or to create a statement. In case of an error, it throws an OpenDBX exception with error message, code and severity.
The parameter backend is the name of the driver the OpenDBX library should use to connect to a database. The name must be one of the implemented and available drivers on the system and is case sensitive. All driver names are in lower case, e.g. 'mysql'.
Depending on the database driver, host can have different mearings. Normally, it's the name or IP address of the server hosting the database server application. In case of serverless database implementations like SQLite it's the directory path where the database file is located. The path must contain the platform specific path separators like slash ('/') on Unix-like systems and backslash ('\') on Windows systems. Also the path must end with the path separator like '/path/to/file/'.
Furthermore port can be the number or name the database server application is listening to. If a name instead of the number is allowed depends on the database client library but the number as string does always work. If an empty string is given, the default port of the database server application is used by the database client library.
Parameters:
Returns:
Exceptions:
See also:
Destroys the connection instance if no other references exist. Each connection object uses a reference counter to remember if the connection is shared with other objects. If this isn't the case or if this object is the last one referencing the connection, the connection is closed and the allocated memory is freed.
Copy constructor. Enables the transfer of the internal state of an object ref of the same type to this object. Both objects share the same database connection and the reference counter afterwards. The reference counter is incremented each time an object is copied and will be decremented if it is destroyed.
Parameters:
Authenticates the user and selects the database using C style string parameters. After initializing the object instance with one of the constructors taking a backend, host and port string as parameter, it's necessary to authenticate the user and select a database. Depending on the database client library, it also establishes the connection to the database server. This method accepts C style strings for the database name, the user name and the password.
The first parameter, the name of the database will be used to select the database all further commands are operating on. The database is the container for the tables, views, etc., which store the records and provide access to them.
Authentication is done in most cases by a combination of a user name and a password. If the user name is known by the database server and the supplied password matches with the stored one, the database server will allow the user to operate on the selected database and its tables provided the user has permissions to read and/or modify the content.
The method parameter can be used to select different methods of authentication. At the moment, only simple authentication (ODBX_BIND_SIMPLE) with user name and password is available.
Parameters:
Exceptions:
Authenticates the user and selects the database using C++ style string parameters. After initializing the object instance with one of the constructors taking a backend, host and port string as parameter, it's necessary to authenticate the user and select a database. Depending on the database client library, it also establishes the connection to the database server. This method accepts C++ style strings for the database name, the user name and the password.
The first parameter, the name of the database will be used to select the database all further commands are operating on. The database is the container for the tables, views, etc., which store the records and provide access to them.
Authentication is done in most cases by a combination of a user name and a password. If the user name is known by the database server and the supplied password matches with the stored one, the database server will allow the user to operate on the selected database and its tables provided the user has permissions to read and/or modify the content.
The method parameter can be used to select different methods of authentication. At the moment, only simple authentication (ODBX_BIND_SIMPLE) with user name and password is available.
Parameters:
Exceptions:
Creates a statement object from a SQL text string using a C style buffer. This method returns an instance of the Stmt class that contains the statement sent to the database server later on. Currently, only simple statements are supported, which are sent to the database server for parsing and returning the result after calling Stmt::execute(). Later on also prepared statements will be possible which are parsed by the database server when creating the object and only the parameters will be sent to the database server on execution. This can speed up processing especially if the statement is executed multiple times with different content for the parameters.
The parameter named sql must contain the SQL text string that should be sent to the database server for execution. It have to be a C style buffer in this case. For a reference of valid SQL statements, please have a look at the documentation of your database server.
The length of the statement excluding the trailing '0' byte should be given via the second parameter size. If the length of the statement is unknown, you can also supply zero (0) as size value which is also the default value if you hand over only one parameter. In this case, the length of the string is calculated internally by this function before the statement is given to the native database library.
Via the last parameter it can be selected how the statement should be treated internally by the OpenDBX and the native database library. Currently, only simple processing is available which sends complete SQL statements including its parameters as string to the database server for parsing and execution. Later on, also prepared statement handling will be available which parses the statements first and sends only the parameters to the server before execution. This parameter is also optional.
The returned object is representing the given statement, which can be executed later on. The statement is only valid for the current connection and as soon as the connection gets invalid or is closed, working with this object will throw an error.
Parameters:
Returns:
Exceptions:
See also:
Creates a statement object from a SQL text string using a C++ string. This method returns an instance of the Stmt class that contains the statement sent to the database server later on. Currently, only simple statements are supported, which are sent to the database server for parsing and returning the result after calling Stmt::execute(). Later on also prepared statements will be possible which are parsed by the database server when creating the object and only the parameters will be sent to the database server on execution. This can speed up processing especially if the statement is executed multiple times with different content for the parameters.
The parameter named sql must contain the SQL text string that should be sent to the database server for execution. It have to be a C++ style string in this case. For a reference of valid SQL statements, please have a look at the documentation of your database server.
Via the last parameter type it can be selected how the statement should be treated internally by the OpenDBX and the native database library. Currently, only simple processing is available which sends complete SQL statements including its parameters as string to the database server for parsing and execution. Later on, also prepared statement handling will be available which parses the statements first and sends only the parameters to the server before execution. This parameter is also optional.
The returned object is representing the given statement, which can be executed later on. The statement is only valid for the current connection and as soon as the connection gets invalid or is closed, working with this object will throw an error.
Parameters:
Returns:
Exceptions:
See also:
Escapes potentially dangerous characters in user input using a C style buffer. For preventing SQL injection attacks which can have desasterous effects, all text input that will be part of an SQL statement must be escaped. This does also apply to user content that is already stored in the database and should be copied to another record or stored again as the escaping is removed before the database server writes the content to disk.
The first parameter must contain the character sequence that should be escaped as C style string. This string itself won't be modified by this method.
The escaped string will be written to the third parameter named to, which have to be also an C++ style string. After transforming the input to an escaped string, the result may be more then twice the size of the original input. The additional escape sequences aren't stored in the database column so only the original string will be written to the disk.
A C++ reference of the third parameter containing the escaped string afterwards is also returned by this method to providing the possibility to write more elegant code.
Parameters:
Returns:
Exceptions:
Escapes potentially dangerous characters in user input using a C++ style string parameter. For preventing SQL injection attacks which can have desasterous effects, all text input that will be part of an SQL statement must be escaped. This does also apply to user content that is already stored in the database and should be copied to another record or stored again as the escaping is removed before the database server writes the content to disk.
The first parameter must contain the character sequence that should be escaped as C++ style string. This string itself won't be modified by this method.
The escaped string will be written to the third parameter named to, which have to be also an C++ style string. After transforming the input to an escaped string, the result may be more then twice the size of the original input. The additional escape sequences aren't stored in the database column so only the original string will be written to the disk.
A C++ reference of the second parameter containing the escaped string afterwards is also returned by this method too, providing the possibility to write more elegant code.
Parameters:
Returns:
Exceptions:
Cleans up the connection object.
Exceptions:
Tests if the database driver module does understand certain extensions. The OpenDBX library consists of a basic set of functions that must be implemented by all drivers and optional sets for additional functionality. This method allows an application to ask the driver selected by the first parameter of the constructor of this object if it supports one or more optional sets. The available sets and its constants are:
The basic set makes sure that all drivers can handle connections to the databases, send statements and retrieve results. It's supported by all drivers and usually don't have to be checked.
Some databases client libraries provide the content of large objects not via the basic functions. Instead, the Result::fieldValue() method returns only a handle to the large object and reading or modifying the content of this data object requires calling additional methods.
Parameters:
Returns:
Exceptions:
Gets the value of a certain option provided by the database driver module. It asks the backend module for implemented options and their current values. This function can be used at every stage and its primary use is to find out supported features of the backend module. This features can be enable with setOption() before the connection to the database server is established by calling bind().
There are several option values defined as named constants in the opendbx/api.h header file. A few of them are for informational purpose only while most of the options can also be set to different values by setOption() to change the behavior of the backend module. These options are:
The parameter value must be a pointer to an integer variable where the backend module will store the result for the supplied option. If it's not stated otherwise, the value assigned to the this parameter will be of boolean nature and therefore is ODBX_ENABLE for a supported option or ODBX_DISABLE for an option which isn't supported.
Parameters:
Exceptions:
Assigns a connection instance to another one. Assigns the internal state of an object ref of the same type to this object. Both objects share the same database connection and the reference counter afterwards. The reference counter is incremented each time an object is copied and will be decremented if it is destroyed.
Parameters:
Returns:
Sets a certain option provided by the database driver module. Changes the value of the specified option in the backend module or the native database library. Before trying to set an option, it should be tested with getOption() first to ensure that it is supported by the backend. Almost all options need to be set before connecting to the database server using bind() to take any effect.
There are several option values defined as named constants in the opendbx/api.h header file. The possible options are:
If not stated otherwise, the type of the variable passed to the second parameter named value must be an integer pointer. Its values must be in the range specified by the option being changed.
Parameters:
Exceptions:
Releases the connection to the database and resets the authentication status.
Exceptions:
Generated automatically by Doxygen for opendbx from the source code.
Mon Jan 28 2019 | Version 1.4.6 |