database

package
v1.11.9 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2023 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package database interacts with the remote datastore for Defacto2. Currently MySQL is implemented with Postgres to be added later.

Index

Examples

Constants

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

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

func Approve(db *sql.DB, w io.Writer, cfg conf.Config, verbose bool) error

Approve automatically checks and clears file records for live.

func CheckID

func CheckID(id string) error

CheckID checks the syntax of the universal unique record id or MySQL auto-generated id.

func CheckUUID

func CheckUUID(uuid string) error

CheckUUID checks the syntax of the universal unique record id.

func Columns added in v1.11.0

func Columns(db *sql.DB, w io.Writer, t Table) error

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 ConnDebug added in v1.11.0

func ConnDebug(cfg conf.Config) (string, error)

ConnDebug will connect to the database and return any errors.

func Connect

func Connect(cfg conf.Config) (*sql.DB, error)

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 DateTime

func DateTime(raw sql.RawBytes) (string, error)

DateTime colours and formats a date and time string.

func DeObfuscate added in v1.7.0

func DeObfuscate(s string) int

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

func DemozooID(db *sql.DB, demozoo uint) (int, error)

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 Distinct added in v1.10.0

func Distinct(db *sql.DB, value string) ([]string, error)

Distinct returns a unique list of values from the table column.

func Execute

func Execute(db *sql.DB, u Update) (int64, error)

Execute Query and Args to update the database and returns the total number of changes.

func FileUpdate

func FileUpdate(name string, db time.Time) (bool, error)

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 Fix

func Fix(db *sql.DB, w io.Writer) error

Fix any malformed section and platforms found in the database.

func GetFile

func GetFile(db *sql.DB, val string) (string, error)

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

func GetID(db *sql.DB, val string) (int, error)

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

func GetKeys(db *sql.DB, stmt string) ([]int, error)

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 IsDemozoo

func IsDemozoo(b []sql.RawBytes) (bool, error)

IsDemozoo reports if a fetched demozoo file record is set to unapproved.

func IsID

func IsID(s string) bool

IsID reports whether string is an auto-generated record id.

func IsUUID

func IsUUID(s string) bool

IsUUID reports whether string is a universal unique record id.

func IsUnApproved added in v1.11.0

func IsUnApproved(b []sql.RawBytes) (bool, error)

IsUnApproved reports if a fetched file record is set to unapproved.

func LastUpdate

func LastUpdate(db *sql.DB) (time.Time, error)

LastUpdate reports the time when the files database was last modified.

func ObfuscateParam

func ObfuscateParam(param string) string

ObfuscateParam hides the param value using the method implemented in CFWheels obfuscateParam() helper.

func StripChars

func StripChars(s string) string

StripChars removes incompatible characters used for groups and author names.

func StripStart

func StripStart(s string) string

StripStart removes non-alphanumeric characters from the start of the string.

func Tbls

func Tbls() string

Tbls are the available tables in the database.

func Total

func Total(db *sql.DB, w io.Writer, s *string) (int, error)

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 TrimSP

func TrimSP(s string) string

TrimSP removes duplicate spaces from a string.

func Val

func Val(col sql.RawBytes) string

Val returns the column value as either a string or "NULL".

func Waiting

func Waiting(db *sql.DB) (int, error)

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 Flags

type Flags = export.Flags

Flags are command line arguments.

type IDs

type IDs map[string]struct{}

IDs are unique UUID values used by the database and filenames.

type Table

type Table int

A database table.

const (
	Files        Table = iota // Files records.
	Groups                    // Groups names.
	Netresources              // Netresources for online websites.
)

func (Table) String

func (t Table) String() string

type Update

type Update update.Update

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.

Jump to

Keyboard shortcuts

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