Documentation
¶
Overview ¶
Package database interacts with the MySQL datastore of Defacto2.
Index ¶
- Constants
- Variables
- func Approve(verbose bool) error
- func CheckID(s string) error
- func CheckUUID(s string) error
- func ColTypes(t Table) (string, error)
- func ConnErr() (*sql.DB, error)
- func ConnInfo() string
- func Connect() *sql.DB
- func DateTime(raw sql.RawBytes) string
- func DeObfuscate(s string) int
- func DemozooID(id uint) (uint, error)
- func Distinct(value string) ([]string, error)
- func Execute(u Update) (int64, error)
- func FileUpdate(name string, db time.Time) (bool, error)
- func Fix() error
- func GetFile(s string) (string, error)
- func GetID(s string) (uint, error)
- func GetKeys(whereStmt string) ([]int, error)
- func Init() connect.Connection
- func IsDemozoo(b []sql.RawBytes) bool
- func IsID(s string) bool
- func IsProof(b []sql.RawBytes) bool
- func IsUUID(s string) bool
- func LastUpdate() (time.Time, error)
- func ObfuscateParam(param string) string
- func StripChars(s string) string
- func StripStart(s string) string
- func Tbls() string
- func Total(s *string) (int, error)
- func TrimSP(s string) string
- func Val(col sql.RawBytes) string
- func Waiting() (uint, error)
- type Empty
- type Flags
- type IDs
- type Table
- type Update
Examples ¶
Constants ¶
const ( // Datetime MySQL format. Datetime = "2006-01-02T15:04:05Z" // UpdateID is a user id to use with the updatedby column. UpdateID = "b66dc282-a029-4e99-85db-2cf2892fffcc" Null = "NULL" CountFiles = "SELECT COUNT(*) FROM `files`" CountWaiting = CountFiles + " WHERE `deletedby` IS NULL AND `deletedat` IS NOT NULL" SelKeys = "SELECT `id` FROM `files`" SelNames = "SELECT `filename` FROM `files`" SelUpdate = "SELECT `updatedat` FROM `files`" + " WHERE `createdat` <> `updatedat` AND `deletedby` IS NULL" + " ORDER BY `updatedat` DESC LIMIT 1" WhereDownloadBlock = "WHERE `file_security_alert_url` IS NOT NULL AND `file_security_alert_url` != ''" WhereAvailable = "WHERE `deletedat` IS NULL" WhereHidden = "WHERE `deletedat` IS NOT NULL" )
Variables ¶
var ( ErrColType = errors.New("the value type is not usable with the mysql column") ErrConnect = errors.New("could not connect to the mysql database server") ErrNoID = errors.New("unique id is does not exist in the database table") ErrSynID = errors.New("id is not a valid id or uuid value") ErrSynUUID = errors.New("id is not a valid uuid") ErrNoTable = errors.New("unknown database table") ErrNoMethod = errors.New("unknown database export type") )
Functions ¶
func CheckID ¶
CheckID reports an error message for an incorrect universal unique record id or MySQL auto-generated id.
func ColTypes ¶
ColTypes details the columns used by the table.
Example ¶
package main import ( "fmt" "github.com/Defacto2/df2/pkg/database" ) func main() { if _, err := database.ColTypes(database.Users); err != nil { fmt.Print(err) } }
Output:
func ConnErr ¶
ConnErr will connect to the database or return any errors.
Example ¶
package main import ( "fmt" "github.com/Defacto2/df2/pkg/database" ) func main() { db, err := database.ConnErr() if err != nil { fmt.Print(err) } defer db.Close() }
Output:
func ConnInfo ¶
func ConnInfo() string
ConnInfo will connect to the database and return any errors.
Example ¶
package main import ( "fmt" "github.com/Defacto2/df2/pkg/database" ) func main() { s := database.ConnInfo() fmt.Print(s) }
Output:
func Connect ¶
Connect will connect to the database and handle any errors.
Example ¶
package main import ( "fmt" "github.com/Defacto2/df2/pkg/database" ) func main() { db := database.Connect() 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 finds a record ID by the saved Demozoo production ID. If no production exists a zero is returned.
func Execute ¶
Execute Query and Args to update the database and returns the total number of changes.
func FileUpdate ¶
FileUpdate reports if the named file is newer than the database time. True is always returned when the named file does not exist.
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 SQL WHERE statement can be provided to filter the results.
func Init ¶
func Init() connect.Connection
Init initialises the database connection using stored settings.
Example ¶
package main import ( "fmt" "github.com/Defacto2/df2/pkg/database" ) func main() { init := database.Init() fmt.Print(init.Port) }
Output: 3306
func LastUpdate ¶
LastUpdate reports the time when the files database was last modified.
Example ¶
package main import ( "fmt" "github.com/Defacto2/df2/pkg/database" ) func main() { if _, err := database.LastUpdate(); err != nil { fmt.Print(err) } }
Output:
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" "github.com/Defacto2/df2/pkg/database" ) func main() { db := database.Connect() defer db.Close() s := "SELECT * FROM `files` WHERE `id` = '1'" i, err := database.Total(&s) if err != nil { fmt.Print(err) } fmt.Print(i) }
Output: 1
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.