storage

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

internal/storage/database.go

internal/storage/metadata_repo.go

internal/storage/userdb_repo.go

Index

Constants

This section is empty.

Variables

View Source
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

View Source
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

func ConnectMetadataDB(cfg *config.Config) (*sql.DB, error)

ConnectMetadataDB initializes the connection pool for the metadata SQLite database and ensures the required tables ('users', 'databases', 'api_key') exist.

func ConnectUserDB

func ConnectUserDB(ctx context.Context, filePath string) (*sql.DB, error)

ConnectUserDB opens and pings a connection to a specific user DB file. The caller is responsible for closing the connection.

func CreateTable

func CreateTable(ctx context.Context, userDB *sql.DB, createSQL string) error

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

func DeleteDatabaseRegistration(ctx context.Context, db *sql.DB, userId, dbName string) error

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

func DropTable(ctx context.Context, userDB *sql.DB, tableName string) error

DropTable executes a DROP TABLE statement in the user DB. tableName should be pre-validated by the caller.

func FindAPIKeyByDatabaseId

func FindAPIKeyByDatabaseId(ctx context.Context, db *sql.DB, databaseId int64) (string, error)

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

func FindDatabasePath(ctx context.Context, db *sql.DB, userId, dbName string) (string, error)

FindDatabasePath retrieves the file path for a given user and database name.

func FindUserByEmail

func FindUserByEmail(ctx context.Context, db *sql.DB, email string) (*domain.UserMetadata, error)

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

func ListTables(ctx context.Context, userDB *sql.DB) ([]domain.TableMetadata, error)

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 ListUserTableSchema(ctx context.Context, userDB *sql.DB, tableName string) ([]domain.TableSchemaMetaData, error)

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

func RegisterDatabase(ctx context.Context, db *sql.DB, userId, dbName, filePath string) error

RegisterDatabase inserts a new database registration record.

func StoreAPIKey

func StoreAPIKey(ctx context.Context, db *sql.DB, userId string, databaseId int64) (string, error)

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.

func UpdateRecord

func UpdateRecord(ctx context.Context, userDB *sql.DB, updateSQL string, values ...interface{}) (int64, error)

UpdateRecord executes an UPDATE statement and returns rows affected.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL