Skip to content

AscendingCreations/AxumOdbc

Repository files navigation

AxumODBC

Library to Provide an ODBC-API layer.

crates.io docs.rs Minimum Rust Version

License

This project is licensed under either Apache License, Version 2.0, zlib License, or MIT License, at your option.

Help

If you need help with this library or have suggestions please go to our Discord Group

Install

Axum ODBC uses tokio runtime and uses odbc-api = "12.0.1" internally.

# Cargo.toml
[dependencies]
axum_odbc = "0.10.0"

Cargo Feature Flags

iodbc: Sets odbc-api to use iodbc connection manager.

Example

use axum::response::IntoResponse;
use axum::{routing::get, Router};
use axum_odbc::{blocking, ODBCConnectionManager};
use std::net::SocketAddr;
use tokio::net::TcpListener;

#[tokio::main]
async fn main() {

    let manager = ODBCConnectionManager::new("Driver={ODBC Driver 17 for SQL Server};Server=localhost;UID=SomeUserName;PWD=My@Test@Password1;Database=Test;", 5);

    
    // build our application with some routes
    let app = Router::new()
        .route("/drop", get(drop_table))
        .route("/create", get(create_table))
        .with_state(manager);

    // run it
    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
    tracing::debug!("listening on {}", addr);
    let listener = TcpListener::bind(addr).await.unwrap();
    axum::serve(
        listener,
        app.into_make_service_with_connect_info::<SocketAddr>(),
    )
    .await
    .unwrap();
}

async fn drop_table(manager: ODBCConnectionManager) -> impl IntoResponse {
    let connection = manager.aquire().await.unwrap();

    blocking!(
        let _ = connection.execute("DROP TABLE IF EXISTS testy", (), None).unwrap();
    );

    "compeleted".to_string()
}

async fn create_table(manager: ODBCConnectionManager) -> impl IntoResponse {
    let connection = manager.aquire().await.unwrap();

    blocking!(
            let _ = connection.execute(
            "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='testy' AND xtype='U')
            CREATE TABLE testy (
            id INT PRIMARY KEY,
            name VARCHAR(100) NOT NULL
            );",
            (),
            None,
        ).unwrap();
    );

    "compeleted".to_string()
}

About

ODBC layer for Axum using odbc-api

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages