#include <conn.h>
Public Member Functions | |
Conn () | |
Default constructor. | |
Conn (char const *host, char const *user, char const *password, char const *db, unsigned int port, char const *us) | |
Constructor with the same parameters as mysql_connect(). | |
Conn (Conn const &c) | |
Copy constructor. | |
Conn & | operator= (Conn const &right) |
Assignment operator. | |
~Conn () | |
Destructor. | |
bool | connect () |
Connects to the MySQL server. | |
bool | change_user (char const *user, char const *password, char const *db) |
Changes the current MySQL user. | |
bool | select_db (char const *db) |
Changes the current MySQL database. | |
bool | reconnect () |
Reconnects to the MySQL server. | |
bool | ping () |
"Pings" the MySQL server. | |
void | close () |
Closes the connection to the MySQL server. | |
unsigned long | escape_string (misc::cstring &dest, char const *src, long len=-1) |
Escapes a string. | |
char const * | escape_string (char const *str, long len=-1) |
Escapes a string. | |
bool | query (char const *query, unsigned long length, Result *result, bool use=false) |
Executes a query. | |
bool | query (char const *query, Result *result, bool use=false) |
Same as query(query, strlen(query), result, use);. | |
bool | queryf (Result *result, char const *fmt,...) FORMAT(printf |
printf()-like interface to query() -- limited to queries with 16384 characters. | |
bool bool | connected () const |
Returns whether the MySQL connection is established. | |
bool | gone () const |
Returns whether the server is gone or lost. | |
unsigned int | queries () const |
Returns the number of queries that have been issued since connection. | |
time_t | since () const |
Returns when the connection was established. | |
double | exectime () const |
Returns the execution time for the last query. | |
unsigned long | thread_id () |
Returns the MySQL connection thread ID. | |
unsigned int | errnum () |
Returns the last error number that happened on the mysql connection. | |
char const * | error () |
Returns a string describing the last error that happened on the mysql connection. | |
char const * | host_info () |
Returns a string describing the connection, including server hostname. | |
unsigned int | proto_info () |
Returns the protocol version used by the connection. | |
char const * | server_info () |
Returns a string that represents the server version number. | |
unsigned long | server_version () |
Returns the version number of the server. | |
char const * | stat () |
Returns server status string. | |
my_ulonglong | insert_id () |
Returns the last insert ID. | |
my_ulonglong | affected_rows () |
Returns the number of rows affected by the last query. | |
unsigned int | warning_count () |
Returns the number of warnings for the last query. | |
char const * | info () |
Returns information about the last query. | |
unsigned int | field_count () |
Returns the number of fields for the last query. | |
unsigned int | connect_timeout () const |
Returns the connection timeout (in seconds). | |
unsigned int | connect_timeout (unsigned int timeout) |
Sets the connection timeout (in seconds). | |
my_bool | reconnect () const |
Returns whether the connection is reestablished automatically. | |
my_bool | reconnect (my_bool reco) |
Sets whether the connection is reestablished automatically. | |
char const * | host () const |
Returns the MySQL hostname. | |
char const * | host (char const *host) |
Sets the MySQL hostname. | |
unsigned int | port () const |
Returns the MySQL port. | |
unsigned int | port (unsigned int port) |
Sets the MySQL port. | |
char const * | unix_socket () const |
Returns the MySQL hostname. | |
char const * | unix_socket (char const *us) |
Sets the MySQL hostname. | |
char const * | user () const |
Returns the MySQL username. | |
char const * | user (char const *user) |
Sets the MySQL username. | |
char const * | password () const |
Returns the MySQL password. | |
char const * | password (char const *password) |
Sets the MySQL username. | |
char const * | db () const |
Returns the MySQL database. | |
char const * | db (char const *db) |
Sets the MySQL database. | |
char const * | charset () |
Returns the connection character set. | |
char const * | charset (char const *charset) |
Sets the connection character set. | |
Events const * | events () const |
Returns the event notifier object. | |
Events * | events (Events *events) |
Sets the event notifier object. | |
bool | log_queries () const |
Returns whether queries are logged. | |
bool | log_queries (bool value) |
Sets whether queries are logged. | |
bool | log_warnings () const |
Returns whether warnings are logged. | |
bool | log_warnings (bool value) |
Sets whether warnings are logged. | |
Static Public Member Functions | |
static char const * | client_info () |
Returns the MySQL client library version string. | |
static unsigned long | client_version () |
Returns the MySQL client library version. | |
Protected Attributes | |
MYSQL * | m_handle |
The MySQL connection handle. | |
bool | m_connected |
Is it currently connected? | |
bool | m_gone |
Is the server lost or gone away? | |
unsigned int | m_queries |
How much queries have been done? | |
time_t | m_since |
Since when are we connected? | |
double | m_exectime |
Last query execution time. | |
unsigned int | m_ctimeout |
Connect timeout (in seconds). | |
my_bool | m_reconnect |
Reconnect automatically ? | |
misc::cstring | m_host |
Server's hostname. | |
unsigned int | m_port |
Server's port. | |
misc::cstring | m_unixsocket |
Server's unix socket. | |
misc::cstring | m_user |
MySQL username. | |
misc::cstring | m_password |
MySQL password. | |
misc::cstring | m_db |
MySQL database. | |
misc::cstring | m_charset |
Connection character set. | |
misc::cstring | m_es |
A shared buffer for escape_string(). | |
size_t | m_essize |
Maximum size of the escape_string buffer. | |
Events * | m_e |
Events notifier. | |
bool | m_logqueries |
Log queries? | |
bool | m_logwarnings |
Log warnings? |
See the MySQL C API for more detailed information on some of the functions.
mysql_cli::Conn::Conn | ( | Conn const & | c | ) | [inline] |
Copy constructor. It copies the various data fields of the connection (like host, username, password...) but does not reestablish a connection.
[in] | c | The connection to copy. |
Assignment operator. Like the copy constructor, a new connection is not established.
[in] | right | The connection that is assigned. |
bool mysql_cli::Conn::connect | ( | ) |
Connects to the MySQL server.
bool mysql_cli::Conn::change_user | ( | char const * | user, | |
char const * | password, | |||
char const * | db | |||
) |
Changes the current MySQL user. If the user, password or database is invalid, then no change is made and the connection is still valid. Else, the user(), password() and db() fields are updated automatically.
[in] | user | The user to change to. |
[in] | password | The user's password. |
[in] | db | The database to use. |
bool mysql_cli::Conn::select_db | ( | char const * | db | ) |
Changes the current MySQL database. If the database can't be changed, then no change is made and the connection is still valid. Else, the db() field is updated automatically.
[in] | db | The database to use. |
bool mysql_cli::Conn::reconnect | ( | ) | [inline] |
bool mysql_cli::Conn::ping | ( | ) |
"Pings" the MySQL server, i.e. checks whether the connection is working. If the connection has gone down, a reconnection is attempted.
void mysql_cli::Conn::close | ( | ) |
Closes the connection to the MySQL server, if a connection is established.
An evClosed event is logged.
unsigned long mysql_cli::Conn::escape_string | ( | misc::cstring & | dest, | |
char const * | src, | |||
long | len = -1 | |||
) |
Escapes a string in a way that is suitable to use in a query.
[in] | dest | The destination string, a misc::cstring object that will automatically be sized accordingly. |
[in] | src | The string to escape. |
[in] | len | The length of the string to escape. If this is -1, then the length is computed automatically. |
char const* mysql_cli::Conn::escape_string | ( | char const * | str, | |
long | len = -1 | |||
) |
Escapes a string in a way that is suitable to use in a query. The returned string is stored in the object instance for which the method is called; therefore, further calls to this method for the same object will overwrite the contents of the returned string.
[in] | str | The string to escape. |
[in] | len | The length of the string to escape. If this is -1, then the length is computed automatically. |
bool mysql_cli::Conn::query | ( | char const * | query, | |
unsigned long | length, | |||
Result * | result, | |||
bool | use = false | |||
) |
Executes a SQL query.
[in] | query | The query to execute. |
[in] | length | The length of the query, in bytes. |
[in] | result | If different than NULL, the result of the query will be assigned to this result object. If it is NULL, and the query returned a result, the function will automatically retrieve and free it, so that another query can take place afterwards. |
[in] | use | If true, mysql_use_result() is used to get the result set instead of mysql_store_result(). |
bool mysql_cli::Conn::gone | ( | ) | const [inline] |
Returns whether the server is gone or lost (i.e. if one of the MySQL functions returned CR_SERVER_GONE_ERROR or CR_SERVER_LOST), if the connection has not been reestablished.
char const* mysql_cli::Conn::info | ( | ) |
Returns information about the last query, but only for a few statements, including INSERT, ALTER TABLE and UPDATE.
unsigned int mysql_cli::Conn::connect_timeout | ( | ) | const [inline] |
Returns the connection timeout (in seconds).
unsigned int mysql_cli::Conn::connect_timeout | ( | unsigned int | timeout | ) | [inline] |
Sets the connection timeout (in seconds).
[in] | timeout | The connection timeout. If it is zero, then the default value is used. |
my_bool mysql_cli::Conn::reconnect | ( | ) | const [inline] |
Returns whether the connection is reestablished automatically if it was dropped and a query is made.
my_bool mysql_cli::Conn::reconnect | ( | my_bool | reco | ) | [inline] |
Sets whether the connection is reestablished automatically if it was dropped and a query is made.
[in] | reco | true if the connection must be reestablished, false otherwise. |
Sets the event notifier object.
[in] | events | A pointer to the event notifier object. Conn will automatically free it when it's no longer needed. |
bool mysql_cli::Conn::log_warnings | ( | bool | value | ) | [inline] |
Sets whether warnings are logged. Warnings may only be logged after a call to the execute() method.
[in] | value | true if the warnings need to be logged, false otherwise. |