dao

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 8 Imported by: 7

README

DAO

Data access object of Golite ecosystem.

Installation

go get "gitlab.com/golight/dao"

Available functions and methods

// NewDAO creates a new data access object.
func NewDAO(db *sqlx.DB, dbConf params.DB, scanner scanner.Scanner) *DAO
// Begin starts a transaction.
func (s *DAO) Begin() (*sqlx.Tx, error)
// Exec executes the specified query.
func (s *DAO) Exec(ctx context.Context, query string, tx *sqlx.Tx, args ...interface{}) (sql.Result, error)
// LockTable locks a table for writing.
func (s *DAO) LockTable(ctx context.Context, tableName string, mode string, tx *sqlx.Tx) error
// Ping verifies that the database connection is still alive.
func (s *DAO) Ping(ctx context.Context) error
// Create creates a new field in the database and returns the index of this field.
func (s *DAO) Create(ctx context.Context, table tabler.Tabler, opts ...interface{}) (interface{}, error)
// Upsert updates an existing row if a specified value already exists in a table, 
// and inserts a new row if the specified value doesn't already exist.
func (s *DAO) Upsert(ctx context.Context, entities []tabler.Tabler, opts ...interface{}) error
// GetCount returns the number of rows that match a specified criterion.
func (s *DAO) GetCount(ctx context.Context, entity tabler.Tabler, condition params.Condition, opts ...interface{}) (uint64, error)
// List combines the values of a table column from multiple rows into a single comma-separated list of values.
func (s *DAO) List(ctx context.Context, dest interface{}, table tabler.Tabler, condition params.Condition, opts ...interface{}) error
// Update modifies the existing records in a table.
func (s *DAO) Update(ctx context.Context, entity tabler.Tabler, condition params.Condition, operation string, opts ...interface{}) error
// getTransaction returns a transaction.
func getTransaction(opts ...interface{}) *sqlx.Tx

Example of implementation

// Обратите внимание, что вам нужно будет заменить "user=postgres password=secret dbname=postgres sslmode=disable" на строку подключения к вашей базе данных.
// Создайте подключение к базе данных
db, err := sqlx.Connect("postgres", "user=postgres password=secret dbname=postgres sslmode=disable")
if err != nil {
    log.Fatalln(err)
}

// Создайте экземпляр сканера
s := scanner.NewTableScanner()

// Создайте экземпляр DAO
d := dao.NewDAO(db, params.DB{Driver: "postgres"}, s)

// Используйте методы DAO
err = d.Ping(context.Background())
if err != nil {
    log.Fatalln(err)
}

fmt.Println("Ping successful")

// Здесь вы можете использовать другие методы DAO, такие как Create, Upsert и т.д.
// Например, d.Create(context.Background(), ваша_сущность)

Documentation

Overview

Package dao provides a data access object for a database.

Index

Constants

View Source
const (
	DriverMysql    = "mysql"
	DriverPostgres = "postgres"
	DriverSqlite3  = "sqlite3"
	DriverRamsql   = "ramsql"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DAO

type DAO struct {
	// contains filtered or unexported fields
}

DAO is a data access object.

func NewDAO

func NewDAO(db *sqlx.DB, dbConf params.DB, scanner scanner.Scanner) *DAO

NewDAO creates a new data access object.

func (*DAO) Begin

func (s *DAO) Begin() (*sqlx.Tx, error)

Begin starts a transaction.

func (*DAO) Create

func (s *DAO) Create(ctx context.Context, table tabler.Tabler, opts ...interface{}) (int64, error)

Create creates a new field in the database and returns the index of this field.

func (*DAO) Exec

func (s *DAO) Exec(ctx context.Context, query string, tx *sqlx.Tx, args ...interface{}) (sql.Result, error)

Exec executes the specified query.

func (*DAO) GetCount

func (s *DAO) GetCount(ctx context.Context, entity tabler.Tabler, condition params.Condition, opts ...interface{}) (uint64, error)

GetCount returns the number of rows that match a specified criterion.

func (*DAO) List

func (s *DAO) List(ctx context.Context, dest interface{}, table tabler.Tabler, condition params.Condition, opts ...interface{}) error

List combines the values of a table column from multiple rows into a single comma-separated list of values.

func (*DAO) LockTable

func (s *DAO) LockTable(ctx context.Context, tableName string, mode string, tx *sqlx.Tx) error

LockTable locks a table for writing.

func (*DAO) Ping

func (s *DAO) Ping(ctx context.Context) error

Ping verifies that the database connection is still alive.

func (*DAO) Update

func (s *DAO) Update(ctx context.Context, entity tabler.Tabler, condition params.Condition, operation string, opts ...interface{}) error

Update modifies the existing records in a table.

func (*DAO) Upsert

func (s *DAO) Upsert(ctx context.Context, entities []tabler.Tabler, opts ...interface{}) error

Upsert updates an existing row if a specified value already exists in a table, and inserts a new row if the specified value doesn't already exist.

type DAOFace

type DAOFace interface {
	Begin() (*sqlx.Tx, error)
	Ping(ctx context.Context) error
	Create(ctx context.Context, entity tabler.Tabler, opts ...interface{}) (int64, error)
	Upsert(ctx context.Context, entities []tabler.Tabler, opts ...interface{}) error
	GetCount(ctx context.Context, entity tabler.Tabler, condition params.Condition, opts ...interface{}) (uint64, error)
	List(ctx context.Context, dest interface{}, table tabler.Tabler, condition params.Condition, opts ...interface{}) error
	Update(ctx context.Context, entity tabler.Tabler, condition params.Condition, operation string, opts ...interface{}) error
	Exec(ctx context.Context, query string, tx *sqlx.Tx, args ...interface{}) (sql.Result, error)
	LockTable(ctx context.Context, tableName string, mode string, tx *sqlx.Tx) error
}

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.
sql
builder
Package builder makes SQL queries for data access object
Package builder makes SQL queries for data access object

Jump to

Keyboard shortcuts

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