dbs

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	GenericErrorCode               = iota + 100 // generic DBS error
	DatabaseErrorCode                           // 101 database error
	TransactionErrorCode                        // 102 transaction error
	QueryErrorCode                              // 103 query error
	RowsScanErrorCode                           // 104 row scan error
	SessionErrorCode                            // 105 db session error
	CommitErrorCode                             // 106 db commit error
	ParseErrorCode                              // 107 parser error
	LoadErrorCode                               // 108 loading error, e.g. load template
	GetIDErrorCode                              // 109 get id db error
	InsertErrorCode                             // 110 db insert error
	UpdateErrorCode                             // 111 update error
	LastInsertErrorCode                         // 112 db last insert error
	ValidateErrorCode                           // 113 validation error
	PatternErrorCode                            // 114 pattern error
	DecodeErrorCode                             // 115 decode error
	EncodeErrorCode                             // 116 encode error
	ContentTypeErrorCode                        // 117 content type error
	ParametersErrorCode                         // 118 parameters error
	NotImplementedApiCode                       // 119 not implemented API error
	ReaderErrorCode                             // 120 io reader error
	WriterErrorCode                             // 121 io writer error
	UnmarshalErrorCode                          // 122 json unmarshal error
	MarshalErrorCode                            // 123 marshal error
	HttpRequestErrorCode                        // 124 HTTP request error
	MigrationErrorCode                          // 125 Migration error
	RemoveErrorCode                             // 126 remove error
	InvalidRequestErrorCode                     // 127 invalid request error
	BlockAlreadyExists                          // 128 block xxx already exists in DBS
	FileDataTypesDoesNotExist                   // 129 FileDataTypes does not exist in DBS
	FileParentDoesNotExist                      // 130 FileParent does not exist in DBS
	DatasetParentDoesNotExist                   // 131 DatasetParent does not exist in DBS
	ProcessedDatasetDoesNotExist                // 132 ProcessedDataset does not exist in DBS
	PrimaryDatasetTypeDoesNotExist              // 133 PrimaryDatasetType does not exist in DBS
	PrimaryDatasetDoesNotExist                  // 134 PrimaryDataset does not exist in DBS
	ProcessingEraDoesNotExist                   // 135 ProcessingEra does not exist in DBS
	AcquisitionEraDoesNotExist                  // 136 AcquisitionEra does not exist in DBS
	DataTierDoesNotExist                        // 137 DataTier does not exist in DBS
	PhysicsGroupDoesNotExist                    // 138 PhysicsGroup does not exist in DBS
	DatasetAccessTypeDoesNotExist               // 139 DatasetAccessType does not exist in DBS
	DatasetDoesNotExist                         // 140 Dataset does not exist in DBS
	LastAvailableErrorCode                      // last available DBS error code
)

DBS Error codes provides static representation of DBS errors, they cover 1xx range

Variables

View Source
var ConcurrencyErr = errors.New("concurrency error")

ConcurrencyErr represents generic concurrency error

View Source
var ConcurrentBulkBlocks bool

ConcurrentBulkBlocks defines if code should use concurrent bulkblocks API

View Source
var ContentTypeErr = errors.New("content-type error")

ContentTypeErr represents generic content-type error

View Source
var DB *sql.DB

DB represents sql DB pointer

View Source
var DBOWNER string

DBOWNER represents DBS DB owner

View Source
var DBSQL map[string]any

DBSQL represents DBS SQL record

View Source
var DBTYPE string

DBTYPE represents DBS DB type, e.g. ORACLE or SQLite

View Source
var DRYRUN bool

DRYRUN allows to skip query execution and printout DB statements along with passed parameters

View Source
var DatabaseErr = errors.New("database error")

DatabaseErr represents generic database error

View Source
var FileLumiChunkSize int

FileLumiChunkSize controls chunk size for FileLumi list insertion

View Source
var FileLumiInsertMethod string

FileLumiInsertMethod controls which method to use for insertion of FileLumi list

View Source
var FileLumiMaxSize int

FileLumiMaxSize controls max size for FileLumi list insertion

View Source
var GenericErr = errors.New("dbs error")

GenericErr represents generic dbs error

View Source
var InvalidParamErr = errors.New("invalid parameter(s)")

InvalidParamErr represents generic error for invalid input parameter

View Source
var InvalidRequestErr = errors.New("invalid request error")

InvalidRequestErr represents generic invalid request error

View Source
var NotImplementedApiErr = errors.New("not implemented api error")

NotImplementedApiErr represents generic not implemented api error

View Source
var RecordErr = errors.New("record error")

RecordErr represents generic record error

View Source
var RecordValidator *validator.Validate

RecordValidator pointer to validator Validate method

View Source
var StaticDir string

StaticDir provides location of static directory

View Source
var Timeout int

Timeout represents DBS timeout used by HttpClient

View Source
var ValidationErr = errors.New("validation error")

ValidationErr represents generic validation error

View Source
var Verbose int

Verbose controls verbosity level

Functions

func AddParam

func AddParam(
	name, sqlName string,
	params map[string]any,
	conds []string,
	args []interface{}) ([]string, []interface{})

AddParam adds single parameter to SQL statement

func CastInt

func CastInt(val interface{}) (int, error)

CastInt function to check and cast interface{} to int data-type

func CastInt64

func CastInt64(val interface{}) (int64, error)

CastInt64 function to check and cast interface{} to int64 data-type

func CastString

func CastString(val interface{}) (string, error)

CastString function to check and cast interface{} to string data-type

func CleanStatement

func CleanStatement(stm string) string

CleanStatement cleans the given SQL statement to remove empty strings, etc.

func ConvertFloat

func ConvertFloat(val string) string

ConvertFloat converts string representation of float scientific number to string int

func Date

func Date() int64

Date provides default date for DB records

func DecodeValidatorError

func DecodeValidatorError(r, err interface{}) error

DecodeValidatorError provides uniform error representation of DBRecord validation errors

func Error

func Error(err error, code int, msg, function string) error

helper function to create dbs error

func GetChunks

func GetChunks(vals []string, limit int) []string

GetChunks helper function to get ORACLE chunks from provided list of values

func GetID

func GetID(tx *sql.Tx, table, id, attr string, val ...interface{}) (int64, error)

GetID function fetches table primary id for a given value

func GetRecID

func GetRecID(tx *sql.Tx, rec DBRecord, table, id, attr string, val ...interface{}) (int64, error)

GetRecID function fetches table primary id for a given value and insert it if necessary

func GetTestData

func GetTestData() error

GetTestData executes simple query to ensure that connection to DB is valid. So far we can ask for a data tier id of specific tier since this table is very small and query execution will be really fast.

func HttpClient

func HttpClient(tout int) *http.Client

HttpClient is HTTP client for urlfetch server

func IfExist

func IfExist(tx *sql.Tx, table, rid, attr string, val ...interface{}) bool

IfExist check if given rid, attr exists in given table for provided value conditions

func IfExistMulti

func IfExistMulti(tx *sql.Tx, table, rid string, args []string, vals ...interface{}) bool

IfExistMulti checks if given rid exists in given table for provided value conditions

func IncrementSequence

func IncrementSequence(tx *sql.Tx, seq string) (int64, error)

IncrementSequence API returns single unique ID for a given sequence

func IncrementSequences

func IncrementSequences(tx *sql.Tx, seq string, n int) ([]int64, error)

IncrementSequences API provide a way to get N unique IDs for given sequence name

func LastInsertID

func LastInsertID(tx *sql.Tx, table, idName string) (int64, error)

LastInsertID returns last insert id of given table and idname parameter

func ListFiles

func ListFiles(dir string) []string

ListFiles lists files in a given directory

func LoadSQL

func LoadSQL(owner string) map[string]any

LoadSQL function loads DBS SQL statements with Owner

func LoadTemplateSQL

func LoadTemplateSQL(tmpl string, tmplData map[string]any) (string, error)

LoadTemplateSQL function loads DBS SQL templated statements

func OperatorValue

func OperatorValue(arg string) (string, string)

OperatorValue function generates operator and value pair for a given argument

func ParseDBFile

func ParseDBFile(dbfile string) (string, string, string)

ParseDBFile function parses given file name and extracts from it dbtype and dburi file should contain the "dbtype dburi" string

func ParseRuns

func ParseRuns(runs []string) ([]string, error)

ParseRuns parse run_num parameter and convert it to run list

func ParseTmpl

func ParseTmpl(tdir, tmpl string, data interface{}) (string, error)

ParseTmpl parses template with given data

func PrintSQL

func PrintSQL(stm string, args []interface{}, msg string)

PrintSQL prints SQL/args

func QueryRow

func QueryRow(table, id, attr string, val interface{}) (int64, error)

QueryRow function fetches results from given table

func ReplaceBinds

func ReplaceBinds(stm string) string

ReplaceBinds replaces given pattern in string

func RunsConditions

func RunsConditions(runs []string, table string) (string, []string, []interface{}, error)

RunsConditions function to handle runs conditions

func TokenCondition

func TokenCondition() string

TokenCondition provides proper condition statement for TokenGenerator

func TokenGenerator

func TokenGenerator(runs []string, limit int, name string) (string, []string)

TokenGenerator creates a SQL token generator statement

func TokenGeneratorORACLE

func TokenGeneratorORACLE(runs []string, limit int, name string) (string, []string)

TokenGeneratorORACLE creates a SQL token generator statement using ORACLE syntax https://betteratoracle.com/posts/20-how-do-i-bind-a-variable-in-list

func TokenGeneratorSQLite

func TokenGeneratorSQLite(runs []string, name string) (string, []string)

TokenGeneratorSQLite creates a SQL token generator statement using SQLite syntax https://stackoverflow.com/questions/67372811/what-is-equivalent-of-token-generator-oracle-sql-statement-in-sqlite

func WhereClause

func WhereClause(stm string, conds []string) string

WhereClause function construct proper SQL statement from given statement and list of conditions

Types

type API

type API struct {
	Reader      io.Reader           // reader to read data payload
	Writer      http.ResponseWriter // writer to write results back to client
	Context     context.Context     // HTTP context
	ContentType string              // HTTP content-type
	Params      map[string]any      // HTTP parameters, i.e. map of any data type
	Separator   string              // string separator for ndjson format
	CreateBy    string              // create by value from run-time
	Api         string              // api name
}

API structure represents DBS API. Each API has reader (to read HTTP POST payload), HTTP writer to write results back to client, HTTP context, input HTTP GET paramers, separator for writer, create by and api string values passed at run-time.

func (*API) DeleteBucket

func (a *API) DeleteBucket() error

DeleteBucket deletes bucket record in DB

func (*API) DeleteDataset

func (a *API) DeleteDataset() error

func (*API) DeleteFile

func (a *API) DeleteFile() error

func (*API) DeleteParent

func (a *API) DeleteParent() error

DeleteParent deletes parent record in DB

func (*API) DeleteProcessing

func (a *API) DeleteProcessing() error

DeleteProcessing deletes processing record in DB

func (*API) DeleteSite

func (a *API) DeleteSite() error

DeleteSite deletes site record in DB

func (*API) GetBucket

func (a *API) GetBucket() error

Buckets DBS API

func (*API) GetDataset

func (a *API) GetDataset() error

Datasets API

func (*API) GetFile

func (a *API) GetFile() error

Files DBS API

func (*API) GetParent

func (a *API) GetParent() error

Parents DBS API

func (*API) GetProcessing

func (a *API) GetProcessing() error

Processing DBS API

func (*API) GetSite

func (a *API) GetSite() error

Sites DBS API

func (*API) InsertBucket

func (a *API) InsertBucket() error

InsertBucket inserts bucket record into DB

func (*API) InsertDataset

func (a *API) InsertDataset() error

func (*API) InsertFile

func (a *API) InsertFile() error

func (*API) InsertParent

func (a *API) InsertParent() error

InsertParent inserts parent record into DB

func (*API) InsertProcessing

func (a *API) InsertProcessing() error

InsertProcessing inserts processing record into DB

func (*API) InsertSite

func (a *API) InsertSite() error

InsertSite inserts site record into DB

func (*API) String

func (a *API) String() string

String provides string representation of API struct

func (*API) UpdateBucket

func (a *API) UpdateBucket() error

UpdateBucket inserts bucket record in DB

func (*API) UpdateDataset

func (a *API) UpdateDataset() error

func (*API) UpdateFile

func (a *API) UpdateFile() error

func (*API) UpdateParent

func (a *API) UpdateParent() error

UpdateParent inserts parent record in DB

func (*API) UpdateProcessing

func (a *API) UpdateProcessing() error

UpdateProcessing inserts processing record in DB

func (*API) UpdateSite

func (a *API) UpdateSite() error

UpdateSite inserts site record in DB

type Buckets

type Buckets struct {
	BUCKET_ID  int64  `json:"bucket_id"`
	BUCKET     string `json:"bucket" validate:"required"`
	DATASET_ID int64  `json:"dataset_id" validate:"required"`
	CREATE_AT  int64  `json:"create_at"`
	CREATE_BY  string `json:"create_by"`
	MODIFY_AT  int64  `json:"modify_at"`
	MODIFY_BY  string `json:"modify_by"`
}

Buckets represents Buckets DBS DB table

func (*Buckets) Decode

func (r *Buckets) Decode(reader io.Reader) error

Decode implementation for Buckets

func (*Buckets) Insert

func (r *Buckets) Insert(tx *sql.Tx) error

Insert implementation of Buckets

func (*Buckets) SetDefaults

func (r *Buckets) SetDefaults()

SetDefaults implements set defaults for Buckets

func (*Buckets) Update added in v0.0.3

func (r *Buckets) Update(tx *sql.Tx) error

Update implementation of Buckets

func (*Buckets) Validate

func (r *Buckets) Validate() error

Validate implementation of Buckets

type DBRecord

type DBRecord interface {
	Insert(tx *sql.Tx) error  // used to insert given record to DB
	Update(tx *sql.Tx) error  // used to update given record to DB
	Validate() error          // used to validate given record
	SetDefaults()             // used to set proper defaults for given record
	Decode(r io.Reader) error // used to decode given record
}

DBRecord interface represents general DB record used by DBS APIs. Each DBS API represents specific Table in back-end DB. And, each individual DBS API implements logic for its own DB records

type DBSError

type DBSError struct {
	Reason     string `json:"reason"`     // error string
	Message    string `json:"message"`    // additional message describing the issue
	Function   string `json:"function"`   // DBS function
	Code       int    `json:"code"`       // DBS error code
	Stacktrace string `json:"stacktrace"` // Go stack trace
}

DBSError represents common structure for DBS errors

func (*DBSError) Error

func (e *DBSError) Error() string

Error function implements details of DBS error message

func (*DBSError) ErrorStacktrace

func (e *DBSError) ErrorStacktrace() string

ErrorStacktrace function implements details of DBS error message and stacktrace

func (*DBSError) Explain

func (e *DBSError) Explain() string

type DatasetRecord

type DatasetRecord struct {
	Did        string   `json:"did" validate:"required"`
	Buckets    []string `json:"buckets" validate:"required"`
	Site       string   `json:"site" validate:"required"`
	Processing string   `json:"processing" validate:"required"`
	Parent     string   `json:"parent" validate:"required"`
	Files      []string `json:"files" validate:"required"`
}

DatasetRecord represents input dataset record from HTTP request

type Datasets

type Datasets struct {
	DATASET_ID    int64  `json:"dataset_id"`
	DID           string `json:"did" validate:"required"`
	SITE_ID       int64  `json:"site_id" validate:"required"`
	PROCESSING_ID int64  `json:"processing_id" validate:"required"`
	PARENT_ID     int64  `json:"parent_id" validate:"required"`
	CREATE_AT     int64  `json:"create_at" validate:"required,number"`
	CREATE_BY     string `json:"create_by" validate:"required"`
	MODIFY_AT     int64  `json:"modify_at" validate:"required,number"`
	MODIFY_BY     string `json:"modify_by" validate:"required"`
}

Datasets represents Datasets DBS DB table

func (*Datasets) Decode

func (r *Datasets) Decode(reader io.Reader) error

Decode implementation for Datasets

func (*Datasets) Insert

func (r *Datasets) Insert(tx *sql.Tx) error

Insert implementation of Datasets

func (*Datasets) SetDefaults

func (r *Datasets) SetDefaults()

SetDefaults implements set defaults for Datasets

func (*Datasets) Update added in v0.0.3

func (r *Datasets) Update(tx *sql.Tx) error

Update implementation of Datasets

func (*Datasets) Validate

func (r *Datasets) Validate() error

Validate implementation of Datasets

type Files

type Files struct {
	FILE_ID       int64  `json:"file_id"`
	FILE          string `json:"file" validate:"required"`
	IS_FILE_VALID int64  `json:"is_file_valid" validate:"number"`
	DATASET_ID    int64  `json:"dataset_id" validate:"number,gt=0"`
	CREATE_AT     int64  `json:"create_at" validate:"required,number,gt=0"`
	CREATE_BY     string `json:"create_by" validate:"required"`
	MODIFY_AT     int64  `json:"modify_at" validate:"required,number,gt=0"`
	MODIFY_BY     string `json:"modify_by" validate:"required"`
}

Files represents Files DBS DB table

func (*Files) Decode

func (r *Files) Decode(reader io.Reader) error

Decode implementation for Files

func (*Files) Insert

func (r *Files) Insert(tx *sql.Tx) error

Insert implementation of Files

func (*Files) SetDefaults

func (r *Files) SetDefaults()

SetDefaults implements set defaults for Files

func (*Files) Update added in v0.0.3

func (r *Files) Update(tx *sql.Tx) error

Update implementation of Files

func (*Files) Validate

func (r *Files) Validate() error

Validate implementation of Files

type Parents

type Parents struct {
	PARENT_ID int64  `json:"parent_id"`
	PARENT    string `json:"parent" validate:"required"`
	CREATE_AT int64  `json:"create_at"`
	CREATE_BY string `json:"create_by"`
	MODIFY_AT int64  `json:"modify_at"`
	MODIFY_BY string `json:"modify_by"`
}

Parents represents Parents DBS DB table

func (*Parents) Decode

func (r *Parents) Decode(reader io.Reader) error

Decode implementation for Parents

func (*Parents) Insert

func (r *Parents) Insert(tx *sql.Tx) error

Insert implementation of Parents

func (*Parents) SetDefaults

func (r *Parents) SetDefaults()

SetDefaults implements set defaults for Parents

func (*Parents) Update added in v0.0.3

func (r *Parents) Update(tx *sql.Tx) error

Update implementation of Parents

func (*Parents) Validate

func (r *Parents) Validate() error

Validate implementation of Parents

type Processing

type Processing struct {
	PROCESSING_ID int64  `json:"processing_id"`
	PROCESSING    string `json:"processing" validate:"required"`
	CREATE_AT     int64  `json:"create_at"`
	CREATE_BY     string `json:"create_by"`
	MODIFY_AT     int64  `json:"modify_at"`
	MODIFY_BY     string `json:"modify_by"`
}

Processing represents Processing DBS DB table

func (*Processing) Decode

func (r *Processing) Decode(reader io.Reader) error

Decode implementation for Processing

func (*Processing) Insert

func (r *Processing) Insert(tx *sql.Tx) error

Insert implementation of Processing

func (*Processing) SetDefaults

func (r *Processing) SetDefaults()

SetDefaults implements set defaults for Processing

func (*Processing) Update added in v0.0.3

func (r *Processing) Update(tx *sql.Tx) error

Update implementation of Processing

func (*Processing) Validate

func (r *Processing) Validate() error

Validate implementation of Processing

type Sites

type Sites struct {
	SITE_ID   int64  `json:"site_id"`
	SITE      string `json:"site" validate:"required"`
	CREATE_AT int64  `json:"create_at"`
	CREATE_BY string `json:"create_by"`
	MODIFY_AT int64  `json:"modify_at"`
	MODIFY_BY string `json:"modify_by"`
}

Sites represents Sites DBS DB table

func (*Sites) Decode

func (r *Sites) Decode(reader io.Reader) error

Decode implementation for Sites

func (*Sites) Insert

func (r *Sites) Insert(tx *sql.Tx) error

Insert implementation of Sites

func (*Sites) SetDefaults

func (r *Sites) SetDefaults()

SetDefaults implements set defaults for Sites

func (*Sites) Update added in v0.0.3

func (r *Sites) Update(tx *sql.Tx) error

Update implementation of Sites

func (*Sites) Validate

func (r *Sites) Validate() error

Validate implementation of Sites

Jump to

Keyboard shortcuts

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