-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56ac426
commit b6b031d
Showing
3 changed files
with
81 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |