database

package
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package database interacts with the MySQL 5.7 datastore of Defacto2.

Index

Examples

Constants

View Source
const (
	// Datetime MySQL 5.7 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"
)

Variables

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

func Approve(verbose bool) error

Approve automatically checks and clears file records for live.

func CheckID

func CheckID(s string) error

CheckID reports an error message for an incorrect universal unique record id or MySQL auto-generated id.

func CheckUUID

func CheckUUID(s string) error

CheckUUID reports an error message for an incorrect universal unique record id.

func ColTypes

func ColTypes(t Table) (string, error)

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

func ConnErr() (*sql.DB, error)

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

func Connect() *sql.DB

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 DateTime

func DateTime(raw sql.RawBytes) string

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(id uint) (uint, error)

DemozooID finds a record ID by the saved Demozoo production ID. If no production exists a zero is returned.

func Execute

func Execute(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 reports if the named file is newer than the database time. True is always returned when the named file does not exist.

func Fix

func Fix() error

Fix any malformed section and platforms found in the database.

func GetFile

func GetFile(s string) (string, error)

GetFile returns the filename from a supplied UUID or database ID value.

func GetID

func GetID(s string) (uint, error)

GetID returns a numeric Id from a UUID or database id s value.

func Init

func Init() my57.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 IsDemozoo

func IsDemozoo(b []sql.RawBytes) bool

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

func IsID

func IsID(s string) bool

IsID reports whether string is a autogenerated record id.

func IsProof

func IsProof(b []sql.RawBytes) bool

IsProof reports if a fetched proof file record is set to unapproved.

func IsUUID

func IsUUID(s string) bool

IsUUID reports whether string is a universal unique record id.

func LastUpdate

func LastUpdate() (time.Time, error)

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

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(s *string) (int, error)

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

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() (uint, error)

Waiting returns the number of files requiring approval for public display.

Example
package main

import (
	"fmt"

	"github.com/Defacto2/df2/pkg/database"
)

func main() {
	i, err := database.Waiting()
	if err != nil {
		fmt.Print(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.
	Users                     // Users are site logins.
)

func (Table) String

func (t Table) String() string

type Update

type Update update.Update

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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