odbc_ffi/
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 sqlreturn;
12pub use self::sqlreturn::*;
13mod info_type;
14pub use self::info_type::*;
15mod fetch_orientation;
16pub use self::fetch_orientation::*;
17mod attributes;
18pub use self::attributes::*;
19use std::os::raw::{c_void, c_short, c_ushort, c_int};
20mod c_data_type;
21pub use self::c_data_type::*;
22
23//These types can never be instantiated in Rust code.
24pub enum Obj {}
25pub enum Env {}
26pub enum Dbc {}
27pub enum Stmt {}
28
29pub type SQLHANDLE = *mut Obj;
30pub type SQLHENV = *mut Env;
31
32/// The connection handle references storage of all information about the connection to the data
33/// source, including status, transaction state, and error information.
34pub type SQLHDBC = *mut Dbc;
35pub type SQLHSTMT = *mut Stmt;
36
37/// 16 Bit signed integer
38pub type SQLSMALLINT = c_short;
39pub type SQLUSMALLINT = c_ushort;
40pub type SQLINTEGER = c_int;
41pub type SQLPOINTER = *mut c_void;
42pub type SQLCHAR = u8;
43
44#[cfg(all(windows, target_pointer_width = "64"))]
45pub type SQLLEN = i64;
46#[cfg(all(windows, target_pointer_width = "32"))]
47pub type SQLLEN = SQLINTEGER;
48#[cfg(not(windows))]
49pub type SQLLEN = SQLINTEGER;
50
51pub type SQLHWND = SQLPOINTER;
52
53// flags for null-terminated string
54pub const SQL_NTS: SQLSMALLINT = -3;
55pub const SQL_NTSL: SQLINTEGER = -3;
56
57/// Maximum message length
58pub const SQL_MAX_MESSAGE_LENGTH: SQLSMALLINT = 512;
59pub const SQL_SQLSTATE_SIZE: usize = 5;
60
61// Special SQLGetData indicator values
62pub const SQL_NULL_DATA: SQLLEN = -1;
63pub const SQL_NO_TOTAL: SQLLEN = -4;
64
65/// SQL Data Types
66#[repr(i16)]
67#[allow(non_camel_case_types)]
68#[derive(Debug, PartialEq, Eq, Clone, Copy)]
69pub enum SqlDataType {
70    SQL_UNKNOWN_TYPE = 0, // also called SQL_VARIANT_TYPE since odbc 4.0
71    SQL_CHAR = 1,
72    SQL_NUMERIC = 2,
73    SQL_DECIMAL = 3,
74    SQL_INTEGER = 4,
75    SQL_SMALLINT = 5,
76    SQL_FLOAT = 6,
77    SQL_REAL = 7,
78    SQL_DOUBLE = 8,
79    SQL_DATETIME = 9,
80    SQL_VARCHAR = 12,
81    #[cfg(feature = "odbc_version_4")]
82    SQL_UDT = 17,
83    #[cfg(feature = "odbc_version_4")]
84    SQL_ROW = 19,
85    #[cfg(feature = "odbc_version_4")]
86    SQL_ARRAY = 50,
87    #[cfg(feature = "odbc_version_4")]
88    SQL_MULTISET = 55,
89}
90pub use self::SqlDataType::*;
91
92/// Represented in C headers as SQLSMALLINT
93#[repr(i16)]
94#[allow(non_camel_case_types)]
95#[derive(Debug, PartialEq, Eq, Clone, Copy)]
96pub enum HandleType {
97    SQL_HANDLE_ENV = 1,
98    SQL_HANDLE_DBC = 2,
99    SQL_HANDLE_STMT = 3,
100    SQL_HANDLE_DESC = 4,
101}
102pub use self::HandleType::*;
103
104/// Options for `SQLDriverConnect`
105#[repr(u16)]
106#[allow(non_camel_case_types)]
107#[derive(Debug, PartialEq, Eq, Clone, Copy)]
108pub enum SqlDriverConnectOption {
109    SQL_DRIVER_NOPROMPT = 0,
110    SQL_DRIVER_COMPLETE = 1,
111    SQL_DRIVER_PROMPT = 2,
112    SQL_DRIVER_COMPLETE_REQUIRED = 3,
113}
114pub use self::SqlDriverConnectOption::*;
115
116#[cfg_attr(windows, link(name="odbc32"))]
117#[cfg_attr(not(windows), link(name="odbc"))]
118extern "C" {
119    /// Allocates an environment, connection, statement, or descriptor handle.
120    ///
121    /// # Returns
122    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR`, or `SQL_INVALID_HANDLE`
123    pub fn SQLAllocHandle(handle_type: HandleType,
124                          input_handle: SQLHANDLE,
125                          output_Handle: *mut SQLHANDLE)
126                          -> SQLRETURN;
127
128    /// Frees resources associated with a specific environment, connection, statement, or
129    /// descriptor handle.
130    ///
131    /// If `SQL_ERRQR` is returned the handle is still valid.
132    /// # Returns
133    /// `SQL_SUCCESS`, `SQL_ERROR`, or `SQL_INVALID_HANDLE`
134    pub fn SQLFreeHandle(handle_type: HandleType, handle: SQLHANDLE) -> SQLRETURN;
135
136    /// Sets attributes that govern aspects of environments
137    ///
138    /// # Returns
139    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR`, or `SQL_INVALID_HANDLE`
140    pub fn SQLSetEnvAttr(environment_handle: SQLHENV,
141                         attribute: EnvironmentAttribute,
142                         value: SQLPOINTER,
143                         string_length: SQLINTEGER)
144                         -> SQLRETURN;
145
146    /// Closes the connection associated with a specific connection handle.
147    ///
148    /// # Returns
149    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR`, or `SQL_INVALID_HANDLE`
150    pub fn SQLDisconnect(connection_handle: SQLHDBC) -> SQLRETURN;
151
152    pub fn SQLGetDiagRec(handle_type: HandleType,
153                         handle: SQLHANDLE,
154                         RecNumber: SQLSMALLINT,
155                         state: *mut SQLCHAR,
156                         native_error_ptr: *mut SQLINTEGER,
157                         message_text: *mut SQLCHAR,
158                         buffer_length: SQLSMALLINT,
159                         text_length_ptr: *mut SQLSMALLINT)
160                         -> SQLRETURN;
161
162    pub fn SQLExecDirect(statement_handle: SQLHSTMT,
163                         statement_text: *const SQLCHAR,
164                         text_length: SQLINTEGER)
165                         -> SQLRETURN;
166
167    pub fn SQLNumResultCols(statement_handle: SQLHSTMT,
168                            column_count_ptr: *mut SQLSMALLINT)
169                            -> SQLRETURN;
170
171    // Can be used since odbc version 3.8 to stream results
172    pub fn SQLGetData(statement_handle: SQLHSTMT,
173                      col_or_param_num: SQLUSMALLINT,
174                      target_type: SqlCDataType,
175                      target_value_ptr: SQLPOINTER,
176                      buffer_length: SQLLEN,
177                      str_len_or_ind_ptr: *mut SQLLEN)
178                      -> SQLRETURN;
179
180    /// SQLFetch fetches the next rowset of data from the result set and returns data for all bound
181    /// columns.
182    ///
183    /// # Returns
184    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR`, `SQL_INVALID_HANDLE`, `SQL_NO_DATA` or
185    /// `SQL_STILL_EXECUTING`
186    pub fn SQLFetch(statement_handle: SQLHSTMT) -> SQLRETURN;
187
188    /// Returns general information about the driver and data source associated with a connection
189    ///
190    /// # Returns
191    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR`, or `SQL_INVALID_HANDLE`
192    pub fn SQLGetInfo(connection_handle: SQLHDBC,
193                      info_type: InfoType,
194                      info_value_ptr: SQLPOINTER,
195                      buffer_length: SQLSMALLINT,
196                      string_length_ptr: *mut SQLSMALLINT)
197                      -> SQLRETURN;
198
199    /// SQLConnect establishes connections to a driver and a data source.
200    ///
201    /// The connection handle references storage of all information about the connection to the
202    /// data source, including status, transaction state, and error information.
203    ///
204    /// # Returns
205    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR`, `SQL_INVALID_HANDLE`, or
206    /// `SQL_STILL_EXECUTING`
207    pub fn SQLConnect(connection_handle: SQLHDBC,
208                      server_name: *const SQLCHAR,
209                      name_length_1: SQLSMALLINT,
210                      user_name: *const SQLCHAR,
211                      name_length_2: SQLSMALLINT,
212                      authentication: *const SQLCHAR,
213                      name_length_3: SQLSMALLINT)
214                      -> SQLRETURN;
215
216    /// Returns the list of table, catalog, or schema names, and table types, stored in a specific
217    /// data source. The driver returns the information as a result set
218    ///
219    /// # Returns
220    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR`, `SQL_INVALID_HANDLE`, or
221    /// `SQL_STILL_EXECUTING`
222    pub fn SQLTables(statement_handle: SQLHSTMT,
223                     catalog_name: *const SQLCHAR,
224                     name_length_1: SQLSMALLINT,
225                     schema_name: *const SQLCHAR,
226                     name_length_2: SQLSMALLINT,
227                     table_name: *const SQLCHAR,
228                     name_length_3: SQLSMALLINT,
229                     TableType: *const SQLCHAR,
230                     name_length_4: SQLSMALLINT)
231                     -> SQLRETURN;
232
233    /// Returns information about a data source. This function is implemented only by the Driver
234    /// Manager.
235    ///
236    /// # Returns
237    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR`, `SQL_INVALID_HANDLE`, or `SQL_NO_DATA`
238    pub fn SQLDataSources(environment_handle: SQLHENV,
239                          direction: FetchOrientation,
240                          server_name: *mut SQLCHAR,
241                          buffer_length_1: SQLSMALLINT,
242                          name_length_1: *mut SQLSMALLINT,
243                          description: *mut SQLCHAR,
244                          buffer_length_2: SQLSMALLINT,
245                          name_length_2: *mut SQLSMALLINT)
246                          -> SQLRETURN;
247
248    /// An alternative to `SQLConnect`. It supports data sources that require more connection
249    /// information than the three arguments in `SQLConnect`, dialog boxes to prompt the user for
250    /// all connection information, and data sources that are not defined in the system information
251    ///
252    /// # Returns
253    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR`, `SQL_INVALID_HANDLE`, `SQL_NO_DATA`,
254    /// or `SQL_STILL_EXECUTING`
255    pub fn SQLDriverConnect(connection_handle: SQLHDBC,
256                            window_handle: SQLHWND,
257                            in_connection_string: *const SQLCHAR,
258                            string_length_1: SQLSMALLINT,
259                            out_connection_string: *mut SQLCHAR,
260                            buffer_length: SQLSMALLINT,
261                            string_length_2: *mut SQLSMALLINT,
262                            DriverCompletion: SqlDriverConnectOption)
263                            -> SQLRETURN;
264
265    /// Lists driver descriptions and driver attribute keywords. This function is implemented only
266    /// by the Driver Manager.
267    ///
268    /// # Returns
269    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR`, `SQL_INVALID_HANDLE`, or `SQL_NO_DATA`
270    pub fn SQLDrivers(henv: SQLHENV,
271                      direction: FetchOrientation,
272                      driver_desc: *mut SQLCHAR,
273                      driver_desc_max: SQLSMALLINT,
274                      out_driver_desc: *mut SQLSMALLINT,
275                      driver_attributes: *mut SQLCHAR,
276                      drvr_attr_max: SQLSMALLINT,
277                      out_drvr_attr: *mut SQLSMALLINT)
278                      -> SQLRETURN;
279
280    /// Closes a cursor that has been opened on a statement and discards pending results.
281    ///
282    /// # Returns
283    /// `SQL_SUCCESS`, `SQL_SUCCESS_WITH_INFO`, `SQL_ERROR` or `SQL_INVALID_HANDLE`
284    pub fn SQLCloseCursor(hstmt: SQLHSTMT) -> SQLRETURN;
285}