Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrot-lc committed Oct 23, 2024
1 parent 56ac426 commit b6b031d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 42 deletions.
52 changes: 35 additions & 17 deletions src/sqlite_merger.gleam
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
import gleam/dynamic
import gleam/io
import gleam/list
import gleam/set.{type Set}
import sqlight
import gleam/string
import sqlight.{type Connection}

fn list_items(db: String) -> Set(Int) {
use conn <- sqlight.with_connection("file:" <> db)
let query = "SELECT id, id FROM bookmarks;"
pub type Id {
Id(Int)
}

pub type TableDiff {
TableDiff(added: List(Id), removed: List(Id), modified: List(Id))
}

/// Find the element present in the target table but not in source table. An
/// element identified by its ID and URL column value. Returns the primary IDs of the
/// selected rows.
pub fn bookmarks_added(
conn: Connection,
source source: String,
target target: String,
) -> List(Id) {
let query =
[
"SELECT t1.id FROM",
target,
"as t1 JOIN",
source,
"as t2 on t1.id = t2.id WHERE t1.url != t2.url;",
]
|> string.join(" ")
let decoder = dynamic.element(0, dynamic.int)
let assert Ok(ids) =
sqlight.query(query, on: conn, with: [], expecting: decoder)

ids
|> list.map(fn(x) { x })
|> set.from_list
ids |> list.map(fn(id) { Id(id) })
}

fn diff_items(db1: String, db2: String) -> Set(Int) {
let items1 = list_items(db1)
let items2 = list_items(db2)

set.difference(items1, items2)
pub fn bookmarks_diff(
conn: Connection,
source: String,
target: String,
) -> TableDiff {
todo
}

pub fn main() {
let ids = list_items("bookmarks.db")
io.println("Hey")
todo
}
33 changes: 11 additions & 22 deletions test/db_generator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ import gleam/int
import gleam/io
import gleam/list
import gleam/string
import sqlight.{type Connection, type Value}
import sqlight.{type Connection}

pub type Bookmark {
Bookmark(url: String, desc: String)
}

fn bookmark_value(bookmark: Bookmark) -> Value {
let bookmark = "('" <> bookmark.url <> "', '" <> bookmark.desc <> "')"
sqlight.text(bookmark)
}

pub fn print_db(conn: Connection) {
let query = "SELECT id, url, desc FROM bookmarks;"
let decoder = dynamic.tuple3(dynamic.int, dynamic.string, dynamic.string)
Expand Down Expand Up @@ -43,18 +38,16 @@ pub fn print_db(conn: Connection) {
/// use conn <- db_generator.fictive_bookmarks(bookmarks)
/// ```
///
pub fn fictive_bookmarks(bookmarks: List(Bookmark), f: fn(Connection) -> a) -> a {
use conn <- sqlight.with_connection(":memory:")

// Create the database.
pub fn insert_bookmarks(
bookmarks: List(Bookmark),
table: String,
conn: Connection,
) {
// Create the table if necessary.
let query =
"
CREATE TABLE bookmarks (
id INTEGER PRIMARY KEY,
url text,
desc text
);
"
" CREATE TABLE IF NOT EXISTS "
<> table
<> " ( id INTEGER PRIMARY KEY, url text, desc text);"
let assert Ok(Nil) = sqlight.exec(query, conn)

// Insert the bookmarks.
Expand All @@ -63,8 +56,7 @@ pub fn fictive_bookmarks(bookmarks: List(Bookmark), f: fn(Connection) -> a) -> a
|> list.map(fn(_) { "(?, ?)" })
|> string.join(", ")
|> string.append(";")
|> string.append("INSERT INTO bookmarks (url, desc) VALUES ", _)
|> io.debug
|> string.append("INSERT INTO " <> table <> " (url, desc) VALUES ", _)

let with =
bookmarks
Expand All @@ -73,7 +65,4 @@ pub fn fictive_bookmarks(bookmarks: List(Bookmark), f: fn(Connection) -> a) -> a

let assert Ok(_) =
sqlight.query(query, on: conn, with: with, expecting: dynamic.dynamic)

// Use the function now that the database is created.
f(conn)
}
38 changes: 35 additions & 3 deletions test/sqlite_merger_test.gleam
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
import db_generator.{Bookmark}
import gleam/dynamic
import gleam/list
import gleeunit
import gleeunit/should
import sqlight
import sqlite_merger.{Id}

pub fn main() {
gleeunit.main()
}

// gleeunit test functions end in `_test`
pub fn added_bookmarks_test() {
let bookmarks_1 = [
Bookmark("www.google.fr", "Google website"),
Bookmark("www.gleam.run", "Try Gleam online!"),
]
let bookmarks_2 = [
Bookmark("www.google.fr", "Google website"),
Bookmark("www.gleam.run", "Try Gleam online!"),
Bookmark("overfitted.dev", "My personal website."),
]
let added_ids = [Id(0)]

use conn <- sqlight.with_connection(":memory:")
let _ = db_generator.insert_bookmarks(bookmarks_1, "source", conn)
let _ = db_generator.insert_bookmarks(bookmarks_2, "target", conn)

sqlite_merger.bookmarks_added(conn, "source", "target")
|> should.equal(added_ids)
}

/// Test the construction of a fictive bookmarks table.
pub fn fictive_bookmarks_test() {
let bookmarks = [
Bookmark("www.google.fr", "Google desc"),
Bookmark("www.gleam.run", "Gleam!"),
]

use conn <- db_generator.fictive_bookmarks(bookmarks)
db_generator.print_db(conn)
use conn <- sqlight.with_connection(":memory:")
let _ = db_generator.insert_bookmarks(bookmarks, "bookmarks", conn)
let query = "SELECT url, desc FROM bookmarks;"
let decoder = dynamic.tuple2(dynamic.string, dynamic.string)
let assert Ok(res) =
sqlight.query(query, on: conn, with: [], expecting: decoder)

res
|> list.map(fn(b) { Bookmark(b.0, b.1) })
|> should.equal(bookmarks)
}

0 comments on commit b6b031d

Please sign in to comment.