csharp72c0d4fb
| 1 | using System.Globalization; |
| 2 | using Microsoft.Data.Sqlite; |
| 3 | |
| 4 | namespace HybridCodebaseIndex.Core; |
| 5 | |
| 6 | internal static partial class SqliteFtsIndex |
| 7 | { |
| 8 | private static void Exec(SqliteConnection conn, string sql, SqliteTransaction? tx = null) |
| 9 | { |
| 10 | using var c = conn.CreateCommand(); |
| 11 | c.Transaction = tx; |
| 12 | c.CommandText = sql; |
| 13 | c.ExecuteNonQuery(); |
| 14 | } |
| 15 | |
| 16 | private static void EnsureMetaTable(SqliteConnection conn) |
| 17 | { |
| 18 | Exec(conn, "PRAGMA journal_mode=WAL;"); |
| 19 | Exec(conn, """ |
| 20 | CREATE TABLE IF NOT EXISTS meta( |
| 21 | key TEXT PRIMARY KEY, |
| 22 | value TEXT |
| 23 | ); |
| 24 | """); |
| 25 | } |
| 26 | |
| 27 | private static void UpsertMeta(SqliteConnection conn, string key, string value) |
| 28 | { |
| 29 | using var cmd = conn.CreateCommand(); |
| 30 | cmd.CommandText = """ |
| 31 | INSERT INTO meta(key,value) VALUES($k, $v) |
| 32 | ON CONFLICT(key) DO UPDATE SET value=excluded.value; |
| 33 | """; |
| 34 | cmd.Parameters.AddWithValue("$k", key); |
| 35 | cmd.Parameters.AddWithValue("$v", value); |
| 36 | cmd.ExecuteNonQuery(); |
| 37 | } |
| 38 | |
| 39 | private static string? ReadMeta(SqliteConnection conn, string key) |
| 40 | { |
| 41 | using var cmd = conn.CreateCommand(); |
| 42 | cmd.CommandText = "SELECT value FROM meta WHERE key=$k LIMIT 1;"; |
| 43 | cmd.Parameters.AddWithValue("$k", key); |
| 44 | var o = cmd.ExecuteScalar(); |
| 45 | return o is null or DBNull ? null : Convert.ToString(o, CultureInfo.InvariantCulture); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |