mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-19 08:39:44 +00:00
Run rustfmt and remove some unneeded stuff
This commit is contained in:
@@ -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(())
|
||||
}
|
||||
Reference in New Issue
Block a user