Skip to content
This repository has been archived by the owner on Nov 19, 2023. It is now read-only.

Commit

Permalink
Adds support for querying Sqlite databases
Browse files Browse the repository at this point in the history
Tobias Brandt authored and Tobias Brandt committed Oct 13, 2022
1 parent bf3a9e1 commit 57630a0
Showing 4 changed files with 27 additions and 7 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# pq Changelog

## 0.0.5 - UNRELEASED
## 0.0.5 - 2022-10-13

* Added support for DuckDB database files.
* Added support for querying PostgreSQL databases (through DuckDB).
* Added support for querying Sqlite databases (through DuckDB).
* Added support for querying DuckDB databases.
* Added UNLICENSED.

## 0.0.4 - 2022-10-13
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -194,6 +194,15 @@ beginning with "duckdb://".
$ pq --database duckdb://examples/chinook/duckdb/chinook.duckdb \
'from albums | join artists [artist_id] | group name (aggregate [num_albums = count]) | sort [-num_albums] | take 10'

### Querying Sqlite databases (WIP)

Sqlite is currently supported through the [sqlite_scanner](https://github.com/duckdblabs/sqlite_scanner)
DuckDB extension. This appears to work for some queries but not for others
so this is currently EXPERIMENTAL.

$ pq --database sqlite://examples/chinook/sqlite/chinook.sqlite \
'from albums | select title | take 10'

### Querying PostgreSQL databases

PostgreSQL is currently supported through the
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
* [x] Add leading examples
* [x] Add support for DuckDB database files
* [x] Add support for PostgreSQL through DuckDB
* [ ] Add support for SQLite through DuckDB
* [x] Add support for SQLite through DuckDB
* [ ] Add support for ?currentSchema=schema option postgres URI
* [ ] Add tests
* [ ] Publish to crates.io
17 changes: 13 additions & 4 deletions src/backends/duckdb.rs
Original file line number Diff line number Diff line change
@@ -79,13 +79,22 @@ pub fn query(
let conn = if database == "" {
debug!("Opening in-memory DuckDB database");
Connection::open_in_memory()?
} else if database.starts_with("sqlite://") {
let con = Connection::open_in_memory()?;
// Install and load the sqlite_scanner extension
let load_extension = "INSTALL sqlite_scanner; LOAD sqlite_scanner;";
con.execute_batch(load_extension)?;
let dbpath = database.strip_prefix("sqlite://").map_or(database, |p| p);
let attach_sql = format!("CALL sqlite_attach('{dbpath}')");
con.execute_batch(&attach_sql)?;
con
} else if database.starts_with("postgres") {
let con = Connection::open_in_memory()?;
// Install and load the postgres_scanner extension
let load_postgres_extension = "INSTALL postgres_scanner; LOAD postgres_scanner;";
con.execute_batch(load_postgres_extension)?;
let attach_sql = format!("CALL postgres_attach('{database}')");
con.execute_batch(&attach_sql)?;
let load_extension = "INSTALL postgres_scanner; LOAD postgres_scanner;";
con.execute_batch(load_extension)?;
let attach_sql = format!("CALL postgres_attach('{database}')");
con.execute_batch(&attach_sql)?;
con
} else {
let dbpath = database.strip_prefix("duckdb://").map_or(database, |p| p);

0 comments on commit 57630a0

Please sign in to comment.