Documentation ¶
Overview ¶
Package database interacts with the remote datastore for Defacto2. Currently MySQL is implemented with Postgres to be added later.
Index ¶
- Constants
- Variables
- func Approve(db *sql.DB, w io.Writer, cfg conf.Config, verbose bool) error
- func CheckID(id string) error
- func CheckUUID(uuid string) error
- func Columns(db *sql.DB, w io.Writer, t Table) error
- func ConnDebug(cfg conf.Config) (string, error)
- func Connect(cfg conf.Config) (*sql.DB, error)
- func DateTime(raw sql.RawBytes) (string, error)
- func DeObfuscate(s string) int
- func DemozooID(db *sql.DB, demozoo uint) (int, error)
- func Distinct(db *sql.DB, value string) ([]string, error)
- func Execute(db *sql.DB, u Update) (int64, error)
- func FileUpdate(name string, db time.Time) (bool, error)
- func Fix(db *sql.DB, w io.Writer) error
- func GetFile(db *sql.DB, val string) (string, error)
- func GetID(db *sql.DB, val string) (int, error)
- func GetKeys(db *sql.DB, stmt string) ([]int, error)
- func IsDemozoo(b []sql.RawBytes) (bool, error)
- func IsID(s string) bool
- func IsUUID(s string) bool
- func IsUnApproved(b []sql.RawBytes) (bool, error)
- func LastUpdate(db *sql.DB) (time.Time, error)
- func ObfuscateParam(param string) string
- func StripChars(s string) string
- func StripStart(s string) string
- func Tbls() string
- func Total(db *sql.DB, w io.Writer, s *string) (int, error)
- func TrimSP(s string) string
- func Val(col sql.RawBytes) string
- func Waiting(db *sql.DB) (int, error)
- type Empty
- type Flags
- type IDs
- type Table
- type Update
Examples ¶
Constants ¶
const ( Datetime = "2006-01-02T15:04:05Z" // Datetime MySQL format. // ExampleID is an invalid placeholder UUID, where x represents a digit. ExampleID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" // TestID is a generic UUID that can be used for unit tests. TestID = "00000000-0000-0000-0000-000000000000" // UpdateID is a user id to use with the updatedby column. UpdateID = "b66dc282-a029-4e99-85db-2cf2892fffcc" WhereAvailable = templ.WhereAvailable WhereDownloadBlock = templ.WhereDownloadBlock WhereHidden = templ.WhereHidden )
Variables ¶
var ( ErrDB = errors.New("database handle pointer cannot be nil") ErrNoID = errors.New("unique id is does not exist in the database table") ErrPointer = errors.New("pointer value cannot be nil") ErrSynID = errors.New("id value is not a valid") ErrValue = errors.New("argument cannot be an empty value") )
Functions ¶
func CheckID ¶
CheckID checks the syntax of the universal unique record id or MySQL auto-generated id.
func Columns ¶ added in v1.11.0
Columns details the columns used by the table.
Example ¶
package main import ( "io" "log" "github.com/Defacto2/df2/pkg/conf" "github.com/Defacto2/df2/pkg/database" ) func main() { db, err := database.Connect(conf.Defaults()) if err != nil { log.Fatal(err) } if err := database.Columns(db, io.Discard, database.Netresources); err != nil { log.Fatal(err) } }
Output:
func Connect ¶
Connect the database and handle any errors. The DB connection must be closed after use.
Example ¶
package main import ( "fmt" "log" "github.com/Defacto2/df2/pkg/conf" "github.com/Defacto2/df2/pkg/database" ) func main() { db, err := database.Connect(conf.Defaults()) if err != nil { log.Fatal(err) } defer db.Close() fmt.Print(db.Stats().WaitCount) }
Output: 0
func DeObfuscate ¶ added in v1.7.0
DeObfuscate a public facing, obfuscated file ID or file URL. A URL can point to a Defacto2 file download or detail page.
func DemozooID ¶ added in v1.6.0
DemozooID looks up a Demozoo productions ID in the files table, and returns the ID of the first matched Defacto2 file record. If no match is found then a zero is returned.
func Execute ¶
Execute Query and Args to update the database and returns the total number of changes.
func FileUpdate ¶
FileUpdate returns true when named file is newer than the database time. True is always returned when the named file does not exist or whenever it is 0 bytes in size.
func GetFile ¶
GetFile looks up val and returns the filename of the file record. The val string must be either a UUID of the record or an increment ID.
func GetID ¶
GetID looks up the val and returns a matching auto-increment ID of the file record. The val string must be either a UUID of the record or an increment ID.
func GetKeys ¶ added in v1.10.0
GetKeys returns all the primary keys used by the files table. The integer keys are sorted incrementally. An optional, statement can be provided to filter the results.
func IsUnApproved ¶ added in v1.11.0
IsUnApproved reports if a fetched file record is set to unapproved.
func LastUpdate ¶
LastUpdate reports the time when the files database was last modified.
func ObfuscateParam ¶
ObfuscateParam hides the param value using the method implemented in CFWheels obfuscateParam() helper.
func StripChars ¶
StripChars removes incompatible characters used for groups and author names.
func StripStart ¶
StripStart removes non-alphanumeric characters from the start of the string.
func Total ¶
Total reports the number of records fetched by the supplied SQL query.
Example ¶
package main import ( "fmt" "log" "os" "github.com/Defacto2/df2/pkg/conf" "github.com/Defacto2/df2/pkg/database" ) func main() { db, err := database.Connect(conf.Defaults()) if err != nil { log.Fatal(err) } s := "SELECT * FROM `files` WHERE `id` = '1'" i, err := database.Total(db, os.Stdout, &s) if err != nil { log.Fatal(err) } fmt.Print(i) }
Output: 1
func Waiting ¶
Waiting returns the number of files requiring approval for public display.
Example ¶
package main import ( "fmt" "log" "github.com/Defacto2/df2/pkg/conf" "github.com/Defacto2/df2/pkg/database" ) func main() { db, err := database.Connect(conf.Defaults()) if err != nil { log.Fatal(err) } i, err := database.Waiting(db) if err != nil { log.Fatal(err) } fmt.Print(i >= 0) }
Output: true
Types ¶
type Empty ¶
type Empty struct{}
Empty is used as a blank value for search maps. See: https://dave.cheney.net/2014/03/25/the-empty-struct
type IDs ¶
type IDs map[string]struct{}
IDs are unique UUID values used by the database and filenames.
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
export
Package export queries records for data exports.
|
Package export queries records for data exports. |
recd
Package recd has various checks for record values.
|
Package recd has various checks for record values. |
templ
Package templ has the templates for the database SQL statements.
|
Package templ has the templates for the database SQL statements. |
update
Package update handles edits and updates to the database records.
|
Package update handles edits and updates to the database records. |
Package msql creates connections to MySQL datastores.
|
Package msql creates connections to MySQL datastores. |
Package psql creates connections to Postgres datastores.
|
Package psql creates connections to Postgres datastores. |