1 /*
2  * Copyright (c) 2002-2012 Balabit
3  * Copyright (c) 1998-2012 Balázs Scheidler
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * As an additional exemption you are allowed to compile & link against the
19  * OpenSSL libraries as published by the OpenSSL project. See the file
20  * COPYING for details.
21  *
22  */
23 
24 #ifndef AFSQL_H_INCLUDED
25 #define AFSQL_H_INCLUDED
26 
27 #include "logthrdest/logthrdestdrv.h"
28 #include "mainloop-worker.h"
29 #include "string-list.h"
30 
31 #include <dbi.h>
32 
33 enum
34 {
35   AFSQL_COLUMN_DEFAULT = 1,
36 };
37 
38 /* field flags */
39 enum
40 {
41   AFSQL_FF_DEFAULT = 0x0001,
42 };
43 
44 /* destination driver flags */
45 enum
46 {
47   AFSQL_DDF_EXPLICIT_COMMITS = 0x0001,
48   AFSQL_DDF_DONT_CREATE_TABLES = 0x0002,
49 };
50 
51 typedef struct _AFSqlField
52 {
53   guint32 flags;
54   gchar *name;
55   gchar *type;
56   LogTemplate *value;
57 } AFSqlField;
58 
59 /**
60  * AFSqlDestDriver:
61  *
62  * This structure encapsulates an SQL destination driver. SQL insert
63  * statements are generated from a separate thread because of the blocking
64  * nature of the DBI API. It is ensured that while the thread is running,
65  * the reference count to the driver structure is increased, thus the db
66  * thread can read any of the fields in this structure. To do anything more
67  * than simple reading out a value, some kind of locking mechanism shall be
68  * used.
69  **/
70 typedef struct _AFSqlDestDriver
71 {
72   LogThreadedDestDriver super;
73   /* read by the db thread */
74   gchar *type;
75   gchar *host;
76   gchar *port;
77   gchar *user;
78   gchar *password;
79   gchar *database;
80   gchar *encoding;
81   gchar *create_statement_append;
82   GList *columns;
83   GList *values;
84   GList *indexes;
85   LogTemplate *table;
86   gint fields_len;
87   AFSqlField *fields;
88   gchar *null_value;
89   gint flags;
90   gboolean ignore_tns_config;
91   GList *session_statements;
92 
93   LogTemplateOptions template_options;
94 
95   GHashTable *dbd_options;
96   GHashTable *dbd_options_numeric;
97 
98   /* used exclusively by the db thread */
99   dbi_conn dbi_ctx;
100   GHashTable *syslogng_conform_tables;
101   guint32 failed_message_counter;
102   gboolean transaction_active;
103 } AFSqlDestDriver;
104 
105 
106 void afsql_dd_set_type(LogDriver *s, const gchar *type);
107 void afsql_dd_set_host(LogDriver *s, const gchar *host);
108 gboolean afsql_dd_check_port(const gchar *port);
109 void afsql_dd_set_port(LogDriver *s, const gchar *port);
110 void afsql_dd_set_user(LogDriver *s, const gchar *user);
111 void afsql_dd_set_password(LogDriver *s, const gchar *password);
112 void afsql_dd_set_database(LogDriver *s, const gchar *database);
113 void afsql_dd_set_table(LogDriver *s, LogTemplate *table_template);
114 void afsql_dd_set_columns(LogDriver *s, GList *columns);
115 void afsql_dd_set_values(LogDriver *s, GList *values);
116 void afsql_dd_set_null_value(LogDriver *s, const gchar *null);
117 void afsql_dd_set_indexes(LogDriver *s, GList *indexes);
118 void afsql_dd_set_session_statements(LogDriver *s, GList *session_statements);
119 void afsql_dd_set_flags(LogDriver *s, gint flags);
120 void afsql_dd_set_create_statement_append(LogDriver *s, const gchar *create_statement_append);
121 LogDriver *afsql_dd_new(GlobalConfig *cfg);
122 gint afsql_dd_lookup_flag(const gchar *flag);
123 void afsql_dd_add_dbd_option(LogDriver *s, const gchar *name, const gchar *value);
124 void afsql_dd_add_dbd_option_numeric(LogDriver *s, const gchar *name, gint value);
125 void afsql_dd_set_ignore_tns_config(LogDriver *s, const gboolean ignore_tns_config);
126 
127 #endif
128