Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

mysql.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <mysql/errmsg.h>
#include "config.h"
#include "mysql_header.h"
#include "strings_utils.h"
#include "mysqld_error.h"

Go to the source code of this file.

Functions

void set_last_error (struct smysql *mysql_info)
 Safely copy error message from MySql. More...

void clear_error_reporting (struct smysql *mysql_info)
 Clear all error reporting data. More...

void copy_error (struct smysql *mysql_info, char *m)
 Safely copy a specific error message to the error reporting system. This function is intended to set functional error message (that does not have MySql system error defined). More...

int standard_query (struct smysql *mysql_info, char *query)
 Execute SQL statements that does not return data (UPDATE, DELETE, INSERT, REPLACE and otjer). More...

int my_fake_syslog (const char *file, const char *fmt,...)
 This is a handler to a fake loggin service. It is used if no logging service is specified in the MySql configuration handler. 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_replace (struct smysql *mysql_info, char *query)
 Execute a REPLACE SQL statement. More...

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

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

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...

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


Variables

char mysql_error_str [MYSQL_MAX_ERROR_SIZE]
 Last MySql error message. This variable is defined in the file 'mysql.c'.

int mysql_error_errno
 Last MySql error code. This variable is defined in the file 'mysql.c'.


Detailed Description

Simple interface to MySql.

Definition in file mysql.c.


Function Documentation

void clear_error_reporting struct smysql   mysql_info [static]
 

Clear all error reporting data.

Author:
Denis BEURIVE

Definition at line 55 of file mysql.c.

References smysql::last_error_int, smysql::last_error_str, mysql_error_errno, mysql_error_str, and MYSQL_MAX_ERROR_SIZE.

Referenced by connect_to_mysql(), copy_error(), init_mysql_structure(), load_mysql_configuration_from_file(), set_last_error(), sql_select(), and standard_query().

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(), smysql::my_syslog, my_syslog(), smysql::passwd, smysql::port, set_last_error(), smysql::socket, and smysql::user.

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

void copy_error struct smysql   mysql_info,
char *    m
[static]
 

Safely copy a specific error message to the error reporting system. This function is intended to set functional error message (that does not have MySql system error defined).

Author:
Denis BEURIVE
Parameters:
mysql_info  mysql_info Pointer to a Mysql management structure.
m  Pointer to zero terminated string of characters that represents the error message to report.

Definition at line 99 of file mysql.c.

References clear_error_reporting(), smysql::last_error_int, smysql::last_error_str, mysql_error_errno, mysql_error_str, and MYSQL_MAX_ERROR_SIZE.

Referenced by 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(), smysql::my_syslog, 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(), smysql::my_syslog, 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().

int my_fake_syslog const char *    file,
const char *    fmt,
...   
[static]
 

This is a handler to a fake loggin service. It is used if no logging service is specified in the MySql configuration handler.

Author:
Denis BEURIVE

Definition at line 37 of file mysql.c.

Referenced by connect_to_mysql(), get_unix_timestamp(), load_mysql_configuration_from_file(), sql_select(), and standard_query().

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().

void set_last_error struct smysql   mysql_info [static]
 

Safely copy error message from MySql.

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

Definition at line 68 of file mysql.c.

References clear_error_reporting(), smysql::handler, smysql::last_error_int, smysql::last_error_str, mysql_error_errno, mysql_error_str, and MYSQL_MAX_ERROR_SIZE.

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

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(), smysql::my_syslog, 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().

int standard_query struct smysql   mysql_info,
char *    query
[static]
 

Execute SQL statements that does not return data (UPDATE, DELETE, INSERT, REPLACE and otjer).

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).
  • If the connection to the MySql server is lost, then the client tries to reconnect. If it can not reconnect, then the function returns on failure.

Definition at line 278 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_CONNECTION_LOST, MYSQL_RECONNECTION_FAILED, MYSQL_REQUEST_SKIPED, MYSQL_SQL_PROBLEM, MYSQL_UNEXPECTED_ERROR, smysql::reconnection_delay, and set_last_error().

Referenced by sql_delete(), sql_insert(), sql_replace(), and sql_update().


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