odbc_sys/
lib.rs

1//! ODBC types those representation is compatible with the ODBC C API.
2//!
3//! This layer has not been created using automatic code generation. It is incomplete, i.e. it does
4//! not contain every symbol or constant defined in the ODBC C headers. Symbols which are
5//! deprecated since ODBC 3 have been left out intentionally. While some extra type safety has been
6//! added by grouping some of C's `#define` constants into `enum`-types it mostly offers the same
7//! power (all) and safety guarantess(none) as the wrapped C-API.
8//! ODBC 4.0 is still under development by Microsoft, so these symbols are deactivated by default
9//! in the cargo.toml
10
11mod attributes;
12mod bulk_operation;
13mod c_data_type;
14mod desc;
15mod fetch_orientation;
16mod functions;
17mod handles;
18mod indicator;
19mod info_type;
20mod interval;
21mod nullability;
22mod param_type;
23mod set_pos;
24mod sql_data_type;
25mod sqlreturn;
26
27pub use self::{
28    attributes::*, bulk_operation::*, c_data_type::*, desc::*, fetch_orientation::*, functions::*,
29    handles::*, indicator::*, info_type::*, interval::*, nullability::*, param_type::*, set_pos::*,
30    sql_data_type::*, sqlreturn::*,
31};
32use std::os::raw::{c_int, c_void};
33
34pub type SmallInt = i16;
35pub type USmallInt = u16;
36pub type Integer = i32;
37pub type UInteger = u32;
38pub type Pointer = *mut c_void;
39pub type Char = u8;
40pub type SChar = i8;
41pub type WChar = u16;
42
43pub type Len = isize;
44pub type ULen = usize;
45pub type HWnd = Pointer;
46pub type RetCode = i16;
47
48/// Row index parameter for [`crate::SQLSetPos`]
49#[cfg(target_pointer_width = "64")]
50pub type SetPosIRow = u64;
51/// Row index parameter for [`crate::SQLSetPos`]
52#[cfg(not(target_pointer_width = "64"))]
53pub type SetPosIRow = i16;
54
55// flags for null-terminated string
56pub const NTS: isize = -3;
57pub const NTSL: isize = -3;
58
59/// Maximum message length
60pub const MAX_MESSAGE_LENGTH: SmallInt = 512;
61pub const SQLSTATE_SIZE: usize = 5;
62pub const SQLSTATE_SIZEW: usize = 10;
63
64/// SQL Free Statement options
65#[repr(u16)]
66#[derive(Debug, PartialEq, Eq, Clone, Copy)]
67pub enum FreeStmtOption {
68    /// Closes the cursor associated with StatementHandle (if one was defined) and discards all
69    /// pending results. The application can reopen this cursor later by executing a SELECT
70    /// statement again with the same or different parameter values. If no cursor is open, this
71    /// option has no effect for the application. `SQLCloseCursor` can also be called to close a
72    /// cursor.
73    Close = 0,
74    // SQL_DROP = 1, is deprecated in favour of SQLFreeHandle
75    /// Sets the `SQL_DESC_COUNT` field of the ARD to 0, releasing all column buffers bound by
76    /// `SQLBindCol` for the given StatementHandle. This does not unbind the bookmark column; to do
77    /// that, the `SQL_DESC_DATA_PTR` field of the ARD for the bookmark column is set to NULL.
78    /// Notice that if this operation is performed on an explicitly allocated descriptor that is
79    /// shared by more than one statement, the operation will affect the bindings of all statements
80    /// that share the descriptor.
81    Unbind = 2,
82    /// Sets the `SQL_DESC_COUNT` field of the APD to 0, releasing all parameter buffers set by
83    /// `SQLBindParameter` for the given StatementHandle. If this operation is performed on an
84    /// explicitly allocated descriptor that is shared by more than one statement, this operation
85    /// will affect the bindings of all the statements that share the descriptor.
86    ResetParams = 3,
87}
88
89/// Represented in C headers as SQLSMALLINT
90#[repr(i16)]
91#[derive(Debug, PartialEq, Eq, Clone, Copy)]
92pub enum HandleType {
93    Env = 1,
94    Dbc = 2,
95    Stmt = 3,
96    Desc = 4,
97    // Only used between Drivers and Driver Manager to enable connection pooling.
98    // https://learn.microsoft.com/en-us/sql/odbc/reference/develop-driver/developing-connection-pool-awareness-in-an-odbc-driver?view=sql-server-ver16
99    // Defined in sqlspi.h
100    DbcInfoToken = 6,
101}
102
103/// Options for `SQLDriverConnect`
104#[repr(u16)]
105#[derive(Debug, PartialEq, Eq, Clone, Copy)]
106pub enum DriverConnectOption {
107    NoPrompt = 0,
108    Complete = 1,
109    Prompt = 2,
110    CompleteRequired = 3,
111}
112
113// Attribute for string lengths
114
115/// SQL_IS_POINTER
116pub const IS_POINTER: i32 = -4;
117/// SQL_IS_UINTEGER
118pub const IS_UINTEGER: i32 = -5;
119/// SQL_IS_INTEGER
120pub const IS_INTEGER: i32 = -6;
121/// SQL_IS_USMALLINT
122pub const IS_USMALLINT: i32 = -7;
123/// SQL_IS_SMALLINT
124pub const IS_SMALLINT: i32 = -8;
125
126/// SQL_YEAR_MONTH_STRUCT
127#[repr(C)]
128#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
129pub struct YearMonth {
130    pub year: UInteger,
131    pub month: UInteger,
132}
133
134/// SQL_DAY_SECOND_STRUCT
135#[repr(C)]
136#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
137pub struct DaySecond {
138    pub day: UInteger,
139    pub hour: UInteger,
140    pub minute: UInteger,
141    pub second: UInteger,
142    pub fraction: UInteger,
143}
144
145/// SQL_INTERVAL_UNION
146#[repr(C)]
147#[derive(Copy, Clone)]
148pub union IntervalUnion {
149    pub year_month: YearMonth,
150    pub day_second: DaySecond,
151}
152
153/// SQL_INTERVAL_STRUCT
154#[repr(C)]
155#[derive(Clone, Copy)]
156pub struct IntervalStruct {
157    pub interval_type: c_int,
158    pub interval_sign: SmallInt,
159    pub interval_value: IntervalUnion,
160}
161
162/// SQL_DATE_STRUCT
163#[repr(C)]
164#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
165pub struct Date {
166    pub year: SmallInt,
167    pub month: USmallInt,
168    pub day: USmallInt,
169}
170
171/// SQL_TIME_STRUCT
172#[repr(C)]
173#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
174pub struct Time {
175    pub hour: USmallInt,
176    pub minute: USmallInt,
177    pub second: USmallInt,
178}
179
180/// SQL_TIMESTAMP_STRUCT
181#[repr(C)]
182#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
183pub struct Timestamp {
184    pub year: SmallInt,
185    pub month: USmallInt,
186    pub day: USmallInt,
187    pub hour: USmallInt,
188    pub minute: USmallInt,
189    pub second: USmallInt,
190    pub fraction: UInteger,
191}
192
193/// SQLGUID
194#[repr(C)]
195#[derive(Debug, Default, PartialEq, Eq, Clone, Copy, Hash)]
196pub struct Guid {
197    pub d1: u32,
198    pub d2: u16,
199    pub d3: u16,
200    pub d4: [u8; 8],
201}
202
203/// Connection attributes for `SQLSetConnectAttr`
204#[repr(i32)]
205#[derive(Debug, PartialEq, Eq, Clone, Copy)]
206pub enum ConnectionAttribute {
207    AsyncEnable = 4,
208    AccessMode = 101,
209    AutoCommit = 102,
210    LoginTimeout = 103,
211    Trace = 104,
212    TraceFile = 105,
213    TranslateLib = 106,
214    TranslateOption = 107,
215    TxnIsolation = 108,
216    CurrentCatalog = 109,
217    OdbcCursors = 110,
218    QuietMode = 111,
219    PacketSize = 112,
220    ConnectionTimeout = 113,
221    DisconnectBehaviour = 114,
222    AsyncDbcFunctionsEnable = 117,
223    AsyncDbcEvent = 119,
224    EnlistInDtc = 1207,
225    EnlistInXa = 1208,
226    ConnectionDead = 1209,
227    AutoIpd = 10001,
228    MetadataId = 10014,
229}
230
231/// `DiagIdentifier` for `SQLGetDiagField`
232#[repr(i32)]
233#[derive(Debug, PartialEq, Eq, Clone, Copy)]
234pub enum HeaderDiagnosticIdentifier {
235    /// SQL_DIAG_RETURNCODE
236    ReturnCode = 1,
237    /// SQL_DIAG_NUMBER
238    Number = 2,
239    /// SQL_DIAG_ROW_COUNT
240    RowCount = 3,
241    /// SQL_DIAG_SQLSTATE
242    SqlState = 4,
243    /// SQL_DIAG_NATIVE
244    Native = 5,
245    /// SQL_DIAG_MESSAGE_TEXT
246    MessageText = 6,
247    /// SQL_DIAG_DYNAMIC_FUNCTION
248    DynamicFunction = 7,
249    /// SQL_DIAG_CLASS_ORIGIN
250    ClassOrigin = 8,
251    /// SQL_DIAG_SUBCLASS_ORIGIN
252    SubclassOrigin = 9,
253    /// SQL_DIAG_CONNECTION_NAME
254    ConnectionName = 10,
255    /// SQL_DIAG_SERVER_NAME
256    ServerName = 11,
257    /// SQL_DIAG_DYNAMIC_FUNCTION_CODE
258    DynamicFunctionCode = 12,
259    /// SQL_DIAG_CURSOR_ROW_COUNT
260    CursorRowCount = -1249,
261    /// SQL_DIAG_ROW_NUMBER
262    RowNumber = -1248,
263    /// SQL_DIAG_COLUMN_NUMBER
264    ColumnNumber = -1247,
265}
266
267#[repr(i32)]
268#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
269pub enum AsyncConnectionBehavior {
270    /// SQL_ASYNC_DBC_ENABLE_ON
271    On = 1,
272    /// SQL_ASYNC_DBC_ENABLE_OFF = 0
273    #[default]
274    Off = 0,
275}
276
277#[repr(i32)]
278#[derive(Debug, PartialEq, Eq, Clone, Copy)]
279pub enum DynamicDiagnosticIdentifier {
280    /// SQL_DIAG_ALTER_DOMAIN
281    AlterDomain = 3,
282    /// SQL_DIAG_ALTER_TABLE,
283    AlterTable = 4,
284    /// SQL_DIAG_CALL
285    Call = 7,
286    /// SQL_DIAG_CREATE_ASSERTION
287    CreateAssertion = 6,
288    /// SQL_DIAG_CREATE_CHARACTER_SET
289    CreateCharacterSet = 8,
290    /// SQL_DIAG_CREATE_COLLATION,
291    CreateCollation = 10,
292    /// SQL_DIAG_CREATE_DOMAIN
293    CreateDomain = 23,
294    /// SQL_DIAG_CREATE_INDEX
295    CreateIndex = -1,
296    /// SQL_DIAG_CREATE_SCHEMA
297    CreateSchema = 64,
298    /// SQL_DIAG_CREATE_TABLE
299    CreateTable = 77,
300    /// SQL_DIAG_CREATE_TRANSLATION
301    CreateTranslation = 79,
302    /// SQL_DIAG_CREATE_VIEW
303    CreateView = 84,
304    /// SQL_DIAG_DELETE_WHERE
305    DeleteWhere = 19,
306    /// SQL_DIAG_DROP_ASSERTION
307    DropAssertion = 24,
308    /// SQL_DIAG_DROP_CHARACTER_SET
309    DropCharacterSet = 25,
310    /// SQL_DIAG_DROP_COLLATION
311    DropCollation = 26,
312    /// SQL_DIAG_DROP_DOMAIN
313    DropDomain = 27,
314    /// SQL_DIAG_DROP_INDEX
315    DropIndex = -2,
316    /// SQL_DIAG_DROP_SCHEMA
317    DropSchema = 31,
318    /// SQL_DIAG_DROP_TABLE
319    DropTable = 32,
320    /// SQL_DIAG_DROP_TRANSLATION
321    DropTranslation = 33,
322    /// SQL_DIAG_DROP_VIEW
323    DropView = 36,
324    /// SQL_DIAG_DYNAMIC_DELETE_CURSOR
325    DynamicDeleteCursor = 38,
326    /// SQL_DIAG_DYNAMIC_UPDATE_CURSOR
327    DynamicUpdateCursor = 81,
328    /// SQL_DIAG_GRANT
329    Grant = 48,
330    /// SQL_DIAG_INSERT
331    Insert = 50,
332    /// SQL_DIAG_REVOKE
333    Revoke = 59,
334    // SQL_DIAG_SELECT_CURSOR
335    SelectCursor = 85,
336    /// SQL_DIAG_UNKNOWN_STATEMENT = 0,
337    UnknownStatement = 0,
338    /// SQL_DIAG_UPDATE_WHERE = 82,
339    UpdateWhere = 82,
340}
341
342/// Completion types for `SQLEndTrans`
343#[repr(i16)]
344#[derive(Debug, PartialEq, Eq, Clone, Copy)]
345pub enum CompletionType {
346    Commit = 0,
347    Rollback = 1,
348}
349
350pub const MAX_NUMERIC_LEN: usize = 16;
351#[repr(C)]
352#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
353pub struct Numeric {
354    pub precision: Char,
355    /// Number of decimal digits to the right of the decimal point.
356    pub scale: SChar,
357    /// 1 if positive, 0 if negative
358    pub sign: Char,
359    pub val: [Char; MAX_NUMERIC_LEN],
360}