Add create/drop database to the main driver

This commit is contained in:
Ryan Leckey 2019-07-26 08:36:28 -07:00
parent 04f56570a4
commit 3d01db84a0
2 changed files with 37 additions and 6 deletions

View file

@ -9,11 +9,11 @@ edition = "2018"
[dependencies]
byteorder = "1.3.2"
bytes = "0.4.12"
env_logger = "0.6.2"
futures-preview = "=0.3.0-alpha.17"
hex = "0.3.2"
itoa = "0.4.4"
log = "0.4.7"
md-5 = "0.8.0"
memchr = "2.2.1"
runtime = "=0.3.0-alpha.6"
runtime = { version = "=0.3.0-alpha.6", default-features = false }
runtime-tokio = { version = "=0.3.0-alpha.5" }

View file

@ -7,9 +7,30 @@ use std::io;
// TODO: ToSql and FromSql (to [de]serialize values from/to Rust and SQL)
// TODO: Connection strings ala postgres@localhost/sqlx_dev
#[runtime::main]
#[runtime::main(runtime_tokio::Tokio)]
async fn main() -> io::Result<()> {
env_logger::init();
// Connect as postgres / postgres and DROP the sqlx__dev database
// if exists and then re-create it
let mut conn = Connection::establish(
ConnectOptions::new()
.host("127.0.0.1")
.port(5432)
.user("postgres")
.database("postgres"),
)
.await?;
println!(" :: drop database (if exists) sqlx__dev");
conn.prepare("DROP DATABASE IF EXISTS sqlx__dev")
.execute()
.await?;
println!(" :: create database sqlx__dev");
conn.prepare("CREATE DATABASE sqlx__dev").execute().await?;
conn.close().await?;
let mut conn = Connection::establish(
ConnectOptions::new()
@ -20,6 +41,8 @@ async fn main() -> io::Result<()> {
)
.await?;
println!(" :: create schema");
conn.prepare(
r#"
CREATE TABLE IF NOT EXISTS users (
@ -31,18 +54,26 @@ CREATE TABLE IF NOT EXISTS users (
.execute()
.await?;
let new_id = conn
println!(" :: insert");
let new_row = conn
.prepare("INSERT INTO users (name) VALUES ($1) RETURNING id")
.bind(b"Joe")
.get()
.await?;
let new_id = new_row.as_ref().unwrap().get(0);
println!("insert {:?}", new_id);
println!(" :: select");
conn.prepare("SELECT id FROM users")
.select()
.try_for_each(|row| {
println!("select {:?}", row.get(0));
let id = row.get(0);
println!("select {:?}", id);
future::ok(())
})