Further non-Unicode fixes; LoraWAN subagent refactoring
[public/netxms.git] / include / nxdbapi.h
CommitLineData
8b1de1cc
AK
1/*
2** NetXMS - Network Management System
3** DB Library
4** Copyright (C) 2003-2011 Victor Kirhenshtein
5**
6** This program 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** This program 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 General Public License for more details.
15**
16** You should have received a copy of the GNU Lesser General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19**
20** File: nxdbapi.h
21**
22**/
23
24#ifndef _nxdbapi_h_
25#define _nxdbapi_h_
26
27#ifdef _WIN32
28#ifdef LIBNXDB_EXPORTS
29#define LIBNXDB_EXPORTABLE __declspec(dllexport)
30#else
31#define LIBNXDB_EXPORTABLE __declspec(dllimport)
32#endif
33#else /* _WIN32 */
34#define LIBNXDB_EXPORTABLE
35#endif
36
37#include <dbdrv.h>
38#include <uuid.h>
39
40
41//
42// DB-related constants
43//
44
45#define MAX_DB_LOGIN 64
46#define MAX_DB_PASSWORD 64
47#define MAX_DB_NAME 256
48
25681328
VK
49/**
50 * DB events
51 */
8b1de1cc
AK
52#define DBEVENT_CONNECTION_LOST 0
53#define DBEVENT_CONNECTION_RESTORED 1
54#define DBEVENT_QUERY_FAILED 2
55
56
3c074e25
VP
57/**
58 * Database syntax codes
59 */
8b1de1cc
AK
60#define DB_SYNTAX_MYSQL 0
61#define DB_SYNTAX_PGSQL 1
62#define DB_SYNTAX_MSSQL 2
63#define DB_SYNTAX_ORACLE 3
64#define DB_SYNTAX_SQLITE 4
65#define DB_SYNTAX_DB2 5
66#define DB_SYNTAX_INFORMIX 6
67#define DB_SYNTAX_UNKNOWN -1
68
9c133761
VK
69/**
70 * Database connection structures
71 */
8b1de1cc
AK
72struct db_driver_t;
73typedef db_driver_t * DB_DRIVER;
74
75struct db_handle_t;
76typedef db_handle_t * DB_HANDLE;
77
78struct db_statement_t;
79typedef db_statement_t * DB_STATEMENT;
80
81struct db_result_t;
82typedef db_result_t * DB_RESULT;
83
f17cf019
VK
84struct db_unbuffered_result_t;
85typedef db_unbuffered_result_t * DB_UNBUFFERED_RESULT;
8b1de1cc 86
9c133761
VK
87/**
88 * Pool connection information
89 */
90struct PoolConnectionInfo
91{
92 DB_HANDLE handle;
93 bool inUse;
94 time_t lastAccessTime;
95 time_t connectTime;
e3c5f43a 96 UINT32 usageCount;
9c133761
VK
97 char srcFile[128];
98 int srcLine;
99};
8b1de1cc 100
9c133761 101/**
a4809f58
VK
102 * DB library performance counters
103 */
104struct LIBNXDB_PERF_COUNTERS
105{
106 UINT64 selectQueries;
107 UINT64 nonSelectQueries;
108 UINT64 totalQueries;
109 UINT64 longRunningQueries;
110 UINT64 failedQueries;
111};
112
113/**
9c133761
VK
114 * Functions
115 */
8b1de1cc
AK
116bool LIBNXDB_EXPORTABLE DBInit(DWORD logMsgCode, DWORD sqlErrorMsgCode);
117DB_DRIVER LIBNXDB_EXPORTABLE DBLoadDriver(const TCHAR *module, const TCHAR *initParameters,
526ae8b8 118 bool dumpSQL, void (* fpEventHandler)(DWORD, const WCHAR *, const WCHAR *, bool, void *),
8b1de1cc
AK
119 void *userArg);
120void LIBNXDB_EXPORTABLE DBUnloadDriver(DB_DRIVER driver);
121const char LIBNXDB_EXPORTABLE *DBGetDriverName(DB_DRIVER driver);
85dfb586 122void LIBNXDB_EXPORTABLE DBSetDefaultPrefetchLimit(DB_DRIVER driver, int limit);
8b1de1cc
AK
123
124DB_HANDLE LIBNXDB_EXPORTABLE DBConnect(DB_DRIVER driver, const TCHAR *server, const TCHAR *dbName,
125 const TCHAR *login, const TCHAR *password, const TCHAR *schema, TCHAR *errorText);
126void LIBNXDB_EXPORTABLE DBDisconnect(DB_HANDLE hConn);
127void LIBNXDB_EXPORTABLE DBEnableReconnect(DB_HANDLE hConn, bool enabled);
85dfb586 128bool LIBNXDB_EXPORTABLE DBSetPrefetchLimit(DB_HANDLE hConn, int limit);
3abf5b29 129void LIBNXDB_EXPORTABLE DBSetSessionInitCallback(void (*cb)(DB_HANDLE));
8b1de1cc
AK
130
131DB_STATEMENT LIBNXDB_EXPORTABLE DBPrepare(DB_HANDLE hConn, const TCHAR *szQuery);
132DB_STATEMENT LIBNXDB_EXPORTABLE DBPrepareEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
133void LIBNXDB_EXPORTABLE DBFreeStatement(DB_STATEMENT hStmt);
84ee0f9c 134const TCHAR LIBNXDB_EXPORTABLE *DBGetStatementSource(DB_STATEMENT hStmt);
66c11d49
VK
135bool LIBNXDB_EXPORTABLE DBOpenBatch(DB_STATEMENT hStmt);
136void LIBNXDB_EXPORTABLE DBNextBatchRow(DB_STATEMENT hStmt);
8b1de1cc
AK
137void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, int cType, void *buffer, int allocType);
138void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const TCHAR *value, int allocType);
139void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const TCHAR *value, int allocType, int maxLen);
140void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, INT32 value);
141void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, UINT32 value);
142void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, INT64 value);
143void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, UINT64 value);
144void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, double value);
de4af576 145void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const uuid& value);
ce9e00cc 146void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const MacAddress& value);
750d59f2
VK
147bool LIBNXDB_EXPORTABLE DBExecute(DB_STATEMENT hStmt);
148bool LIBNXDB_EXPORTABLE DBExecuteEx(DB_STATEMENT hStmt, TCHAR *errorText);
8b1de1cc
AK
149DB_RESULT LIBNXDB_EXPORTABLE DBSelectPrepared(DB_STATEMENT hStmt);
150DB_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedEx(DB_STATEMENT hStmt, TCHAR *errorText);
f17cf019
VK
151DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedUnbuffered(DB_STATEMENT hStmt);
152DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedUnbufferedEx(DB_STATEMENT hStmt, TCHAR *errorText);
8b1de1cc 153
750d59f2
VK
154bool LIBNXDB_EXPORTABLE DBQuery(DB_HANDLE hConn, const TCHAR *szQuery);
155bool LIBNXDB_EXPORTABLE DBQueryEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
8b1de1cc
AK
156
157DB_RESULT LIBNXDB_EXPORTABLE DBSelect(DB_HANDLE hConn, const TCHAR *szQuery);
158DB_RESULT LIBNXDB_EXPORTABLE DBSelectEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
159int LIBNXDB_EXPORTABLE DBGetColumnCount(DB_RESULT hResult);
750d59f2 160bool LIBNXDB_EXPORTABLE DBGetColumnName(DB_RESULT hResult, int column, TCHAR *buffer, int bufSize);
8b1de1cc
AK
161int LIBNXDB_EXPORTABLE DBGetNumRows(DB_RESULT hResult);
162void LIBNXDB_EXPORTABLE DBFreeResult(DB_RESULT hResult);
163
60b9b39c
VK
164TCHAR LIBNXDB_EXPORTABLE *DBGetField(DB_RESULT hResult, int row, int col, TCHAR *pszBuffer, int nBufLen);
165TCHAR LIBNXDB_EXPORTABLE *DBGetFieldForXML(DB_RESULT hResult, int row, int col);
07c9f3b0 166char LIBNXDB_EXPORTABLE *DBGetFieldA(DB_RESULT hResult, int iRow, int iColumn, char *pszBuffer, int nBufLen);
43526096 167char LIBNXDB_EXPORTABLE *DBGetFieldUTF8(DB_RESULT hResult, int iRow, int iColumn, char *pszBuffer, int nBufLen);
8b1de1cc
AK
168INT32 LIBNXDB_EXPORTABLE DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
169UINT32 LIBNXDB_EXPORTABLE DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
170INT64 LIBNXDB_EXPORTABLE DBGetFieldInt64(DB_RESULT hResult, int iRow, int iColumn);
171UINT64 LIBNXDB_EXPORTABLE DBGetFieldUInt64(DB_RESULT hResult, int iRow, int iColumn);
172double LIBNXDB_EXPORTABLE DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn);
173UINT32 LIBNXDB_EXPORTABLE DBGetFieldIPAddr(DB_RESULT hResult, int iRow, int iColumn);
43709a0c 174InetAddress LIBNXDB_EXPORTABLE DBGetFieldInetAddr(DB_RESULT hResult, int iRow, int iColumn);
ce9e00cc 175MacAddress LIBNXDB_EXPORTABLE DBGetFieldMacAddr(DB_RESULT hResult, int iRow, int iColumn);
750d59f2
VK
176bool LIBNXDB_EXPORTABLE DBGetFieldByteArray(DB_RESULT hResult, int iRow, int iColumn,
177 int *pnArray, int nSize, int nDefault);
178bool LIBNXDB_EXPORTABLE DBGetFieldByteArray2(DB_RESULT hResult, int iRow, int iColumn,
8b1de1cc 179 BYTE *data, int nSize, int nDefault);
de4af576 180uuid LIBNXDB_EXPORTABLE DBGetFieldGUID(DB_RESULT hResult, int iRow, int iColumn);
8b1de1cc 181
f17cf019
VK
182DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectUnbuffered(DB_HANDLE hConn, const TCHAR *szQuery);
183DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectUnbufferedEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
184bool LIBNXDB_EXPORTABLE DBFetch(DB_UNBUFFERED_RESULT hResult);
185int LIBNXDB_EXPORTABLE DBGetColumnCount(DB_UNBUFFERED_RESULT hResult);
186bool LIBNXDB_EXPORTABLE DBGetColumnName(DB_UNBUFFERED_RESULT hResult, int column, TCHAR *buffer, int bufSize);
187void LIBNXDB_EXPORTABLE DBFreeResult(DB_UNBUFFERED_RESULT hResult);
188
189TCHAR LIBNXDB_EXPORTABLE *DBGetField(DB_UNBUFFERED_RESULT hResult, int iColumn, TCHAR *pBuffer, int iBufSize);
7f8e3ccf 190char LIBNXDB_EXPORTABLE *DBGetFieldUTF8(DB_UNBUFFERED_RESULT hResult, int iColumn, char *buffer, int iBufSize);
f17cf019
VK
191INT32 LIBNXDB_EXPORTABLE DBGetFieldLong(DB_UNBUFFERED_RESULT hResult, int iColumn);
192UINT32 LIBNXDB_EXPORTABLE DBGetFieldULong(DB_UNBUFFERED_RESULT hResult, int iColumn);
193INT64 LIBNXDB_EXPORTABLE DBGetFieldInt64(DB_UNBUFFERED_RESULT hResult, int iColumn);
194UINT64 LIBNXDB_EXPORTABLE DBGetFieldUInt64(DB_UNBUFFERED_RESULT hResult, int iColumn);
195double LIBNXDB_EXPORTABLE DBGetFieldDouble(DB_UNBUFFERED_RESULT hResult, int iColumn);
196UINT32 LIBNXDB_EXPORTABLE DBGetFieldIPAddr(DB_UNBUFFERED_RESULT hResult, int iColumn);
197InetAddress LIBNXDB_EXPORTABLE DBGetFieldInetAddr(DB_UNBUFFERED_RESULT hResult, int iColumn);
198uuid LIBNXDB_EXPORTABLE DBGetFieldGUID(DB_UNBUFFERED_RESULT hResult, int iColumn);
8b1de1cc 199
750d59f2
VK
200bool LIBNXDB_EXPORTABLE DBBegin(DB_HANDLE hConn);
201bool LIBNXDB_EXPORTABLE DBCommit(DB_HANDLE hConn);
202bool LIBNXDB_EXPORTABLE DBRollback(DB_HANDLE hConn);
8b1de1cc 203
daf3c104
VK
204int LIBNXDB_EXPORTABLE DBIsTableExist(DB_HANDLE conn, const TCHAR *table);
205
8b1de1cc
AK
206int LIBNXDB_EXPORTABLE DBGetSchemaVersion(DB_HANDLE conn);
207int LIBNXDB_EXPORTABLE DBGetSyntax(DB_HANDLE conn);
208
209String LIBNXDB_EXPORTABLE DBPrepareString(DB_HANDLE conn, const TCHAR *str, int maxSize = -1);
9bd1bace 210String LIBNXDB_EXPORTABLE DBPrepareString(DB_DRIVER drv, const TCHAR *str, int maxSize = -1);
8b1de1cc
AK
211#ifdef UNICODE
212String LIBNXDB_EXPORTABLE DBPrepareStringA(DB_HANDLE conn, const char *str, int maxSize = -1);
9bd1bace 213String LIBNXDB_EXPORTABLE DBPrepareStringA(DB_DRIVER drv, const char *str, int maxSize = -1);
8b1de1cc
AK
214#else
215#define DBPrepareStringA DBPrepareString
216#endif
217TCHAR LIBNXDB_EXPORTABLE *EncodeSQLString(const TCHAR *pszIn);
218void LIBNXDB_EXPORTABLE DecodeSQLString(TCHAR *pszStr);
219
220bool LIBNXDB_EXPORTABLE DBConnectionPoolStartup(DB_DRIVER driver, const TCHAR *server, const TCHAR *dbName,
221 const TCHAR *login, const TCHAR *password, const TCHAR *schema,
222 int basePoolSize, int maxPoolSize, int cooldownTime,
14228410 223 int connTTL);
8b1de1cc 224void LIBNXDB_EXPORTABLE DBConnectionPoolShutdown();
9c133761
VK
225DB_HANDLE LIBNXDB_EXPORTABLE __DBConnectionPoolAcquireConnection(const char *srcFile, int srcLine);
226#define DBConnectionPoolAcquireConnection() __DBConnectionPoolAcquireConnection(__FILE__, __LINE__)
8b1de1cc
AK
227void LIBNXDB_EXPORTABLE DBConnectionPoolReleaseConnection(DB_HANDLE connection);
228int LIBNXDB_EXPORTABLE DBConnectionPoolGetSize();
229int LIBNXDB_EXPORTABLE DBConnectionPoolGetAcquiredCount();
230
58f1f627 231void LIBNXDB_EXPORTABLE DBSetLongRunningThreshold(UINT32 threshold);
9c133761 232ObjectArray<PoolConnectionInfo> LIBNXDB_EXPORTABLE *DBConnectionPoolGetConnectionList();
a4809f58 233void LIBNXDB_EXPORTABLE DBGetPerfCounters(LIBNXDB_PERF_COUNTERS *counters);
8b1de1cc 234
296ae03d 235bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, UINT32 id);
de4af576 236bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, const uuid& id);
d1609c79 237bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, const TCHAR *id);
296ae03d 238
797a2b25
VK
239bool LIBNXDB_EXPORTABLE DBRenameTable(DB_HANDLE hdb, const TCHAR *oldName, const TCHAR *newName);
240bool LIBNXDB_EXPORTABLE DBDropPrimaryKey(DB_HANDLE hdb, const TCHAR *table);
91c4b99c 241bool LIBNXDB_EXPORTABLE DBAddPrimaryKey(DB_HANDLE hdb, const TCHAR *table, const TCHAR *columns);
797a2b25
VK
242bool LIBNXDB_EXPORTABLE DBRemoveNotNullConstraint(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column);
243bool LIBNXDB_EXPORTABLE DBSetNotNullConstraint(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column);
74724c20 244bool LIBNXDB_EXPORTABLE DBResizeColumn(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column, int newSize, bool nullable);
16684f16 245bool LIBNXDB_EXPORTABLE DBDropColumn(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column);
797a2b25 246
8b1de1cc 247#endif /* _nxsrvapi_h_ */