Documentation
¶
Index ¶
- Constants
- Variables
- func ConvertRQLiteError(err error) error
- func FormatRQLiteError(err error) string
- func GetRQLiteErrorDetails(err error) (operation, table, query, message, detail string, statusCode int)
- func GetStatusInfoFromResponse(raw map[string]interface{}) (orm.NodeStatusStruct, error)
- func IsAuthenticationError(err error) bool
- func IsCheckViolation(err error) bool
- func IsColumnNotFound(err error) bool
- func IsConnectionError(err error) bool
- func IsConstraintViolation(err error) bool
- func IsDatabaseLocked(err error) bool
- func IsForeignKeyViolation(err error) bool
- func IsHTTPError(err error, statusCode int) bool
- func IsNodeUnavailable(err error) bool
- func IsNotNullViolation(err error) bool
- func IsPrimaryKeyViolation(err error) bool
- func IsReadonlyError(err error) bool
- func IsRetryable(err error) bool
- func IsSyntaxError(err error) bool
- func IsTableNotFound(err error) bool
- func IsUniqueViolation(err error) bool
- func WrapRQLiteError(err error, operation, table, query string) error
- func WrapRQLiteHTTPError(err error, operation, table, query string, statusCode int) error
- type ExecuteResponse
- type ExecuteResult
- type QueryResponse
- type QueryResult
- type RQLiteDirectDB
- func (db *RQLiteDirectDB) BeginTransaction() (orm.Transaction, error)
- func (db *RQLiteDirectDB) ExecManySQL(sqls []string) ([]orm.BasicSQLResult, error)
- func (db *RQLiteDirectDB) ExecManySQLParameterized(paramSQLs []orm.ParametereizedSQL) ([]orm.BasicSQLResult, error)
- func (db *RQLiteDirectDB) ExecOneSQL(sql string) orm.BasicSQLResult
- func (db *RQLiteDirectDB) ExecOneSQLParameterized(paramSQL orm.ParametereizedSQL) orm.BasicSQLResult
- func (db *RQLiteDirectDB) GetSchema(hideSQL, hideSureSQL bool) []orm.SchemaStruct
- func (db *RQLiteDirectDB) InsertManyDBRecords(records []orm.DBRecord, queue bool) ([]orm.BasicSQLResult, error)
- func (db *RQLiteDirectDB) InsertManyDBRecordsSameTable(records []orm.DBRecord, queue bool) ([]orm.BasicSQLResult, error)
- func (db *RQLiteDirectDB) InsertManyTableStructs(objs []orm.TableStruct, queue bool) ([]orm.BasicSQLResult, error)
- func (db *RQLiteDirectDB) InsertOneDBRecord(record orm.DBRecord, queue bool) orm.BasicSQLResult
- func (db *RQLiteDirectDB) InsertOneTableStruct(obj orm.TableStruct, queue bool) orm.BasicSQLResult
- func (db *RQLiteDirectDB) IsConnected() bool
- func (db *RQLiteDirectDB) Leader() (string, error)
- func (db *RQLiteDirectDB) Peers() ([]string, error)
- func (db *RQLiteDirectDB) SelectMany(tableName string) (orm.DBRecords, error)
- func (db *RQLiteDirectDB) SelectManyComplex(query *orm.ComplexQuery) ([]orm.DBRecord, error)
- func (db *RQLiteDirectDB) SelectManySQL(sqls []string) ([]orm.DBRecords, error)
- func (db *RQLiteDirectDB) SelectManySQLParameterized(paramSQLs []orm.ParametereizedSQL) ([]orm.DBRecords, error)
- func (db *RQLiteDirectDB) SelectManyWithCondition(tableName string, condition *orm.Condition) ([]orm.DBRecord, error)
- func (db *RQLiteDirectDB) SelectOne(tableName string) (orm.DBRecord, error)
- func (db *RQLiteDirectDB) SelectOneComplex(query *orm.ComplexQuery) (orm.DBRecord, error)
- func (db *RQLiteDirectDB) SelectOneSQL(sql string) (orm.DBRecords, error)
- func (db *RQLiteDirectDB) SelectOneSQLParameterized(paramSQL orm.ParametereizedSQL) (orm.DBRecords, error)
- func (db *RQLiteDirectDB) SelectOneWithCondition(tableName string, condition *orm.Condition) (orm.DBRecord, error)
- func (db *RQLiteDirectDB) SelectOnlyOneSQL(sql string) (orm.DBRecord, error)
- func (db *RQLiteDirectDB) SelectOnlyOneSQLParameterized(paramSQL orm.ParametereizedSQL) (orm.DBRecord, error)
- func (db *RQLiteDirectDB) Status() (orm.NodeStatusStruct, error)
- type RQLiteError
- type RqliteDirectConfig
Constants ¶
const ( // Primary result codes ErrCodeSQLiteOK = 0 // Successful result ErrCodeSQLiteError = 1 // Generic error ErrCodeSQLiteBusy = 5 // Database is locked ErrCodeSQLiteReadonly = 8 // Attempt to write a readonly database ErrCodeSQLiteConstraint = 19 // Constraint violation ErrCodeSQLiteNotADB = 26 // File opened that is not a database // Extended result codes for constraint violations ErrCodeSQLiteConstraintUnique = 2067 // UNIQUE constraint failed ErrCodeSQLiteConstraintPrimaryKey = 1555 // PRIMARY KEY constraint failed ErrCodeSQLiteConstraintNotNull = 1299 // NOT NULL constraint failed ErrCodeSQLiteConstraintForeignKey = 787 // FOREIGN KEY constraint failed ErrCodeSQLiteConstraintCheck = 275 // CHECK constraint failed // HTTP status codes for RQLite-specific errors HTTPStatusForbidden = http.StatusForbidden // 403 HTTPStatusNotFound = http.StatusNotFound // 404 HTTPStatusBadRequest = http.StatusBadRequest // 400 HTTPStatusInternalError = http.StatusInternalServerError // 500 )
SQLite error codes (RQLite uses SQLite under the hood) See: https://www.sqlite.org/rescode.html
const ( ErrMsgUniqueConstraint = "UNIQUE constraint failed" ErrMsgPrimaryKeyConstraint = "PRIMARY KEY constraint failed" ErrMsgNotNullConstraint = "NOT NULL constraint failed" ErrMsgForeignKeyConstraint = "FOREIGN KEY constraint failed" ErrMsgCheckConstraint = "CHECK constraint failed" ErrMsgDatabaseLocked = "database is locked" ErrMsgReadonlyDatabase = "attempt to write a readonly database" ErrMsgNoSuchTable = "no such table" ErrMsgNoSuchColumn = "no such column" ErrMsgSyntaxError = "syntax error" )
Common error messages from SQLite/RQLite
const ( PREFIX_SURESQL_TABLE = "_" PREFIX_SQLITE_TABLE = "sqlite_" SCHEMA_TABLE = "sqlite_master" // Default HTTP timeouts // TODO: maybe read this from env or pass it from Config? DEFAULT_CONNECTION_TIMEOUT = 60 * time.Second DEFAULT_TIMEOUT = 60 * time.Second DEFAULT_DIAL_TIMEOUT = 60 * time.Second DEFAULT_KEEP_ALIVE = 30 * time.Second DEFAULT_TLS_HANDSHAKE_TIMEOUT = 30 * time.Second DEFAULT_RESPONSE_TIMEOUT = 60 * time.Second DEFAULT_CONTINUE_TIMEOUT = 5 * time.Second DEFAULT_MAX_IDLE_CONNECTIONS = 100 DEFAULT_MAX_IDLE_CONNECTIONS_PER_HOST = 100 DEFAULT_MAX_CONNECTIONS_PER_HOST = 1000 DEFAULT_IDLE_CONNECTION_TIMEOUT = 90 * time.Second DEFAULT_RETRY_TIMEOUT = 2 * time.Second DEFAULT_MAX_RETRIES = 3 // RQLite API endpoints ENDPOINT_EXECUTE = "/db/execute" ENDPOINT_QUERY = "/db/query" ENDPOINT_UNIFIED = "/db/request" ENDPOINT_LOAD = "/db/load" ENDPOINT_BACKUP = "/db/backup" // curl -s -XGET localhost:4001/db/backup -o bak.sqlite3 (returns might need to be saved as a file right away) ENDPOINT_BOOT = "/boot" ENDPOINT_SNAPSHOT = "/snapshot" ENDPOINT_STATUS = "/status" // option are /status?pretty ENDPOINT_READY = "/readyz" // option readyz?noleader or /readyz?sync&timeout=5s ENDPOINT_JOIN = "/join" ENDPOINT_REMOVE = "/remove" ENDPOINT_NODE = "/nodes" // can have nodes?pretty&ver=2 for improved JSON format ENDPOINT_DEBUG = "/debug" ENDPOINT_VARS = "/debug/vars" ENDPOINT_PPROF_CMDLINE = "/debug/pprof/cmdline" ENDPOINT_PPROF_PROFILE = "/debug/pprof/profile" ENDPOINT_PPROF_SYMBOL = "/debug/pprof/symbol" DEFAULT_MAX_POOL = 25 )
Variables ¶
var ( ErrRQLiteNotConnected medaerror.MedaError = medaerror.MedaError{Message: "RQLite database is not connected"} ErrRQLiteInvalidURL medaerror.MedaError = medaerror.MedaError{Message: "invalid RQLite URL"} ErrRQLiteConnectionFailed medaerror.MedaError = medaerror.MedaError{Message: "failed to connect to RQLite server"} ErrRQLiteQueryFailed medaerror.MedaError = medaerror.MedaError{Message: "RQLite query execution failed"} ErrRQLiteExecuteFailed medaerror.MedaError = medaerror.MedaError{Message: "RQLite execute command failed"} ErrRQLiteInvalidConfig medaerror.MedaError = medaerror.MedaError{Message: "invalid RQLite configuration"} ErrRQLiteTimeout medaerror.MedaError = medaerror.MedaError{Message: "RQLite operation timed out"} ErrRQLiteReadonly medaerror.MedaError = medaerror.MedaError{Message: "RQLite node is in readonly mode"} ErrRQLiteDatabaseLocked medaerror.MedaError = medaerror.MedaError{Message: "RQLite database is locked"} ErrRQLiteInvalidJSON medaerror.MedaError = medaerror.MedaError{Message: "invalid JSON response from RQLite"} ErrRQLiteConsistencyFailure medaerror.MedaError = medaerror.MedaError{Message: "RQLite consistency requirement not met"} )
Custom RQLite errors using medaerror
Functions ¶
func ConvertRQLiteError ¶ added in v0.0.4
ConvertRQLiteError converts an RQLite error to an appropriate ORM error
func FormatRQLiteError ¶ added in v0.0.4
FormatRQLiteError formats an RQLite error for logging or display
func GetRQLiteErrorDetails ¶ added in v0.0.4
func GetRQLiteErrorDetails(err error) (operation, table, query, message, detail string, statusCode int)
GetRQLiteErrorDetails extracts detailed error information
func GetStatusInfoFromResponse ¶
func GetStatusInfoFromResponse(raw map[string]interface{}) (orm.NodeStatusStruct, error)
When calling rqlite/status it returns long JSON format we only take what is needed
func IsAuthenticationError ¶ added in v0.0.4
IsAuthenticationError checks if the error is an authentication failure
func IsCheckViolation ¶ added in v0.0.4
IsCheckViolation checks if the error is a CHECK constraint violation
func IsColumnNotFound ¶ added in v0.0.4
IsColumnNotFound checks if the error is due to a non-existent column
func IsConnectionError ¶ added in v0.0.4
IsConnectionError checks if the error is related to connection failure
func IsConstraintViolation ¶ added in v0.0.4
IsConstraintViolation checks if the error is any type of constraint violation
func IsDatabaseLocked ¶ added in v0.0.4
IsDatabaseLocked checks if the error is due to database being locked
func IsForeignKeyViolation ¶ added in v0.0.4
IsForeignKeyViolation checks if the error is a FOREIGN KEY constraint violation
func IsHTTPError ¶ added in v0.0.4
IsHTTPError checks if the error is an HTTP error with a specific status code
func IsNodeUnavailable ¶ added in v0.0.4
IsNodeUnavailable checks if the error is due to node being unavailable
func IsNotNullViolation ¶ added in v0.0.4
IsNotNullViolation checks if the error is a NOT NULL constraint violation
func IsPrimaryKeyViolation ¶ added in v0.0.4
IsPrimaryKeyViolation checks if the error is a PRIMARY KEY constraint violation
func IsReadonlyError ¶ added in v0.0.4
IsReadonlyError checks if the error is due to attempting to write to a readonly database
func IsRetryable ¶ added in v0.0.4
IsRetryable checks if the error is transient and the operation can be retried
func IsSyntaxError ¶ added in v0.0.4
IsSyntaxError checks if the error is a SQL syntax error
func IsTableNotFound ¶ added in v0.0.4
IsTableNotFound checks if the error is due to a non-existent table
func IsUniqueViolation ¶ added in v0.0.4
IsUniqueViolation checks if the error is a UNIQUE constraint violation
func WrapRQLiteError ¶ added in v0.0.4
WrapRQLiteError wraps an error with RQLite-specific context
Types ¶
type ExecuteResponse ¶
type ExecuteResponse struct {
Results []ExecuteResult `json:"results"`
Time float64 `json:"time"`
}
ExecuteResponse represents the response from a write operation
type ExecuteResult ¶
type ExecuteResult struct {
LastInsertID int `json:"last_insert_id"`
RowsAffected int `json:"rows_affected"`
Time float64 `json:"time"`
Error string `json:"error,omitempty"`
}
ExecuteResult represents a single result from a write operation
type QueryResponse ¶
type QueryResponse struct {
Results []QueryResult `json:"results"`
Time float64 `json:"time"`
}
QueryResponse represents the response from a read operation
type QueryResult ¶
type QueryResult struct {
Columns []string `json:"columns"`
Types []string `json:"types"`
Values [][]interface{} `json:"values"`
Time float64 `json:"time"`
Error string `json:"error,omitempty"`
}
QueryResult represents a single result from a read operation
type RQLiteDirectDB ¶
type RQLiteDirectDB struct {
Config RqliteDirectConfig
HTTPClient *http.Client
}
RQLiteDirectDB implements the orm.Database interface for direct HTTP access to RQLite
func NewDatabase ¶
func NewDatabase(config RqliteDirectConfig) (*RQLiteDirectDB, error)
NewDatabase creates a new RQLiteDirectDB instance
func (*RQLiteDirectDB) BeginTransaction ¶ added in v0.2.0
func (db *RQLiteDirectDB) BeginTransaction() (orm.Transaction, error)
BeginTransaction starts a new transaction by creating a transaction buffer
func (*RQLiteDirectDB) ExecManySQL ¶
func (db *RQLiteDirectDB) ExecManySQL(sqls []string) ([]orm.BasicSQLResult, error)
ExecManySQL executes multiple SQL statements
func (*RQLiteDirectDB) ExecManySQLParameterized ¶
func (db *RQLiteDirectDB) ExecManySQLParameterized(paramSQLs []orm.ParametereizedSQL) ([]orm.BasicSQLResult, error)
ExecManySQLParameterized executes multiple parameterized SQL statements
func (*RQLiteDirectDB) ExecOneSQL ¶
func (db *RQLiteDirectDB) ExecOneSQL(sql string) orm.BasicSQLResult
ExecOneSQL executes a single SQL statement
func (*RQLiteDirectDB) ExecOneSQLParameterized ¶
func (db *RQLiteDirectDB) ExecOneSQLParameterized(paramSQL orm.ParametereizedSQL) orm.BasicSQLResult
ExecOneSQLParameterized executes a single parameterized SQL statement
func (*RQLiteDirectDB) GetSchema ¶
func (db *RQLiteDirectDB) GetSchema(hideSQL, hideSureSQL bool) []orm.SchemaStruct
GetSchema returns the database schema
func (*RQLiteDirectDB) InsertManyDBRecords ¶
func (db *RQLiteDirectDB) InsertManyDBRecords(records []orm.DBRecord, queue bool) ([]orm.BasicSQLResult, error)
InsertManyDBRecords inserts multiple records
func (*RQLiteDirectDB) InsertManyDBRecordsSameTable ¶
func (db *RQLiteDirectDB) InsertManyDBRecordsSameTable(records []orm.DBRecord, queue bool) ([]orm.BasicSQLResult, error)
InsertManyDBRecordsSameTable inserts multiple records into the same table
func (*RQLiteDirectDB) InsertManyTableStructs ¶
func (db *RQLiteDirectDB) InsertManyTableStructs(objs []orm.TableStruct, queue bool) ([]orm.BasicSQLResult, error)
InsertManyTableStructs inserts multiple table structs
func (*RQLiteDirectDB) InsertOneDBRecord ¶
func (db *RQLiteDirectDB) InsertOneDBRecord(record orm.DBRecord, queue bool) orm.BasicSQLResult
InsertOneDBRecord inserts a single record
func (*RQLiteDirectDB) InsertOneTableStruct ¶
func (db *RQLiteDirectDB) InsertOneTableStruct(obj orm.TableStruct, queue bool) orm.BasicSQLResult
InsertOneTableStruct inserts a single table struct
func (*RQLiteDirectDB) IsConnected ¶
func (db *RQLiteDirectDB) IsConnected() bool
IsConnected checks if the database connection is alive
func (*RQLiteDirectDB) Leader ¶
func (db *RQLiteDirectDB) Leader() (string, error)
Leader returns the leader node of the RQLite cluster
func (*RQLiteDirectDB) Peers ¶
func (db *RQLiteDirectDB) Peers() ([]string, error)
Peers returns the peer nodes of the RQLite cluster
func (*RQLiteDirectDB) SelectMany ¶
func (db *RQLiteDirectDB) SelectMany(tableName string) (orm.DBRecords, error)
SelectMany selects multiple records from the table
func (*RQLiteDirectDB) SelectManyComplex ¶ added in v0.1.0
func (db *RQLiteDirectDB) SelectManyComplex(query *orm.ComplexQuery) ([]orm.DBRecord, error)
SelectManyComplex executes a complex query with JOINs, custom SELECT fields, GROUP BY, etc. It supports advanced SQL features through the ComplexQuery struct.
Example:
query := &orm.ComplexQuery{
Select: []string{"users.id", "users.name", "COUNT(orders.id) as order_count"},
From: "users",
Joins: []orm.Join{
{Type: orm.LeftJoin, Table: "orders", Condition: "users.id = orders.user_id"},
},
Where: &orm.Condition{Field: "users.status", Operator: "=", Value: "active"},
GroupBy: []string{"users.id", "users.name"},
Having: "COUNT(orders.id) > 5",
OrderBy: []string{"order_count DESC"},
Limit: 10,
}
records, err := db.SelectManyComplex(query)
func (*RQLiteDirectDB) SelectManySQL ¶
func (db *RQLiteDirectDB) SelectManySQL(sqls []string) ([]orm.DBRecords, error)
SelectManySQL executes multiple SQL queries and returns the results of each
func (*RQLiteDirectDB) SelectManySQLParameterized ¶
func (db *RQLiteDirectDB) SelectManySQLParameterized(paramSQLs []orm.ParametereizedSQL) ([]orm.DBRecords, error)
SelectManySQLParameterized executes multiple parameterized SQL queries
func (*RQLiteDirectDB) SelectManyWithCondition ¶
func (db *RQLiteDirectDB) SelectManyWithCondition(tableName string, condition *orm.Condition) ([]orm.DBRecord, error)
SelectManyWithCondition selects multiple records with a condition
func (*RQLiteDirectDB) SelectOne ¶
func (db *RQLiteDirectDB) SelectOne(tableName string) (orm.DBRecord, error)
SelectOne selects a single record from the table
func (*RQLiteDirectDB) SelectOneComplex ¶ added in v0.1.0
func (db *RQLiteDirectDB) SelectOneComplex(query *orm.ComplexQuery) (orm.DBRecord, error)
SelectOneComplex executes a complex query and ensures exactly one row is returned. It's similar to SelectManyComplex but validates that only one record is returned.
Example:
query := &orm.ComplexQuery{
Select: []string{"users.*", "profiles.bio"},
From: "users",
Joins: []orm.Join{
{Type: orm.InnerJoin, Table: "profiles", Condition: "users.id = profiles.user_id"},
},
Where: &orm.Condition{Field: "users.id", Operator: "=", Value: 123},
}
record, err := db.SelectOneComplex(query)
func (*RQLiteDirectDB) SelectOneSQL ¶
func (db *RQLiteDirectDB) SelectOneSQL(sql string) (orm.DBRecords, error)
SelectOneSQL executes a single SQL query and returns the results
func (*RQLiteDirectDB) SelectOneSQLParameterized ¶
func (db *RQLiteDirectDB) SelectOneSQLParameterized(paramSQL orm.ParametereizedSQL) (orm.DBRecords, error)
SelectOneSQLParameterized executes a single parameterized SQL query
func (*RQLiteDirectDB) SelectOneWithCondition ¶
func (db *RQLiteDirectDB) SelectOneWithCondition(tableName string, condition *orm.Condition) (orm.DBRecord, error)
SelectOneWithCondition selects a single record with a condition
func (*RQLiteDirectDB) SelectOnlyOneSQL ¶
func (db *RQLiteDirectDB) SelectOnlyOneSQL(sql string) (orm.DBRecord, error)
SelectOnlyOneSQL executes a SQL query and ensures exactly one row is returned
func (*RQLiteDirectDB) SelectOnlyOneSQLParameterized ¶
func (db *RQLiteDirectDB) SelectOnlyOneSQLParameterized(paramSQL orm.ParametereizedSQL) (orm.DBRecord, error)
SelectOnlyOneSQLParameterized executes a parameterized SQL query and ensures exactly one row is returned
func (*RQLiteDirectDB) Status ¶
func (db *RQLiteDirectDB) Status() (orm.NodeStatusStruct, error)
Status returns the status of the RQLite cluster
type RQLiteError ¶ added in v0.0.4
type RQLiteError struct {
Operation string // The operation that failed (e.g., "SELECT", "INSERT", "EXECUTE")
Table string // The table involved (if applicable)
Query string // The SQL query that failed (if applicable)
StatusCode int // HTTP status code (if applicable)
Message string // Error message
Detail string // Detailed error information from RQLite
Err error // Original error
}
RQLiteError wraps RQLite-specific errors with additional context
func (*RQLiteError) Error ¶ added in v0.0.4
func (e *RQLiteError) Error() string
Error implements the error interface
func (*RQLiteError) Unwrap ¶ added in v0.0.4
func (e *RQLiteError) Unwrap() error
Unwrap returns the underlying error
type RqliteDirectConfig ¶
type RqliteDirectConfig struct {
URL string // Base URL for the RQLite node (e.g. "http://localhost:4001")
Consistency string // Consistency level: "none", "weak", "strong"
Username string // Optional username for authentication
Password string // Optional password for authentication
Timeout time.Duration // HTTP client timeout
RetryCount int // Number of retries for failed requests
}
RqliteDirectConfig holds configuration for direct RQLite connections