From 1814a1f06486d6e9e99417909aa50e0d2440b9a2 Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Mon, 24 Sep 2018 13:40:59 +0300 Subject: [PATCH] Add notes for 2018-09-24 --- content/posts/2018-09.md | 30 ++++++++++++++++++++++++++++++ docs/2018-09/index.html | 40 +++++++++++++++++++++++++++++++++++++--- docs/sitemap.xml | 10 +++++----- 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/content/posts/2018-09.md b/content/posts/2018-09.md index 3f1e61ba9..82ab3f30f 100644 --- a/content/posts/2018-09.md +++ b/content/posts/2018-09.md @@ -401,4 +401,34 @@ dspace=# select item_id from item where in_archive is True and withdrawn is Fals - Also, I need to test the new LDAP server, so I will deploy that on DSpace Test today - Rename my cgspace-statistics-api to [dspace-statistics-api](https://github.com/alanorth/dspace-statistics-api) on GitHub +## 2018-09-24 + +- Trying to figure out how to get item views and downloads from SQLite in a join +- It appears SQLite doesn't support `FULL OUTER JOIN` so some people on StackOverflow have emulated it with `LEFT JOIN` and `UNION`: + +``` +> SELECT views.views, views.id, downloads.downloads, downloads.id FROM itemviews views +LEFT JOIN itemdownloads downloads USING(id) +UNION ALL +SELECT views.views, views.id, downloads.downloads, downloads.id FROM itemdownloads downloads +LEFT JOIN itemviews views USING(id) +WHERE views.id IS NULL; +``` + +- This "works" but the resulting rows are kinda messy so I'd have to do extra logic in Python +- Maybe we can use one "items" table with defaults values and UPSERT (aka insert... on conflict ... do update): + +``` +sqlite> CREATE TABLE items(id INT PRIMARY KEY, views INT DEFAULT 0, downloads INT DEFAULT 0); +sqlite> INSERT INTO items(id, views) VALUES(0, 52); +sqlite> INSERT INTO items(id, downloads) VALUES(1, 171); +sqlite> INSERT INTO items(id, downloads) VALUES(1, 176) ON CONFLICT(id) DO UPDATE SET downloads=176; +sqlite> INSERT INTO items(id, views) VALUES(0, 78) ON CONFLICT(id) DO UPDATE SET views=78; +sqlite> INSERT INTO items(id, views) VALUES(0, 3) ON CONFLICT(id) DO UPDATE SET downloads=3; +sqlite> INSERT INTO items(id, views) VALUES(0, 7) ON CONFLICT(id) DO UPDATE SET downloads=excluded.views; +``` + +- This totally works! +- Note the special `excluded.views` form! See [SQLite's lang_UPSERT documentation](https://www.sqlite.org/lang_UPSERT.html) + diff --git a/docs/2018-09/index.html b/docs/2018-09/index.html index afa854cb4..a3a9eb283 100644 --- a/docs/2018-09/index.html +++ b/docs/2018-09/index.html @@ -18,7 +18,7 @@ I’m testing the new DSpace 5.8 branch in my Ubuntu 18.04 environment and I " /> - + Rename my cgspace-statistics-api to dspace-statistics-api on GitHub +

2018-09-24

+ + + +
> SELECT views.views, views.id, downloads.downloads, downloads.id FROM itemviews views
+LEFT JOIN itemdownloads downloads USING(id)
+UNION ALL
+SELECT views.views, views.id, downloads.downloads, downloads.id FROM itemdownloads downloads
+LEFT JOIN itemviews views USING(id)
+WHERE views.id IS NULL;
+
+ + + +
sqlite> CREATE TABLE items(id INT PRIMARY KEY, views INT DEFAULT 0, downloads INT DEFAULT 0);
+sqlite> INSERT INTO items(id, views) VALUES(0, 52);
+sqlite> INSERT INTO items(id, downloads) VALUES(1, 171);
+sqlite> INSERT INTO items(id, downloads) VALUES(1, 176) ON CONFLICT(id) DO UPDATE SET downloads=176;
+sqlite> INSERT INTO items(id, views) VALUES(0, 78) ON CONFLICT(id) DO UPDATE SET views=78;
+sqlite> INSERT INTO items(id, views) VALUES(0, 3) ON CONFLICT(id) DO UPDATE SET downloads=3;
+sqlite> INSERT INTO items(id, views) VALUES(0, 7) ON CONFLICT(id) DO UPDATE SET downloads=excluded.views;
+
+ + + diff --git a/docs/sitemap.xml b/docs/sitemap.xml index 109e71827..bf894cbf6 100644 --- a/docs/sitemap.xml +++ b/docs/sitemap.xml @@ -4,7 +4,7 @@ https://alanorth.github.io/cgspace-notes/2018-09/ - 2018-09-23T16:51:17+03:00 + 2018-09-24T00:31:59+03:00 @@ -184,7 +184,7 @@ https://alanorth.github.io/cgspace-notes/ - 2018-09-23T16:51:17+03:00 + 2018-09-24T00:31:59+03:00 0 @@ -195,7 +195,7 @@ https://alanorth.github.io/cgspace-notes/tags/notes/ - 2018-09-23T16:51:17+03:00 + 2018-09-24T00:31:59+03:00 0 @@ -207,13 +207,13 @@ https://alanorth.github.io/cgspace-notes/posts/ - 2018-09-23T16:51:17+03:00 + 2018-09-24T00:31:59+03:00 0 https://alanorth.github.io/cgspace-notes/tags/ - 2018-09-23T16:51:17+03:00 + 2018-09-24T00:31:59+03:00 0