-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin implementing db for RepositoryMeta
- Loading branch information
Showing
9 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
DATABASE_URL=sqlite://crates/tabby-repositories/schema.sqlite |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "tabby-repositories" | ||
version.workspace = true | ||
edition.workspace = true | ||
authors.workspace = true | ||
homepage.workspace = true | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
sqlx = { workspace = true } | ||
anyhow = { workspace = true } | ||
tabby-common = { path = "../tabby-common" } | ||
serde_json = { workspace = true } |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
DROP TABLE IF EXISTS repository_meta; | ||
CREATE TABLE repository_meta( | ||
git_url TEXT NOT NULL, | ||
filepath TEXT NOT NULL, | ||
language TEXT NOT NULL, | ||
max_line_length INTEGER NOT NULL, | ||
avg_line_length REAL NOT NULL, | ||
alphanum_fraction REAL NOT NULL, | ||
tags TEXT NOT NULL | ||
); |
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use anyhow::Result; | ||
use sqlx::sqlite::SqlitePoolOptions; | ||
use sqlx::{query, SqlitePool}; | ||
use sqlx::{sqlite::SqliteConnectOptions, Pool, Sqlite}; | ||
use std::str::FromStr; | ||
use tabby_common::Tag; | ||
|
||
pub struct RepositoryCache { | ||
pool: Pool<Sqlite>, | ||
} | ||
|
||
struct RepositoryMetaDAO { | ||
git_url: String, | ||
filepath: String, | ||
language: String, | ||
max_line_length: usize, | ||
avg_line_length: f32, | ||
alphanum_fraction: f32, | ||
tags: String, | ||
} | ||
|
||
impl RepositoryCache { | ||
pub async fn new() -> Result<Self> { | ||
let init_query = include_str!("../schema.sql"); | ||
let options = SqliteConnectOptions::new() | ||
.filename(tabby_common::path::repository_meta_db()) | ||
.create_if_missing(true); | ||
let pool = SqlitePool::connect_with(options).await?; | ||
sqlx::query(init_query).execute(&pool).await?; | ||
Ok(RepositoryCache { pool }) | ||
} | ||
|
||
pub async fn clear(&self) -> Result<()> { | ||
query!("DELETE FROM repository_meta") | ||
.execute(&self.pool) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn add_repository_meta( | ||
&self, | ||
git_url: String, | ||
filepath: String, | ||
language: String, | ||
max_line_length: i64, | ||
avg_line_length: f32, | ||
alphanum_fraction: f32, | ||
tags: Vec<Tag>, | ||
) -> Result<()> { | ||
let tags = serde_json::to_string(&tags)?; | ||
query!("INSERT INTO repository_meta (git_url, filepath, language, max_line_length, avg_line_length, alphanum_fraction, tags) | ||
VALUES ($1, $2, $3, $4, $5, $6, $7)", | ||
git_url, filepath, language, max_line_length, avg_line_length, alphanum_fraction, tags | ||
).execute(&self.pool).await?; | ||
Ok(()) | ||
} | ||
} |
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