| 1 |
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
| 2 |
/*
|
| 3 |
* database
|
| 4 |
* - configuration and function of DB connection
|
| 5 |
*
|
| 6 |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
|
| 7 |
*/
|
| 8 |
|
| 9 |
#include "common.h"
|
| 10 |
#include "database.h"
|
| 11 |
#include "log.h"
|
| 12 |
#include <stdio.h>
|
| 13 |
#include <mysql/mysql.h>
|
| 14 |
|
| 15 |
// Global declaration for database
|
| 16 |
char DB_host[256];
|
| 17 |
char DB_username[50];
|
| 18 |
char DB_password[50];
|
| 19 |
char DB_database[50];
|
| 20 |
char DB_timezone[50];
|
| 21 |
|
| 22 |
MYSQL *db_open()
|
| 23 |
{
|
| 24 |
MYSQL *db = NULL;
|
| 25 |
char sql[SQL_BUFFER_LEN];
|
| 26 |
|
| 27 |
db = mysql_init(NULL);
|
| 28 |
if (db == NULL)
|
| 29 |
{
|
| 30 |
log_error("mysql_init() failed\n");
|
| 31 |
return NULL;
|
| 32 |
}
|
| 33 |
|
| 34 |
if (mysql_real_connect(db, DB_host, DB_username, DB_password, DB_database,
|
| 35 |
0, NULL, 0) == NULL)
|
| 36 |
{
|
| 37 |
log_error("mysql_real_connect() error: %s\n", mysql_error(db));
|
| 38 |
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 |
}
|