aboutsummaryrefslogtreecommitdiff
path: root/src/db.rs
blob: a5c0f85394d1c46b03656e44925d8179ae618fc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use anyhow::Result;
use rusqlite::{Connection, OpenFlags, NO_PARAMS};

use std::path::Path;

pub fn init_db<P: AsRef<Path>>(p: P) -> Result<Connection> {
    let conn = Connection::open_with_flags(
        p,
        OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE,
    )?;
    conn.execute(
        "CREATE TABLE IF NOT EXISTS urls (
            link TEXT PRIMARY KEY,
            shortlink TEXT NOT NULL
        )",
        NO_PARAMS,
    )?;
    return Ok(conn);
}