initial commit

This commit is contained in:
2026-04-10 15:31:52 -04:00
commit 39fcfc2968
18 changed files with 1066 additions and 0 deletions

31
db/db.go Normal file
View File

@@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
package db
import (
"context"
"database/sql"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
return &Queries{
db: tx,
}
}

31
db/models.go Normal file
View File

@@ -0,0 +1,31 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
package db
import (
"database/sql"
)
type File struct {
ID int64
Path string
Language string
Package sql.NullString
Hash string
IndexedAt sql.NullTime
}
type Symbol struct {
ID int64
FileID int64
Name string
Kind string
Line int64
LineEnd sql.NullInt64
Col sql.NullInt64
ColEnd sql.NullInt64
Exported sql.NullBool
ParentID sql.NullInt64
}

25
db/queries.sql Normal file
View File

@@ -0,0 +1,25 @@
-- name: GetFileByPath :one
SELECT id, path, language, package, hash, indexed_at
FROM files
WHERE path = ?;
-- name: UpsertFile :one
INSERT INTO files (path, language, package, hash)
VALUES (?, ?, ?, ?)
ON CONFLICT(path) DO UPDATE SET
language = excluded.language,
package = excluded.package,
hash = excluded.hash,
indexed_at = CURRENT_TIMESTAMP
RETURNING id, path, language, package, hash, indexed_at;
-- name: DeleteSymbolsByFileID :exec
DELETE FROM symbols WHERE file_id = ?;
-- name: InsertSymbol :one
INSERT INTO symbols (file_id, name, kind, line, line_end, col, col_end, exported, parent_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
RETURNING id;
-- name: DeleteStaleFiles :exec
DELETE FROM files WHERE path NOT IN (sqlc.slice('paths'));

132
db/queries.sql.go Normal file
View File

@@ -0,0 +1,132 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: queries.sql
package db
import (
"context"
"database/sql"
"strings"
)
const deleteStaleFiles = `-- name: DeleteStaleFiles :exec
DELETE FROM files WHERE path NOT IN (/*SLICE:paths*/?)
`
func (q *Queries) DeleteStaleFiles(ctx context.Context, paths []string) error {
query := deleteStaleFiles
var queryParams []interface{}
if len(paths) > 0 {
for _, v := range paths {
queryParams = append(queryParams, v)
}
query = strings.Replace(query, "/*SLICE:paths*/?", strings.Repeat(",?", len(paths))[1:], 1)
} else {
query = strings.Replace(query, "/*SLICE:paths*/?", "NULL", 1)
}
_, err := q.db.ExecContext(ctx, query, queryParams...)
return err
}
const deleteSymbolsByFileID = `-- name: DeleteSymbolsByFileID :exec
DELETE FROM symbols WHERE file_id = ?
`
func (q *Queries) DeleteSymbolsByFileID(ctx context.Context, fileID int64) error {
_, err := q.db.ExecContext(ctx, deleteSymbolsByFileID, fileID)
return err
}
const getFileByPath = `-- name: GetFileByPath :one
SELECT id, path, language, package, hash, indexed_at
FROM files
WHERE path = ?
`
func (q *Queries) GetFileByPath(ctx context.Context, path string) (File, error) {
row := q.db.QueryRowContext(ctx, getFileByPath, path)
var i File
err := row.Scan(
&i.ID,
&i.Path,
&i.Language,
&i.Package,
&i.Hash,
&i.IndexedAt,
)
return i, err
}
const insertSymbol = `-- name: InsertSymbol :one
INSERT INTO symbols (file_id, name, kind, line, line_end, col, col_end, exported, parent_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
RETURNING id
`
type InsertSymbolParams struct {
FileID int64
Name string
Kind string
Line int64
LineEnd sql.NullInt64
Col sql.NullInt64
ColEnd sql.NullInt64
Exported sql.NullBool
ParentID sql.NullInt64
}
func (q *Queries) InsertSymbol(ctx context.Context, arg InsertSymbolParams) (int64, error) {
row := q.db.QueryRowContext(ctx, insertSymbol,
arg.FileID,
arg.Name,
arg.Kind,
arg.Line,
arg.LineEnd,
arg.Col,
arg.ColEnd,
arg.Exported,
arg.ParentID,
)
var id int64
err := row.Scan(&id)
return id, err
}
const upsertFile = `-- name: UpsertFile :one
INSERT INTO files (path, language, package, hash)
VALUES (?, ?, ?, ?)
ON CONFLICT(path) DO UPDATE SET
language = excluded.language,
package = excluded.package,
hash = excluded.hash,
indexed_at = CURRENT_TIMESTAMP
RETURNING id, path, language, package, hash, indexed_at
`
type UpsertFileParams struct {
Path string
Language string
Package sql.NullString
Hash string
}
func (q *Queries) UpsertFile(ctx context.Context, arg UpsertFileParams) (File, error) {
row := q.db.QueryRowContext(ctx, upsertFile,
arg.Path,
arg.Language,
arg.Package,
arg.Hash,
)
var i File
err := row.Scan(
&i.ID,
&i.Path,
&i.Language,
&i.Package,
&i.Hash,
&i.IndexedAt,
)
return i, err
}

34
db/schema.sql Normal file
View File

@@ -0,0 +1,34 @@
CREATE TABLE files (
id INTEGER PRIMARY KEY,
path TEXT NOT NULL UNIQUE,
language TEXT NOT NULL,
package TEXT,
hash TEXT NOT NULL,
indexed_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE symbols (
id INTEGER PRIMARY KEY,
file_id INTEGER NOT NULL REFERENCES files(id) ON DELETE CASCADE,
name TEXT NOT NULL,
kind TEXT NOT NULL CHECK(kind IN (
'function', 'method', 'class', 'type',
'interface', 'constant', 'variable', 'constructor'
)),
line INTEGER NOT NULL,
line_end INTEGER,
col INTEGER,
col_end INTEGER,
exported BOOLEAN,
parent_id INTEGER REFERENCES symbols(id),
UNIQUE(file_id, name, kind, line)
);
CREATE INDEX idx_symbols_name ON symbols(name);
CREATE INDEX idx_symbols_kind ON symbols(kind);
CREATE INDEX idx_symbols_file_line ON symbols(file_id, line);
CREATE INDEX idx_symbols_parent ON symbols(parent_id);
CREATE INDEX idx_symbols_exported ON symbols(exported, kind);
CREATE INDEX idx_files_path ON files(path);
CREATE INDEX idx_files_language ON files(language);
CREATE INDEX idx_files_package ON files(package);

9
db/sqlc.yaml Normal file
View File

@@ -0,0 +1,9 @@
version: "2"
sql:
- engine: "sqlite"
queries: "queries.sql"
schema: "schema.sql"
gen:
go:
package: "db"
out: "."