sqlite

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2022 License: MIT Imports: 5 Imported by: 0

README

sqlite

Support SQLite database for github.com/gopsql/psql.

This package uses modernc.org/sqlite, a CGo-free port of SQLite.

Example

package main

import (
	"database/sql/driver"
	"fmt"
	"strings"
	"time"

	"github.com/gopsql/logger"
	"github.com/gopsql/psql"
	"github.com/gopsql/sqlite"
	"github.com/shopspring/decimal"
)

type Page struct {
	Id        int
	Foo       int64
	Bar       bool
	Title     string
	Cats      strArray
	Price     decimal.Decimal
	CreatedAt time.Time
	UpdatedAt time.Time
}

type strArray []string

func (c *strArray) Scan(src interface{}) error {
	if value, ok := src.(string); ok {
		*c = strings.Split(strings.TrimSpace(value), "\n")
	}
	return nil
}

func (c strArray) Value() (driver.Value, error) {
	return strings.Join(c, "\n") + "\n", nil
}

func main() {
	conn := sqlite.MustOpen("mydbfile.sqlite3")
	m := psql.NewModel(Page{}, conn, logger.StandardLogger)
	m.NewSQL(m.Schema()).MustExecute()
	// CREATE TABLE Pages (
	//         Id INTEGER PRIMARY KEY AUTOINCREMENT,
	//         Foo bigint DEFAULT 0 NOT NULL,
	//         Bar boolean DEFAULT false NOT NULL,
	//         Title text DEFAULT '' NOT NULL,
	//         Cats text DEFAULT '' NOT NULL,
	//         Price decimal(10, 2) DEFAULT 0.0 NOT NULL,
	//         CreatedAt timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
	//         UpdatedAt timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL
	// );
	m.Insert(
		"Title", "Hello",
		"Price", decimal.RequireFromString("1.2345"),
		"Cats", strArray{"foobar", "hello"},
		"CreatedAt", time.Now(),
	).MustExecute()
	var pages []Page
	m.Find().Where("Cats = $1", strArray{"foobar", "hello"}).MustQuery(&pages)
	fmt.Println(pages)
	conn.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FieldDataType

func FieldDataType(fieldName, fieldType string) (dataType string)

Generate data type based on struct's field name and type.

func MustOpen

func MustOpen(conn string) *sqliteDB

MustOpen is like Open but panics if connect operation fails.

func Open

func Open(conn string) (*sqliteDB, error)

Open creates and establishes one connection to database.

Types

This section is empty.

Jump to

Keyboard shortcuts

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