Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

mysql_header.h File Reference

#include <time.h>
#include "mysql.h"

Go to the source code of this file.

Data Structures

struct  smysql
 Data structure used to store the configuration for MySql connection. More...


Defines

#define MYSQL_CONFIGURATION_TAG_NUMBER   12
 Number of configuration tags for MySql in a configuration file.

#define RADONLINE_START   0
 This constant is used by the function send_to_radonline(). It tells the function that a START ticket should be inserted into the Radonline database.

#define RADONLINE_STOP   1
 This constant is used by the function send_to_radonline(). It tells the function that a STOP ticket should be inserted into the Radonline database.

#define MYDNS_START   0
 This constant is used by the function send_to_mydns(). It tells the function that a START ticket should be inserted into the MyDns database.

#define MYDNS_STOP   1
 This constant is used by the function send_to_mydns(). It tells the function that a STOP ticket should be inserted into the MyDns database.

#define MYSQL_MAX_ERROR_SIZE   4096
 Maximum number of characters for the buffer that hold the message associated with the last MySql error.

#define MAX_SQL_REQUEST_SIZE   4096
 Maximum number of characters for a SQL request.

#define DB_HOST_MAX_SIZE   256
 Maximum number of characters for the database host name.

#define DB_USER_MAX_SIZE   256
 Maximum number of characters for a database user login.

#define DB_NAME_MAX_SIZE   256
 Maximum number of characters for a database name.

#define DB_PASSWD_MAX_SIZE   256
 Maximum number of characters for a database password.

#define MYSQL_SOCKET_FILE_MAX_SIZE   256
 Maximum number of characters for the path to the MySql UNIX socket.

#define MYSQL_DEBUG_FILE_MAX_SIZE   256
 Maximum number of characters for the path to the MySql debug file.

#define MYSQL_REQUEST_SKIPED   -1
 Return value for the functions:
  • standard_query
  • sql_update
  • sql_insert
  • sql_delete
  • sql_select
  • get_unix_timestamp
The SQL request was skiped because the reconnection delay did not expired.


#define MYSQL_CONNECTION_LOST   -2
 Return value for the functions:
  • standard_query
  • sql_update
  • sql_insert
  • sql_delete
  • sql_select
  • get_unix_timestamp
The connection to the MySql server was lost.


#define MYSQL_RECONNECTION_FAILED   -3
 Return value for the functions:
  • standard_query
  • sql_update
  • sql_insert
  • sql_delete
  • sql_select
  • get_unix_timestamp
The reconnection process to the MySql server failed.


#define MYSQL_SQL_PROBLEM   -4
 Return value for the functions:
  • standard_query
  • sql_update
  • sql_insert
  • sql_delete
  • sql_select
  • get_unix_timestamp
A SQL error occured.


#define MYSQL_UNEXPECTED_ERROR   -5
 Return value for the functions:
  • standard_query
  • sql_update
  • sql_insert
  • sql_delete
  • sql_select
  • get_unix_timestamp
Unexpected error.


#define MYSQL_NO_TIMESTAMP   -6
 Return value for the functions:
  • get_unix_timestamp
For some strange reasons, the MySql could not return the current timestamp.



Functions

int load_mysql_configuration_from_file (char *path, struct smysql *mysql_info, char *tags[MYSQL_CONFIGURATION_TAG_NUMBER], int(*logger)(const char *file, const char *fmt,...))
 Load MySql configuration from a given configuration file. More...

void mysql_set_logger (int(*logger)(const char *file, const char *fmt,...), struct smysql *mysql_info)
 Assign a logging service to a given MySql handler. More...

void init_mysql_structure (struct smysql *mysql_info)
 Initialise the Mysql management structure. More...

int connect_to_mysql (struct smysql *mysql_info)
 Connect to Mysql server. More...

void disconnect_from_mysql (struct smysql *mysql_info)
 Close the connection with the Mysql server and free allocated memory. More...

int sql_update (struct smysql *mysql_info, char *query)
 Execute an UPDATE SQL statement. More...

int sql_insert (struct smysql *mysql_info, char *query)
 Execute an INSERT SQL statement. More...

int sql_delete (struct smysql *mysql_info, char *query)
 Execute a DELETE SQL statement. More...

int sql_replace (struct smysql *mysql_info, char *query)
 Execute a REPLACE SQL statement. More...

int sql_select (struct smysql *mysql_info, char *query)
 Execute a SELECT SQL statement. More...

time_t get_unix_timestamp (struct smysql *mysql_info, int *error)
 Return the UNIX timestamp of the MySql server. More...


Detailed Description

Header file for the MySql module 'mysql.c'.

Definition in file mysql_header.h.


Function Documentation

int connect_to_mysql struct smysql   mysql_info
 

Connect to Mysql server.

Author:
Denis BEURIVE
Parameters:
mysql_info  Pointer to a Mysql management structure that contains all the configuration for the connection.
Returns:
Upon successful completion, the function returns the value 0. Otherwise it returns the value 1.
Warning:
  • You must call the function init_mysql_structure() first.
  • Then you may:
  • Don't forget to call disconnect_from_mysql() in order to free the allocated memory.
  • We use the option CLIENT_FOUND_ROWS which modify the behavior of the function mysql_affected_raws(). With this option, the function mysql_affected_raws() returns the number of rows that matched the "WHERE" statement. In standard mode, the function mysql_affected_raws() returns the number of rows that have been changed.

Definition at line 149 of file mysql.c.

References clear_error_reporting(), smysql::connect_retry, smysql::connect_sleep, smysql::connect_timeout, smysql::connected, smysql::db, smysql::handler, smysql::host, smysql::log_file, my_fake_syslog(), my_syslog(), smysql::my_syslog, smysql::passwd, smysql::port, set_last_error(), smysql::socket, and smysql::user.

Referenced by main(), sql_select(), and standard_query().

void disconnect_from_mysql struct smysql   mysql_info
 

Close the connection with the Mysql server and free allocated memory.

Author:
Denis BEURIVE
Parameters:
mysql_info  Pointer to a Mysql management structure.

Definition at line 253 of file mysql.c.

References smysql::handler.

Referenced by sql_select(), and standard_query().

time_t get_unix_timestamp struct smysql   mysql_info,
int *    error
 

Return the UNIX timestamp of the MySql server.

Author:
Denis BEURIVE
Parameters:
mysql_info  Pointer to a 'struct smysql' that contains the Mysql configuration.
error  Pointer to an integer used to store an error code. Upon successful completion, the value pointed by 'error' is set to 0. Otherwise, the value pointed by 'error' could be:
  • MYSQL_REQUEST_SKIPED
  • MYSQL_CONNECTION_LOST
  • MYSQL_RECONNECTION_FAILED
  • MYSQL_SQL_PROBLEM
  • MYSQL_UNEXPECTED_ERROR
  • MYSQL_NO_TIMESTAMP
Returns:
Upon successful completion (the value pointed by 'error' is set to 0), then the function returns the current UNIX timestamp of the MySql server. Otherwize, the function returns the value 0.

Definition at line 1261 of file mysql.c.

References smysql::debug, smysql::log_file, my_fake_syslog(), my_syslog(), smysql::my_syslog, MYSQL_CONNECTION_LOST, MYSQL_NO_TIMESTAMP, MYSQL_RECONNECTION_FAILED, MYSQL_REQUEST_SKIPED, MYSQL_SQL_PROBLEM, MYSQL_UNEXPECTED_ERROR, smysql::result, and sql_select().

Referenced by load_pool(), load_vlan(), release_ip_from_pool(), try_find_and_reserve_ip_from_pool(), and try_reserve_ip_from_pool().

void init_mysql_structure struct smysql   mysql_info
 

Initialise the Mysql management structure.

Author:
Denis BEURIVE
Parameters:
mysql_info  Pointer to a Mysql management structure.

Definition at line 114 of file mysql.c.

References clear_error_reporting(), smysql::connected, smysql::handler, smysql::last_connection_timestamp, smysql::num_fields, smysql::num_rows, and smysql::result.

Referenced by main().

int load_mysql_configuration_from_file char *    path,
struct smysql   mysql_info,
char *    tags[MYSQL_CONFIGURATION_TAG_NUMBER],
int(*    logger)(const char *file, const char *fmt,...)
 

Load MySql configuration from a given configuration file.

Author:
Denis BEURIVE
Parameters:
path  Path to the configuration file to parse.
mysql_info  Pointer to a MySql configuration data structure unsed to store the loaded configuration.
tags  Pointer to an array of zero terminated strinfs of characters. Each string represents one tag to load from the configuration file. Tags must be written in the following order:
  • 0 - Host name of the server that runs the database server.
  • 1 - User used to log to the database server.
  • 2 - Password used to log to the database server.
  • 3 - Name of the database.
  • 4 - TCP port number use to connect to the database server.
  • 5 - Path to the UNIX socket used for local connection to the database server.
  • 6 - Number of connection retry.
  • 7 - Connection timeout in seconds.
  • 8 - Delay of inactivity between 2 connection attempts.
  • 9 - Debug verbosity level.
  • 10 - Path to the log file used to log debug messages.
  • 11 - Number of seconds between 2 reconnection attempts.

The size of the array is MYSQL_CONFIGURATION_TAG_NUMBER.

Parameters:
logger  Pointer to a logging function used to log message, if required. The function' signature must be:

int (*logger) (const char *file, const char * fmt,...)

If NULL, then logging is not activated (no matter if a log file is defined).

Returns:
Upon successful completion, the function returns the value 0. Otherwize, the function returns the value 1.
Warning:
If you do not want to specify a log file in the configuration file, then set the value of the tag "log-file" (index 10 in the array 'tags') to "desactivate logs".

Definition at line 786 of file mysql.c.

References clear_error_reporting(), smysql::connect_retry, smysql::connect_sleep, smysql::connect_timeout, smysql::db, DB_HOST_MAX_SIZE, DB_NAME_MAX_SIZE, DB_PASSWD_MAX_SIZE, DB_USER_MAX_SIZE, smysql::debug, get_value(), smysql::host, smysql::last_error_str, smysql::log_file, my_fake_syslog(), my_syslog(), smysql::my_syslog, MYSQL_CONFIGURATION_TAG_NUMBER, MYSQL_DEBUG_FILE_MAX_SIZE, MYSQL_MAX_ERROR_SIZE, mysql_set_logger(), MYSQL_SOCKET_FILE_MAX_SIZE, parse_configuration_file(), PARSE_CONFIGURATION_OK, smysql::passwd, smysql::port, smysql::reconnection_delay, smysql::socket, and smysql::user.

Referenced by main().

void mysql_set_logger int(*    logger)(const char *file, const char *fmt,...),
struct smysql   mysql_info
 

Assign a logging service to a given MySql handler.

Author:
Denis BEURIVE
Parameters:
logger  Pointer to a guven logging service.
mysql_info  Pointer to a MySql handler.
Remarks:
To desactivate logging, just set the value of 'logger' to NULL.
Warning:
Unless you call load_mysql_configuration_from_file(), you MUST call mysql_set_logger() first! Failing to initialize the logging service may lead to process segmentation fault.

Definition at line 48 of file mysql.c.

References smysql::my_syslog.

Referenced by load_mysql_configuration_from_file().

int sql_delete struct smysql   mysql_info,
char *    query
 

Execute a DELETE SQL statement.

Author:
Denis BEURIVE
Parameters:
mysql_info  Pointer to a Mysql management structure.
query  SQL statement to execute.
Returns:
Upon successful completion, the function returns the number of rows matched by the "WHERE" statement. Otherwize, the function may return one of the following values:
  • MYSQL_REQUEST_SKIPED
  • MYSQL_CONNECTION_LOST
  • MYSQL_RECONNECTION_FAILED
  • MYSQL_SQL_PROBLEM
  • MYSQL_UNEXPECTED_ERROR
Warning:
Keep in mind that the function does __NOT__ return the number of rows that have been changed by the request. The function returns the number of rows that matched the "WHERE" statement (which may be different).

Definition at line 542 of file mysql.c.

References standard_query().

Referenced by send_to_mydns(), and send_to_radonline().

int sql_insert struct smysql   mysql_info,
char *    query
 

Execute an INSERT SQL statement.

Author:
Denis BEURIVE
Parameters:
mysql_info  Pointer to a Mysql management structure.
query  SQL statement to execute.
Returns:
Upon successful completion, the function returns the number of rows matched by the "WHERE" statement. Otherwize, the function may return one of the following values:
  • MYSQL_REQUEST_SKIPED
  • MYSQL_CONNECTION_LOST
  • MYSQL_RECONNECTION_FAILED
  • MYSQL_SQL_PROBLEM
  • MYSQL_UNEXPECTED_ERROR
Warning:
Keep in mind that the function does __NOT__ return the number of rows that have been changed by the request. The function returns the number of rows that matched the "WHERE" statement (which may be different).

Definition at line 500 of file mysql.c.

References standard_query().

Referenced by send_to_mydns(), send_to_radonline(), and signal_unknown_mac_address().

int sql_replace struct smysql   mysql_info,
char *    query
 

Execute a REPLACE SQL statement.

Author:
Denis BEURIVE
Parameters:
mysql_info  Pointer to a Mysql management structure.
query  SQL statement to execute.
Returns:
Upon successful completion, the function returns the number of rows matched by the "WHERE" statement. Otherwize, the function may return one of the following values:
  • MYSQL_REQUEST_SKIPED
  • MYSQL_CONNECTION_LOST
  • MYSQL_RECONNECTION_FAILED
  • MYSQL_SQL_PROBLEM
  • MYSQL_UNEXPECTED_ERROR
Warning:
Keep in mind that the function does __NOT__ return the number of rows that have been changed by the request. The function returns the number of rows that matched the "WHERE" statement (which may be different).

Definition at line 521 of file mysql.c.

References standard_query().

int sql_select struct smysql   mysql_info,
char *    query
 

Execute a SELECT SQL statement.

Author:
Denis BEURIVE
Parameters:
mysql_info  Pointer to a Mysql management structure.
query  SQL statement to execute.
Returns:
Upon successful completion, the function returns the number of selected items. Otherwize, the function may return one of the following values:
  • MYSQL_REQUEST_SKIPED
  • MYSQL_CONNECTION_LOST
  • MYSQL_RECONNECTION_FAILED
  • MYSQL_SQL_PROBLEM
  • MYSQL_UNEXPECTED_ERROR
Warning:
Upon successful completion, the fonction stores results into the MySql data structure mysql_info->result. You must call the function mysql_free_result() in order to free all allocated memory. Note that if an error occured, you must __NOT__ call mysql_free_result().

Definition at line 563 of file mysql.c.

References clear_error_reporting(), connect_to_mysql(), smysql::connected, copy_error(), disconnect_from_mysql(), smysql::handler, smysql::last_connection_timestamp, smysql::last_error_int, smysql::last_error_str, smysql::log_file, my_fake_syslog(), my_syslog(), smysql::my_syslog, MYSQL_RECONNECTION_FAILED, MYSQL_REQUEST_SKIPED, MYSQL_SQL_PROBLEM, MYSQL_UNEXPECTED_ERROR, smysql::num_fields, smysql::num_rows, smysql::reconnection_delay, smysql::result, and set_last_error().

Referenced by get_unix_timestamp(), load_abonne_ip(), load_abonnes(), load_ip_lease(), load_logistic(), load_options(), load_pool(), load_vlan(), and mydns_init().

int sql_update struct smysql   mysql_info,
char *    query
 

Execute an UPDATE SQL statement.

Author:
Denis BEURIVE
Parameters:
mysql_info  Pointer to a Mysql management structure.
query  SQL statement to execute.
Returns:
Upon successful completion, the function returns the number of rows matched by the "WHERE" statement. Otherwize, the function may return one of the following values:
  • MYSQL_REQUEST_SKIPED
  • MYSQL_CONNECTION_LOST
  • MYSQL_RECONNECTION_FAILED
  • MYSQL_SQL_PROBLEM
  • MYSQL_UNEXPECTED_ERROR
Warning:
Keep in mind that the function does __NOT__ return the number of rows that have been changed by the request. The function returns the number of rows that matched the "WHERE" statement (which may be different).

Definition at line 479 of file mysql.c.

References standard_query().

Referenced by blackhole_abonne(), nack_flag_abonne(), release_ip_from_pool(), remove_autoconf_bh(), set_flag_check_radius(), try_find_and_reserve_ip_from_pool(), and try_reserve_ip_from_pool().


Generated on Mon Jun 19 12:31:06 2006 for MyDhcp_V2 by doxygen1.2.15