github.com/haraldrudell/parl@v0.4.176/if-dsn.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package parl
     7  
     8  import (
     9  	"context"
    10  	"database/sql"
    11  )
    12  
    13  // DataSourceNamer provides data source names for SQL
    14  // based on possibly appplication name and partition year
    15  //   - a data source represents a set of SQL tables,
    16  //     possibly partitioned,
    17  //     against which queries can be prepared and executed
    18  //   - DataSourceNamer applies to any database implementation
    19  //   - sqliter provides implementations for SQLite3
    20  type DataSourceNamer interface {
    21  	// DSN returns the data source name based on a partition selector
    22  	DSN(partition ...DBPartition) (dataSourceName DataSourceName)
    23  	// DataSource returns a usable data source based on a data source name
    24  	DataSource(dsn DataSourceName) (dataSource DataSource, err error)
    25  }
    26  
    27  // DataSource is a value referring to a set of SQL tables,
    28  // possibly a partition
    29  type DataSourceName string
    30  
    31  // DataSource represents a set of SQL tables,
    32  // possibly partitioned,
    33  // against which queries can be prepared and executed
    34  //   - DataSource applies to any database implementation
    35  type DataSource interface {
    36  	// PrepareContext prepares a statement that is a query for reading or writing data
    37  	//	- a prepared statement can be executed multiple times
    38  	PrepareContext(ctx context.Context, query string) (stmt *sql.Stmt, err error)
    39  	// Close closes the data-source
    40  	Close() (err error)
    41  }
    42  
    43  // DSNrFactory describes the signature for a data source namer new function
    44  //   - the data source namer provides data sources for query operations
    45  //   - DSNrFactory applies to any database implementation
    46  type DSNrFactory interface {
    47  	// NewDSNr returns an object that can
    48  	//	- provide data source names from partition selectors and
    49  	//	- provide data sources from a data source name
    50  	DataSourceNamer(appName string) (dsnr DataSourceNamer, err error)
    51  }