2021-09-19 17:19:59 +05:30
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package db
import (
2021-09-23 21:15:36 +05:30
"context"
2021-11-21 21:11:00 +05:30
"database/sql"
2021-09-23 21:15:36 +05:30
2021-09-19 17:19:59 +05:30
"code.gitea.io/gitea/modules/setting"
"xorm.io/builder"
2022-05-22 00:20:50 +05:30
"xorm.io/xorm/schemas"
2021-09-19 17:19:59 +05:30
)
2021-09-23 21:15:36 +05:30
// DefaultContext is the default context to run xorm queries in
// will be overwritten by Init with HammerContext
var DefaultContext context . Context
// contextKey is a value for use with context.WithValue.
type contextKey struct {
name string
}
2022-06-20 18:08:58 +05:30
// enginedContextKey is a context key. It is used with context.Value() to get the current Engined for the context
var enginedContextKey = & contextKey { "engined" }
var _ Engined = & Context { }
2021-09-23 21:15:36 +05:30
2021-09-19 17:19:59 +05:30
// Context represents a db context
type Context struct {
2021-09-23 21:15:36 +05:30
context . Context
2022-06-20 18:08:58 +05:30
e Engine
transaction bool
2021-09-19 17:19:59 +05:30
}
2022-06-20 18:08:58 +05:30
func newContext ( ctx context . Context , e Engine , transaction bool ) * Context {
2021-10-14 01:17:02 +05:30
return & Context {
2022-06-20 18:08:58 +05:30
Context : ctx ,
e : e ,
transaction : transaction ,
2021-10-14 01:17:02 +05:30
}
}
2022-06-20 18:08:58 +05:30
// InTransaction if context is in a transaction
func ( ctx * Context ) InTransaction ( ) bool {
return ctx . transaction
}
2021-09-19 17:19:59 +05:30
// Engine returns db engine
func ( ctx * Context ) Engine ( ) Engine {
return ctx . e
}
2021-09-23 21:15:36 +05:30
// Value shadows Value for context.Context but allows us to get ourselves and an Engined object
func ( ctx * Context ) Value ( key interface { } ) interface { } {
2022-06-20 18:08:58 +05:30
if key == enginedContextKey {
2021-09-23 21:15:36 +05:30
return ctx
}
return ctx . Context . Value ( key )
}
2022-01-20 04:56:57 +05:30
// WithContext returns this engine tied to this context
func ( ctx * Context ) WithContext ( other context . Context ) * Context {
2022-06-20 18:08:58 +05:30
return newContext ( ctx , ctx . e . Context ( other ) , ctx . transaction )
2022-01-20 04:56:57 +05:30
}
2021-09-23 21:15:36 +05:30
// Engined structs provide an Engine
type Engined interface {
Engine ( ) Engine
}
// GetEngine will get a db Engine from this context or return an Engine restricted to this context
func GetEngine ( ctx context . Context ) Engine {
if engined , ok := ctx . ( Engined ) ; ok {
return engined . Engine ( )
}
2022-06-20 18:08:58 +05:30
enginedInterface := ctx . Value ( enginedContextKey )
2021-09-23 21:15:36 +05:30
if enginedInterface != nil {
return enginedInterface . ( Engined ) . Engine ( )
}
return x . Context ( ctx )
}
2021-09-19 17:19:59 +05:30
// Committer represents an interface to Commit or Close the Context
type Committer interface {
Commit ( ) error
Close ( ) error
}
// TxContext represents a transaction Context
func TxContext ( ) ( * Context , Committer , error ) {
sess := x . NewSession ( )
if err := sess . Begin ( ) ; err != nil {
sess . Close ( )
return nil , nil , err
}
2022-06-20 18:08:58 +05:30
return newContext ( DefaultContext , sess , true ) , sess , nil
2021-09-19 17:19:59 +05:30
}
// WithTx represents executing database operations on a transaction
2022-05-04 01:16:28 +05:30
// you can optionally change the context to a parrent one
func WithTx ( f func ( ctx context . Context ) error , stdCtx ... context . Context ) error {
parentCtx := DefaultContext
if len ( stdCtx ) != 0 && stdCtx [ 0 ] != nil {
// TODO: make sure parent context has no open session
parentCtx = stdCtx [ 0 ]
}
2021-09-19 17:19:59 +05:30
sess := x . NewSession ( )
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return err
}
2022-06-20 18:08:58 +05:30
if err := f ( newContext ( parentCtx , sess , true ) ) ; err != nil {
2021-09-19 17:19:59 +05:30
return err
}
return sess . Commit ( )
}
// Iterate iterates the databases and doing something
2021-09-23 21:15:36 +05:30
func Iterate ( ctx context . Context , tableBean interface { } , cond builder . Cond , fun func ( idx int , bean interface { } ) error ) error {
return GetEngine ( ctx ) . Where ( cond ) .
2021-09-19 17:19:59 +05:30
BufferSize ( setting . Database . IterateBufferSize ) .
Iterate ( tableBean , fun )
}
// Insert inserts records into database
2021-09-23 21:15:36 +05:30
func Insert ( ctx context . Context , beans ... interface { } ) error {
_ , err := GetEngine ( ctx ) . Insert ( beans ... )
2021-09-19 17:19:59 +05:30
return err
}
2021-11-21 21:11:00 +05:30
// Exec executes a sql with args
func Exec ( ctx context . Context , sqlAndArgs ... interface { } ) ( sql . Result , error ) {
return GetEngine ( ctx ) . Exec ( sqlAndArgs ... )
}
// GetByBean filled empty fields of the bean according non-empty fields to query in database.
func GetByBean ( ctx context . Context , bean interface { } ) ( bool , error ) {
return GetEngine ( ctx ) . Get ( bean )
}
// DeleteByBean deletes all records according non-empty fields of the bean as conditions.
func DeleteByBean ( ctx context . Context , bean interface { } ) ( int64 , error ) {
return GetEngine ( ctx ) . Delete ( bean )
}
2022-02-17 14:07:48 +05:30
// DeleteBeans deletes all given beans, beans should contain delete conditions.
func DeleteBeans ( ctx context . Context , beans ... interface { } ) ( err error ) {
e := GetEngine ( ctx )
for i := range beans {
if _ , err = e . Delete ( beans [ i ] ) ; err != nil {
return err
}
}
return nil
}
2021-11-21 21:11:00 +05:30
// CountByBean counts the number of database records according non-empty fields of the bean as conditions.
func CountByBean ( ctx context . Context , bean interface { } ) ( int64 , error ) {
return GetEngine ( ctx ) . Count ( bean )
}
// TableName returns the table name according a bean object
func TableName ( bean interface { } ) string {
return x . TableName ( bean )
}
2022-05-22 00:20:50 +05:30
// EstimateCount returns an estimate of total number of rows in table
func EstimateCount ( ctx context . Context , bean interface { } ) ( int64 , error ) {
e := GetEngine ( ctx )
e . Context ( ctx )
var rows int64
var err error
tablename := TableName ( bean )
switch x . Dialect ( ) . URI ( ) . DBType {
case schemas . MYSQL :
_ , err = e . Context ( ctx ) . SQL ( "SELECT table_rows FROM information_schema.tables WHERE tables.table_name = ? AND tables.table_schema = ?;" , tablename , x . Dialect ( ) . URI ( ) . DBName ) . Get ( & rows )
case schemas . POSTGRES :
_ , err = e . Context ( ctx ) . SQL ( "SELECT reltuples AS estimate FROM pg_class WHERE relname = ?;" , tablename ) . Get ( & rows )
case schemas . MSSQL :
_ , err = e . Context ( ctx ) . SQL ( "sp_spaceused ?;" , tablename ) . Get ( & rows )
default :
return e . Context ( ctx ) . Count ( tablename )
}
return rows , err
}