import.c: Check if host is interesting in case no service is
[nagios-reports-module.git] / sql.c
blob056d4434b72c581a778f4b1da24b48eb89c04c95
1 #include "ndbneb.h"
2 #include <stdarg.h>
3 #include <stdio.h>
4 #include "sql.h"
5 #include "logging.h"
7 static struct {
8 char *host;
9 char *name;
10 char *user;
11 char *pass;
12 char *table;
13 unsigned int port;
14 MYSQL *link;
15 } db;
17 #undef ESC_BUFSIZE
18 #define ESC_BUFSIZE (8192 * 2)
19 #undef ESC_BUFS
20 #define ESC_BUFS 8 /* must be a power of 2 */
21 #define MAX_ESC_STRING ((ESC_BUFSIZE * 2) + 1)
23 #define esc(s) sql_escape(s)
24 char *sql_escape(const char *str)
26 static int idx = 0;
27 static char buf_ary[ESC_BUFS][ESC_BUFSIZE];
28 char *buf;
29 int len;
31 if (!str || !*str)
32 return "";
34 len = strlen(str);
36 if (len >= MAX_ESC_STRING) {
37 lerr("len > MAX_ESC_STRING in sql_escape (%d > %d)", len, MAX_ESC_STRING);
38 return "";
41 buf = buf_ary[idx++ & (ESC_BUFS - 1)];
42 idx &= ESC_BUFS - 1;
43 mysql_real_escape_string(db.link, buf, str, len);
45 return buf;
49 * these two functions are only here to allow callers
50 * access to error reporting without having to expose
51 * the db-link to theh callers. It's also nifty as we
52 * want to remain database layer agnostic
54 const char *sql_error()
56 return mysql_error(db.link);
59 int sql_errno(void)
61 return mysql_errno(db.link);
64 SQL_RESULT *sql_get_result(void)
66 return mysql_use_result(db.link);
69 SQL_ROW sql_fetch_row(MYSQL_RES *result)
71 return mysql_fetch_row(result);
74 void sql_free_result(SQL_RESULT *result)
76 mysql_free_result(result);
79 int sql_query(const char *fmt, ...)
81 char *query;
82 int len, result = 0;
83 va_list ap;
85 va_start(ap, fmt);
86 len = vasprintf(&query, fmt, ap);
87 va_end(ap);
89 if (len == -1 || !query) {
90 linfo("sql_query: Failed to build query from format-string '%s'", fmt);
91 return -1;
93 if ((result = mysql_real_query(db.link, query, len)))
94 linfo("mysql_query(): Failed to run [%s]: %s",
95 query, mysql_error(db.link));
97 free(query);
99 return result;
102 int sql_init(void)
104 if (!(db.link = mysql_init(NULL)))
105 return -1;
107 if (!db.host) {
108 db.host = "";
109 db.port = 0;
112 if (!(mysql_real_connect(db.link, db.host, db.user, db.pass,
113 db.name, db.port, NULL, 0)))
115 lerr("Failed to connect to '%s' at '%s':'%d' using %s:%s as credentials: %s",
116 db.name, db.host, db.port, db.user, db.pass, mysql_error(db.link));
118 db.link = NULL;
119 return -1;
122 return 0;
125 int sql_close(void)
127 mysql_close(db.link);
128 return 0;
131 const char *sql_table_name(void)
133 if (!db.table)
134 return "report_data";
136 return db.table;
139 int sql_config(const char *key, const char *value)
141 if (!prefixcmp(key, "db_database"))
142 db.name = strdup(value);
143 else if (!prefixcmp(key, "db_user"))
144 db.user = strdup(value);
145 else if (!prefixcmp(key, "db_pass"))
146 db.pass = strdup(value);
147 else if (!prefixcmp(key, "db_host"))
148 db.host = strdup(value);
149 else if (!prefixcmp(key, "db_table")) {
150 db.table = strdup(value);
152 else if (!prefixcmp(key, "db_port")) {
153 char *endp;
155 db.port = (unsigned int)strtoul(value, &endp, 0);
157 if (endp == value || *endp != 0)
158 return -1;
160 else
161 return -1; /* config error */
163 return 0;