Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : #ifndef _MSETTINGS_H
14 : #define _MSETTINGS_H 1
15 :
16 : #include "mapi.h"
17 : #include <stdbool.h>
18 :
19 : #define MP__BOOL_START (100)
20 : #define MP__LONG_START (200)
21 : #define MP__STRING_START (300)
22 : #define MP__MAX (400)
23 :
24 : #ifdef __cplusplus
25 : extern "C" {
26 : #endif
27 :
28 : /* avoid using "#ifdef WIN32" so that this file does not need our config.h */
29 : #if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__)
30 : #ifndef LIBMAPI
31 : #define mapi_export extern __declspec(dllimport)
32 : #else
33 : #define mapi_export extern __declspec(dllexport)
34 : #endif
35 : #else
36 : #define mapi_export extern
37 : #endif
38 :
39 : /////////////////////////////////////////////////////////////////////
40 : // This enum identifies properties that can be set that affect how a
41 : // connection is made. In particular we have functies to parse strings
42 : // into a an enum value, and back.
43 :
44 : typedef enum mparm {
45 : MP_UNKNOWN,
46 : MP_IGNORE,
47 :
48 : // bool
49 : MP_TLS = MP__BOOL_START,
50 : MP_AUTOCOMMIT,
51 : MP_CLIENT_INFO,
52 : // Note: if you change anything about this enum whatsoever, make sure to
53 : // make the corresponding change to struct msettings in msettings.c as well.
54 :
55 : // long
56 : MP_PORT = MP__LONG_START,
57 : MP_TIMEZONE,
58 : MP_REPLYSIZE,
59 : MP_MAPTOLONGVARCHAR, // specific to ODBC
60 : MP_CONNECT_TIMEOUT,
61 : MP_REPLY_TIMEOUT,
62 : // Note: if you change anything about this enum whatsoever, make sure to
63 : // make the corresponding change to struct msettings in msettings.c as well.
64 :
65 : // string
66 : MP_SOCK = MP__STRING_START,
67 : MP_SOCKDIR,
68 : MP_CERT,
69 : MP_CLIENTKEY,
70 : MP_CLIENTCERT,
71 : MP_HOST,
72 : MP_DATABASE,
73 : MP_TABLESCHEMA,
74 : MP_TABLE,
75 : MP_CERTHASH,
76 : MP_USER,
77 : MP_PASSWORD,
78 : MP_LANGUAGE,
79 : MP_SCHEMA, // TODO implement this
80 : MP_BINARY,
81 : MP_LOGFILE,
82 : MP_CLIENT_APPLICATION,
83 : MP_CLIENT_REMARK,
84 : // Note: if you change anything about this enum whatsoever, make sure to
85 : // make the corresponding change to struct msettings in msettings.c as well.
86 :
87 : // !! Make sure to keep them all below MP__MAX !!
88 : } mparm;
89 :
90 : typedef enum mparm_class {
91 : MPCLASS_BOOL,
92 : MPCLASS_LONG,
93 : MPCLASS_STRING,
94 : } mparm_class;
95 :
96 : static inline mparm_class
97 95618 : mparm_classify(mparm parm)
98 : {
99 95618 : if (parm < MP__LONG_START)
100 : return MPCLASS_BOOL;
101 85894 : else if (parm >= MP__STRING_START)
102 : return MPCLASS_STRING;
103 : else
104 49483 : return MPCLASS_LONG;
105 : }
106 :
107 :
108 : /* returns NULL if not found, pointer to mparm if found */
109 : mapi_export mparm mparm_parse(const char *name);
110 : mapi_export const char *mparm_name(mparm parm);
111 : mapi_export mparm mparm_enumerate(int i);
112 : mapi_export bool mparm_is_core(mparm parm);
113 :
114 :
115 : /////////////////////////////////////////////////////////////////////
116 : // This type hold all properties that can be set that affect how a
117 : // connection is made. There are methods to create/destroy etc.,
118 : // getters and setters based on enum mparm above, and getters
119 : // and setters based on string values.
120 : // Also, msettings_validate, msettings_parse_url and a number
121 : // of helper functions.
122 :
123 : typedef struct msettings msettings;
124 :
125 : /* NULL means OK. non-NULL is error message. Valid until next call. Do not free. */
126 : typedef const char *msettings_error;
127 : mapi_export bool msettings_malloc_failed(msettings_error err);
128 :
129 : /* returns NULL if could not allocate */
130 : mapi_export msettings *msettings_create(void);
131 : mapi_export msettings *msettings_clone(const msettings *mp);
132 : mapi_export void msettings_reset(msettings *mp);
133 : mapi_export const msettings *msettings_default;
134 :
135 : /* always returns NULL */
136 : mapi_export msettings *msettings_destroy(msettings *mp);
137 :
138 : mapi_export const char *msetting_parm_name(const msettings *mp, mparm parm);
139 : mapi_export void msettings_set_localizer(msettings *mp, const char* (*localizer)(const void *data, mparm parm), void *data);
140 :
141 : /* retrieve and set; call abort() on type error */
142 :
143 : mapi_export const char* msetting_string(const msettings *mp, mparm parm);
144 : mapi_export msettings_error msetting_set_string(msettings *mp, mparm parm, const char* value)
145 : __attribute__((__nonnull__(3)));
146 :
147 : mapi_export long msetting_long(const msettings *mp, mparm parm);
148 : mapi_export msettings_error msetting_set_long(msettings *mp, mparm parm, long value);
149 :
150 : mapi_export bool msetting_bool(const msettings *mp, mparm parm);
151 : mapi_export msettings_error msetting_set_bool(msettings *mp, mparm parm, bool value);
152 :
153 : /* parse into the appropriate type, or format into newly malloc'ed string (NULL means malloc failed) */
154 : mapi_export msettings_error msetting_parse(msettings *mp, mparm parm, const char *text);
155 : mapi_export char *msetting_as_string(const msettings *mp, mparm parm);
156 :
157 : /* store ignored parameter */
158 : mapi_export msettings_error msetting_set_ignored(msettings *mp, const char *key, const char *value);
159 :
160 : /* store named parameter */
161 : mapi_export msettings_error msetting_set_named(msettings *mp, bool allow_core, const char *key, const char *value);
162 :
163 : /* update the msettings from the URL. set *error_buffer to NULL and return true
164 : * if success, set *error_buffer to malloc'ed error message and return false on failure.
165 : * if return value is true but *error_buffer is NULL, malloc failed. */
166 : mapi_export bool msettings_parse_url(msettings *mp, const char *url, char **error_buffer);
167 :
168 : /* 1 = true, 0 = false, -1 = could not parse */
169 : mapi_export int msetting_parse_bool(const char *text);
170 :
171 : /* return an error message if the validity rules are not satisfied */
172 : mapi_export bool msettings_validate(msettings *mp, char **errmsg);
173 :
174 :
175 : /* virtual parameters */
176 : enum msetting_tls_verify {
177 : verify_none,
178 : verify_system,
179 : verify_cert,
180 : verify_hash,
181 : };
182 : mapi_export bool msettings_connect_scan(const msettings *mp);
183 : mapi_export const char *msettings_connect_unix(const msettings *mp);
184 : mapi_export const char *msettings_connect_tcp(const msettings *mp);
185 : mapi_export long msettings_connect_port(const msettings *mp);
186 : mapi_export const char *msettings_connect_certhash_digits(const msettings *mp);
187 : mapi_export long msettings_connect_binary(const msettings *mp);
188 : mapi_export enum msetting_tls_verify msettings_connect_tls_verify(const msettings *mp);
189 : mapi_export const char *msettings_connect_clientkey(const msettings *mp);
190 : mapi_export const char *msettings_connect_clientcert(const msettings *mp);
191 :
192 : /* automatically incremented each time the corresponding field is updated */
193 : long msettings_user_generation(const msettings *mp);
194 : long msettings_password_generation(const msettings *mp);
195 :
196 : /* convenience helpers*/
197 : bool msettings_lang_is_mal(const msettings *mp);
198 : bool msettings_lang_is_sql(const msettings *mp);
199 : bool msettings_lang_is_profiler(const msettings *mp);
200 :
201 : /////////////////////////////////////////////////////////////////////
202 : // Extend mapi.h
203 :
204 : // Mutable access settings of existing Mapi.
205 : // Do not make changes while connected.
206 : mapi_export msettings *mapi_get_settings(Mapi mid)
207 : __attribute__((__nonnull__(1)));
208 :
209 : // Create Mapi from settings.
210 : // Takes ownership of the settings except if malloc fails etc.
211 : // In that case NULL is returned and ownership of the settings remains with
212 : // the caller.
213 : mapi_export Mapi mapi_settings(msettings *settings)
214 : __attribute__((__nonnull__(1)));
215 :
216 :
217 : #ifdef __cplusplus
218 : }
219 : #endif
220 : #endif /* _MSETTINGS_H */
|