Documentation
¶
Overview ¶
Package pcre2 registers a sqlite3 PCRE2 extension for use with github.com/mattn/go-sqlite3.
This adds the REGEXP and IREGEXP (case-insensitive) functions to sqlite3. Both functions use PCRE2 for matching, and if available, will JIT compile the PCRE2 code before use.
Example ¶
package main
import (
"database/sql"
"fmt"
pcre2 "github.com/charlievieth/sqlite3-pcre2"
)
func main() {
// This example requires the sqlite3_pcre2 shared library to exist in
// the current working directory or in a well known library path such
// as "/lib", "/usr/lib", or "/usr/local/lib". The shared library can
// be create by running `make build`.
//
// Search the current working directory for the "sqlite3_pcre2" shared library.
pcre2.SearchWorkingDirectory(true)
// We need to use the pcre2.DriverName to open the database connection
// since includes the sqlite3 connection hook.
db, err := sql.Open(pcre2.DriverName, ":memory:")
if err != nil {
panic(err)
}
defer db.Close()
var ok bool
if err := db.QueryRow(`SELECT REGEXP('^a$', 'a');`).Scan(&ok); err != nil {
panic(err)
}
if !ok {
panic(fmt.Sprintf("got: %t want: %t", ok, true))
}
}
Output:
Index ¶
Examples ¶
Constants ¶
const ( // Name of the sqlite3_pcre2 database driver. To use this library with // go-sqlite3 open the database with: // // sql.Open(pcre2.DriverName, "opts..."). // DriverName = "sqlite3_pcre2" // Name of the sqlite3_pcre2 library without file extension(s). LibraryName = "sqlite3_pcre2" // Environment variable key to set a custom path to the sqlite3_pcre2 // library, which can be the path to the library itself or the directory // containing it. // // The path must be absolute. EnvKey = "SQLITE3_PCRE2_LIBRARY" )
Variables ¶
var ErrLibraryNotFound = errors.New("pcre2: could not find sqlite3_pcre2 shared library")
ErrLibraryNotFound is returned if the sqlite3_pcre2 library could not be found.
var LibExts = []string{".so"}
LibExts are the library file extensions searched for (LibraryName + ext).
Functions ¶
func SearchPaths ¶
func SearchPaths() []string
SearchPaths returns the system paths searched for the sqlite3_pcre2 shared library.
The search order is:
The path set by SetLibraryPath(), if any, which can be the path to the shared library or the directory containing it. If set, the seach ends here (that is the below locations will not be checked).
"SQLITE3_PCRE2_LIBRARY" environment variable, which can be the path to the shared library or the directory containing it.
Directory containing the executable that started the current process. If the executable is a symlink the directory containing the symlink is also searched.
System specific directories ("/usr/local/lib").
The current working directory, but only if SearchWorkingDirectory(true) is enabled.
Relative paths are joined with the current working directory. Empty strings are ignored.
func SearchWorkingDirectory ¶
SearchWorkingDirectory toggles if the current working directory is searched for the sqlite3_pcre2 shared library and returns the previous state.
Searching the current working directory is disabled by default because it can be exploited to load malicious code.
func SetLibraryPath ¶
func SetLibraryPath(path string)
SetLibraryPath sets the path to the sqlite3_pcre2 shared library, which can be the path to the shared library or the directory containing it. Relative paths are allowed.
If set, then *only* this path will be searched.
func Sqlite3ConnectHook ¶
func Sqlite3ConnectHook(conn *sqlite3.SQLiteConn) error
Sqlite3ConnectHook is the connection hook for sqlite3.
Usage:
sql.Register("NAME", &sqlite3.SQLiteDriver{
ConnectHook: Sqlite3ConnectHook,
})
Types ¶
This section is empty.