Documentation ¶
Index ¶
- func GenerateObjectIdIfNotExists[T any](obj any) T
- func GenerateObjectMapIdIfNotExists(objectMap map[string]any)
- func GetObjectId[K any](obj any) (id K)
- func ItemsToAnySlice[T any](items []T) []any
- type ISqlServerPersistenceOverrides
- type IdentifiableJsonSqlServerPersistence
- func (c *IdentifiableJsonSqlServerPersistence[T, K]) ConvertFromPublic(value T) (map[string]any, error)
- func (c *IdentifiableJsonSqlServerPersistence[T, K]) ConvertFromPublicPartial(value map[string]any) (map[string]any, error)
- func (c *IdentifiableJsonSqlServerPersistence[T, K]) ConvertToPublic(rows *sql.Rows) (T, error)
- func (c *IdentifiableJsonSqlServerPersistence[T, K]) EnsureTable(idType string, dataType string)
- func (c *IdentifiableJsonSqlServerPersistence[T, K]) UpdatePartially(ctx context.Context, id K, data cdata.AnyValueMap) (result T, err error)
- type IdentifiableSqlServerPersistence
- func (c *IdentifiableSqlServerPersistence[T, K]) Create(ctx context.Context, item T) (result T, err error)
- func (c *IdentifiableSqlServerPersistence[T, K]) DeleteById(ctx context.Context, id K) (result T, err error)
- func (c *IdentifiableSqlServerPersistence[T, K]) DeleteByIds(ctx context.Context, ids []K) error
- func (c *IdentifiableSqlServerPersistence[T, K]) GetListByIds(ctx context.Context, ids []K) (items []T, err error)
- func (c *IdentifiableSqlServerPersistence[T, K]) GetOneById(ctx context.Context, id K) (item T, err error)
- func (c *IdentifiableSqlServerPersistence[T, K]) Set(ctx context.Context, item T) (result T, err error)
- func (c *IdentifiableSqlServerPersistence[T, K]) Update(ctx context.Context, item T) (result T, err error)
- func (c *IdentifiableSqlServerPersistence[T, K]) UpdatePartially(ctx context.Context, id K, data cdata.AnyValueMap) (result T, err error)
- type SqlServerPersistence
- func (c *SqlServerPersistence[T]) Clear(ctx context.Context) error
- func (c *SqlServerPersistence[T]) ClearSchema()
- func (c *SqlServerPersistence[T]) Close(ctx context.Context) (err error)
- func (c *SqlServerPersistence[T]) Configure(ctx context.Context, config *cconf.ConfigParams)
- func (c *SqlServerPersistence[T]) ConvertFromPublic(value T) (map[string]any, error)
- func (c *SqlServerPersistence[T]) ConvertFromPublicPartial(value map[string]any) (map[string]any, error)
- func (c *SqlServerPersistence[T]) ConvertToPublic(rows *sql.Rows) (T, error)
- func (c *SqlServerPersistence[T]) Create(ctx context.Context, item T) (result T, err error)
- func (c *SqlServerPersistence[T]) CreateSchema(ctx context.Context) (err error)
- func (c *SqlServerPersistence[T]) DefineSchema()
- func (c *SqlServerPersistence[T]) DeleteByFilter(ctx context.Context, filter string) error
- func (c *SqlServerPersistence[T]) EnsureIndex(name string, keys map[string]string, options map[string]string)
- func (c *SqlServerPersistence[T]) EnsureSchema(schemaStatement string)
- func (c *SqlServerPersistence[T]) GenerateColumns(columns []string) string
- func (c *SqlServerPersistence[T]) GenerateColumnsAndValues(objMap map[string]any) ([]string, []any)
- func (c *SqlServerPersistence[T]) GenerateParameters(valuesCount int) string
- func (c *SqlServerPersistence[T]) GenerateSetParameters(columns []string) string
- func (c *SqlServerPersistence[T]) GetCountByFilter(ctx context.Context, filter string) (int64, error)
- func (c *SqlServerPersistence[T]) GetListByFilter(ctx context.Context, filter string, sort string, selection string) (items []T, err error)
- func (c *SqlServerPersistence[T]) GetOneRandom(ctx context.Context, filter string) (item T, err error)
- func (c *SqlServerPersistence[T]) GetPageByFilter(ctx context.Context, filter string, paging cquery.PagingParams, sort string, ...) (page cquery.DataPage[T], err error)
- func (c *SqlServerPersistence[T]) IsOpen() bool
- func (c *SqlServerPersistence[T]) IsTerminated() bool
- func (c *SqlServerPersistence[T]) Open(ctx context.Context) (err error)
- func (c *SqlServerPersistence[T]) QuoteIdentifier(value string) string
- func (c *SqlServerPersistence[T]) QuotedTableName() string
- func (c *SqlServerPersistence[T]) SetReferences(ctx context.Context, references cref.IReferences)
- func (c *SqlServerPersistence[T]) UnsetReferences()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetObjectId ¶
func ItemsToAnySlice ¶
Types ¶
type IdentifiableJsonSqlServerPersistence ¶
type IdentifiableJsonSqlServerPersistence[T any, K any] struct { *IdentifiableSqlServerPersistence[T, K] }
IdentifiableJsonSqlServerPersistence is an abstract persistence component that stores data in SqlServer in JSON or JSONB fields and implements a number of CRUD operations over data items with unique ids. The data items must implement IIdentifiable interface.
The JSON table has only two fields: id and data.
In basic scenarios child classes shall only override GetPageByFilter, getListByFilter or deleteByFilter operations with specific filter function. All other operations can be used out of the box.
In complex scenarios child classes can implement additional operations by accessing c._collection and c._model properties.
Configuration parameters - table: (optional) SQLServer table name - schema: (optional) SQLServer table name - connection(s): - discovery_key: (optional) a key to retrieve the connection from IDiscovery - host: host name or IP address - port: port number (default: 27017) - uri: resource URI or connection string with all parameters in it - credential(s): - store_key: (optional) a key to retrieve the credentials from ICredentialStore - username: (optional) user name - password: (optional) user password - options: - connect_timeout: (optional) number of milliseconds to wait before timing out when connecting a new client (default: 0) - idle_timeout: (optional) number of milliseconds a client must sit idle in the pool and not be checked out (default: 10000) - max_pool_size: (optional) maximum number of clients the pool should contain (default: 10) References - *:logger:*:*:1.0 (optional) ILogger components to pass log messages components to pass log messages - *:discovery:*:*:1.0 (optional) IDiscovery services - *:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials
Example:
type MySqlServerPersistence struct { *persist.IdentifiableJsonSqlServerPersistence[MyData, string] } func NewMySqlServerPersistence() *MySqlServerPersistence { c := &MySqlServerPersistence{} c.IdentifiableJsonSqlServerPersistence = persist.InheritIdentifiableJsonSqlServerPersistence[MyData, string](c, "mydata") return c } func (c *MySqlServerPersistence) DefineSchema() { c.ClearSchema() c.EnsureTable("", "") c.EnsureSchema("ALTER TABLE [" + c.TableName + "] ADD [data_key] AS JSON_VALUE([data],'$.key')") c.EnsureIndex(c.TableName+"_key", map[string]string{"data_key": "1"}, map[string]string{"unique": "true"}) } func (c *MySqlServerPersistence) GetPageByFilter(ctx context.Context, filter cdata.FilterParams, paging cdata.PagingParams) (page cdata.DataPage[MyData], err error) { key, ok := filter.GetAsNullableString("Key") filterObj := "" if ok && key != "" { filterObj += "JSON_VALUE([data],'$.key')='" + key + "'" } return c.IdentifiableJsonSqlServerPersistence.GetPageByFilter(ctx, filterObj, paging, "", "", ) } func main() { persistence := NewMySqlServerPersistence() persistence.Configure(context.Background(), cconf.NewConfigParamsFromTuples( "host", "localhost", "port", 1433, )) err := persitence.Open(context.Background()) item, err := persistence.Create(context.Background(), Mydata{Id: "1", Name: "ABC"}) page, err := persistence.GetPageByFilter(context.Background(), *NewFilterParamsFromTuples("name", "ABC"), nil) fmt.Println(page.data) // Result: { id: "1", name: "ABC" } res, err := persistence.DeleteById(context.Background(), "1") }
func InheritIdentifiableJsonSqlServerPersistence ¶
func InheritIdentifiableJsonSqlServerPersistence[T any, K any](overrides ISqlServerPersistenceOverrides[T], tableName string) *IdentifiableJsonSqlServerPersistence[T, K]
InheritIdentifiableJsonSqlServerPersistence creates a new instance of the persistence component.
Parameters: - overrides References to override virtual methods - tableName (optional) a table name.
func (*IdentifiableJsonSqlServerPersistence[T, K]) ConvertFromPublic ¶
func (c *IdentifiableJsonSqlServerPersistence[T, K]) ConvertFromPublic(value T) (map[string]any, error)
ConvertFromPublic convert object value from public to internal format.
Parameters: - value an object in public format to convert.
Returns converted object in internal format.
func (*IdentifiableJsonSqlServerPersistence[T, K]) ConvertFromPublicPartial ¶
func (c *IdentifiableJsonSqlServerPersistence[T, K]) ConvertFromPublicPartial(value map[string]any) (map[string]any, error)
ConvertFromPublicPartial convert object value from public to internal format.
Parameters: - value an object in public format to convert. Returns: converted object in internal format.
func (*IdentifiableJsonSqlServerPersistence[T, K]) ConvertToPublic ¶
func (c *IdentifiableJsonSqlServerPersistence[T, K]) ConvertToPublic(rows *sql.Rows) (T, error)
ConvertToPublic converts object value from internal to public format.
Parameters: - value an object in internal format to convert. Returns: converted object in public format.
func (*IdentifiableJsonSqlServerPersistence[T, K]) EnsureTable ¶
func (c *IdentifiableJsonSqlServerPersistence[T, K]) EnsureTable(idType string, dataType string)
EnsureTable Adds DML statement to automatically create JSON(B) table
Parameters: - idType type of the id column (default: VARCHAR(32)) - dataType type of the data column (default: NVARCHAR(MAX))
func (*IdentifiableJsonSqlServerPersistence[T, K]) UpdatePartially ¶
func (c *IdentifiableJsonSqlServerPersistence[T, K]) UpdatePartially(ctx context.Context, id K, data cdata.AnyValueMap) (result T, err error)
UpdatePartially updates only few selected fields in a data item.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - id an id of data item to be updated. - data a map with fields to be updated.
Returns: receives updated item or error.
type IdentifiableSqlServerPersistence ¶
type IdentifiableSqlServerPersistence[T any, K any] struct { *SqlServerPersistence[T] }
IdentifiableSqlServerPersistence Abstract persistence component that stores data in SqlServer and implements a number of CRUD operations over data items with unique ids. The data items must implement IIdentifiable interface.
In basic scenarios child classes shall only override GetPageByFilter, getListByFilter or deleteByFilter operations with specific filter function. All other operations can be used out of the box.
In complex scenarios child classes can implement additional operations by accessing c._collection and c._model properties.
Configuration parameters - collection: (optional) SqlServer collection name - connection(s): - discovery_key: (optional) a key to retrieve the connection from IDiscovery - host: host name or IP address - port: port number (default: 27017) - uri: resource URI or connection string with all parameters in it - credential(s): - store_key: (optional) a key to retrieve the credentials from ICredentialStore - username: (optional) user name - password: (optional) user password - options: - connect_timeout: (optional) number of milliseconds to wait before timing out when connecting a new client (default: 0) - idle_timeout: (optional) number of milliseconds a client must sit idle in the pool and not be checked out (default: 10000) - max_pool_size: (optional) maximum number of clients the pool should contain (default: 10) References - *:logger:*:*:1.0 (optional) ILogger components to pass log messages components to pass log messages - *:discovery:*:*:1.0 (optional) IDiscovery services - *:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials
Example:
type SqlServerServerPersistence struct { *persist.IdentifiableSqlServerPersistence[MyData, string] } func NewSqlServerServerPersistence() *SqlServerServerPersistence { c := &SqlServerServerPersistence{} c.IdentifiableSqlServerPersistence = persist.InheritIdentifiableSqlServerPersistence[MyData, string](c, "mydata") return c } func (c *SqlServerServerPersistence) DefineSchema() { c.ClearSchema() c.EnsureSchema("CREATE TABLE [" + c.TableName + "] ([id] VARCHAR(32) PRIMARY KEY, [key] VARCHAR(50), [content] VARCHAR(MAX))") c.EnsureIndex(c.IdentifiableSqlServerPersistence.TableName+"_key", map[string]string{"key": "1"}, map[string]string{"unique": "true"}) } func (c *SqlServerServerPersistence) GetPageByFilter(ctx context.Context, filter cdata.FilterParams, paging cdata.PagingParams) (page cdata.DataPage[MyData], err error) { key, ok := filter.GetAsNullableString("Key") filterObj := "" if ok && key != "" { filterObj += "[key]='" + key + "'" } sorting := "" return c.IdentifiableSqlServerPersistence.GetPageByFilter(ctx, filterObj, paging, sorting, "", ) } func main() { persistence := NewSqlServerServerPersistence() persistence.Configure(context.Background(), NewConfigParamsFromTuples( "host", "localhost", "port", 1433, )) err := persistence.Open(context.Background()) item, err := persistence.Create(context.Background(), MyData{Id: "1", Name: "ABC"}) page, err := persistence.GetPageByFilter(context.Background(), *NewFilterParamsFromTuples("name", "ABC"), nil) fmt.Println(page.Data) res, err := persistence.DeleteById(context.Background(), "1") }
func InheritIdentifiableSqlServerPersistence ¶
func InheritIdentifiableSqlServerPersistence[T any, K any](overrides ISqlServerPersistenceOverrides[T], tableName string) *IdentifiableSqlServerPersistence[T, K]
InheritIdentifiableSqlServerPersistence creates a new instance of the persistence component.
Parameters: - ctx context.Context - overrides References to override virtual methods - tableName (optional) a table name.
func (*IdentifiableSqlServerPersistence[T, K]) Create ¶
func (c *IdentifiableSqlServerPersistence[T, K]) Create(ctx context.Context, item T) (result T, err error)
Create a data item.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - item an item to be created. Returns: (optional) created item or error.
func (*IdentifiableSqlServerPersistence[T, K]) DeleteById ¶
func (c *IdentifiableSqlServerPersistence[T, K]) DeleteById(ctx context.Context, id K) (result T, err error)
DeleteById deletes a data item by its unique id.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - id an id of the item to be deleted Returns: (optional) deleted item or error.
func (*IdentifiableSqlServerPersistence[T, K]) DeleteByIds ¶
func (c *IdentifiableSqlServerPersistence[T, K]) DeleteByIds(ctx context.Context, ids []K) error
DeleteByIds deletes multiple data items by their unique ids.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - ids of data items to be deleted. Returns: (optional) error or null for success.
func (*IdentifiableSqlServerPersistence[T, K]) GetListByIds ¶
func (c *IdentifiableSqlServerPersistence[T, K]) GetListByIds(ctx context.Context, ids []K) (items []T, err error)
GetListByIds gets a list of data items retrieved by given unique ids.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - ids of data items to be retrieved Returns: a data list or error.
func (*IdentifiableSqlServerPersistence[T, K]) GetOneById ¶
func (c *IdentifiableSqlServerPersistence[T, K]) GetOneById(ctx context.Context, id K) (item T, err error)
GetOneById gets a data item by its unique id.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - id an id of data item to be retrieved.
Returns: data item or error.
func (*IdentifiableSqlServerPersistence[T, K]) Set ¶
func (c *IdentifiableSqlServerPersistence[T, K]) Set(ctx context.Context, item T) (result T, err error)
Set a data item. If the data item exists it updates it, otherwise it creates a new data item.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - item an item to be set. Returns: (optional) updated item or error.
func (*IdentifiableSqlServerPersistence[T, K]) Update ¶
func (c *IdentifiableSqlServerPersistence[T, K]) Update(ctx context.Context, item T) (result T, err error)
Update a data item.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - item an item to be updated. Returns (optional) updated item or error.
func (*IdentifiableSqlServerPersistence[T, K]) UpdatePartially ¶
func (c *IdentifiableSqlServerPersistence[T, K]) UpdatePartially(ctx context.Context, id K, data cdata.AnyValueMap) (result T, err error)
UpdatePartially updates only few selected fields in a data item.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - id an id of data item to be updated. - data a map with fields to be updated. Returns: updated item or error.
type SqlServerPersistence ¶
type SqlServerPersistence[T any] struct { Overrides ISqlServerPersistenceOverrides[T] // Defines general JSON convertors JsonConvertor cconv.IJSONEngine[T] JsonMapConvertor cconv.IJSONEngine[map[string]any] //The dependency resolver. DependencyResolver *cref.DependencyResolver //The logger. Logger *clog.CompositeLogger //The SqlServer connection component. Connection *conn.SqlServerConnection //The SqlServer connection pool object. Client *sql.DB //The SqlServer database name. DatabaseName string //The SqlServer database schema name. If not set use "public" by default SchemaName string //The SqlServer table object. TableName string MaxPageSize int // contains filtered or unexported fields }
SqlServerPersistence Abstract persistence component that stores data in SqlServer using plain driver.
This is the most basic persistence component that is only able to store data items of any type. Specific CRUD operations over the data items must be implemented in child classes by accessing c._db or c.collection properties.
Configuration parameters - collection: (optional) SqlServer collection name - schema: (optional) SqlServer schema, default "public" - connection(s): - discovery_key: (optional) a key to retrieve the connection from IDiscovery - host: host name or IP address - port: port number (default: 27017) - uri: resource URI or connection string with all parameters in it - credential(s): - store_key: (optional) a key to retrieve the credentials from ICredentialStore - username: (optional) user name - password: (optional) user password - options: - connect_timeout: (optional) number of milliseconds to wait before timing out when connecting a new client (default: 0) - idle_timeout: (optional) number of milliseconds a client must sit idle in the pool and not be checked out (default: 10000) - max_pool_size: (optional) maximum number of clients the pool should contain (default: 10) References: - *:logger:*:*:1.0 (optional) ILogger components to pass log messages - *:discovery:*:*:1.0 (optional) IDiscovery services - *:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials
Example:
import ( "context" "fmt" cconf "github.com/pip-services4/pip-services4-go/pip-services4-commons-go/config" cpersist "github.com/pip-services4/pip-services4-go/pip-services4-data-go/persistence" persist "github.com/pip-services4/pip-services4-go/pip-services4-sqlserver-go/persistence" "github.com/pip-services4/pip-services4-go/pip-services4-sqlserver-go/test/fixtures" ) type SqlServerServerPersistence struct { *persist.SqlServerPersistence[fixtures.Dummy] } func NewSqlServerServerPersistence() *SqlServerServerPersistence { c := &SqlServerServerPersistence{} c.SqlServerPersistence = persist.InheritSqlServerPersistence[fixtures.Dummy](c, "mydata") return c } func (c *SqlServerServerPersistence) GetOneByName(ctx context.Context, name string) (item fixtures.Dummy, err error) { query := "SELECT * FROM " + c.QuotedTableName() + " WHERE [id]=@p1" rows, err := c.Client.QueryContext(ctx, query, name) if err != nil { return item, err } defer rows.Close() if !rows.Next() { return item, rows.Err() } if err == nil { return c.Overrides.ConvertToPublic(rows) } return item, err } func (c *SqlServerServerPersistence) Set(ctx context.Context, item fixtures.Dummy) (result fixtures.Dummy, err error) { objMap, convErr := c.Overrides.ConvertFromPublic(item) if convErr != nil { return result, convErr } GenerateObjectMapIdIfNotExists(objMap) columns, values := c.GenerateColumnsAndValues(objMap) paramsStr := c.GenerateParameters(len(values)) columnsStr := c.GenerateColumns(columns) setParams := c.GenerateSetParameters(columns) id := cpersist.GetObjectId(objMap) query := "INSERT INTO " + c.QuotedTableName() + " (" + columnsStr + ") OUTPUT INSERTED.* VALUES (" + paramsStr + ")" rows, err := c.Client.QueryContext(ctx, query, values...) if err != nil { return result, err } defer rows.Close() if rows.Next() { result, convErr = c.Overrides.ConvertToPublic(rows) if convErr != nil { return result, convErr } return result, nil } values = append(values, id) query = "UPDATE " + c.QuotedTableName() + " SET " + setParams + " OUTPUT INSERTED.* WHERE [id]=@p" + strconv.FormatInt(int64(len(values)), 10) rows, err = c.Client.QueryContext(ctx, query, values...) if err != nil { return result, err } defer rows.Close() if !rows.Next() { return result, rows.Err() } result, convErr = c.Overrides.ConvertToPublic(rows) if convErr != nil { return result, convErr } return result, rows.Err() } func main() { persistence := NewSqlServerServerPersistence() persistence.Configure(context.Background(), cconf.NewConfigParamsFromTuples( "host", "localhost", "port", 27017, )) err := persistence.Open(context.Background()) res, err := persistence.Set(context.Background(), MyData{Id: "1", Name: "ABC"}) item, err := persistence.GetOneByName(context.Background(), "ABC") fmt.Println(item) // Result: { Id: "1", Name: "ABC" } }
func InheritSqlServerPersistence ¶
func InheritSqlServerPersistence[T any](overrides ISqlServerPersistenceOverrides[T], tableName string) *SqlServerPersistence[T]
InheritSqlServerPersistence creates a new instance of the persistence component.
Parameters: - overrides References to override virtual methods - tableName (optional) a table name.
func (*SqlServerPersistence[T]) Clear ¶
func (c *SqlServerPersistence[T]) Clear(ctx context.Context) error
Clear component state.
Parameters: - ctx context.Context transaction id to trace execution through call chain. Returns: error or nil no errors occured.
func (*SqlServerPersistence[T]) ClearSchema ¶
func (c *SqlServerPersistence[T]) ClearSchema()
ClearSchema clears all auto-created objects
func (*SqlServerPersistence[T]) Close ¶
func (c *SqlServerPersistence[T]) Close(ctx context.Context) (err error)
Close component and frees used resources.
Parameters: - ctx context.Context transaction id to trace execution through call chain. Returns: error or nil no errors occurred.
func (*SqlServerPersistence[T]) Configure ¶
func (c *SqlServerPersistence[T]) Configure(ctx context.Context, config *cconf.ConfigParams)
Configure component by passing configuration parameters.
Parameters: - ctx context.Context - config configuration parameters to be set.
func (*SqlServerPersistence[T]) ConvertFromPublic ¶
func (c *SqlServerPersistence[T]) ConvertFromPublic(value T) (map[string]any, error)
ConvertFromPublic сonvert object value from func (c * SqlServerPersistence) to internal format.
Parameters: - value an object in func (c * SqlServerPersistence) format to convert. Returns: converted object in internal format.
func (*SqlServerPersistence[T]) ConvertFromPublicPartial ¶
func (c *SqlServerPersistence[T]) ConvertFromPublicPartial(value map[string]any) (map[string]any, error)
ConvertFromPublicPartial converts the given object from the public partial format.
Parameters: - value the object to convert from the public partial format. Returns: the initial object.
func (*SqlServerPersistence[T]) ConvertToPublic ¶
func (c *SqlServerPersistence[T]) ConvertToPublic(rows *sql.Rows) (T, error)
ConvertToPublic converts object value from internal to func (c * SqlServerPersistence) format.
Parameters: - value an object in internal format to convert. Returns: converted object in func (c * SqlServerPersistence) format.
func (*SqlServerPersistence[T]) Create ¶
func (c *SqlServerPersistence[T]) Create(ctx context.Context, item T) (result T, err error)
Create creates a data item.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - item an item to be created. Returns: (optional) callback function that receives created item or error.
func (*SqlServerPersistence[T]) CreateSchema ¶
func (c *SqlServerPersistence[T]) CreateSchema(ctx context.Context) (err error)
func (*SqlServerPersistence[T]) DefineSchema ¶
func (c *SqlServerPersistence[T]) DefineSchema()
DefineSchema a database schema for this persistence, have to call in child class Override in child classes
func (*SqlServerPersistence[T]) DeleteByFilter ¶
func (c *SqlServerPersistence[T]) DeleteByFilter(ctx context.Context, filter string) error
DeleteByFilter deletes data items that match to a given filter. This method shall be called by a func (c * SqlServerPersistence) deleteByFilter method from child class that receives FilterParams and converts them into a filter function.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - filter (optional) a filter JSON object. Returns: error or nil for success.
func (*SqlServerPersistence[T]) EnsureIndex ¶
func (c *SqlServerPersistence[T]) EnsureIndex(name string, keys map[string]string, options map[string]string)
EnsureIndex adds index definition to create it on opening
Parameters: - keys index keys (fields) - options index options
func (*SqlServerPersistence[T]) EnsureSchema ¶
func (c *SqlServerPersistence[T]) EnsureSchema(schemaStatement string)
EnsureSchema adds a statement to schema definition
Parameters: - schemaStatement a statement to be added to the schema
func (*SqlServerPersistence[T]) GenerateColumns ¶
func (c *SqlServerPersistence[T]) GenerateColumns(columns []string) string
GenerateColumns generates a list of column names to use in SQL statements like: "column1,column2,column3"
Parameters: - columns an array with column values Returns: a generated list of column names
func (*SqlServerPersistence[T]) GenerateColumnsAndValues ¶
func (c *SqlServerPersistence[T]) GenerateColumnsAndValues(objMap map[string]any) ([]string, []any)
GenerateColumnsAndValues generates a list of column parameters
Parameters: - values an array with column values or a key-value map Returns: a generated list of column values
func (*SqlServerPersistence[T]) GenerateParameters ¶
func (c *SqlServerPersistence[T]) GenerateParameters(valuesCount int) string
GenerateParameters generates a list of value parameters to use in SQL statements like: "?,?,?"
Parameters: - values an array with column values or a key-value map Returns: a generated list of value parameters
func (*SqlServerPersistence[T]) GenerateSetParameters ¶
func (c *SqlServerPersistence[T]) GenerateSetParameters(columns []string) string
GenerateSetParameters generates a list of column sets to use in UPDATE statements like: column1=?,column2=?
Parameters: - values an array with column values or a key-value map Returns: a generated list of column sets
func (*SqlServerPersistence[T]) GetCountByFilter ¶
func (c *SqlServerPersistence[T]) GetCountByFilter(ctx context.Context, filter string) (int64, error)
GetCountByFilter gets a number of data items retrieved by a given filter. This method shall be called by a func (c * SqlServerPersistence) getCountByFilter method from child class that receives FilterParams and converts them into a filter function.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - filter (optional) a filter JSON object Returns: data page or error.
func (*SqlServerPersistence[T]) GetListByFilter ¶
func (c *SqlServerPersistence[T]) GetListByFilter(ctx context.Context, filter string, sort string, selection string) (items []T, err error)
GetListByFilter gets a list of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a func (c * SqlServerPersistence) getListByFilter method from child class that receives FilterParams and converts them into a filter function.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - filter (optional) a filter JSON object - paging (optional) paging parameters - sort (optional) sorting JSON object - select (optional) projection JSON object Returns: data list or error.
func (*SqlServerPersistence[T]) GetOneRandom ¶
func (c *SqlServerPersistence[T]) GetOneRandom(ctx context.Context, filter string) (item T, err error)
GetOneRandom gets a random item from items that match to a given filter. This method shall be called by a func (c * SqlServerPersistence) getOneRandom method from child class that receives FilterParams and converts them into a filter function.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - filter (optional) a filter JSON object Returns: random item or error.
func (*SqlServerPersistence[T]) GetPageByFilter ¶
func (c *SqlServerPersistence[T]) GetPageByFilter(ctx context.Context, filter string, paging cquery.PagingParams, sort string, selection string) (page cquery.DataPage[T], err error)
GetPageByFilter gets a page of data items retrieved by a given filter and sorted according to sort parameters. This method shall be called by a func (c * SqlServerPersistence) getPageByFilter method from child class that receives FilterParams and converts them into a filter function.
Parameters: - ctx context.Context transaction id to trace execution through call chain. - filter (optional) a filter JSON object - paging (optional) paging parameters - sort (optional) sorting JSON object - select (optional) projection JSON object Returns: receives a data page or error.
func (*SqlServerPersistence[T]) IsOpen ¶
func (c *SqlServerPersistence[T]) IsOpen() bool
IsOpen checks if the component is opened.
Returns: true if the component has been opened and false otherwise.
func (*SqlServerPersistence[T]) IsTerminated ¶
func (c *SqlServerPersistence[T]) IsTerminated() bool
IsTerminated checks if the wee need to terminate process before close component.
Returns: true if you need terminate your processes.
func (*SqlServerPersistence[T]) Open ¶
func (c *SqlServerPersistence[T]) Open(ctx context.Context) (err error)
Open the component.
Parameters: - ctx context.Context transaction id to trace execution through call chain. Returns: error or nil no errors occurred.
func (*SqlServerPersistence[T]) QuoteIdentifier ¶
func (c *SqlServerPersistence[T]) QuoteIdentifier(value string) string
func (*SqlServerPersistence[T]) QuotedTableName ¶
func (c *SqlServerPersistence[T]) QuotedTableName() string
QuotedTableName return quoted SchemaName with TableName ("schema"."table")
func (*SqlServerPersistence[T]) SetReferences ¶
func (c *SqlServerPersistence[T]) SetReferences(ctx context.Context, references cref.IReferences)
SetReferences to dependent components.
Parameters: - ctx context.Context - references references to locate the component dependencies.
func (*SqlServerPersistence[T]) UnsetReferences ¶
func (c *SqlServerPersistence[T]) UnsetReferences()
UnsetReferences (clears) previously set references to dependent components.