db

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 8 Imported by: 0

README

db-go

The Database Go Package is a collection of tools for working with databases in Go applications. It provides functionalities for managing database connections, executing commands, and handling rows and tables. With this package, developers can easily interact with databases in their Go projects, making database-related tasks more efficient and straightforward.

Install

go get github.com/fatihtatoglu/db-go

Usage

Creating a Database Connection
config := db.CreateNewDBConfig("mysql", "username", "password", "localhost", 3306, "database_name")
connection, err := db.CreateNewDBConnection(config)
if err != nil {
    // handle error
}

err = connection.Open()
if err != nil {
    // handle error
}

defer connection.Close()

Executing Commands
command, err := db.CreateNewDBCommand(connection)
if err != nil {
    // handle error
}

_, err = command.Execute("CREATE TABLE users (name VARCHAR(255), age INT)")
if err != nil {
    // handle error
}

Executing Commands with Parameters
result, err := db.Execute("INSERT INTO users (name, age) VALUES (?, ?)", "John Doe", 30)
 if err != nil {
  // handle error
 }
 fmt.Println("Rows affected:", result.RowsAffected)
Querying Data
query := "SELECT * FROM users"
result, err := command.Query(query)
if err != nil {
    // handle error
}

Querying Data with Parameters
query := "SELECT * FROM users WHERE age > ?"
result, err := command.Query(query, 25)
if err != nil {
    // handle error
}

Features

  • Support for various database drivers including;
    • MySQL
    • PostgreSQL
    • SQLite
    • SQL Server
  • Easy-to-use API for executing SQL queries, fetching data, and managing database connections
  • Efficient handling of database transactions and error handling

Credits

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	NIL_CONNECTION = "connection is nil"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DBColumn

type DBColumn struct {
	Table *DBTable
	// contains filtered or unexported fields
}

func CreateNewDBColumn

func CreateNewDBColumn(name string, ordinal int) DBColumn

func CreateNewDBColumnFromColumnType

func CreateNewDBColumnFromColumnType(ct sql.ColumnType, ordinal int) DBColumn

func (*DBColumn) GetDBType

func (cd *DBColumn) GetDBType() string

func (*DBColumn) GetLength

func (cd *DBColumn) GetLength() int64

func (*DBColumn) GetName

func (cd *DBColumn) GetName() string

func (*DBColumn) GetNullable

func (cd *DBColumn) GetNullable() bool

func (*DBColumn) GetOrdinal

func (cd *DBColumn) GetOrdinal() int

func (*DBColumn) GetPrecision

func (cd *DBColumn) GetPrecision() int64

func (*DBColumn) GetScale

func (cd *DBColumn) GetScale() int64

func (*DBColumn) GetType

func (cd *DBColumn) GetType() reflect.Type

func (*DBColumn) MarshalJSON

func (dc *DBColumn) MarshalJSON() ([]byte, error)

type DBCommandInterface

type DBCommandInterface interface {
	Execute(query string, params ...interface{}) (*sql.Result, error)
	Query(query string, params ...interface{}) (*DBTable, error)
	QueryFirst(query string, params ...interface{}) (*DBRow, error)
}

func CreateNewDBCommand

func CreateNewDBCommand(con DBConnectionInterface) (DBCommandInterface, error)

type DBConnectionInterface

type DBConnectionInterface interface {
	Open() error
	Close() error
	// contains filtered or unexported methods
}

func CreateNewDBConnection

func CreateNewDBConnection(driver string, dsn string) (DBConnectionInterface, error)

type DBRow

type DBRow struct {
	Table *DBTable
	// contains filtered or unexported fields
}

func (*DBRow) GetItemByIndex

func (dr *DBRow) GetItemByIndex(index int) (interface{}, error)

func (*DBRow) GetItemByName

func (dr *DBRow) GetItemByName(columnName string) (interface{}, error)

func (*DBRow) MarshalJSON

func (dr *DBRow) MarshalJSON() ([]byte, error)

func (*DBRow) SetItemByIndex

func (dr *DBRow) SetItemByIndex(index int, value interface{}) error

func (*DBRow) SetItemByName

func (dr *DBRow) SetItemByName(columnName string, value interface{}) error

type DBTable

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

func CreateNewDBTable

func CreateNewDBTable() DBTable

func (*DBTable) AddDBColumn

func (dt *DBTable) AddDBColumn(column DBColumn) error

func (*DBTable) AddDBRow

func (dt *DBTable) AddDBRow(row DBRow) error

func (*DBTable) CreateNewDBRow

func (dt *DBTable) CreateNewDBRow() DBRow

func (*DBTable) GetColumns

func (dt *DBTable) GetColumns() []DBColumn

func (*DBTable) GetRows

func (dt *DBTable) GetRows() []DBRow

func (*DBTable) MarshalJSON

func (dt *DBTable) MarshalJSON() ([]byte, error)

type DataFetcher added in v0.1.1

type DataFetcher[T any] interface {
	// Fetch fetches the entity records by the given limit and offset.
	Fetch(limit, offset int) ([]T, error)
}

DataFetcher is an interface used to fetch entity records by given limit and offset values.

type DataReader added in v0.1.1

type DataReader[T any, K any] interface {
	// GetById gets the entity record by the id value.
	// For composite primary keys, a struct can be used as a parameter.
	GetById(id K) (T, error)

	// Retrieve retrieves all the records of the entity from the database.
	Retrieve() ([]T, error)
}

DataReader is an interface used to read data from the database.

type DataWriter added in v0.1.1

type DataWriter[T any, K any] interface {
	// Insert inserts a new record into the database and returns the primary key value.
	// For composite primary keys, a struct can be used as a result.
	Insert(entity T) (K, error)

	// Update updates the given entity by the id value.
	// For composite primary keys, a struct can be used as a parameter.
	Update(id K, entity T) (bool, error)

	// Delete deletes the database entity record by the id value.
	// For composite primary keys, a struct can be used as a parameter.
	Delete(id K) (bool, error)
}

DataWriter is an interface used to write data to the database.

type ExistenceChecker added in v0.1.1

type ExistenceChecker[T any] interface {
	// CheckExistence checks the existence of a record by the id column.
	// For composite primary keys, a struct can be used as a parameter.
	CheckExistence(id T) (bool, error)
}

ExistenceChecker is an interface used to check the existence of an entity.

type MockConn

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

func (*MockConn) Begin

func (m *MockConn) Begin() (driver.Tx, error)

Fake-it

func (*MockConn) Close

func (m *MockConn) Close() error

func (*MockConn) Ping

func (m *MockConn) Ping(ctx context.Context) error

Fake-it

func (*MockConn) Prepare

func (m *MockConn) Prepare(query string) (driver.Stmt, error)

type MockConnector

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

func (*MockConnector) Close

func (m *MockConnector) Close() error

Fake-it

func (*MockConnector) Connect

func (m *MockConnector) Connect(context.Context) (driver.Conn, error)

Fake-it

func (*MockConnector) Driver

func (m *MockConnector) Driver() driver.Driver

Fake-it

type MockDriver

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

func (*MockDriver) CleanErrorMessage

func (d *MockDriver) CleanErrorMessage()

func (*MockDriver) Open

func (d *MockDriver) Open(name string) (driver.Conn, error)

Fake-it

func (*MockDriver) OpenConnector

func (d *MockDriver) OpenConnector(name string) (driver.Connector, error)

Fake-it

func (*MockDriver) SetCloseErrorMessage

func (d *MockDriver) SetCloseErrorMessage(message string)

func (*MockDriver) SetConnectorErrorMessage

func (d *MockDriver) SetConnectorErrorMessage(message string)

func (*MockDriver) SetPingerErrorMessage

func (d *MockDriver) SetPingerErrorMessage(message string)

type Repository added in v0.1.1

type Repository[T any, K any] interface {
	DataReader[T, K]
	DataFetcher[T]
	ExistenceChecker[K]
	DataWriter[T, K]
}

Repository is an interface used for general database operations.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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