1 /** |
1 /** |
2 * @file mysql.c |
2 * @file mysql.c |
3 * @brief MySQL/MariaDB access. |
3 * @brief MySQL/MariaDB access. |
4 * @author Michiel Broek <mbroek at mbse dot eu> |
4 * @author Michiel Broek <mbroek at mbse dot eu> |
5 * |
5 * |
6 * Copyright (C) 2018-2021 |
6 * Copyright (C) 2018-2022 |
7 * |
7 * |
8 * This file is part of the bms (Brewery Management System) |
8 * This file is part of the bms (Brewery Management System) |
9 * |
9 * |
10 * This is free software; you can redistribute it and/or modify it |
10 * This is free software; you can redistribute it and/or modify it |
11 * under the terms of the GNU General Public License as published by the |
11 * under the terms of the GNU General Public License as published by the |
30 |
30 |
31 MYSQL *con = NULL; |
31 MYSQL *con = NULL; |
32 MYSQL_RES *res_set; |
32 MYSQL_RES *res_set; |
33 MYSQL_ROW row; |
33 MYSQL_ROW row; |
34 |
34 |
|
35 my_bool reconnect = 1; |
|
36 |
35 extern sys_config Config; |
37 extern sys_config Config; |
36 extern sys_node_list *nodes; |
38 extern sys_node_list *nodes; |
37 extern sys_fermenter_list *fermenters; |
39 extern sys_fermenter_list *fermenters; |
38 extern sys_co2meter_list *co2meters; |
40 extern sys_co2meter_list *co2meters; |
39 extern sys_ispindel_list *ispindels; |
41 extern sys_ispindel_list *ispindels; |
69 con = mysql_init(NULL); |
71 con = mysql_init(NULL); |
70 if (con == NULL) { |
72 if (con == NULL) { |
71 syslog(LOG_NOTICE, "MySQL: mysql_init() failed"); |
73 syslog(LOG_NOTICE, "MySQL: mysql_init() failed"); |
72 return 1; |
74 return 1; |
73 } |
75 } |
|
76 |
|
77 mysql_options(con, MYSQL_OPT_RECONNECT, &reconnect); |
74 |
78 |
75 if (mysql_real_connect(con, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) { |
79 if (mysql_real_connect(con, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) { |
76 syslog(LOG_NOTICE, "MySQL: mysql_real_connect() %s", mysql_error(con)); |
80 syslog(LOG_NOTICE, "MySQL: mysql_real_connect() %s", mysql_error(con)); |
77 return 2; |
81 return 2; |
78 } |
82 } |
473 syslog(LOG_NOTICE, query); |
477 syslog(LOG_NOTICE, query); |
474 } else { |
478 } else { |
475 return 0; |
479 return 0; |
476 } |
480 } |
477 |
481 |
478 /* Any error execpt server gone away */ |
|
479 if (err != 2006) |
|
480 return rc; |
|
481 |
|
482 syslog(LOG_NOTICE, "Trying to reconnect"); |
|
483 /* Try to reconnect and do the query again */ |
|
484 mysql_close(con); |
|
485 if (mysql_real_connect(con, Config.mysql_host, Config.mysql_user, Config.mysql_pass, Config.mysql_database, Config.mysql_port, NULL, 0) == NULL) { |
|
486 syslog(LOG_NOTICE, "MySQL: mysql_real_connect() %s", mysql_error(con)); |
|
487 return 2; |
|
488 } |
|
489 syslog(LOG_NOTICE, "MySQL: reconnected."); |
|
490 rc = mysql_query(con, query); |
|
491 if (rc) { |
|
492 syslog(LOG_NOTICE, "MySQL: error %u (%s)", mysql_errno(con), mysql_error(con)); |
|
493 syslog(LOG_NOTICE, query); |
|
494 } |
|
495 return rc; |
482 return rc; |
496 } |
|
497 |
|
498 |
|
499 |
|
500 void bms_mysql_ping(void) |
|
501 { |
|
502 } |
483 } |
503 |
484 |
504 |
485 |
505 |
486 |
506 void node_mysql_insert(sys_node_list *node) |
487 void node_mysql_insert(sys_node_list *node) |