orm

package module
v0.0.0-...-d6a6539 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2023 License: MIT Imports: 11 Imported by: 0

README

example

My SDK is a Go library for interacting with the My API.

Installation

go get github.com/{your_username}/{your_repository}/sdk

Usage

To use My SDK, import it in your Go code:

import "github.com/{your_username}/{your_repository}/sdk"

Here is an example of how to use My SDK to ping the My API:

package main

import (
    "log"

    "github.com/{your_username}/{your_repository}/sdk"
)

func main() {
    client := sdk.NewClient("<https://my-api.com>")
    err := client.Ping()
    if err != nil {
        log.Fatal(err)
    }
    log.Println("API is available.")
}

API Documentation

For detailed API documentation and usage examples, please refer to the documentation.

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for more information.

License

My SDK is licensed under the MIT License. See LICENSE for more information.

Documentation

Index

Constants

View Source
const (
	MySQL = "mysql"
)

Variables

This section is empty.

Functions

func MapToString

func MapToString(m map[string]string) string

MapToString is a utility function that converts a map into a string. Each key-value pair in the map is converted into a "key=value" string, and these strings are joined with "&" as the separator. The resulting string does not end with "&".

func ToSnakeCase

func ToSnakeCase(s string) string

ToSnakeCase is a utility function that converts a given string into snake case.

Types

type Builder

type Builder struct {
}

type DB

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

func NewDB

func NewDB(d *DataSource) (*DB, error)

NewDB creates a new ORM DB instance

func (*DB) Close

func (d *DB) Close() error

Close closes the connection to the database

func (*DB) Conn

func (d *DB) Conn(ctx context.Context) (*sql.Conn, error)

Conn returns a single connection by either opening a new connection or returning an existing connection from the connection pool. Conn will block until either a connection is returned or ctx is canceled. Queries run on the same Conn will be run in the same database session.

Every Conn must be returned to the database pool after use by calling Conn.Close.

func (*DB) DataSourceName

func (d *DB) DataSourceName() string

func (*DB) DisableDebug

func (d *DB) DisableDebug()

DisableDebug sets the debug flag to false

func (*DB) Driver

func (d *DB) Driver() driver.Driver

Driver returns the database's underlying driver.

func (*DB) DriverName

func (d *DB) DriverName() string

func (*DB) DropTable

func (d *DB) DropTable(name string) bool

func (*DB) EnableDebug

func (d *DB) EnableDebug()

EnableDebug sets the debug flag to true

func (*DB) Exec

func (d *DB) Exec(query string, args ...any) (sql.Result, error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*DB) ExecContext

func (d *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*DB) IsTableExist

func (d *DB) IsTableExist(name string) bool

IsTableExist checks if a table with the given name exists in the database. It returns true if the table exists, false otherwise.

func (*DB) NewSession

func (d *DB) NewSession() *session.Session

NewSession creates a new session with the current database connection

func (*DB) Ping

func (d *DB) Ping() error

Ping verifies a connection to the database is still alive, establishing a connection if necessary.

func (*DB) PingContext

func (d *DB) PingContext(ctx context.Context) error

PingContext verifies a connection to the database is still alive, establishing a connection if necessary.

func (*DB) Query

func (d *DB) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*DB) QueryContext

func (d *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*DB) QueryRow

func (d *DB) QueryRow(query string, args ...any) *sql.Row

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*DB) QueryRowContext

func (d *DB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*DB) SetConnMaxIdleTime

func (d *DB) SetConnMaxIdleTime(t time.Duration)

SetConnMaxIdleTime sets the maximum amount of time a connection may be idle before being closed.

func (*DB) SetConnMaxLifetime

func (d *DB) SetConnMaxLifetime(t time.Duration)

SetConnMaxLifetime sets the maximum amount of time a connection may be reused for before being closed.

func (*DB) SetMaxIdleConns

func (d *DB) SetMaxIdleConns(n int)

SetMaxIdleConns sets the maximum number of connections in the idle connection pool.

func (*DB) SetMaxOpenConns

func (d *DB) SetMaxOpenConns(n int)

SetMaxOpenConns sets the maximum number of open connections to the database.

func (*DB) Stats

func (d *DB) Stats() sql.DBStats

Stats returns database statistics.

func (*DB) Version

func (d *DB) Version() (string, error)

type DataSource

type DataSource struct {
	User     string
	Password string
	Net      string
	Host     string
	Port     int32
	DBName   string
	Params   map[string]string

	Driver DriverName
}

func NewDataSource

func NewDataSource(options ...Option) *DataSource

NewDataSource creates a new DataSource with the given options

func (*DataSource) DSN

func (d *DataSource) DSN() string

DSN returns the data source name for the DataSource

type DriverName

type DriverName string
const (
	MySQLDriver DriverName = MySQL
)

type Option

type Option func(*DataSource)

func SetDBName

func SetDBName(dbname string) Option

SetDBName sets the database name for the DataSource

func SetHost

func SetHost(host string) Option

SetHost sets the host for the DataSource

func SetNet

func SetNet(net string) Option

SetNet sets the network for the DataSource

func SetParams

func SetParams(params map[string]string) Option

SetParams sets the parameters for the DataSource

func SetPassword

func SetPassword(password string) Option

SetPassword sets the password for the DataSource

func SetPort

func SetPort(port int32) Option

SetPort sets the port for the DataSource

func SetUser

func SetUser(user string) Option

SetUser sets the user for the DataSource

Directories

Path Synopsis
Package dialect provides an interface for different SQL dialects
Package dialect provides an interface for different SQL dialects

Jump to

Keyboard shortcuts

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