sqlite3

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Example (Sqlite3)
package main

import (
	"context"
	"fmt"

	"github.com/Breeze0806/go-etl/config"
	"github.com/Breeze0806/go-etl/element"
	"github.com/Breeze0806/go-etl/storage/database"
	"github.com/Breeze0806/go-etl/storage/database/sqlite3"
)

func testJSONFromString(s string) *config.JSON {
	json, err := config.NewJSONFromString(s)
	if err != nil {
		panic(err)
	}
	return json
}

type TableParam struct {
	*database.BaseParam
}

func NewTableParam() *TableParam {
	return &TableParam{
		BaseParam: database.NewBaseParam(sqlite3.NewTable(database.NewBaseTable("", "", "test")), nil),
	}
}

func (t *TableParam) Query(_ []element.Record) (string, error) {
	return "select * from test", nil
}

func (t *TableParam) Agrs(_ []element.Record) ([]interface{}, error) {
	return nil, nil
}

type FetchHandler struct {
}

func (f *FetchHandler) OnRecord(r element.Record) error {
	fmt.Println(r)
	return nil
}

func (f *FetchHandler) CreateRecord() (element.Record, error) {
	return element.NewDefaultRecord(), nil
}

func main() {
	fmt.Println("strat")
	db, err := database.Open("sqlite3", testJSONFromString(`{"url":"E:\\projects\\sqlite3\\test.db"}`))
	if err != nil {
		fmt.Printf("open fail. err: %v", err)
		return
	}
	defer db.Close()
	err = db.FetchRecord(context.TODO(), NewTableParam(), &FetchHandler{})
	if err != nil {
		fmt.Printf("fetchRecord fail. err: %v", err)
		return
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSource

func NewSource(bs *database.BaseSource) (s database.Source, err error)

NewSource generates a PostgreSQL data source and will report an error if there's an issue with the configuration file

func Quoted

func Quoted(s string) string

Quoted is the quoting function for PostgreSQL

Types

type Config

type Config struct {
	URL string `json:"url"` // Database URL, including the database address and other database parameters
}

Config is the Sqlite3 configuration

func NewConfig

func NewConfig(conf *config.JSON) (c *Config, err error)

NewConfig creates a Sqlite3 configuration and will report an error if the format does not meet the requirements

func (*Config) FormatDSN

func (c *Config) FormatDSN() (dsn string, err error)

FormatDSN generates data source connection information and will report an error if the URL is incorrect

type Dialect

type Dialect struct{}

Dialect represents the database dialect for PostgreSQL

func (Dialect) Name

func (d Dialect) Name() string

Name is the registered name of the database dialect

func (Dialect) Source

func (d Dialect) Source(bs *database.BaseSource) (database.Source, error)

Source generates a PostgreSQL data source

type Field

type Field struct {
	*database.BaseField
	database.BaseConfigSetter
}

Field - Represents a field in a database table.

func NewField

func NewField(bf *database.BaseField) *Field

NewField - Generates a field based on basic column attributes.

func (*Field) BindVar

func (f *Field) BindVar(i int) string

BindVar - SQL placeholder used in SQL statements.

func (*Field) Quoted

func (f *Field) Quoted() string

Quoted - Used for quoting in SQL statements.

func (*Field) Scanner

func (f *Field) Scanner() database.Scanner

Scanner - Used for reading data from a field.

func (*Field) Select

func (f *Field) Select() string

Select - Represents a field for querying purposes in SQL query statements.

func (*Field) Type

func (f *Field) Type() database.FieldType

Type - Represents the type of the field.

func (*Field) Valuer

func (f *Field) Valuer(c element.Column) database.Valuer

Valuer - Handles data processing using GoValuer.

type FieldType

type FieldType struct {
	*database.BaseFieldType
	// contains filtered or unexported fields
}

FieldType - Represents the type of a field.

func NewFieldType

func NewFieldType(typ database.ColumnType) *FieldType

NewFieldType - Creates a new field type.

func (*FieldType) GoType

func (f *FieldType) GoType() database.GoType

GoType - Returns the Golang type used when processing numerical values.

func (*FieldType) IsSupported

func (f *FieldType) IsSupported() bool

IsSupported - Indicates whether parsing is supported for a specific type.

type Scanner

type Scanner struct {
	database.BaseScanner
	// contains filtered or unexported fields
}

Scanner - A scanner used for reading data based on the column type.

func NewScanner

func NewScanner(f *Field) *Scanner

NewScanner - Generates a scanner based on the column type.

func (*Scanner) Scan

func (s *Scanner) Scan(src any) (err error)

Scan - Reads data from a column based on its type.

type Source

type Source struct {
	*database.BaseSource // Basic data source
	// contains filtered or unexported fields
}

Source postgres refers to the PostgreSQL data source

func (*Source) ConnectName

func (s *Source) ConnectName() string

ConnectName is the connection information for the Sqlite3 data source

func (*Source) DriverName

func (s *Source) DriverName() string

DriverName is the driver name

func (*Source) Key

func (s *Source) Key() string

Key is a keyword for the data source, used for reuse by DBWrapper

func (*Source) Table

func (s *Source) Table(b *database.BaseTable) database.Table

Table generates a table for Sqlite3 (Note: This line seems inconsistent with the context, as it mentions MySQL while the surrounding text is about PostgreSQL. It might be a mistake or needs clarification.)

type Table

type Table struct {
	*database.BaseTable
	database.BaseConfigSetter
}

Table represents a Sqlite3 table.

func NewTable

func NewTable(b *database.BaseTable) *Table

NewTable creates a new Sqlite3 table. Note that at this point, the schema parameter in BaseTable refers to the schema name, instance is the database name, and name is the table name.

func (*Table) AddField

func (t *Table) AddField(baseField *database.BaseField)

AddField adds a new column to the table.

func (*Table) ExecParam

func (t *Table) ExecParam(mode string, txOpts *sql.TxOptions) (database.Parameter, bool)

ExecParam retrieves execution parameters, where the copy in parameter mode has been registered.

func (*Table) Quoted

func (t *Table) Quoted() string

Quoted refers to the fully qualified name of the table.

func (*Table) ShouldOneByOne

func (t *Table) ShouldOneByOne(err error) bool

ShouldOneByOne specifies whether to retry one operation at a time.

func (*Table) ShouldRetry

func (t *Table) ShouldRetry(err error) bool

ShouldRetry determines whether a retry is necessary.

func (*Table) String

func (t *Table) String() string

Jump to

Keyboard shortcuts

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