Commit | Line | Data |
---|---|---|
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 |
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 | ||
f17cf019 VK |
84 | struct db_unbuffered_result_t; |
85 | typedef db_unbuffered_result_t * DB_UNBUFFERED_RESULT; | |
8b1de1cc | 86 | |
9c133761 VK |
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; | |
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 | */ | |
104 | struct 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 |
116 | bool LIBNXDB_EXPORTABLE DBInit(DWORD logMsgCode, DWORD sqlErrorMsgCode); |
117 | DB_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); |
120 | void LIBNXDB_EXPORTABLE DBUnloadDriver(DB_DRIVER driver); | |
121 | const char LIBNXDB_EXPORTABLE *DBGetDriverName(DB_DRIVER driver); | |
85dfb586 | 122 | void LIBNXDB_EXPORTABLE DBSetDefaultPrefetchLimit(DB_DRIVER driver, int limit); |
8b1de1cc AK |
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); | |
85dfb586 | 128 | bool LIBNXDB_EXPORTABLE DBSetPrefetchLimit(DB_HANDLE hConn, int limit); |
3abf5b29 | 129 | void LIBNXDB_EXPORTABLE DBSetSessionInitCallback(void (*cb)(DB_HANDLE)); |
8b1de1cc AK |
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); | |
84ee0f9c | 134 | const TCHAR LIBNXDB_EXPORTABLE *DBGetStatementSource(DB_STATEMENT hStmt); |
66c11d49 VK |
135 | bool LIBNXDB_EXPORTABLE DBOpenBatch(DB_STATEMENT hStmt); |
136 | void LIBNXDB_EXPORTABLE DBNextBatchRow(DB_STATEMENT hStmt); | |
8b1de1cc AK |
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); | |
de4af576 | 145 | void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const uuid& value); |
ce9e00cc | 146 | void LIBNXDB_EXPORTABLE DBBind(DB_STATEMENT hStmt, int pos, int sqlType, const MacAddress& value); |
750d59f2 VK |
147 | bool LIBNXDB_EXPORTABLE DBExecute(DB_STATEMENT hStmt); |
148 | bool LIBNXDB_EXPORTABLE DBExecuteEx(DB_STATEMENT hStmt, TCHAR *errorText); | |
8b1de1cc AK |
149 | DB_RESULT LIBNXDB_EXPORTABLE DBSelectPrepared(DB_STATEMENT hStmt); |
150 | DB_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedEx(DB_STATEMENT hStmt, TCHAR *errorText); | |
f17cf019 VK |
151 | DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedUnbuffered(DB_STATEMENT hStmt); |
152 | DB_UNBUFFERED_RESULT LIBNXDB_EXPORTABLE DBSelectPreparedUnbufferedEx(DB_STATEMENT hStmt, TCHAR *errorText); | |
8b1de1cc | 153 | |
750d59f2 VK |
154 | bool LIBNXDB_EXPORTABLE DBQuery(DB_HANDLE hConn, const TCHAR *szQuery); |
155 | bool LIBNXDB_EXPORTABLE DBQueryEx(DB_HANDLE hConn, const TCHAR *szQuery, TCHAR *errorText); | |
8b1de1cc AK |
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); | |
750d59f2 | 160 | bool LIBNXDB_EXPORTABLE DBGetColumnName(DB_RESULT hResult, int column, TCHAR *buffer, int bufSize); |
8b1de1cc AK |
161 | int LIBNXDB_EXPORTABLE DBGetNumRows(DB_RESULT hResult); |
162 | void LIBNXDB_EXPORTABLE DBFreeResult(DB_RESULT hResult); | |
163 | ||
60b9b39c VK |
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); | |
07c9f3b0 | 166 | char LIBNXDB_EXPORTABLE *DBGetFieldA(DB_RESULT hResult, int iRow, int iColumn, char *pszBuffer, int nBufLen); |
43526096 | 167 | char LIBNXDB_EXPORTABLE *DBGetFieldUTF8(DB_RESULT hResult, int iRow, int iColumn, char *pszBuffer, int nBufLen); |
8b1de1cc AK |
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); | |
43709a0c | 174 | InetAddress LIBNXDB_EXPORTABLE DBGetFieldInetAddr(DB_RESULT hResult, int iRow, int iColumn); |
ce9e00cc | 175 | MacAddress LIBNXDB_EXPORTABLE DBGetFieldMacAddr(DB_RESULT hResult, int iRow, int iColumn); |
750d59f2 VK |
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, | |
8b1de1cc | 179 | BYTE *data, int nSize, int nDefault); |
de4af576 | 180 | uuid LIBNXDB_EXPORTABLE DBGetFieldGUID(DB_RESULT hResult, int iRow, int iColumn); |
8b1de1cc | 181 | |
f17cf019 VK |
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); | |
7f8e3ccf | 190 | char LIBNXDB_EXPORTABLE *DBGetFieldUTF8(DB_UNBUFFERED_RESULT hResult, int iColumn, char *buffer, int iBufSize); |
f17cf019 VK |
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); | |
8b1de1cc | 199 | |
750d59f2 VK |
200 | bool LIBNXDB_EXPORTABLE DBBegin(DB_HANDLE hConn); |
201 | bool LIBNXDB_EXPORTABLE DBCommit(DB_HANDLE hConn); | |
202 | bool LIBNXDB_EXPORTABLE DBRollback(DB_HANDLE hConn); | |
8b1de1cc | 203 | |
daf3c104 VK |
204 | int LIBNXDB_EXPORTABLE DBIsTableExist(DB_HANDLE conn, const TCHAR *table); |
205 | ||
8b1de1cc AK |
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); | |
9bd1bace | 210 | String LIBNXDB_EXPORTABLE DBPrepareString(DB_DRIVER drv, const TCHAR *str, int maxSize = -1); |
8b1de1cc AK |
211 | #ifdef UNICODE |
212 | String LIBNXDB_EXPORTABLE DBPrepareStringA(DB_HANDLE conn, const char *str, int maxSize = -1); | |
9bd1bace | 213 | String LIBNXDB_EXPORTABLE DBPrepareStringA(DB_DRIVER drv, const char *str, int maxSize = -1); |
8b1de1cc AK |
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, | |
14228410 | 223 | int connTTL); |
8b1de1cc | 224 | void LIBNXDB_EXPORTABLE DBConnectionPoolShutdown(); |
9c133761 VK |
225 | DB_HANDLE LIBNXDB_EXPORTABLE __DBConnectionPoolAcquireConnection(const char *srcFile, int srcLine); |
226 | #define DBConnectionPoolAcquireConnection() __DBConnectionPoolAcquireConnection(__FILE__, __LINE__) | |
8b1de1cc AK |
227 | void LIBNXDB_EXPORTABLE DBConnectionPoolReleaseConnection(DB_HANDLE connection); |
228 | int LIBNXDB_EXPORTABLE DBConnectionPoolGetSize(); | |
229 | int LIBNXDB_EXPORTABLE DBConnectionPoolGetAcquiredCount(); | |
230 | ||
58f1f627 | 231 | void LIBNXDB_EXPORTABLE DBSetLongRunningThreshold(UINT32 threshold); |
9c133761 | 232 | ObjectArray<PoolConnectionInfo> LIBNXDB_EXPORTABLE *DBConnectionPoolGetConnectionList(); |
a4809f58 | 233 | void LIBNXDB_EXPORTABLE DBGetPerfCounters(LIBNXDB_PERF_COUNTERS *counters); |
8b1de1cc | 234 | |
296ae03d | 235 | bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, UINT32 id); |
de4af576 | 236 | bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, const uuid& id); |
d1609c79 | 237 | bool LIBNXDB_EXPORTABLE IsDatabaseRecordExist(DB_HANDLE hdb, const TCHAR *table, const TCHAR *idColumn, const TCHAR *id); |
296ae03d | 238 | |
797a2b25 VK |
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); | |
91c4b99c | 241 | bool LIBNXDB_EXPORTABLE DBAddPrimaryKey(DB_HANDLE hdb, const TCHAR *table, const TCHAR *columns); |
797a2b25 VK |
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); | |
74724c20 | 244 | bool LIBNXDB_EXPORTABLE DBResizeColumn(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column, int newSize, bool nullable); |
16684f16 | 245 | bool LIBNXDB_EXPORTABLE DBDropColumn(DB_HANDLE hdb, const TCHAR *table, const TCHAR *column); |
797a2b25 | 246 | |
8b1de1cc | 247 | #endif /* _nxsrvapi_h_ */ |