|
| 1 | +use anyhow::Result; |
| 2 | +use serde_json::Value; |
| 3 | +use sqlx::{PgPool, Row}; |
| 4 | + |
| 5 | +#[derive(Debug, Clone)] |
| 6 | +pub struct UastCandidate { |
| 7 | + pub blob_hash: String, |
| 8 | + pub path: String, |
| 9 | +} |
| 10 | + |
| 11 | +#[derive(Debug, Clone)] |
| 12 | +pub struct UastFunctionRecord { |
| 13 | + pub blob_hash: String, |
| 14 | + pub name: String, |
| 15 | + pub start_line: Option<i32>, |
| 16 | + pub end_line: Option<i32>, |
| 17 | + pub signature: Option<String>, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, Clone)] |
| 21 | +pub struct UastImportRecord { |
| 22 | + pub blob_hash: String, |
| 23 | + pub source: String, |
| 24 | + pub target: Option<String>, |
| 25 | +} |
| 26 | + |
| 27 | +pub async fn uast_cache_exists(pool: &PgPool, blob_hash: &str) -> Result<bool> { |
| 28 | + let exists = sqlx::query_scalar::<_, bool>( |
| 29 | + "SELECT EXISTS(SELECT 1 FROM gitbase.uast_cache WHERE blob_hash = $1)", |
| 30 | + ) |
| 31 | + .bind(blob_hash) |
| 32 | + .fetch_one(pool) |
| 33 | + .await?; |
| 34 | + Ok(exists) |
| 35 | +} |
| 36 | + |
| 37 | +pub async fn fetch_uast_candidates(pool: &PgPool, limit: Option<i64>) -> Result<Vec<UastCandidate>> { |
| 38 | + let base_query = |
| 39 | + "SELECT DISTINCT f.blob_hash, f.path FROM gitbase.files f\ |
| 40 | + JOIN gitbase.blobs b ON b.hash = f.blob_hash\ |
| 41 | + WHERE b.content IS NOT NULL"; |
| 42 | + |
| 43 | + let rows = if let Some(limit) = limit { |
| 44 | + sqlx::query(&format!("{base_query} LIMIT $1")) |
| 45 | + .bind(limit) |
| 46 | + .fetch_all(pool) |
| 47 | + .await? |
| 48 | + } else { |
| 49 | + sqlx::query(base_query).fetch_all(pool).await? |
| 50 | + }; |
| 51 | + |
| 52 | + Ok(rows |
| 53 | + .into_iter() |
| 54 | + .map(|row| UastCandidate { |
| 55 | + blob_hash: row.get("blob_hash"), |
| 56 | + path: row.get("path"), |
| 57 | + }) |
| 58 | + .collect()) |
| 59 | +} |
| 60 | + |
| 61 | +pub async fn upsert_uast_cache( |
| 62 | + pool: &PgPool, |
| 63 | + blob_hash: &str, |
| 64 | + language: &str, |
| 65 | + uast: &Value, |
| 66 | +) -> Result<()> { |
| 67 | + sqlx::query( |
| 68 | + "INSERT INTO gitbase.uast_cache (blob_hash, language, uast, generated_at)\ |
| 69 | + VALUES ($1, $2, $3, now())\ |
| 70 | + ON CONFLICT (blob_hash) DO UPDATE\ |
| 71 | + SET language = EXCLUDED.language,\ |
| 72 | + uast = EXCLUDED.uast,\ |
| 73 | + generated_at = now()", |
| 74 | + ) |
| 75 | + .bind(blob_hash) |
| 76 | + .bind(language) |
| 77 | + .bind(uast) |
| 78 | + .execute(pool) |
| 79 | + .await?; |
| 80 | + |
| 81 | + Ok(()) |
| 82 | +} |
| 83 | + |
| 84 | +pub async fn clear_uast_projections(pool: &PgPool, blob_hash: &str) -> Result<()> { |
| 85 | + sqlx::query("DELETE FROM gitbase.uast_functions WHERE blob_hash = $1") |
| 86 | + .bind(blob_hash) |
| 87 | + .execute(pool) |
| 88 | + .await?; |
| 89 | + sqlx::query("DELETE FROM gitbase.uast_imports WHERE blob_hash = $1") |
| 90 | + .bind(blob_hash) |
| 91 | + .execute(pool) |
| 92 | + .await?; |
| 93 | + Ok(()) |
| 94 | +} |
| 95 | + |
| 96 | +pub async fn insert_uast_function(pool: &PgPool, record: &UastFunctionRecord) -> Result<()> { |
| 97 | + sqlx::query( |
| 98 | + "INSERT INTO gitbase.uast_functions (blob_hash, name, start_line, end_line, signature)\ |
| 99 | + VALUES ($1, $2, $3, $4, $5)\ |
| 100 | + ON CONFLICT (blob_hash, name, start_line) DO UPDATE\ |
| 101 | + SET end_line = EXCLUDED.end_line,\ |
| 102 | + signature = EXCLUDED.signature", |
| 103 | + ) |
| 104 | + .bind(&record.blob_hash) |
| 105 | + .bind(&record.name) |
| 106 | + .bind(record.start_line) |
| 107 | + .bind(record.end_line) |
| 108 | + .bind(&record.signature) |
| 109 | + .execute(pool) |
| 110 | + .await?; |
| 111 | + Ok(()) |
| 112 | +} |
| 113 | + |
| 114 | +pub async fn insert_uast_import(pool: &PgPool, record: &UastImportRecord) -> Result<()> { |
| 115 | + sqlx::query( |
| 116 | + "INSERT INTO gitbase.uast_imports (blob_hash, source, target)\ |
| 117 | + VALUES ($1, $2, $3)\ |
| 118 | + ON CONFLICT (blob_hash, source, target) DO NOTHING", |
| 119 | + ) |
| 120 | + .bind(&record.blob_hash) |
| 121 | + .bind(&record.source) |
| 122 | + .bind(&record.target) |
| 123 | + .execute(pool) |
| 124 | + .await?; |
| 125 | + Ok(()) |
| 126 | +} |
0 commit comments