Documentation
¶
Overview ¶
internal/storage/database.go
internal/storage/metadata_repo.go
internal/storage/userdb_repo.go
Index ¶
- Variables
- func ConnectMetadataDB(cfg *config.Config) (*sql.DB, error)
- func ConnectUserDB(ctx context.Context, filePath string) (*sql.DB, error)
- func CreateTable(ctx context.Context, userDB *sql.DB, createSQL string) error
- func CreateUser(ctx context.Context, db *sql.DB, user_id, username, email, passwordHash string) (string, error)
- func DeleteDatabaseRegistration(ctx context.Context, db *sql.DB, userId, dbName string) error
- func DeleteRecord(ctx context.Context, userDB *sql.DB, deleteSQL string, recordID int64) (int64, error)
- func DropTable(ctx context.Context, userDB *sql.DB, tableName string) error
- func FindAPIKeyByDatabaseId(ctx context.Context, db *sql.DB, databaseId int64) (string, error)
- func FindDatabaseIDByNameAndUser(ctx context.Context, db *sql.DB, userId string, dbName string) (int64, error)
- func FindDatabasePath(ctx context.Context, db *sql.DB, userId, dbName string) (string, error)
- func FindUserByEmail(ctx context.Context, db *sql.DB, email string) (*domain.UserMetadata, error)
- func FindUserByUserId(ctx context.Context, db *sql.DB, user_id string) (*domain.UserMetadata, error)
- func GetRecord(ctx context.Context, userDB *sql.DB, selectSQL string, recordID int64) (map[string]interface{}, error)
- func InsertRecord(ctx context.Context, userDB *sql.DB, insertSQL string, values ...interface{}) (int64, error)
- func ListRecords(ctx context.Context, userDB *sql.DB, tableName string, queryParams url.Values) ([]map[string]any, error)
- func ListTables(ctx context.Context, userDB *sql.DB) ([]domain.TableMetadata, error)
- func ListUserDatabases(ctx context.Context, db *sql.DB, userId string) ([]domain.DatabaseMetadata, error)
- func ListUserTableSchema(ctx context.Context, userDB *sql.DB, tableName string) ([]domain.TableSchemaMetaData, error)
- func PragmaTableInfo(ctx context.Context, userDB *sql.DB, tableName string) (map[string]string, error)
- func RegisterDatabase(ctx context.Context, db *sql.DB, userId, dbName, filePath string) error
- func StoreAPIKey(ctx context.Context, db *sql.DB, userId string, databaseId int64) (string, error)
- func UpdateRecord(ctx context.Context, userDB *sql.DB, updateSQL string, values ...interface{}) (int64, error)
Constants ¶
This section is empty.
Variables ¶
var ( ErrUserNotFound = errors.New("user not found") ErrEmailExists = errors.New("email already exists") ErrDatabaseExists = errors.New("database name already exists for this user") ErrDatabaseNotFound = errors.New("database not found or not registered for this user") ErrInvalidCredentials = errors.New("invalid credentials") ErrConflict = errors.New("cannot generate more than one api key for a database") ErrAPIKeyGeneration = errors.New("failed to generate api key components") )
Specific errors for metadata operations
var ( ErrRecordNotFound = errors.New("record not found") ErrTableNotFound = errors.New("table not found") // Derived from specific error strings ErrColumnNotFound = errors.New("column not found") // Derived ErrTypeMismatch = errors.New("datatype mismatch") // Derived ErrConstraintViolation = errors.New("constraint violation") // Derived ErrInvalidFilterValue = errors.New("invalid value provided for filter") // New error )
Specific errors for user DB operations
Functions ¶
func ConnectMetadataDB ¶
ConnectMetadataDB initializes the connection pool for the metadata SQLite database and ensures the required tables ('users', 'databases', 'api_key') exist.
func ConnectUserDB ¶
ConnectUserDB opens and pings a connection to a specific user DB file. The caller is responsible for closing the connection.
func CreateTable ¶
CreateTable executes a CREATE TABLE statement in the user DB.
func CreateUser ¶
func CreateUser(ctx context.Context, db *sql.DB, user_id, username, email, passwordHash string) (string, error)
CreateUser inserts a new user into the metadata database.
func DeleteDatabaseRegistration ¶
DeleteDatabaseRegistration removes the database entry from the metadata table. It returns ErrDatabaseNotFound if no matching entry was found.
func DeleteRecord ¶
func DeleteRecord(ctx context.Context, userDB *sql.DB, deleteSQL string, recordID int64) (int64, error)
DeleteRecord executes a DELETE statement and returns rows affected.
func DropTable ¶
DropTable executes a DROP TABLE statement in the user DB. tableName should be pre-validated by the caller.
func FindAPIKeyByDatabaseId ¶
FindAPIKeyByDatabaseId retrieves potential key for a particular user
func FindDatabaseIDByNameAndUser ¶
func FindDatabaseIDByNameAndUser(ctx context.Context, db *sql.DB, userId string, dbName string) (int64, error)
FindDatabaseIDByNameAndUser retrieves the ID of a database owned by a specific user. Returns the database ID or ErrDatabaseNotFound if no match.
func FindDatabasePath ¶
FindDatabasePath retrieves the file path for a given user and database name.
func FindUserByEmail ¶
FindUserByEmail retrieves a user by their email address.
func FindUserByUserId ¶
func FindUserByUserId(ctx context.Context, db *sql.DB, user_id string) (*domain.UserMetadata, error)
FindUserByUserId finds a user with user_id
func GetRecord ¶
func GetRecord(ctx context.Context, userDB *sql.DB, selectSQL string, recordID int64) (map[string]interface{}, error)
GetRecord executes SELECT * WHERE id = ? and returns a single map or ErrRecordNotFound.
func InsertRecord ¶
func InsertRecord(ctx context.Context, userDB *sql.DB, insertSQL string, values ...interface{}) (int64, error)
InsertRecord executes an INSERT statement and returns the last insert ID.
func ListRecords ¶
func ListRecords(ctx context.Context, userDB *sql.DB, tableName string, queryParams url.Values) ([]map[string]any, error)
Accepts tableName and query parameters directly.
func ListTables ¶
ListTables retrieves a list of table names from the user's database file.
func ListUserDatabases ¶
func ListUserDatabases(ctx context.Context, db *sql.DB, userId string) ([]domain.DatabaseMetadata, error)
ListUserDatabases retrieves a list of database names registered by a specific user.
func ListUserTableSchema ¶
func PragmaTableInfo ¶
func PragmaTableInfo(ctx context.Context, userDB *sql.DB, tableName string) (map[string]string, error)
PragmaTableInfo retrieves schema information for a table.
func RegisterDatabase ¶
RegisterDatabase inserts a new database registration record.
func StoreAPIKey ¶
StoreAPIKey generates and stores a new API key scoped to a specific user and database. It returns the *full, unhashed* key (prefix + secret) ONCE upon successful creation.
Types ¶
This section is empty.