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