log parser status kept as integer; minor refactoing
[public/netxms.git] / include / nxdbapi.h
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
49 /**
50 * DB events
51 */
52 #define DBEVENT_CONNECTION_LOST 0
53 #define DBEVENT_CONNECTION_RESTORED 1
54 #define DBEVENT_QUERY_FAILED 2
55
56
57 /**
58 * Database syntax codes
59 */
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
69 /**
70 * Database connection structures
71 */
72 struct db_driver_t;
73 typedef db_driver_t * DB_DRIVER;
74
75 struct db_handle_t;
76 typedef db_handle_t * DB_HANDLE;
77
78 struct db_statement_t;
79 typedef db_statement_t * DB_STATEMENT;
80
81 struct db_result_t;
82 typedef db_result_t * DB_RESULT;
83
84 struct db_unbuffered_result_t;
85 typedef db_unbuffered_result_t * DB_UNBUFFERED_RESULT;
86
87 /**
88 * Pool connection information
89 */
90 struct PoolConnectionInfo
91 {
92 DB_HANDLE handle;
93 bool inUse;
94 time_t lastAccessTime;
95 time_t connectTime;
96 UINT32 usageCount;
97 char srcFile[128];
98 int srcLine;
99 };
100
101 /**
102 * DB library performance counters
103 */
104 struct LIBNXDB_PERF_COUNTERS
105 {
106 UINT64 selectQueries;
107 UINT64 nonSelectQueries;
108 UINT64 totalQueries;
109 UINT64 longRunningQueries;
110 UINT64 failedQueries;
111 };
112
113 /**
114 * Functions
115 */
116 bool LIBNXDB_EXPORTABLE DBInit(DWORD logMsgCode, DWORD sqlErrorMsgCode);
117 DB_DRIVER LIBNXDB_EXPORTABLE DBLoadDriver(const TCHAR *module, const TCHAR *initParameters,
118 bool dumpSQL, void (* fpEventHandler)(DWORD, const WCHAR *, const WCHAR *, bool, void *),
119 void *userArg);
120 void LIBNXDB_EXPORTABLE DBUnloadDriver(DB_DRIVER driver);
121 const char LIBNXDB_EXPORTABLE *DBGetDriverName(DB_DRIVER driver);
122 void LIBNXDB_EXPORTABLE DBSetDefaultPrefetchLimit(DB_DRIVER driver, int limit);
123
124 DB_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);
126 void LIBNXDB_EXPORTABLE DBDisconnect(DB_HANDLE hConn);
127 void LIBNXDB_EXPORTABLE DBEnableReconnect(DB_HANDLE hConn, bool enabled);
128 bool LIBNXDB_EXPORTABLE DBSetPrefetchLimit(DB_HANDLE hConn, int limit);
129 void LIBNXDB_EXPORTABLE DBSetSessionInitCallback(void (*cb)(DB_HANDLE));
130
131 DB_STATEMENT LIBNXDB_EXPORTABLE DBPrepare(DB_HANDLE hConn, const TCHAR *szQuery);
132 DB_STATEMENT LIBNXDB_EXPORTABLE DBPrepareEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
133 void LIBNXDB_EXPORTABLE DBFreeStatement(DB_STATEMENT hStmt);
134 const TCHAR LIBNXDB_EXPORTABLE *DBGetStatementSource(DB_STATEMENT hStmt);
135 bool LIBNXDB_EXPORTABLE DBOpenBatch(DB_STATEMENT hStmt);
136 void LIBNXDB_EXPORTABLE DBNextBatchRow(DB_STATEMENT hStmt);
137 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, int cType, void *buffer, int allocType);
138 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const TCHAR *value, int allocType);
139 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const TCHAR *value, int allocType, int maxLen);
140 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, INT32 value);
141 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, UINT32 value);
142 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, INT64 value);
143 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, UINT64 value);
144 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, double value);
145 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const uuid& value);
146 void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const MacAddress& value);
147 bool LIBNXDB_EXPORTABLE DBExecute(DB_STATEMENT hStmt);
148 bool LIBNXDB_EXPORTABLE DBExecuteEx(DB_STATEMENT hStmt, TCHAR *errorText);
149 DB_RESULT LIBNXDB_EXPORTABLE DBSelectPrepared(DB_STATEMENT hStmt);
150 DB_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedEx(DB_STATEMENT hStmt, TCHAR *errorText);
151 DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedUnbuffered(DB_STATEMENT hStmt);
152 DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedUnbufferedEx(DB_STATEMENT hStmt, TCHAR *errorText);
153
154 bool LIBNXDB_EXPORTABLE DBQuery(DB_HANDLE hConn, const TCHAR *szQuery);
155 bool LIBNXDB_EXPORTABLE DBQueryEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
156
157 DB_RESULT LIBNXDB_EXPORTABLE DBSelect(DB_HANDLE hConn, const TCHAR *szQuery);
158 DB_RESULT LIBNXDB_EXPORTABLE DBSelectEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
159 int LIBNXDB_EXPORTABLE DBGetColumnCount(DB_RESULT hResult);
160 bool LIBNXDB_EXPORTABLE DBGetColumnName(DB_RESULT hResult, int column, TCHAR *buffer, int bufSize);
161 int LIBNXDB_EXPORTABLE DBGetNumRows(DB_RESULT hResult);
162 void LIBNXDB_EXPORTABLE DBFreeResult(DB_RESULT hResult);
163
164 TCHAR LIBNXDB_EXPORTABLE *DBGetField(DB_RESULT hResult, int row, int col, TCHAR *pszBuffer, int nBufLen);
165 TCHAR LIBNXDB_EXPORTABLE *DBGetFieldForXML(DB_RESULT hResult, int row, int col);
166 char LIBNXDB_EXPORTABLE *DBGetFieldA(DB_RESULT hResult, int iRow, int iColumn, char *pszBuffer, int nBufLen);
167 char LIBNXDB_EXPORTABLE *DBGetFieldUTF8(DB_RESULT hResult, int iRow, int iColumn, char *pszBuffer, int nBufLen);
168 INT32 LIBNXDB_EXPORTABLE DBGetFieldLong(DB_RESULT hResult, int iRow, int iColumn);
169 UINT32 LIBNXDB_EXPORTABLE DBGetFieldULong(DB_RESULT hResult, int iRow, int iColumn);
170 INT64 LIBNXDB_EXPORTABLE DBGetFieldInt64(DB_RESULT hResult, int iRow, int iColumn);
171 UINT64 LIBNXDB_EXPORTABLE DBGetFieldUInt64(DB_RESULT hResult, int iRow, int iColumn);
172 double LIBNXDB_EXPORTABLE DBGetFieldDouble(DB_RESULT hResult, int iRow, int iColumn);
173 UINT32 LIBNXDB_EXPORTABLE DBGetFieldIPAddr(DB_RESULT hResult, int iRow, int iColumn);
174 InetAddress LIBNXDB_EXPORTABLE DBGetFieldInetAddr(DB_RESULT hResult, int iRow, int iColumn);
175 MacAddress LIBNXDB_EXPORTABLE DBGetFieldMacAddr(DB_RESULT hResult, int iRow, int iColumn);
176 bool LIBNXDB_EXPORTABLE DBGetFieldByteArray(DB_RESULT hResult, int iRow, int iColumn,
177 int *pnArray, int nSize, int nDefault);
178 bool LIBNXDB_EXPORTABLE DBGetFieldByteArray2(DB_RESULT hResult, int iRow, int iColumn,
179 BYTE *data, int nSize, int nDefault);
180 uuid LIBNXDB_EXPORTABLE DBGetFieldGUID(DB_RESULT hResult, int iRow, int iColumn);
181
182 DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectUnbuffered(DB_HANDLE hConn, const TCHAR *szQuery);
183 DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectUnbufferedEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText);
184 bool LIBNXDB_EXPORTABLE DBFetch(DB_UNBUFFERED_RESULT hResult);
185 int LIBNXDB_EXPORTABLE DBGetColumnCount(DB_UNBUFFERED_RESULT hResult);
186 bool LIBNXDB_EXPORTABLE DBGetColumnName(DB_UNBUFFERED_RESULT hResult, int column, TCHAR *buffer, int bufSize);
187 void LIBNXDB_EXPORTABLE DBFreeResult(DB_UNBUFFERED_RESULT hResult);
188
189 TCHAR LIBNXDB_EXPORTABLE *DBGetField(DB_UNBUFFERED_RESULT hResult, int iColumn, TCHAR *pBuffer, int iBufSize);
190 char LIBNXDB_EXPORTABLE *DBGetFieldUTF8(DB_UNBUFFERED_RESULT hResult, int iColumn, char *buffer, int iBufSize);
191 INT32 LIBNXDB_EXPORTABLE DBGetFieldLong(DB_UNBUFFERED_RESULT hResult, int iColumn);
192 UINT32 LIBNXDB_EXPORTABLE DBGetFieldULong(DB_UNBUFFERED_RESULT hResult, int iColumn);
193 INT64 LIBNXDB_EXPORTABLE DBGetFieldInt64(DB_UNBUFFERED_RESULT hResult, int iColumn);
194 UINT64 LIBNXDB_EXPORTABLE DBGetFieldUInt64(DB_UNBUFFERED_RESULT hResult, int iColumn);
195 double LIBNXDB_EXPORTABLE DBGetFieldDouble(DB_UNBUFFERED_RESULT hResult, int iColumn);
196 UINT32 LIBNXDB_EXPORTABLE DBGetFieldIPAddr(DB_UNBUFFERED_RESULT hResult, int iColumn);
197 InetAddress LIBNXDB_EXPORTABLE DBGetFieldInetAddr(DB_UNBUFFERED_RESULT hResult, int iColumn);
198 uuid LIBNXDB_EXPORTABLE DBGetFieldGUID(DB_UNBUFFERED_RESULT hResult, int iColumn);
199
200 bool LIBNXDB_EXPORTABLE DBBegin(DB_HANDLE hConn);
201 bool LIBNXDB_EXPORTABLE DBCommit(DB_HANDLE hConn);
202 bool LIBNXDB_EXPORTABLE DBRollback(DB_HANDLE hConn);
203
204 int LIBNXDB_EXPORTABLE DBIsTableExist(DB_HANDLE conn, const TCHAR *table);
205
206 int LIBNXDB_EXPORTABLE DBGetSchemaVersion(DB_HANDLE conn);
207 int LIBNXDB_EXPORTABLE DBGetSyntax(DB_HANDLE conn);
208
209 String LIBNXDB_EXPORTABLE DBPrepareString(DB_HANDLE conn, const TCHAR *str, int maxSize = -1);
210 String LIBNXDB_EXPORTABLE DBPrepareString(DB_DRIVER drv, const TCHAR *str, int maxSize = -1);
211 #ifdef UNICODE
212 String LIBNXDB_EXPORTABLE DBPrepareStringA(DB_HANDLE conn, const char *str, int maxSize = -1);
213 String LIBNXDB_EXPORTABLE DBPrepareStringA(DB_DRIVER drv, const char *str, int maxSize = -1);
214 #else
215 #define DBPrepareStringA DBPrepareString
216 #endif
217 TCHAR LIBNXDB_EXPORTABLE *EncodeSQLString(const TCHAR *pszIn);
218 void LIBNXDB_EXPORTABLE DecodeSQLString(TCHAR *pszStr);
219
220 bool 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,
223 int connTTL);
224 void LIBNXDB_EXPORTABLE DBConnectionPoolShutdown();
225 DB_HANDLE LIBNXDB_EXPORTABLE __DBConnectionPoolAcquireConnection(const char *srcFile, int srcLine);
226 #define DBConnectionPoolAcquireConnection() __DBConnectionPoolAcquireConnection(__FILE__, __LINE__)
227 void LIBNXDB_EXPORTABLE DBConnectionPoolReleaseConnection(DB_HANDLE connection);
228 int LIBNXDB_EXPORTABLE DBConnectionPoolGetSize();
229 int LIBNXDB_EXPORTABLE DBConnectionPoolGetAcquiredCount();
230
231 void LIBNXDB_EXPORTABLE DBSetLongRunningThreshold(UINT32 threshold);
232 ObjectArray<PoolConnectionInfo> LIBNXDB_EXPORTABLE *DBConnectionPoolGetConnectionList();
233 void LIBNXDB_EXPORTABLE DBGetPerfCounters(LIBNXDB_PERF_COUNTERS *counters);
234
235 bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, UINT32 id);
236 bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, const uuid& id);
237 bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, const TCHAR *id);
238
239 bool LIBNXDB_EXPORTABLE DBRenameTable(DB_HANDLE hdb, const TCHAR *oldName, const TCHAR *newName);
240 bool LIBNXDB_EXPORTABLE DBDropPrimaryKey(DB_HANDLE hdb, const TCHAR *table);
241 bool LIBNXDB_EXPORTABLE DBAddPrimaryKey(DB_HANDLE hdb, const TCHAR *table, const TCHAR *columns);
242 bool LIBNXDB_EXPORTABLE DBRemoveNotNullConstraint(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column);
243 bool LIBNXDB_EXPORTABLE DBSetNotNullConstraint(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column);
244 bool LIBNXDB_EXPORTABLE DBResizeColumn(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column, int newSize, bool nullable);
245 bool LIBNXDB_EXPORTABLE DBDropColumn(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column);
246
247 #endif /* _nxsrvapi_h_ */