master
h 93 lines 2.7 KB
Raw
1 #ifndef ODBC_BRIDGE_H
2 #define ODBC_BRIDGE_H
3
4 #include <stdint.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7
8 // Error codes
9 #define ODBC_SUCCESS 0
10 #define ODBC_ERROR -1
11 #define ODBC_NO_DATA 100
12 #define ODBC_ERROR_CONNECT -10
13 #define ODBC_ERROR_QUERY -20
14 #define ODBC_ERROR_STMT_RESET -30
15 #define ODBC_ERROR_FETCH -40
16
17 // Connection handle
18 typedef void* odbc_conn_t;
19
20 // Data type indicators
21 typedef enum {
22 ODBC_TYPE_NULL = 0,
23 ODBC_TYPE_INT64 = 1,
24 ODBC_TYPE_DOUBLE = 2,
25 ODBC_TYPE_STRING = 3,
26 ODBC_TYPE_BINARY = 4
27 } odbc_data_type_t;
28
29 // Column metadata
30 typedef struct {
31 char name[256];
32 odbc_data_type_t type;
33 int sql_type;
34 size_t size;
35 int precision;
36 int scale;
37 bool nullable;
38 } odbc_column_info_t;
39
40 // Value union for different data types
41 typedef struct {
42 odbc_data_type_t type;
43 bool is_null;
44 union {
45 int64_t int_val;
46 double double_val;
47 char* string_val;
48 struct {
49 void* data;
50 size_t len;
51 } binary_val;
52 } data;
53 } odbc_value_t;
54
55 // Connection management
56 odbc_conn_t odbc_connect(const char* dsn, char* error_buf, int error_buf_size);
57 void odbc_disconnect(odbc_conn_t conn);
58 int odbc_is_connected(odbc_conn_t conn);
59
60 // Query execution modes
61 int odbc_prepare(odbc_conn_t conn, const char* query, char* error_buf, int error_buf_size);
62 int odbc_execute(odbc_conn_t conn, char* error_buf, int error_buf_size);
63 int odbc_execute_direct(odbc_conn_t conn, const char* query, char* error_buf, int error_buf_size);
64
65 // Statement management
66 int odbc_reset_statement(odbc_conn_t conn);
67 int odbc_close_cursor(odbc_conn_t conn);
68 int odbc_free_statement(odbc_conn_t conn);
69
70 // Result metadata
71 int odbc_get_column_count(odbc_conn_t conn);
72 int odbc_get_column_info(odbc_conn_t conn, int column_index, odbc_column_info_t* info);
73 int64_t odbc_get_row_count(odbc_conn_t conn); // Can return negative on AS400!
74
75 // Result fetching
76 int odbc_fetch_row(odbc_conn_t conn);
77 int odbc_get_value(odbc_conn_t conn, int column_index, odbc_value_t* value);
78 void odbc_free_value(odbc_value_t* value);
79
80 // Optimized bulk operations
81 int odbc_set_array_size(odbc_conn_t conn, int size);
82 int odbc_bind_column(odbc_conn_t conn, int column_index, void* buffer, size_t buffer_size);
83
84 // Error handling
85 const char* odbc_get_last_error(odbc_conn_t conn);
86 int odbc_get_sqlstate(odbc_conn_t conn, char* state, size_t state_size);
87
88 // Helpers for extracting values from odbc_value_t (return neutral values when NULL or type mismatch)
89 int64_t odbc_value_get_int64(const odbc_value_t* value);
90 double odbc_value_get_double(const odbc_value_t* value);
91 const char* odbc_value_get_string(const odbc_value_t* value);
92
93 #endif // ODBC_BRIDGE_H