Code Monkey home page Code Monkey logo

odbc-sys's Introduction

odbc-sys

Docs MIT licensed Crates.io version

FFI (Foreign Function Interface) bindings for ODBC (Open Database Connectivity) As ffi bindings to C-APIs are low level by nature this library is intended to be the foundation of other libraries to build on top, rather than to be used directly.

Design Goals

  • Providing declarations of ODBC Symbols compatible to the C-Interface of an ODBC Driver Manager
  • Provide correct definition of symbols for Unix and Windows in either 32Bit or 64Bit flavour
  • Not to abstract away any power of the underlying API
  • Increase type safety where feasible
  • As it is as of now unlikely to happen that anyone is writing ODBC 2.0 applications in Rust therefore deprecated symbols like 'SQLAllocEnv' have been left out intentionally.

Linking

This library will link against odbc32.dll (preinstalled) on Windows systems. On Linux and macOS it links against libodbc.so by default. This is typically provided by unix-odbc. Using the --feature iodbc you can also link against libiodbc.so. This may be interesting if you are trying to connect to some older data sources on macOS.

Installing unix-odbc

Linux

Use your systems packet manager to install unixodbc-dev. E.g. on Ubuntu / Debian

sudo apt install unixodbc-dev

macOS

On Intel based architectures you can install unix-odbc using homebrew.

brew install unixodbc

Note for ARM based macOS Systems (M1 processors and later):

cargo build is not going to pick up libodbc.so installed via homebrew due to the fact that homebrew on ARM Mac installs into /opt/homebrew/Cellar as opposed to /usr/local/opt/.

You find documentation on what directories are searched during build here: https://doc.rust-lang.org/cargo/reference/environment-variables.html#dynamic-library-paths.

You can also install unixODBC from source:

  1. copy the unixODBC-2.3.9.tar.gz file somewhere you can create files and directories
  2. gunzip unixODBC*.tar.gz
  3. tar xvf unixODBC*.tar
  4. ./configure
  5. make
  6. make install

Thanks to @TBPixel for testing this!

Windows

As windows does ship with ODBC preinstalled, you are good to go out of the box.

Current State

Symbols are added to this library as we go along implementing uses cases in higher level APIs. If you miss something please do not hesitate to contribute.

Documentation

Thanks to the folks of docs.rs for building and hosting the documentation!

Contributing

Want to help out? Just create an issue or pull request.

odbc-sys's People

Contributors

adamreichold avatar brandonros avatar cristianberneanu avatar itn3000 avatar lfrancke avatar mversic avatar nazar554 avatar pacman82 avatar palfrey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

odbc-sys's Issues

Open to extensions required to write an ODBC driver?

Hi,

I'm wondering if you're open to extending this crate with additional information / code required to implement ODBC drivers as opposed to applications calling ODBC functions.

I have listed two simple examples below but I'm sure I'll come up with more.
I'd be happy to send PRs but as they are obviously a lot of work I wanted to make sure that you'd be willing to accept something along those lines in principle. I understand if not as it clutters this library but as it concerns the core types mostly it can't be easily moved elsewhere.

Example 1

pub fn SQLAllocHandle(
    handle_type: u16,
    input_handle: *mut c_void,
    output_handle: *mut *mut c_void,
) -> SqlReturn

In the driver I do not want handle_type to take a HandleType so I can handle a potential error in case an invalid value is passed in.
The Rust behaviour is undefined here (converting an undefined u16 to the enum). I'm currently using TryFromPrimitive from the num_enum crate for that:

use num_enum::TryFromPrimitive;

#[repr(i16)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, TryFromPrimitive)]
pub enum HandleType {
    Env = 1,
    Dbc = 2,
    Stmt = 3,
    Desc = 4,
}

Example 2

Another example is the InfoType which requires information about the expected return type for each variant.

Example snippet:

impl InfoType {
    pub fn return_type(&self) -> InfoTypeTypeInformation {
        match self {
            InfoType::MaxDriverConnections => InfoTypeTypeInformation::SqlUSmallInt,
            InfoType::MaxConcurrentActivities => InfoTypeTypeInformation::SqlUSmallInt,
            InfoType::DataSourceName => InfoTypeTypeInformation::String,
            InfoType::ServerName => InfoTypeTypeInformation::String,
[...]

target_value_ptr of SQLGetData return 0x0

Hi,
I want to use SQLGetData function to get some data from mysql (ver:8) table.
But,target_value_ptr of SQLGetData always return 0x0 .Why?
All SqlReturn return success before using SQLGetData function.

Table name xxx
(
id int unsigned NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb3

Souce

    let mut id = null_mut();
    let mut str_len = null_mut();
    loop {
        let rc1 = SQLFetch(stmt_handle as HStmt);
        if (SqlReturn::SUCCESS == rc1) || (SqlReturn::SUCCESS_WITH_INFO == rc1) {
            let subrc = SQLGetData(
                      stmt_handle as HStmt,
                       1 as u16,
                       CDataType::SLong,
                       id,
                       8,
                       str_len as *mut isize
            );
            println!("{:?},{:?},{:?}",subrc,id,str_len);
        } else {
            break;
         }
    }

Result
SqlReturn(-1),0x0,0x0
SqlReturn(-1),0x0,0x0

How to Get Rows Updated when executing Insert Command

I am trying to insert a record in to table using select statement and I want to know how many records inserted. I am not able to get the results using cursor. Is there any other ways to get number of records affected in ODBC

Support build based in Mac M1

Hello,
Currently build seems failing on Apple Silicon arch:

$ cargo build    
   Compiling proc-macro2 v1.0.24
 ...
   Compiling odbc-api v0.13.2 (/Users/kination/workspace/oss/odbc-api/odbc-api)
   Compiling odbcsv v0.3.10 (/Users/kination/workspace/oss/odbc-api/odbcsv)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-arch" "arm64" "-L" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib" "/Users/kination/workspace/oss/odbc-api/target/debug/deps/odbcsv.11niznwudken0zxs.rcgu.o" "/Users/kination/workspace/oss/odbc-api/target/debug/deps/odbcsv.14ixz821r572n068.rcgu.o" 
...
...
...
"/Users/kination/workspace/oss/odbc-api/target/debug/deps/libanyhow-0c6d6a486596233a.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd-66c007ef72d8663e.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libpanic_unwind-a9451d2661722154.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libobject-762dc53d4ee196bf.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libaddr2line-8574628b478a5c23.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libgimli-81d5bba5f99dc60c.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_demangle-2ca8284befe9e594.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libhashbrown-b6e7b0a96c79838a.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_alloc-a91baa24b7903563.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libunwind-be586febb1d2abe5.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcfg_if-91703e117e420011.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/liblibc-8293d4970e524cf2.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/liballoc-59ea5e5ce6eb23cb.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_core-92186cf5c5d5835d.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcore-80a2abfc52170c57.rlib" "/Users/kination/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcompiler_builtins-e46c25039c9cecae.rlib" "-lodbc" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: ld: warning: directory not found for option '-L/Users/kination/lib'
          ld: library not found for -lodbc
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: aborting due to previous error

error: could not compile `odbcsv`

Not sure whether it caused by unsupported Rust version, or other dependencies(such as odbc-sys)...
Please let me know if you need more information ๐Ÿ™

AttrOdbcVersion - ODBC 2/3.8/4 support

An outside application might call SQLSetEnvAttr with the argument SQL_OV_ODBC2.
Currently this is not part of the AttrOdbcVersion enum because you don't support ODBC2.
That's fair and reasonable for applications but as a driver there is little choice over the incoming requests.

I didn't immediately open a PR for this to ask your opinion first: This is one example but there might be others down the line where a driver needs a method or a constant or whatever that is not applicable to the applications.

In this case I need the constant to then be able to say that it's not supported. The same goes for 3.8 and 4.0 which are currently behind a feature gate.

I hope the explanation makes sense?

My concrete ask:

  • Add ODBC 2 to AttrOdbcVersion
  • Make 3.8 & 4 available without feature gate

Unsafe enum usage

Hi @pacman82 , looks like we have an issue in FFI binding which causes UB. Passing mut enum ref across FFI boundaries causes UB in cases when ODBC driver returns value absent in enum.

Details: https://users.rust-lang.org/t/undefined-behaviour-after-unsafe-enum-usage/15572
Related odbc-rs issue: Koka/odbc-rs#66
Related discussion in rust-bindgen: rust-lang/rust-bindgen#667

Looks like it needs to be fixed at lowest level available i.e. in this crate, I'm unsure ho to fix this without harming usability though.

Looks like we could use manual conversion crates like https://crates.io/crates/enum_primitive or declare enums as #[non_exhaustive] rust-lang/rust#44109 when it will be ready. Or just declare everything as constants and don't use enums at all

no example

Hi,
I need to get a simple example to use this crate.
1 connect any DB
2 execute SQL
HELP please.

SQLPutData support

Hi!
First of all, thanks for your library.

We are trying to use another library that depends on this one (odbc-rs) in order to connect to an Oracle Database.

Due to how Oracle Databases work with BLOB types (they cap at 2kb for a bind_parameter), one needs to use the SQLPutData method:

https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlputdata-function?view=sql-server-ver15

However, the library does not expose it.

Is there any reason? Is it planned to be supported in the future?

Thanks!

Why rename to CamelCase?

I see there was some work being done here. I like that enums were replaced by newtype + consts

I see that some of the structs and types were shortened and/or renamed to CamelCase. One may find that to be more aesthetically pleasing but I think it introduces unnecessary mental switch. If a user consults the ODBC standard, they will have a problem of finding the equivalent identifier(i.e. struct or type name) in this library because it's been shortened or CamelCased.

If this library aims to be a port of the ODBC standard to Rust I maintain that it should not deviate in it's naming conventions. Naming conventions are a part of the standard and if a standard defines names of identifiers then those names should be exposed to users of this library.

Higher level wrappers of this library have not yet caught up with API changes of this library so revert will still be painless if it's decided upon

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.