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.
[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);
let app = Router::new()
.route("/drop", get(drop_table))
.route("/create", get(create_table))
.with_state(manager);
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()
}