mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
Run rustfmt and remove some unneeded stuff
This commit is contained in:
parent
7077790452
commit
ee30296e32
24
Cargo.toml
24
Cargo.toml
@ -1,41 +1,35 @@
|
||||
[workspace]
|
||||
members = [
|
||||
".",
|
||||
"examples/todos",
|
||||
"examples/contacts"
|
||||
]
|
||||
|
||||
[package]
|
||||
name = "sqlx"
|
||||
version = "0.0.0"
|
||||
authors = ["Ryan Leckey <leckey.ryan@gmail.com>"]
|
||||
version = "0.1.1-pre"
|
||||
authors = [
|
||||
"Ryan Leckey <leckey.ryan@gmail.com>"
|
||||
]
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "Asynchronous and expressive database client in pure Rust."
|
||||
edition = "2018"
|
||||
|
||||
[features]
|
||||
default = ["postgres"]
|
||||
default = []
|
||||
postgres = []
|
||||
mariadb = []
|
||||
|
||||
[dependencies]
|
||||
bitflags = "1.1.0"
|
||||
async-stream = "0.1.0"
|
||||
bitflags = "1.1.0"
|
||||
byteorder = { version = "1.3.2", default-features = false }
|
||||
bytes = "0.4.12"
|
||||
crossbeam-queue = "0.1.2"
|
||||
crossbeam-utils = { version = "0.6.6", default-features = false }
|
||||
failure = "0.1.5"
|
||||
futures-util-preview = "=0.3.0-alpha.18"
|
||||
futures-channel-preview = "=0.3.0-alpha.18"
|
||||
futures-core-preview = "=0.3.0-alpha.18"
|
||||
futures-util-preview = "=0.3.0-alpha.18"
|
||||
hex = { version = "0.3.2", default-features = false }
|
||||
# itoa = "0.4.4"
|
||||
log = "0.4.8"
|
||||
md-5 = "0.8.0"
|
||||
url = "2.1.0"
|
||||
memchr = "2.2.1"
|
||||
async-stream = "0.1.0"
|
||||
tokio = { version = "=0.2.0-alpha.2", default-features = false, features = [ "tcp" ] }
|
||||
url = "2.1.0"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
[package]
|
||||
name = "contacts"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
sqlx = { path = "../..", features = [ "postgres" ] }
|
||||
failure = "0.1.5"
|
||||
env_logger = "0.6.2"
|
||||
tokio = { version = "=0.2.0-alpha.2" }
|
||||
futures-preview = "=0.3.0-alpha.18"
|
||||
fake = { version = "2.0", features=[ "derive" ] }
|
||||
rand = "0.7.0"
|
||||
@ -1,141 +0,0 @@
|
||||
#![feature(async_await, try_blocks)]
|
||||
|
||||
use failure::Fallible;
|
||||
use fake::{
|
||||
faker::{
|
||||
internet::en::{Password, SafeEmail, Username},
|
||||
name::en::Name,
|
||||
phone_number::en::PhoneNumber,
|
||||
},
|
||||
Dummy, Fake, Faker,
|
||||
};
|
||||
use futures::{channel::oneshot::channel, future, stream::TryStreamExt};
|
||||
use sqlx::{Pool, Postgres};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
type PostgresPool = Pool<Postgres>;
|
||||
|
||||
#[derive(Debug, Dummy)]
|
||||
struct Contact {
|
||||
#[dummy(faker = "Name()")]
|
||||
name: String,
|
||||
|
||||
#[dummy(faker = "Username()")]
|
||||
username: String,
|
||||
|
||||
#[dummy(faker = "Password(5..25)")]
|
||||
password: String,
|
||||
|
||||
#[dummy(faker = "SafeEmail()")]
|
||||
email: String,
|
||||
|
||||
#[dummy(faker = "PhoneNumber()")]
|
||||
phone: String,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Fallible<()> {
|
||||
env_logger::try_init()?;
|
||||
|
||||
let pool = PostgresPool::new("postgres://postgres@127.0.0.1/sqlx__dev", 85);
|
||||
|
||||
ensure_schema(&pool).await?;
|
||||
insert(&pool, 50_000).await?;
|
||||
select(&pool, 1_000).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn ensure_schema(pool: &PostgresPool) -> Result<(), sqlx::Error> {
|
||||
sqlx::query(
|
||||
r#"
|
||||
CREATE TABLE IF NOT EXISTS contacts (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
name TEXT NOT NULL,
|
||||
username TEXT NOT NULL,
|
||||
password TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
phone TEXT NOT NULL
|
||||
)
|
||||
"#,
|
||||
)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
|
||||
pool.execute("TRUNCATE contacts", ()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn insert(pool: &PostgresPool, count: usize) -> Result<(), sqlx::Error> {
|
||||
let start_at = Instant::now();
|
||||
let mut handles = vec![];
|
||||
|
||||
for _ in 0..count {
|
||||
let pool = pool.clone();
|
||||
let contact: Contact = Faker.fake();
|
||||
let (tx, rx) = channel::<()>();
|
||||
|
||||
tokio::spawn(async move {
|
||||
pool.execute(
|
||||
r#"
|
||||
INSERT INTO contacts (name, username, password, email, phone)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
"#,
|
||||
(
|
||||
contact.name,
|
||||
contact.username,
|
||||
contact.password,
|
||||
contact.email,
|
||||
contact.phone,
|
||||
),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
tx.send(()).unwrap();
|
||||
});
|
||||
|
||||
handles.push(rx);
|
||||
}
|
||||
|
||||
future::join_all(handles).await;
|
||||
|
||||
let elapsed = start_at.elapsed();
|
||||
|
||||
println!("insert {} rows in {:?}", count, elapsed);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn select(pool: &PostgresPool, iterations: usize) -> Result<(), sqlx::Error> {
|
||||
let start_at = Instant::now();
|
||||
let mut rows: usize = 0;
|
||||
|
||||
for _ in 0..iterations {
|
||||
// TODO: Once we have FromRow derives we can replace this with Vec<Contact>
|
||||
let contacts: Vec<(String, String, String, String, String)> = pool
|
||||
.fetch(
|
||||
r#"
|
||||
SELECT name, username, password, email, phone
|
||||
FROM contacts
|
||||
"#,
|
||||
(),
|
||||
)
|
||||
.try_collect()
|
||||
.await?;
|
||||
|
||||
rows = contacts.len();
|
||||
}
|
||||
|
||||
let elapsed = start_at.elapsed();
|
||||
let per = Duration::from_nanos((elapsed.as_nanos() / (iterations as u128)) as u64);
|
||||
|
||||
println!(
|
||||
"select {} rows in ~{:?} [ x{} in {:?} ]",
|
||||
rows, per, iterations, elapsed
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
[package]
|
||||
name = "todos"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
sqlx = { path = "../..", features = [ "postgres" ] }
|
||||
failure = "0.1.5"
|
||||
env_logger = "0.6.2"
|
||||
tokio = { version = "=0.2.0-alpha.2" }
|
||||
futures-preview = "=0.3.0-alpha.18"
|
||||
structopt = "0.2.18"
|
||||
@ -1,113 +0,0 @@
|
||||
#![feature(async_await)]
|
||||
|
||||
use failure::Fallible;
|
||||
use futures::{future, TryStreamExt};
|
||||
use sqlx::{Connection, Postgres};
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(StructOpt, Debug)]
|
||||
struct Options {
|
||||
#[structopt(subcommand)]
|
||||
cmd: Option<Command>,
|
||||
}
|
||||
|
||||
#[derive(StructOpt, Debug)]
|
||||
enum Command {
|
||||
#[structopt(name = "add")]
|
||||
Add { text: String },
|
||||
|
||||
#[structopt(name = "done")]
|
||||
MarkAsDone { id: i64 },
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Fallible<()> {
|
||||
env_logger::try_init()?;
|
||||
|
||||
let opt = Options::from_args();
|
||||
|
||||
let mut conn =
|
||||
Connection::<Postgres>::establish("postgres://postgres@127.0.0.1/sqlx__dev").await?;
|
||||
|
||||
ensure_schema(&mut conn).await?;
|
||||
|
||||
match opt.cmd {
|
||||
Some(Command::Add { text }) => {
|
||||
add_task(&mut conn, &text).await?;
|
||||
}
|
||||
|
||||
Some(Command::MarkAsDone { id }) => {
|
||||
mark_task_as_done(&mut conn, id).await?;
|
||||
}
|
||||
|
||||
None => {
|
||||
print_all_tasks(&mut conn).await?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn ensure_schema(conn: &mut Connection<Postgres>) -> Fallible<()> {
|
||||
conn.execute("BEGIN", ()).await?;
|
||||
|
||||
sqlx::query(
|
||||
r#"
|
||||
CREATE TABLE IF NOT EXISTS tasks (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
text TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
done_at TIMESTAMPTZ
|
||||
)
|
||||
"#,
|
||||
)
|
||||
.execute(conn)
|
||||
.await?;
|
||||
|
||||
conn.execute("COMMIT", ()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn print_all_tasks(conn: &mut Connection<Postgres>) -> Fallible<()> {
|
||||
conn.fetch(
|
||||
r#"
|
||||
SELECT id, text
|
||||
FROM tasks
|
||||
WHERE done_at IS NULL
|
||||
"#,
|
||||
(),
|
||||
)
|
||||
.try_for_each(|(id, text): (i64, String)| {
|
||||
// language=text
|
||||
println!("{:>5} | {}", id, text);
|
||||
|
||||
future::ok(())
|
||||
})
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_task(conn: &mut Connection<Postgres>, text: &str) -> Fallible<()> {
|
||||
conn.execute("INSERT INTO tasks ( text ) VALUES ( $1 )", (text,))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn mark_task_as_done(conn: &mut Connection<Postgres>, id: i64) -> Fallible<()> {
|
||||
// language=sql
|
||||
sqlx::query(
|
||||
r#"
|
||||
UPDATE tasks
|
||||
SET done_at = now()
|
||||
WHERE id = $1
|
||||
"#,
|
||||
)
|
||||
.bind(id)
|
||||
.execute(conn)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
30
src/lib.rs
30
src/lib.rs
@ -2,9 +2,6 @@
|
||||
#![allow(clippy::needless_lifetimes)]
|
||||
#![allow(unused)]
|
||||
|
||||
#[macro_use]
|
||||
extern crate bitflags;
|
||||
|
||||
#[cfg(test)]
|
||||
extern crate test;
|
||||
|
||||
@ -20,9 +17,21 @@ pub mod deserialize;
|
||||
#[macro_use]
|
||||
pub mod row;
|
||||
|
||||
mod connection;
|
||||
pub mod error;
|
||||
mod executor;
|
||||
mod pool;
|
||||
mod query;
|
||||
pub mod serialize;
|
||||
pub mod types;
|
||||
|
||||
pub use self::{
|
||||
connection::Connection,
|
||||
error::Error,
|
||||
pool::Pool,
|
||||
query::{query, SqlQuery},
|
||||
};
|
||||
|
||||
#[cfg(feature = "mariadb")]
|
||||
pub mod mariadb;
|
||||
|
||||
@ -31,18 +40,3 @@ pub mod postgres;
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
pub use self::postgres::Postgres;
|
||||
|
||||
pub mod error;
|
||||
|
||||
mod connection;
|
||||
pub mod error;
|
||||
mod executor;
|
||||
mod pool;
|
||||
mod query;
|
||||
|
||||
pub use self::{
|
||||
connection::Connection,
|
||||
error::Error,
|
||||
pool::Pool,
|
||||
query::{query, SqlQuery},
|
||||
};
|
||||
|
||||
@ -1,19 +1,14 @@
|
||||
use super::MariaDbRawConnection;
|
||||
use crate::{
|
||||
mariadb::{
|
||||
Capabilities, ComStmtExec, DeContext, Decode, EofPacket, ErrPacket,
|
||||
HandshakeResponsePacket, InitialHandshakePacket, OkPacket, ProtocolType, StmtExecFlag,
|
||||
},
|
||||
use crate::mariadb::{
|
||||
Capabilities, ComStmtExec, DeContext, Decode, EofPacket, ErrPacket, HandshakeResponsePacket,
|
||||
InitialHandshakePacket, OkPacket, ProtocolType, StmtExecFlag,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
use failure::{err_msg, Error};
|
||||
use std::ops::BitAnd;
|
||||
use url::Url;
|
||||
|
||||
pub async fn establish(
|
||||
conn: &mut MariaDbRawConnection,
|
||||
url: Url
|
||||
) -> Result<(), Error> {
|
||||
pub async fn establish(conn: &mut MariaDbRawConnection, url: Url) -> Result<(), Error> {
|
||||
let buf = conn.stream.next_packet().await?;
|
||||
let mut de_ctx = DeContext::new(&mut conn.context, buf);
|
||||
let initial = InitialHandshakePacket::decode(&mut de_ctx)?;
|
||||
@ -54,17 +49,14 @@ mod test {
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_connect() -> Result<(), Error> {
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306")
|
||||
.await?;
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306").await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_ping() -> Result<(), Error> {
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306")
|
||||
|
||||
.await?;
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306").await?;
|
||||
|
||||
conn.ping().await?;
|
||||
|
||||
@ -73,8 +65,7 @@ mod test {
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_select_db() -> Result<(), Error> {
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306")
|
||||
.await?;
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306").await?;
|
||||
|
||||
conn.select_db("test").await?;
|
||||
|
||||
@ -83,8 +74,7 @@ mod test {
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_query() -> Result<(), Error> {
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306")
|
||||
.await?;
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306").await?;
|
||||
|
||||
conn.select_db("test").await?;
|
||||
|
||||
@ -95,8 +85,7 @@ mod test {
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_prepare() -> Result<(), Error> {
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306")
|
||||
.await?;
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306").await?;
|
||||
|
||||
conn.select_db("test").await?;
|
||||
|
||||
@ -108,8 +97,7 @@ mod test {
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_can_execute_prepared() -> Result<(), Error> {
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306")
|
||||
.await?;
|
||||
let mut conn = MariaDbRawConnection::establish(&"mariadb://root@127.0.0.1:3306").await?;
|
||||
|
||||
conn.select_db("test").await?;
|
||||
|
||||
@ -151,9 +139,7 @@ mod test {
|
||||
|
||||
#[tokio::test]
|
||||
async fn it_does_not_connect() -> Result<(), Error> {
|
||||
match MariaDbRawConnection::establish(&"mariadb//roote@127.0.0.1:3306")
|
||||
.await
|
||||
{
|
||||
match MariaDbRawConnection::establish(&"mariadb//roote@127.0.0.1:3306").await {
|
||||
Ok(_) => Err(err_msg("Bad username still worked?")),
|
||||
Err(_) => Ok(()),
|
||||
}
|
||||
|
||||
@ -5,7 +5,5 @@ pub async fn execute(conn: &mut MariaDbRawConnection) -> io::Result<u64> {
|
||||
conn.flush().await?;
|
||||
|
||||
let mut rows: u64 = 0;
|
||||
while let Some(message) = conn.receive().await? {
|
||||
|
||||
}
|
||||
while let Some(message) = conn.receive().await? {}
|
||||
}
|
||||
|
||||
@ -1,25 +1,24 @@
|
||||
use crate::{
|
||||
connection::RawConnection,
|
||||
error::ErrorKind,
|
||||
mariadb::{
|
||||
protocol::encode, Capabilities, ComInitDb, ComPing, ComQuery, ComQuit, ComStmtPrepare,
|
||||
ComStmtPrepareResp, DeContext, Decode, Decoder, Encode, ErrPacket, OkPacket, PacketHeader,
|
||||
ProtocolType, ResultSet, ServerStatusFlag,
|
||||
},
|
||||
query::RawQuery,
|
||||
};
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use bytes::{BufMut, Bytes, BytesMut};
|
||||
use core::convert::TryFrom;
|
||||
use failure::Error;
|
||||
use futures_core::future::BoxFuture;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWrite, AsyncWriteExt},
|
||||
net::TcpStream,
|
||||
};
|
||||
use std::net::{SocketAddr, IpAddr, Ipv4Addr};
|
||||
use url::Url;
|
||||
use bytes::BufMut;
|
||||
use crate::error::ErrorKind;
|
||||
use crate::connection::RawConnection;
|
||||
use futures_core::future::BoxFuture;
|
||||
use crate::query::RawQuery;
|
||||
|
||||
mod establish;
|
||||
mod execute;
|
||||
@ -31,7 +30,7 @@ pub struct MariaDbRawConnection {
|
||||
pub wbuf: Vec<u8>,
|
||||
|
||||
pub rbuf: BytesMut,
|
||||
pub read_index: usize,
|
||||
pub read_index: usize,
|
||||
|
||||
// Context for the connection
|
||||
// Explicitly declared to easily send to deserializers
|
||||
@ -112,15 +111,15 @@ impl MariaDbRawConnection {
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
// pub async fn send<S>(&mut self, message: S) -> Result<(), Error>
|
||||
// where
|
||||
// S: Encode,
|
||||
// {
|
||||
// self.wbuf.clear();
|
||||
// message.encode(&mut self.wbuf, &mut self.context)?;
|
||||
// self.stream.inner.write_all(&self.wbuf).await?;
|
||||
// Ok(())
|
||||
// }
|
||||
// pub async fn send<S>(&mut self, message: S) -> Result<(), Error>
|
||||
// where
|
||||
// S: Encode,
|
||||
// {
|
||||
// self.wbuf.clear();
|
||||
// message.encode(&mut self.wbuf, &mut self.context)?;
|
||||
// self.stream.inner.write_all(&self.wbuf).await?;
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
pub fn write(&mut self, message: impl Encode) {
|
||||
message.encode(&mut self.wbuf);
|
||||
@ -229,12 +228,19 @@ impl MariaDbRawConnection {
|
||||
|
||||
if let Some(packet_header) = packet_headers.last() {
|
||||
if packet_header.combined_length() > self.rbuf.len() {
|
||||
unsafe { self.rbuf.reserve(packet_header.combined_length() - self.rbuf.len()); }
|
||||
unsafe {
|
||||
self.rbuf
|
||||
.reserve(packet_header.combined_length() - self.rbuf.len());
|
||||
}
|
||||
}
|
||||
} else if self.rbuf.len() == self.read_index {
|
||||
unsafe { self.rbuf.reserve(32); }
|
||||
unsafe {
|
||||
self.rbuf.reserve(32);
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
self.rbuf.set_len(self.rbuf.capacity());
|
||||
}
|
||||
unsafe { self.rbuf.set_len(self.rbuf.capacity()); }
|
||||
|
||||
// If we have a packet_header and the amount of currently read bytes (len) is less than
|
||||
// the specified length inside packet_header, then we can continue reading to self.rbuf.
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
pub mod backend;
|
||||
pub mod connection;
|
||||
pub mod protocol;
|
||||
pub mod types;
|
||||
pub mod backend;
|
||||
pub mod query;
|
||||
pub mod types;
|
||||
|
||||
// Re-export all the things
|
||||
pub use connection::{ConnContext, MariaDbRawConnection, Framed};
|
||||
pub use connection::{ConnContext, Framed, MariaDbRawConnection};
|
||||
pub use protocol::{
|
||||
AuthenticationSwitchRequestPacket, BufMut, Capabilities, ColumnDefPacket, ColumnPacket,
|
||||
ComDebug, ComInitDb, ComPing, ComProcessKill, ComQuery, ComQuit, ComResetConnection,
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{ColumnDefPacket, ConnContext, MariaDbRawConnection, Framed, PacketHeader};
|
||||
use crate::mariadb::{ColumnDefPacket, ConnContext, Framed, MariaDbRawConnection, PacketHeader};
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use bytes::Bytes;
|
||||
use failure::{err_msg, Error};
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{ConnContext, MariaDbRawConnection, FieldType};
|
||||
use crate::mariadb::{ConnContext, FieldType, MariaDbRawConnection};
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
use std::convert::TryInto;
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use crate::mariadb::{
|
||||
BufMut, ColumnDefPacket, ConnContext, MariaDbRawConnection, Encode, FieldDetailFlag, FieldType,
|
||||
BufMut, ColumnDefPacket, ConnContext, Encode, FieldDetailFlag, FieldType, MariaDbRawConnection,
|
||||
StmtExecFlag,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
||||
@ -35,7 +35,9 @@ impl crate::mariadb::Decode for ResultRow {
|
||||
} else {
|
||||
match column_defs[index].field_type {
|
||||
// Ordered by https://mariadb.com/kb/en/library/resultset-row/#binary-resultset-row
|
||||
FieldType::MYSQL_TYPE_DOUBLE => Some(decoder.decode_binary_double()),
|
||||
FieldType::MYSQL_TYPE_DOUBLE => {
|
||||
Some(decoder.decode_binary_double())
|
||||
}
|
||||
FieldType::MYSQL_TYPE_LONGLONG => {
|
||||
Some(decoder.decode_binary_bigint())
|
||||
}
|
||||
@ -51,7 +53,9 @@ impl crate::mariadb::Decode for ResultRow {
|
||||
FieldType::MYSQL_TYPE_FLOAT => Some(decoder.decode_binary_float()),
|
||||
|
||||
// Is this MYSQL_TYPE_SMALLINT?
|
||||
FieldType::MYSQL_TYPE_SHORT => Some(decoder.decode_binary_smallint()),
|
||||
FieldType::MYSQL_TYPE_SHORT => {
|
||||
Some(decoder.decode_binary_smallint())
|
||||
}
|
||||
|
||||
FieldType::MYSQL_TYPE_YEAR => Some(decoder.decode_binary_year()),
|
||||
FieldType::MYSQL_TYPE_TINY => Some(decoder.decode_binary_tinyint()),
|
||||
@ -68,16 +72,24 @@ impl crate::mariadb::Decode for ResultRow {
|
||||
}
|
||||
|
||||
// This group of types are all encoded as byte<lenenc>
|
||||
FieldType::MYSQL_TYPE_TINY_BLOB => Some(decoder.decode_byte_lenenc()),
|
||||
FieldType::MYSQL_TYPE_TINY_BLOB => {
|
||||
Some(decoder.decode_byte_lenenc())
|
||||
}
|
||||
FieldType::MYSQL_TYPE_MEDIUM_BLOB => {
|
||||
Some(decoder.decode_byte_lenenc())
|
||||
}
|
||||
FieldType::MYSQL_TYPE_LONG_BLOB => Some(decoder.decode_byte_lenenc()),
|
||||
FieldType::MYSQL_TYPE_LONG_BLOB => {
|
||||
Some(decoder.decode_byte_lenenc())
|
||||
}
|
||||
FieldType::MYSQL_TYPE_BLOB => Some(decoder.decode_byte_lenenc()),
|
||||
FieldType::MYSQL_TYPE_VARCHAR => Some(decoder.decode_byte_lenenc()),
|
||||
FieldType::MYSQL_TYPE_VAR_STRING => Some(decoder.decode_byte_lenenc()),
|
||||
FieldType::MYSQL_TYPE_VAR_STRING => {
|
||||
Some(decoder.decode_byte_lenenc())
|
||||
}
|
||||
FieldType::MYSQL_TYPE_STRING => Some(decoder.decode_byte_lenenc()),
|
||||
FieldType::MYSQL_TYPE_GEOMETRY => Some(decoder.decode_byte_lenenc()),
|
||||
FieldType::MYSQL_TYPE_GEOMETRY => {
|
||||
Some(decoder.decode_byte_lenenc())
|
||||
}
|
||||
|
||||
// The following did not have defined binary encoding, so I guessed.
|
||||
// Perhaps you cannot get these types back from the server if you're using
|
||||
|
||||
@ -91,7 +91,7 @@ mod test {
|
||||
use super::*;
|
||||
use crate::{
|
||||
__bytes_builder,
|
||||
mariadb::{ConnContext, Decoder}
|
||||
mariadb::{ConnContext, Decoder},
|
||||
};
|
||||
|
||||
#[test]
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, Capabilities, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, Capabilities, ConnContext, Encode, MariaDbRawConnection};
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
|
||||
@ -119,8 +119,8 @@ mod test {
|
||||
use crate::{
|
||||
__bytes_builder,
|
||||
mariadb::{
|
||||
Capabilities, ConnContext, MariaDbRawConnection, EofPacket, ErrPacket, OkPacket, ResultRow,
|
||||
ServerStatusFlag,
|
||||
Capabilities, ConnContext, EofPacket, ErrPacket, MariaDbRawConnection, OkPacket,
|
||||
ResultRow, ServerStatusFlag,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
use crate::mariadb::{BufMut, Capabilities, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, Capabilities, ConnContext, Encode, MariaDbRawConnection};
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct SSLRequestPacket {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
pub struct ComDebug();
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
pub struct ComPing();
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
pub struct ComProcessKill {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
pub struct ComQuit();
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
pub struct ComResetConnection();
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
pub struct ComSleep();
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{BufMut, ConnContext, MariaDbRawConnection, Encode};
|
||||
use crate::mariadb::{BufMut, ConnContext, Encode, MariaDbRawConnection};
|
||||
use failure::Error;
|
||||
|
||||
pub struct ComStatistics();
|
||||
|
||||
@ -81,45 +81,45 @@ pub enum SessionChangeType {
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct FieldType(pub u8);
|
||||
impl FieldType {
|
||||
pub const MYSQL_TYPE_DECIMAL: FieldType = FieldType(0);
|
||||
pub const MYSQL_TYPE_TINY: FieldType = FieldType(1);
|
||||
pub const MYSQL_TYPE_SHORT: FieldType = FieldType(2);
|
||||
pub const MYSQL_TYPE_LONG: FieldType = FieldType(3);
|
||||
pub const MYSQL_TYPE_FLOAT: FieldType = FieldType(4);
|
||||
pub const MYSQL_TYPE_DOUBLE: FieldType = FieldType(5);
|
||||
pub const MYSQL_TYPE_NULL: FieldType = FieldType(6);
|
||||
pub const MYSQL_TYPE_TIMESTAMP: FieldType = FieldType(7);
|
||||
pub const MYSQL_TYPE_LONGLONG: FieldType = FieldType(8);
|
||||
pub const MYSQL_TYPE_INT24: FieldType = FieldType(9);
|
||||
pub const MYSQL_TYPE_DATE: FieldType = FieldType(10);
|
||||
pub const MYSQL_TYPE_TIME: FieldType = FieldType(11);
|
||||
pub const MYSQL_TYPE_DATETIME: FieldType = FieldType(12);
|
||||
pub const MYSQL_TYPE_YEAR: FieldType = FieldType(13);
|
||||
pub const MYSQL_TYPE_NEWDATE: FieldType = FieldType(14);
|
||||
pub const MYSQL_TYPE_VARCHAR: FieldType = FieldType(15);
|
||||
pub const MYSQL_TYPE_BIT: FieldType = FieldType(16);
|
||||
pub const MYSQL_TYPE_TIMESTAMP2: FieldType = FieldType(17);
|
||||
pub const MYSQL_TYPE_DATETIME2: FieldType = FieldType(18);
|
||||
pub const MYSQL_TYPE_TIME2: FieldType = FieldType(19);
|
||||
pub const MYSQL_TYPE_JSON: FieldType = FieldType(245);
|
||||
pub const MYSQL_TYPE_NEWDECIMAL: FieldType = FieldType(246);
|
||||
pub const MYSQL_TYPE_ENUM: FieldType = FieldType(247);
|
||||
pub const MYSQL_TYPE_SET: FieldType = FieldType(248);
|
||||
pub const MYSQL_TYPE_TINY_BLOB: FieldType = FieldType(249);
|
||||
pub const MYSQL_TYPE_MEDIUM_BLOB: FieldType = FieldType(250);
|
||||
pub const MYSQL_TYPE_LONG_BLOB: FieldType = FieldType(251);
|
||||
pub const MYSQL_TYPE_BLOB: FieldType = FieldType(252);
|
||||
pub const MYSQL_TYPE_VAR_STRING: FieldType = FieldType(253);
|
||||
pub const MYSQL_TYPE_STRING: FieldType = FieldType(254);
|
||||
pub const MYSQL_TYPE_DATE: FieldType = FieldType(10);
|
||||
pub const MYSQL_TYPE_DATETIME: FieldType = FieldType(12);
|
||||
pub const MYSQL_TYPE_DATETIME2: FieldType = FieldType(18);
|
||||
pub const MYSQL_TYPE_DECIMAL: FieldType = FieldType(0);
|
||||
pub const MYSQL_TYPE_DOUBLE: FieldType = FieldType(5);
|
||||
pub const MYSQL_TYPE_ENUM: FieldType = FieldType(247);
|
||||
pub const MYSQL_TYPE_FLOAT: FieldType = FieldType(4);
|
||||
pub const MYSQL_TYPE_GEOMETRY: FieldType = FieldType(255);
|
||||
pub const MYSQL_TYPE_INT24: FieldType = FieldType(9);
|
||||
pub const MYSQL_TYPE_JSON: FieldType = FieldType(245);
|
||||
pub const MYSQL_TYPE_LONG: FieldType = FieldType(3);
|
||||
pub const MYSQL_TYPE_LONGLONG: FieldType = FieldType(8);
|
||||
pub const MYSQL_TYPE_LONG_BLOB: FieldType = FieldType(251);
|
||||
pub const MYSQL_TYPE_MEDIUM_BLOB: FieldType = FieldType(250);
|
||||
pub const MYSQL_TYPE_NEWDATE: FieldType = FieldType(14);
|
||||
pub const MYSQL_TYPE_NEWDECIMAL: FieldType = FieldType(246);
|
||||
pub const MYSQL_TYPE_NULL: FieldType = FieldType(6);
|
||||
pub const MYSQL_TYPE_SET: FieldType = FieldType(248);
|
||||
pub const MYSQL_TYPE_SHORT: FieldType = FieldType(2);
|
||||
pub const MYSQL_TYPE_STRING: FieldType = FieldType(254);
|
||||
pub const MYSQL_TYPE_TIME: FieldType = FieldType(11);
|
||||
pub const MYSQL_TYPE_TIME2: FieldType = FieldType(19);
|
||||
pub const MYSQL_TYPE_TIMESTAMP: FieldType = FieldType(7);
|
||||
pub const MYSQL_TYPE_TIMESTAMP2: FieldType = FieldType(17);
|
||||
pub const MYSQL_TYPE_TINY: FieldType = FieldType(1);
|
||||
pub const MYSQL_TYPE_TINY_BLOB: FieldType = FieldType(249);
|
||||
pub const MYSQL_TYPE_VARCHAR: FieldType = FieldType(15);
|
||||
pub const MYSQL_TYPE_VAR_STRING: FieldType = FieldType(253);
|
||||
pub const MYSQL_TYPE_YEAR: FieldType = FieldType(13);
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct StmtExecFlag(pub u8);
|
||||
impl StmtExecFlag {
|
||||
pub const CURSOR_FOR_UPDATE: StmtExecFlag = StmtExecFlag(2);
|
||||
pub const NO_CURSOR: StmtExecFlag = StmtExecFlag(0);
|
||||
pub const READ_ONLY: StmtExecFlag = StmtExecFlag(1);
|
||||
pub const CURSOR_FOR_UPDATE: StmtExecFlag = StmtExecFlag(2);
|
||||
pub const SCROLLABLE_CURSOR: StmtExecFlag = StmtExecFlag(3);
|
||||
}
|
||||
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
use crate::mariadb::{FieldType, MariaDbRawConnection};
|
||||
use crate::mariadb::protocol::types::ParamFlag;
|
||||
use crate::query::RawQuery;
|
||||
use crate::types::HasSqlType;
|
||||
use crate::serialize::{ToSql, IsNull};
|
||||
use crate::{
|
||||
mariadb::{protocol::types::ParamFlag, FieldType, MariaDbRawConnection},
|
||||
query::RawQuery,
|
||||
serialize::{IsNull, ToSql},
|
||||
types::HasSqlType,
|
||||
};
|
||||
|
||||
pub struct MariaDbRawQuery<'q> {
|
||||
query: &'q str,
|
||||
@ -27,21 +28,22 @@ impl<'q> RawQueryQuery<'q> for MariaDbRawQuery<'q> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn bind<T>(mut self, value: T) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
Self::Backend: HasSqlType<T>,
|
||||
T: ToSql<Self::Backend>,
|
||||
{
|
||||
self.types.push(<MariaDb as HasSqlType<T>>::metadata().field_type.0);
|
||||
self.flags.push(<MariaDb as HasSqlType<T>>::metadata().param_flag.0);
|
||||
self.types
|
||||
.push(<MariaDb as HasSqlType<T>>::metadata().field_type.0);
|
||||
self.flags
|
||||
.push(<MariaDb as HasSqlType<T>>::metadata().param_flag.0);
|
||||
|
||||
match value.to_sql(&mut self.buf) {
|
||||
IsNull::Yes => {
|
||||
self.null_bitmap[self.index / 8] =
|
||||
self.null_bitmap[self.index / 8] & (1 << self.index % 8);
|
||||
},
|
||||
}
|
||||
IsNull::No => {}
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1 @@
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
use super::MariaDB;
|
||||
use crate::types::TypeMetadata;
|
||||
use crate::mariadb::FieldType;
|
||||
use crate::mariadb::protocol::types::ParamFlag;
|
||||
use crate::{
|
||||
mariadb::{protocol::types::ParamFlag, FieldType},
|
||||
types::TypeMetadata,
|
||||
};
|
||||
|
||||
mod boolean;
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use super::{Decode, Buf};
|
||||
use super::{Buf, Decode};
|
||||
use std::{
|
||||
convert::TryInto,
|
||||
fmt::{self, Debug},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user