Help us shape the future of Go! Take the 2026 Go Developer Survey

dao

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

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

Go to latest
Published: Dec 5, 2017 License: Apache-2.0 Imports: 2 Imported by: 0

README

go-dao

License Build Status Coverage Status

Data access object pattern in Go.

Documentation

Overview

Package dao provides a data access object library.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package dao provides a data access object library.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package dao provides a data access object library.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package dao provides a data access object library.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package dao provides a data access object library.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package dao provides a data access object library.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package dao provides a data access object library.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	// UnknownDAOMessage error message format for the UnknownDAOError.
	UnknownDAOMessage = "There is no implementation registered for the %s data access object"
	// UnknownFactoryMessage error message format for the UnknownFactoryError.
	UnknownFactoryMessage = "There is no factory registered for the %s data access object"
)

Variables

This section is empty.

Functions

func NewUnknownDAO

func NewUnknownDAO(nm string) error

NewUnknownDAO returns a UnknownDAO error.

func NewUnknownFactory

func NewUnknownFactory(nm string) error

NewUnknownFactory returns a UnknownFactoryError error.

func Process

func Process(m Manager, f TransactionFunc) (err error)

Process wrap database transaction handling around given TransactionFunc.

Types

type BaseFactory

type BaseFactory struct {
	// Source target data source against which to generate data access objects.
	Source *DataSource
	// FactoryFuncs maps data access object names to the factory functions.
	FactoryFuncs map[string]FactoryFunc
}

BaseFactory base type to build data access object factories.

func (*BaseFactory) DataAccessObjects

func (f *BaseFactory) DataAccessObjects() []string

DataAccessObjects returns the list of data access object names the factory can generate.

func (*BaseFactory) NewDataAccessObject

func (f *BaseFactory) NewDataAccessObject(ctx *Context, nm string) (interface{}, error)

NewDataAccessObject returns a data access object.

type BaseManager

type BaseManager struct {
	Sources   map[string]*DataSource
	Factories map[string]Factory
}

BaseManager base implementation of the Manager interface.

func NewBaseManager

func NewBaseManager() *BaseManager

NewBaseManager returns a generic Manager implementation.

func (*BaseManager) CommitTransaction

func (m *BaseManager) CommitTransaction(ctx *Context) error

CommitTransaction commits the context database transaction.

func (*BaseManager) CreateDAO

func (m *BaseManager) CreateDAO(ctx *Context, nm string) (interface{}, error)

CreateDAO returns a new data access object.

func (*BaseManager) EndTransaction

func (m *BaseManager) EndTransaction(ctx *Context)

EndTransaction ends the context database transaction.

func (*BaseManager) RegisterDataSource

func (m *BaseManager) RegisterDataSource(ds *DataSource)

RegisterDataSource registers a new mapping between a name and a data source.

func (*BaseManager) RegisterFactory

func (m *BaseManager) RegisterFactory(f Factory)

RegisterFactory registers the given factory with the data access object names it can generate.

func (*BaseManager) RollbackTransaction

func (m *BaseManager) RollbackTransaction(ctx *Context) error

RollbackTransaction rollback the context database transaction.

func (*BaseManager) Source

func (m *BaseManager) Source(nm string) *DataSource

Source returns the data source associated with the given name.

func (*BaseManager) StartTransaction

func (m *BaseManager) StartTransaction() (*Context, error)

StartTransaction start the context database transaction.

type Context

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

Context struct to hold database transaction contexts.

func NewContext

func NewContext(m Manager) *Context

NewContext returns a new database transaction context.

func (*Context) NewDataAccessObject

func (ctx *Context) NewDataAccessObject(nm string) (*DataAccessObject, error)

NewDataAccessObject returns a data access object with an active database transaction.

func (*Context) Transaction

func (ctx *Context) Transaction(nm string) (*sql.Tx, error)

Transaction returns a new database transaction from the given data source.

type DataAccessObject

type DataAccessObject struct {
	// Tx the database transaction.
	Tx *sql.Tx
}

DataAccessObject base struct for data access objects.

func (*DataAccessObject) SetTransaction

func (dao *DataAccessObject) SetTransaction(tx *sql.Tx)

SetTransaction sets this data access object's database transaction.

func (*DataAccessObject) Transaction

func (dao *DataAccessObject) Transaction() *sql.Tx

Transaction returns this data access object's database transaction.

type DataSource

type DataSource struct {
	// DB the database.
	DB *sql.DB
	// Name the name of the data source.
	Name string
}

DataSource struct to represent data sources whose target are databases.

type Factory

type Factory interface {
	// DataAccessObjects returns the list of data access object names the factory can generate.
	DataAccessObjects() []string
	// NewDataAccessObject returns a data access object.
	NewDataAccessObject(ctx *Context, nm string) (interface{}, error)
}

Factory interface for data access object factories.

type FactoryFunc

type FactoryFunc func(ctx *Context, source string) (interface{}, error)

FactoryFunc function to generate data access object implementations.

type Manager

type Manager interface {
	// CommitTransaction commits the context database transaction.
	CommitTransaction(ctx *Context) error
	// CreateDAO returns a new data access object.
	CreateDAO(ctx *Context, nm string) (interface{}, error)
	// EndTransaction ends the context database transaction.
	EndTransaction(ctx *Context)
	// RegisterDataSource registers a new mapping between a name and a data source.
	RegisterDataSource(ds *DataSource)
	// RegisterFactory registers the given factory with the data access object names it can generate.
	RegisterFactory(f Factory)
	// RollbackTransaction rollback the context database transaction.
	RollbackTransaction(ctx *Context) error
	// Source returns the data source associated with the given name.
	Source(nm string) *DataSource
	// StartTransaction start the context database transaction.
	StartTransaction() (*Context, error)
}

Manager interface for data access object managers.

type TransactionFunc

type TransactionFunc func(ctx *Context) error

TransactionFunc definition of a function wrapped in a database transaction context.

type UnknownDAO

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

UnknownDAO no data access object implementation registered with the given name.

func (*UnknownDAO) Error

func (e *UnknownDAO) Error() string

Error returns the error message.

type UnknownFactory

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

UnknownFactory no data access object implementation registered with the given name.

func (*UnknownFactory) Error

func (e *UnknownFactory) Error() string

Error returns the error message.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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