added function DBAddPrimaryKey
[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
a4809f58
VK
101/**
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
9c133761
VK
113/**
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);
750d59f2
VK
146bool LIBNXDB_EXPORTABLE DBExecute(DB_STATEMENT hStmt);
147bool LIBNXDB_EXPORTABLE DBExecuteEx(DB_STATEMENT hStmt, TCHAR *errorText);
8b1de1cc
AK
148DB_RESULT LIBNXDB_EXPORTABLE DBSelectPrepared(DB_STATEMENT hStmt);
149DB_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedEx(DB_STATEMENT hStmt, TCHAR *errorText);
f17cf019
VK
150DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedUnbuffered(DB_STATEMENT hStmt);
151DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedUnbufferedEx(DB_STATEMENT hStmt, TCHAR *errorText);
8b1de1cc 152
750d59f2
VK
153bool LIBNXDB_EXPORTABLE DBQuery(DB_HANDLE hConn, const TCHAR *szQuery);
154bool LIBNXDB_EXPORTABLE DBQueryEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
8b1de1cc
AK
155
156DB_RESULT LIBNXDB_EXPORTABLE DBSelect(DB_HANDLE hConn, const TCHAR *szQuery);
157DB_RESULT LIBNXDB_EXPORTABLE DBSelectEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
158int LIBNXDB_EXPORTABLE DBGetColumnCount(DB_RESULT hResult);
750d59f2 159bool LIBNXDB_EXPORTABLE DBGetColumnName(DB_RESULT hResult, int column, TCHAR *buffer, int bufSize);
8b1de1cc
AK
160int LIBNXDB_EXPORTABLE DBGetNumRows(DB_RESULT hResult);
161void LIBNXDB_EXPORTABLE DBFreeResult(DB_RESULT hResult);
162
60b9b39c
VK
163TCHAR LIBNXDB_EXPORTABLE *DBGetField(DB_RESULT hResult, int row, int col, TCHAR *pszBuffer, int nBufLen);
164TCHAR LIBNXDB_EXPORTABLE *DBGetFieldForXML(DB_RESULT hResult, int row, int col);
07c9f3b0 165char LIBNXDB_EXPORTABLE *DBGetFieldA(DB_RESULT hResult, int iRow, int iColumn, char *pszBuffer, int nBufLen);
43526096 166char LIBNXDB_EXPORTABLE *DBGetFieldUTF8(DB_RESULT hResult, int iRow, int iColumn, char *pszBuffer, int nBufLen);
8b1de1cc
AK
167INT32 LIBNXDB_EXPORTABLE DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
168UINT32 LIBNXDB_EXPORTABLE DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
169INT64 LIBNXDB_EXPORTABLE DBGetFieldInt64(DB_RESULT hResult, int iRow, int iColumn);
170UINT64 LIBNXDB_EXPORTABLE DBGetFieldUInt64(DB_RESULT hResult, int iRow, int iColumn);
171double LIBNXDB_EXPORTABLE DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn);
172UINT32 LIBNXDB_EXPORTABLE DBGetFieldIPAddr(DB_RESULT hResult, int iRow, int iColumn);
43709a0c 173InetAddress LIBNXDB_EXPORTABLE DBGetFieldInetAddr(DB_RESULT hResult, int iRow, int iColumn);
750d59f2
VK
174bool LIBNXDB_EXPORTABLE DBGetFieldByteArray(DB_RESULT hResult, int iRow, int iColumn,
175 int *pnArray, int nSize, int nDefault);
176bool LIBNXDB_EXPORTABLE DBGetFieldByteArray2(DB_RESULT hResult, int iRow, int iColumn,
8b1de1cc 177 BYTE *data, int nSize, int nDefault);
de4af576 178uuid LIBNXDB_EXPORTABLE DBGetFieldGUID(DB_RESULT hResult, int iRow, int iColumn);
8b1de1cc 179
f17cf019
VK
180DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectUnbuffered(DB_HANDLE hConn, const TCHAR *szQuery);
181DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectUnbufferedEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
182bool LIBNXDB_EXPORTABLE DBFetch(DB_UNBUFFERED_RESULT hResult);
183int LIBNXDB_EXPORTABLE DBGetColumnCount(DB_UNBUFFERED_RESULT hResult);
184bool LIBNXDB_EXPORTABLE DBGetColumnName(DB_UNBUFFERED_RESULT hResult, int column, TCHAR *buffer, int bufSize);
185void LIBNXDB_EXPORTABLE DBFreeResult(DB_UNBUFFERED_RESULT hResult);
186
187TCHAR LIBNXDB_EXPORTABLE *DBGetField(DB_UNBUFFERED_RESULT hResult, int iColumn, TCHAR *pBuffer, int iBufSize);
7f8e3ccf 188char LIBNXDB_EXPORTABLE *DBGetFieldUTF8(DB_UNBUFFERED_RESULT hResult, int iColumn, char *buffer, int iBufSize);
f17cf019
VK
189INT32 LIBNXDB_EXPORTABLE DBGetFieldLong(DB_UNBUFFERED_RESULT hResult, int iColumn);
190UINT32 LIBNXDB_EXPORTABLE DBGetFieldULong(DB_UNBUFFERED_RESULT hResult, int iColumn);
191INT64 LIBNXDB_EXPORTABLE DBGetFieldInt64(DB_UNBUFFERED_RESULT hResult, int iColumn);
192UINT64 LIBNXDB_EXPORTABLE DBGetFieldUInt64(DB_UNBUFFERED_RESULT hResult, int iColumn);
193double LIBNXDB_EXPORTABLE DBGetFieldDouble(DB_UNBUFFERED_RESULT hResult, int iColumn);
194UINT32 LIBNXDB_EXPORTABLE DBGetFieldIPAddr(DB_UNBUFFERED_RESULT hResult, int iColumn);
195InetAddress LIBNXDB_EXPORTABLE DBGetFieldInetAddr(DB_UNBUFFERED_RESULT hResult, int iColumn);
196uuid LIBNXDB_EXPORTABLE DBGetFieldGUID(DB_UNBUFFERED_RESULT hResult, int iColumn);
8b1de1cc 197
750d59f2
VK
198bool LIBNXDB_EXPORTABLE DBBegin(DB_HANDLE hConn);
199bool LIBNXDB_EXPORTABLE DBCommit(DB_HANDLE hConn);
200bool LIBNXDB_EXPORTABLE DBRollback(DB_HANDLE hConn);
8b1de1cc 201
daf3c104
VK
202int LIBNXDB_EXPORTABLE DBIsTableExist(DB_HANDLE conn, const TCHAR *table);
203
8b1de1cc
AK
204int LIBNXDB_EXPORTABLE DBGetSchemaVersion(DB_HANDLE conn);
205int LIBNXDB_EXPORTABLE DBGetSyntax(DB_HANDLE conn);
206
207String LIBNXDB_EXPORTABLE DBPrepareString(DB_HANDLE conn, const TCHAR *str, int maxSize = -1);
9bd1bace 208String LIBNXDB_EXPORTABLE DBPrepareString(DB_DRIVER drv, const TCHAR *str, int maxSize = -1);
8b1de1cc
AK
209#ifdef UNICODE
210String LIBNXDB_EXPORTABLE DBPrepareStringA(DB_HANDLE conn, const char *str, int maxSize = -1);
9bd1bace 211String LIBNXDB_EXPORTABLE DBPrepareStringA(DB_DRIVER drv, const char *str, int maxSize = -1);
8b1de1cc
AK
212#else
213#define DBPrepareStringA DBPrepareString
214#endif
215TCHAR LIBNXDB_EXPORTABLE *EncodeSQLString(const TCHAR *pszIn);
216void LIBNXDB_EXPORTABLE DecodeSQLString(TCHAR *pszStr);
217
218bool LIBNXDB_EXPORTABLE DBConnectionPoolStartup(DB_DRIVER driver, const TCHAR *server, const TCHAR *dbName,
219 const TCHAR *login, const TCHAR *password, const TCHAR *schema,
220 int basePoolSize, int maxPoolSize, int cooldownTime,
14228410 221 int connTTL);
8b1de1cc 222void LIBNXDB_EXPORTABLE DBConnectionPoolShutdown();
9c133761
VK
223DB_HANDLE LIBNXDB_EXPORTABLE __DBConnectionPoolAcquireConnection(const char *srcFile, int srcLine);
224#define DBConnectionPoolAcquireConnection() __DBConnectionPoolAcquireConnection(__FILE__, __LINE__)
8b1de1cc
AK
225void LIBNXDB_EXPORTABLE DBConnectionPoolReleaseConnection(DB_HANDLE connection);
226int LIBNXDB_EXPORTABLE DBConnectionPoolGetSize();
227int LIBNXDB_EXPORTABLE DBConnectionPoolGetAcquiredCount();
228
58f1f627 229void LIBNXDB_EXPORTABLE DBSetLongRunningThreshold(UINT32 threshold);
9c133761 230ObjectArray<PoolConnectionInfo> LIBNXDB_EXPORTABLE *DBConnectionPoolGetConnectionList();
a4809f58 231void LIBNXDB_EXPORTABLE DBGetPerfCounters(LIBNXDB_PERF_COUNTERS *counters);
8b1de1cc 232
296ae03d 233bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, UINT32 id);
de4af576 234bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, const uuid& id);
d1609c79 235bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, const TCHAR *id);
296ae03d 236
797a2b25
VK
237bool LIBNXDB_EXPORTABLE DBRenameTable(DB_HANDLE hdb, const TCHAR *oldName, const TCHAR *newName);
238bool LIBNXDB_EXPORTABLE DBDropPrimaryKey(DB_HANDLE hdb, const TCHAR *table);
91c4b99c 239bool LIBNXDB_EXPORTABLE DBAddPrimaryKey(DB_HANDLE hdb, const TCHAR *table, const TCHAR *columns);
797a2b25
VK
240bool LIBNXDB_EXPORTABLE DBRemoveNotNullConstraint(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column);
241bool LIBNXDB_EXPORTABLE DBSetNotNullConstraint(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column);
74724c20 242bool LIBNXDB_EXPORTABLE DBResizeColumn(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column, int newSize, bool nullable);
797a2b25 243
8b1de1cc 244#endif /* _nxsrvapi_h_ */