| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
database.c - description |
/* |
| 3 |
------------------- |
* database |
| 4 |
begin : Mon Oct 11 2004 |
* - configuration and function of DB connection |
| 5 |
copyright : (C) 2004 by Leaflet |
* |
| 6 |
email : leaflet@leafok.com |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
***************************************************************************/ |
*/ |
|
|
|
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "common.h" |
#include "common.h" |
| 10 |
#include <mysql.h> |
#include "database.h" |
| 11 |
|
#include "log.h" |
| 12 |
#include <stdio.h> |
#include <stdio.h> |
| 13 |
|
#include <mysql/mysql.h> |
| 14 |
|
|
| 15 |
MYSQL * |
// Global declaration for database |
| 16 |
db_open () |
char DB_host[DB_host_max_len + 1]; |
| 17 |
|
char DB_username[DB_username_max_len + 1]; |
| 18 |
|
char DB_password[DB_password_max_len + 1]; |
| 19 |
|
char DB_database[DB_database_max_len + 1]; |
| 20 |
|
char DB_timezone[DB_timezone_max_len + 1]; |
| 21 |
|
|
| 22 |
|
MYSQL *db_open() |
| 23 |
{ |
{ |
| 24 |
MYSQL *db; |
MYSQL *db = NULL; |
| 25 |
|
char sql[SQL_BUFFER_LEN]; |
| 26 |
db = mysql_init(NULL); |
|
| 27 |
if (db == NULL) |
db = mysql_init(NULL); |
| 28 |
{ |
if (db == NULL) |
| 29 |
log_error("mysql_init() failed\n"); |
{ |
| 30 |
return NULL; |
log_error("mysql_init() failed\n"); |
| 31 |
} |
return NULL; |
| 32 |
|
} |
| 33 |
db = mysql_real_connect(db, DB_host, DB_username, DB_password, DB_database, |
|
| 34 |
0, NULL, 0); |
if (mysql_real_connect(db, DB_host, DB_username, DB_password, DB_database, |
| 35 |
if (db == NULL) |
0, NULL, 0) == NULL) |
| 36 |
{ |
{ |
| 37 |
log_error("mysql_connect() failed\n"); |
log_error("mysql_real_connect() error: %s\n", mysql_error(db)); |
| 38 |
return NULL; |
mysql_close(db); |
| 39 |
} |
return NULL; |
| 40 |
|
} |
| 41 |
|
|
| 42 |
|
if (mysql_set_character_set(db, "utf8") != 0) |
| 43 |
|
{ |
| 44 |
|
log_error("Set character set error: %s\n", mysql_error(db)); |
| 45 |
|
mysql_close(db); |
| 46 |
|
return NULL; |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
snprintf(sql, sizeof(sql), |
| 50 |
|
"SET time_zone = '%s'", |
| 51 |
|
DB_timezone); |
| 52 |
|
|
| 53 |
|
if (mysql_query(db, sql) != 0) |
| 54 |
|
{ |
| 55 |
|
log_error("Set timezone error: %s\n", mysql_error(db)); |
| 56 |
|
mysql_close(db); |
| 57 |
|
return NULL; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
return db; |
| 61 |
} |
} |