mirror of
https://github.com/alanorth/cgspace-notes.git
synced 2025-01-27 05:49:12 +01:00
Add notes for 2018-09-24
This commit is contained in:
@ -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)
|
||||
|
||||
<!-- vim: set sw=2 ts=2: -->
|
||||
|
Reference in New Issue
Block a user