dbqueries_header


SUBMITTED BY: davalillocm

DATE: July 29, 2021, 5:24 a.m.

UPDATED: July 29, 2021, 5:39 a.m.

FORMAT: C++

SIZE: 16.1 kB

HITS: 703

  1. #ifndef DBQOP_H
  2. #define DBQOP_H
  3. //FOR LINUX OPERATIVE SYSTEM
  4. #ifdef __linux__
  5. #ifdef BUILD_DBQOP
  6. #define DBQOP_ATTRS extern __attribute__((visibility("default")))
  7. #define DBQOP_STRUCT_EXPORT __attribute__((visibility("default")))
  8. #define DBQOP_DTS_ATTRS
  9. #else
  10. #define DBQOP_ATTRS extern
  11. #define DBQOP_STRUCT_EXPORT
  12. #define DBQOP_DTS_ATTRS
  13. #endif // BUILD_DBQOP
  14. #endif // __linux__
  15. //FOR WINDOWS OPERATIVE SYSTEM
  16. #ifdef __windows__
  17. #ifdef BUILD_DBQOP
  18. #define DBQOP_ATTRS extern __declspec(dllexport)
  19. #define DBQOP_STRUCT_EXPORT __declspec(dllexport)
  20. #define DBQOP_DTS_ATTRS STDCALL
  21. #else
  22. #define DBQOP_ATTRS extern __declspec(dllimport)
  23. #define DBQOP_STRUCT_EXPORT __declspec(dllimport)
  24. #define DBQOP_DTS_ATTRS STDCALL
  25. #endif // BUILD_DBQOP
  26. #include <windows.h>
  27. #include <windef.h>
  28. #endif // __windows__
  29. //THE ORDER OF THESE INCLUDES MATTER
  30. #include <iostream>
  31. #include <cstring>
  32. #include <vector>
  33. #include <ctime>
  34. #include <typeinfo>
  35. #include <string>
  36. #include <sql.h>
  37. #include <sqltypes.h>
  38. #include <sqlext.h>
  39. #include <algorithm>
  40. #include "interop.h"
  41. #include "binrw.h"
  42. #define MAX_COL_TYPE_NAME_LENGTH 64
  43. #define MAX_TYPE_LENGTH sizeof(LONGLONG)
  44. #define MAX_COMMAND_LENGTH 1048576//1024*1024
  45. #define MAX_ERROR_MESSAGES 1024
  46. using namespace canonbit_definitions::interoperatibility;
  47. using namespace canonbit_algorithms::binary_reader_writer;
  48. namespace brw=canonbit_algorithms::binary_reader_writer;
  49. typedef SQLCHAR* PSQLCHAR;
  50. typedef SQLWCHAR* PSQLWCHAR;
  51. typedef SQLSMALLINT* PSQLSMALLINT;
  52. typedef SQLUSMALLINT* PSQLUSMALLINT;
  53. typedef SQLINTEGER* PSQLINTEGER;
  54. typedef SQLLEN* PSQLLEN;
  55. typedef SQL_TIMESTAMP_STRUCT* PTIMESTAMP;
  56. typedef struct DataBuffer
  57. {
  58. SQLLEN buffer_size;
  59. PSQLCHAR buffer;
  60. } CellData;
  61. typedef std::vector<CellData> RowData;
  62. typedef std::vector<RowData> TableData;//Collections of row buffers pointers
  63. namespace canonbit_databases{
  64. namespace odbc_database_operations{
  65. enum TABLE_COLUMN_TYPES
  66. {
  67. PRIMARY_KEY=0,
  68. FOREIGN_KEY,
  69. DATA_COLUMN
  70. };
  71. //Database connection parameters and values
  72. struct DBQOP_STRUCT_EXPORT DBConnection
  73. {
  74. DBConnection();
  75. DBConnection(const DBConnection& other);
  76. ~DBConnection();
  77. DBConnection& operator=(const DBConnection& rhs);
  78. SQLHENV environment_handle;//Environment handle
  79. SQLHDBC database_connection_handle;//Database connection handle
  80. bool environment_handle_allocated;
  81. bool connection_handle_allocated;
  82. bool sql_command_prepared;
  83. bool asynch_support;
  84. bool is_connected;
  85. bool is_open;
  86. int flag;
  87. std::string connection_string;
  88. std::string output_connection_string;
  89. std::string server_string;
  90. std::string user_string;
  91. std::string password_string;
  92. std::string last_message;
  93. };
  94. struct DBQOP_STRUCT_EXPORT DBColumn
  95. {
  96. DBColumn();
  97. ~DBColumn();
  98. explicit DBColumn(const short& new_column_type,const long& buffer_length);
  99. DBColumn(const DBColumn& other);
  100. DBColumn& operator=(const DBColumn& rhs);
  101. brw::Archive& operator&(brw::Archive& ar);
  102. uintptr_t table_owner;
  103. short column_type;
  104. short catalog_name_length;
  105. short schema_name_length;
  106. short table_name_length;
  107. short column_name_length;
  108. short column_type_name_length;
  109. long data_length_indicator;
  110. long column_buffer_length;//Max byte length of the column
  111. SQLCHAR catalog_name[SQL_MAX_CATALOG_NAME_LEN];
  112. SQLCHAR schema_name[SQL_MAX_SCHEMA_NAME_LEN];
  113. SQLCHAR table_name[SQL_MAX_TABLE_NAME_LEN];
  114. SQLCHAR column_name[SQL_MAX_COLUMN_NAME_LEN];
  115. SQLCHAR column_type_name[MAX_COL_TYPE_NAME_LENGTH];
  116. PSQLCHAR column_buffer;
  117. };
  118. struct DBKeyColumn
  119. {
  120. SQLCHAR key_catalog_name[SQL_MAX_CATALOG_NAME_LEN];
  121. SQLCHAR key_schema_name[SQL_MAX_SCHEMA_NAME_LEN];
  122. SQLCHAR key_table_name[SQL_MAX_TABLE_NAME_LEN];
  123. SQLCHAR key_column_name[SQL_MAX_COLUMN_NAME_LEN];
  124. SQLSMALLINT key_ordinal;
  125. SQLCHAR key_name[SQL_MAX_COLUMN_NAME_LEN];
  126. SQLSMALLINT key_catalog_name_length;
  127. SQLSMALLINT key_schema_name_length;
  128. SQLSMALLINT key_table_name_length;
  129. SQLSMALLINT key_column_name_length;
  130. SQLSMALLINT key_name_length;
  131. };
  132. struct DBQOP_STRUCT_EXPORT DBRow
  133. {
  134. public:
  135. DBRow();
  136. DBRow(const DBRow& other);
  137. explicit DBRow(const uintptr_t& row_table_owner,const UINT& new_row_index);
  138. DBRow& operator=(const DBRow& rhs);
  139. ~DBRow();
  140. uintptr_t table_owner;
  141. UINT row_index;
  142. void* getValue(const short& column_index) const;
  143. void getValue(const short& column_index,CellData& value) const;
  144. void setValue(const short& column_index,void* value) const;
  145. void setValue(const short& column_index,const CellData& value);
  146. DBRow cloneRow() const;
  147. private:
  148. RowData row_data;
  149. };
  150. struct DBQOP_STRUCT_EXPORT DBTable
  151. {
  152. public:
  153. DBTable();
  154. ~DBTable();
  155. explicit DBTable(const short& new_total_columns,DBColumn* new_table_columns);
  156. DBTable(const DBTable& other);
  157. DBTable& operator=(const DBTable& rhs);
  158. brw::Archive& operator&(brw::Archive& ar);
  159. std::string sql_statement;
  160. std::string database_name;
  161. std::string catalog_name;
  162. std::string schema_name;
  163. std::string table_name;
  164. short total_columns;
  165. short total_primary_key_columns;
  166. short total_foreign_key_columns;
  167. short* primary_key_columns;//Ordinals in zero based index
  168. short* foreign_key_columns;//Ordinals in zero based index
  169. UINT total_rows;
  170. DBColumn* table_columns;
  171. DBRow newRow() const;
  172. DBRow getRow(const UINT& row_index) const;
  173. void addRow(UINT& new_row_index);
  174. void addRow(const DBRow& new_row);
  175. void* getValue(const UINT& row_index,const short& column_index) const;
  176. void getValue
  177. (
  178. const UINT& row_index,
  179. const short& column_index,
  180. CellData& value
  181. ) const;
  182. void setValue
  183. (
  184. const UINT& row_index,
  185. const short& column_index,
  186. void* value,
  187. const long& value_length=1
  188. );
  189. void setValue
  190. (
  191. const UINT& row_index,
  192. const short& column_index,
  193. const CellData& value
  194. );
  195. template<typename T>
  196. T getValue(const UINT& row_index,const short& column_index)
  197. {
  198. T value;
  199. T* value_ptr=reinterpret_cast<T*>(getValue(row_index,column_index));
  200. if(value_ptr)
  201. {
  202. value=*value_ptr;
  203. }
  204. return value;
  205. }
  206. bool findRow
  207. (
  208. const short& key_column_index,
  209. const CellData& value,
  210. const UINT& starting_row_index,
  211. DBRow& row
  212. );/*Binary search for a complete match value*/
  213. bool findRowPattern
  214. (
  215. const short& key_column_index,
  216. const std::string& value,
  217. const UINT& starting_row_index,
  218. DBRow& row
  219. ) const;/*String search with pattern*/
  220. bool getPrimaryKeys
  221. (
  222. short*& primary_keys,
  223. short& total_primary_keys,
  224. std::string& message
  225. );
  226. bool getForeignKeys
  227. (
  228. short*& foreign_keys,
  229. short& total_foreign_keys,
  230. std::string& message
  231. );
  232. DBTable cloneTable();//Clone table schema and data
  233. DBTable cloneSchema();//Just for cloning table schema
  234. DBTable getSubTable
  235. (
  236. const short columns_indices[],
  237. const short& total_sub_columns
  238. );
  239. void splitPKFKDC
  240. (
  241. DBTable& primary_keys,
  242. DBTable& foreign_keys,
  243. DBTable& data_columns,
  244. bool& has_primary_keys,
  245. bool& has_foreign_keys
  246. );
  247. friend SQLHSTMT& getStatementHandle(DBTable& table);
  248. friend void setStatementHandle(DBTable& table,const SQLHSTMT handle);
  249. private:
  250. SQLHSTMT statement_handle;//Statement handle
  251. TableData table_data;
  252. bool is_primary_keys_set;
  253. bool is_foreign_keys_set;
  254. };
  255. struct DBQOP_STRUCT_EXPORT DBDataSet
  256. {
  257. DBDataSet();
  258. explicit DBDataSet
  259. (
  260. const std::string& sql_statement,
  261. const std::string& driver,
  262. const std::string& server,
  263. const std::string& user,
  264. const std::string& password
  265. );
  266. ~DBDataSet();
  267. DBConnection data_connection;
  268. std::vector<DBTable> data_tables;
  269. std::vector<DBTable>::iterator begin();
  270. std::vector<DBTable>::iterator end();
  271. int total_data_tables;
  272. };
  273. struct DBDataRelation
  274. {
  275. DBTable* table;
  276. short column_ordinal;
  277. };
  278. //METHODS THE CONNECTION ARGUMENT WILL HOLD THE OUTPUT MESSAGE IN CASE
  279. //OF ERROR OR WARNING
  280. DBQOP_ATTRS
  281. bool DBConnect
  282. (
  283. DBConnection& connection,
  284. const std::string& driver,
  285. const std::string& server,
  286. const std::string& user,
  287. const std::string& password
  288. );
  289. DBQOP_ATTRS
  290. bool DBDisconnect(DBConnection& connection);
  291. DBQOP_ATTRS
  292. VECSTR DBGetDrivers(DBConnection& connection);
  293. DBQOP_ATTRS
  294. VECSTR DBGetDataSources(DBConnection& connection);
  295. DBQOP_ATTRS
  296. bool DBSelect
  297. (
  298. DBConnection& connection,
  299. const std::string& sqlcmd,
  300. DBTable& select_table
  301. );
  302. DBQOP_ATTRS
  303. bool DBInsert
  304. (
  305. DBConnection& connection,
  306. const std::string& sqlcmd,
  307. const DBTable& rows_to_insert,
  308. DBTable& rows_rejected
  309. );
  310. DBQOP_ATTRS
  311. bool DBUpdate
  312. (
  313. DBConnection& connection,
  314. const std::string& sqlcmd,
  315. const DBTable& rows_to_update,
  316. const DBTable& primary_keys_values,
  317. DBTable& rows_rejected
  318. );
  319. DBQOP_ATTRS
  320. bool DBDelete
  321. (
  322. DBConnection& connection,
  323. const std::string& sqlcmd,
  324. const DBTable& primary_keys_values,
  325. DBTable& rows_rejected
  326. );
  327. DBQOP_ATTRS
  328. bool DBGetPrimaryKeys
  329. (
  330. DBTable& table,
  331. DBKeyColumn*& primary_keys,
  332. short& total_primary_keys,
  333. std::string& message
  334. );
  335. DBQOP_ATTRS
  336. bool DBGetForeignKeys
  337. (
  338. DBTable& table,
  339. DBKeyColumn*& foreign_keys,
  340. short& total_foreign_keys,
  341. std::string& message
  342. );
  343. DBQOP_ATTRS
  344. bool DBTransaction(const std::string& sqlcmd,std::string& message);
  345. DBQOP_ATTRS
  346. short DBGetNumberOfColumns(SQLHSTMT& statement_handle);//Statement handle must be prepared or executed
  347. DBQOP_ATTRS
  348. UINT DBGetNumberOfRows(SQLHSTMT& statement_handle);//Statement handle must be prepared or executed
  349. DBQOP_ATTRS
  350. std::string DBGetErrorMsg(SQLHANDLE handle,SQLSMALLINT handle_type);
  351. DBQOP_ATTRS
  352. std::string GetSQLTypeName(const short sql_type_id,bool& is_type_variable);
  353. //==========================================================================================
  354. //METHODS FOR INTEROPERABILITY WITH EXCEL AND LIBREOFFICE MACROS
  355. DBQOP_ATTRS DBQOP_DTS_ATTRS
  356. bool DTSDBConnect
  357. (
  358. const char*& driver,
  359. const char*& server,
  360. const char*& user,
  361. const char*& password
  362. );
  363. DBQOP_ATTRS DBQOP_DTS_ATTRS
  364. bool DTSDBDisconnect();
  365. DBQOP_ATTRS DBQOP_DTS_ATTRS
  366. bool DTSDBSelect(const char*& sqlcmd);
  367. DBQOP_ATTRS DBQOP_DTS_ATTRS
  368. bool DTSDBInsert(const char*& sqlcmd);
  369. DBQOP_ATTRS DBQOP_DTS_ATTRS
  370. bool DTSDBUpdate(const char*& sqlcmd);
  371. DBQOP_ATTRS DBQOP_DTS_ATTRS
  372. bool DTSDBDelete(const char*& sqlcmd);
  373. DBQOP_ATTRS DBQOP_DTS_ATTRS
  374. void DTSDBSetColumnIndex(const short& index);
  375. DBQOP_ATTRS DBQOP_DTS_ATTRS
  376. int DTSDBGetColumnType(const short& index);
  377. #ifdef BUILD_DBQOP
  378. //==========================================================================================
  379. //VERY IMPORTANT VARIABLES THAT ARE GOING TO BE USED AS A BUFFER
  380. //TO HOLD VARIABLE DATA ON FETCHING
  381. static RowData sql_cmd_buffer;
  382. //==========================================================================================
  383. //==========================================================================================
  384. //VERY IMPORTANT VARIABLES THAT ARE GOING TO BE USED WITH DATASHEET APPLICATIONS
  385. static DBConnection dts_connection;
  386. static DBTable dts_select_table;
  387. static DBTable dts_rows_to_insert;
  388. static DBTable dts_rows_to_update;
  389. static DBTable dts_primary_keys_values;
  390. static DBTable dts_rows_rejected;
  391. static short dts_current_column_index;
  392. //==========================================================================================
  393. static
  394. bool DBGetColumnsAndBind
  395. (
  396. DBConnection& connection,
  397. SQLHSTMT& statement_handle,
  398. const short& total_columns,
  399. DBColumn*& columns
  400. );
  401. static
  402. bool DBBindInsertParameters
  403. (
  404. DBConnection& connection,
  405. SQLHSTMT& statement_handle,
  406. const short& total_columns,
  407. DBColumn*& columns
  408. );
  409. static
  410. bool DBBindUpdateParameters
  411. (
  412. DBConnection& connection,
  413. SQLHSTMT& statement_handle,
  414. const short& total_data_columns,
  415. const short& total_primary_keys,
  416. DBColumn*& data_columns,
  417. DBColumn*& primary_keys
  418. );
  419. static
  420. bool DBBindDeleteParameters
  421. (
  422. DBConnection& connection,
  423. SQLHSTMT& statement_handle,
  424. const short& total_primary_keys,
  425. DBColumn*& primary_keys
  426. );
  427. static
  428. bool DBPrepareSelect
  429. (
  430. DBConnection& connection,
  431. SQLHSTMT& statement_handle,
  432. const std::string& sqlcmd
  433. );
  434. static
  435. bool DBPrepareInsert
  436. (
  437. DBConnection& connection,
  438. SQLHSTMT& statement_handle,
  439. const std::string& sqlcmd
  440. );
  441. static
  442. bool DBPrepareUpdate
  443. (
  444. DBConnection& connection,
  445. SQLHSTMT& statement_handle,
  446. const std::string& sqlcmd
  447. );
  448. static
  449. bool DBPrepareDelete
  450. (
  451. DBConnection& connection,
  452. SQLHSTMT& statement_handle,
  453. const std::string& sqlcmd
  454. );
  455. #endif // BUILD_DBQOP
  456. }//odbc_database_operations namespace
  457. }//canonbit_databases namespace
  458. #endif // DBQOP_H

comments powered by Disqus