MeteoIODoc 20241221.207bde49
libMysqlWrapper.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2/***********************************************************************************/
3/* Copyright 2022 WSL Institute for Snow and Avalanche Research SLF-DAVOS */
4/***********************************************************************************/
5/* This file is part of MeteoIO.
6 MeteoIO is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 MeteoIO is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with MeteoIO. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef LIBMYSQLWRAPPER_H
20#define LIBMYSQLWRAPPER_H
21
22#ifdef _WIN32
23 #include <winsock.h>
24#endif // _WIN32
25
26#include <mysql.h>
27#include <cstring>
29#include <meteoio/IOUtils.h>
30
31static const int MYSQL_STRING_SIZE = 50;
32
33class SQL_FIELD {
34 public:
35 #if LIBMYSQL_VERSION_ID > 80001
36 typedef bool BOOL_TYPE;
37 #else
38 typedef my_bool BOOL_TYPE;
39 #endif
40
47 };
48
49 SQL_FIELD(const std::string& i_param, const enum_field_types &type, const unsigned int &i_processing=0);
50 SQL_FIELD(const std::string& i_str, const std::string& i_param="", const unsigned int &i_processing=0);
51 SQL_FIELD(const mio::Date& i_dt, const std::string& i_param="", const unsigned int &i_processing=0);
52
53 void resetDate();
54 void reset();
55 void setFromDate(const mio::Date& i_dt, MYSQL_TIME &ts);
56 void setString(const std::string& i_str);
57 void setDate(const mio::Date& i_dt);
58 void setDouble(const double& i_val);
59 mio::Date getDate(const double& TZ) const;
60
61 //several members could be const, but we need a working '=' operator in the plugin for easier code...
62 std::string param;
64 MYSQL_TIME dt;
65 unsigned long int str_len;
66 unsigned long int buffer_len;
67 double val;
68 unsigned int processing;
70 bool isDate;
71 enum_field_types MysqlType;
72};
73
74namespace mysql_wrp {
77 ENCRYPTION = 1 << 1
78 };
79
80 MYSQL* initMysql(const std::string& mysqlhost, const std::string& mysqluser, const std::string& mysqlpass, const std::string& mysqldb, const unsigned int& options=0);
81 MYSQL_STMT* initStmt(MYSQL **mysql, const std::string& query, const long unsigned int& ref_param_count);
82
83 void bindParams(MYSQL_STMT **stmt, std::vector<SQL_FIELD> &params_fields);
84 void bindResults(MYSQL_STMT **stmt, std::vector<SQL_FIELD> &result_fields);
85 double retrieveData(const SQL_FIELD &field, const unsigned int& conversion=SQL_FIELD::NONE);
86}
87
88#endif
Definition: libMysqlWrapper.h:33
void setFromDate(const mio::Date &i_dt, MYSQL_TIME &ts)
Definition: libMysqlWrapper.cc:63
void setString(const std::string &i_str)
Definition: libMysqlWrapper.cc:78
void resetDate()
Definition: libMysqlWrapper.cc:43
bool isDate
for MeteoIO to quickly identify datetime fields
Definition: libMysqlWrapper.h:70
double val
for MySQL to store double data
Definition: libMysqlWrapper.h:67
void reset()
Definition: libMysqlWrapper.cc:54
MYSQL_TIME dt
for MySQL to store datetime data
Definition: libMysqlWrapper.h:64
void setDate(const mio::Date &i_dt)
Definition: libMysqlWrapper.cc:86
unitsConversions
Definition: libMysqlWrapper.h:41
@ CM_TO_M
Definition: libMysqlWrapper.h:44
@ NORMALIZE_PC
Definition: libMysqlWrapper.h:45
@ C_TO_K
Definition: libMysqlWrapper.h:43
@ HPA_TO_PA
Definition: libMysqlWrapper.h:46
@ NONE
Definition: libMysqlWrapper.h:42
BOOL_TYPE is_null
Definition: libMysqlWrapper.h:69
SQL_FIELD(const std::string &i_param, const enum_field_types &type, const unsigned int &i_processing=0)
Definition: libMysqlWrapper.cc:27
my_bool BOOL_TYPE
Definition: libMysqlWrapper.h:38
BOOL_TYPE error
for MySQL ro report a NULL value or errors
Definition: libMysqlWrapper.h:69
mio::Date getDate(const double &TZ) const
Definition: libMysqlWrapper.cc:100
unsigned int processing
what kind of corrections to apply to the raw data (such as C to K conversion, etc
Definition: libMysqlWrapper.h:68
void setDouble(const double &i_val)
Definition: libMysqlWrapper.cc:93
char str[MYSQL_STRING_SIZE]
for MySQL to store string data
Definition: libMysqlWrapper.h:63
unsigned long int str_len
for MySQL, length of a string
Definition: libMysqlWrapper.h:65
std::string param
the parameter name for MeteoIO
Definition: libMysqlWrapper.h:62
unsigned long int buffer_len
for MySQL, allocated data buffer length
Definition: libMysqlWrapper.h:66
enum_field_types MysqlType
for MySQL, data type of the field (see mysql/field_types.h)
Definition: libMysqlWrapper.h:71
A class to handle timestamps. This class handles conversion between different time display formats (I...
Definition: Date.h:87
static const int MYSQL_STRING_SIZE
Definition: libMysqlWrapper.h:31
Definition: libMysqlWrapper.cc:113
void bindParams(MYSQL_STMT **stmt, std::vector< SQL_FIELD > &params_fields)
Definition: libMysqlWrapper.cc:150
void bindResults(MYSQL_STMT **stmt, std::vector< SQL_FIELD > &result_fields)
Definition: libMysqlWrapper.cc:178
MysqlOptions
Definition: libMysqlWrapper.h:75
@ COMPRESSION
Definition: libMysqlWrapper.h:76
@ ENCRYPTION
Definition: libMysqlWrapper.h:77
MYSQL * initMysql(const std::string &mysqlhost, const std::string &mysqluser, const std::string &mysqlpass, const std::string &mysqldb, const unsigned int &options)
Definition: libMysqlWrapper.cc:115
double retrieveData(const SQL_FIELD &field, const unsigned int &conversion)
Definition: libMysqlWrapper.cc:213
MYSQL_STMT * initStmt(MYSQL **mysql, const std::string &query, const long unsigned int &ref_param_count)
Definition: libMysqlWrapper.cc:135