Skip to content

Commit 353a91a

Browse files
committed
Move sql package from codnect.io/procyon to codnect.io/procyon-data
1 parent e6861bd commit 353a91a

14 files changed

Lines changed: 340 additions & 0 deletions

sql/connection.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package sql
2+
3+
import (
4+
"context"
5+
"database/sql/driver"
6+
"time"
7+
)
8+
9+
type Connection interface {
10+
ExecContext(ctx context.Context, query string, args ...any) (Result, error)
11+
PrepareContext(ctx context.Context, query string, args ...any) (Statement, error)
12+
QueryContext(ctx context.Context, query string, args ...any) (RowSet, error)
13+
QueryRowContext(ctx context.Context, query string, args ...any) (Row, error)
14+
PingContext(ctx context.Context) error
15+
16+
SetConnMaxLifetime(d time.Duration) error
17+
SetConnMaxIdleTime(d time.Duration) error
18+
SetMaxIdleConns(n int) error
19+
SetMaxOpenConns(n int) error
20+
21+
BeginTransaction(ctx context.Context) (TransactionStatus, error)
22+
Driver() driver.Driver
23+
24+
Close() error
25+
IsClosed() bool
26+
}

sql/context.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package sql
2+
3+
import (
4+
"context"
5+
"reflect"
6+
)
7+
8+
type ctxTransactionContext struct{}
9+
10+
var (
11+
ctxTransactionContextKey = &ctxTransactionContext{}
12+
rTransactionContextType = reflect.TypeOf((*TransactionContext)(nil))
13+
)
14+
15+
type TransactionContext struct {
16+
}
17+
18+
func FromContext[T *TransactionContext](ctx context.Context) T {
19+
var value T
20+
rType := reflect.TypeOf((*T)(nil)).Elem()
21+
22+
if rType.ConvertibleTo(rTransactionContextType) {
23+
value = ctx.Value(ctxTransactionContextKey).(T)
24+
}
25+
26+
return value
27+
}

sql/datasource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package sql
2+
3+
type DataSource interface {
4+
GetConnection() (Connection, error)
5+
}

sql/extractor.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package sql
2+
3+
func ExtractRow[T any](row Row, callback func(row Row) T) (t T) {
4+
return callback(row)
5+
}
6+
7+
func ExtractRowSet[T any](rowSet RowSet, callback func(rowSet RowSet) []T) []T {
8+
return callback(rowSet)
9+
}

sql/isolation.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package sql
2+
3+
type IsolationLevel int
4+
5+
const (
6+
LevelDefault IsolationLevel = iota
7+
LevelReadUncommitted
8+
LevelReadCommitted
9+
LevelWriteCommitted
10+
LevelRepeatableRead
11+
LevelSnapshot
12+
LevelSerializable
13+
LevelLinearizable
14+
)

sql/manager.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package sql
2+
3+
import "context"
4+
5+
type TransactionManager interface {
6+
GetTransaction(ctx context.Context, options ...any) (TransactionStatus, error)
7+
Commit() error
8+
Rollback() error
9+
}
10+
11+
type DataSourceTransactionManager struct {
12+
dataSource DataSource
13+
}
14+
15+
func NewDataSourceTransactionManager(dataSource DataSource) *DataSourceTransactionManager {
16+
return &DataSourceTransactionManager{
17+
dataSource: dataSource,
18+
}
19+
}
20+
21+
func (m *DataSourceTransactionManager) GetTransaction(ctx context.Context, options ...any) (TransactionStatus, error) {
22+
return nil, nil
23+
}
24+
25+
func (m *DataSourceTransactionManager) Commit() error {
26+
return nil
27+
}
28+
29+
func (m *DataSourceTransactionManager) Rollback() error {
30+
return nil
31+
}

sql/options.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package sql
2+
3+
import "time"
4+
5+
type TransactionOption func(options *TransactionOptions)
6+
7+
type TransactionOptions struct {
8+
propagation Propagation
9+
timeout time.Duration
10+
readOnly bool
11+
isolationLevel IsolationLevel
12+
}
13+
14+
func (o *TransactionOptions) Propagation() Propagation {
15+
return o.propagation
16+
}
17+
18+
func (o *TransactionOptions) Timeout() time.Duration {
19+
return o.timeout
20+
}
21+
22+
func (o *TransactionOptions) IsReadOnly() bool {
23+
return o.readOnly
24+
}
25+
26+
func (o *TransactionOptions) IsolationLevel() IsolationLevel {
27+
return o.isolationLevel
28+
}
29+
30+
func (o *TransactionOptions) Merge(options *TransactionOptions) *TransactionOptions {
31+
copyOptions := new(TransactionOptions)
32+
*copyOptions = *o
33+
34+
copyOptions.propagation = options.propagation
35+
copyOptions.timeout = options.timeout
36+
copyOptions.readOnly = options.readOnly
37+
copyOptions.isolationLevel = options.isolationLevel
38+
39+
return copyOptions
40+
}
41+
42+
func (o *TransactionOptions) Override(options ...TransactionOption) *TransactionOptions {
43+
copyOptions := new(TransactionOptions)
44+
*copyOptions = *o
45+
46+
for _, option := range options {
47+
option(copyOptions)
48+
}
49+
50+
return copyOptions
51+
}
52+
53+
func DefaultTransactionOptions() *TransactionOptions {
54+
return &TransactionOptions{
55+
propagation: PropagationRequired,
56+
timeout: -1,
57+
readOnly: false,
58+
isolationLevel: LevelDefault,
59+
}
60+
}
61+
62+
func NewTransactionOptions(opts ...TransactionOption) *TransactionOptions {
63+
return DefaultTransactionOptions().Override(opts...)
64+
}
65+
66+
func WithPropagation(propagation Propagation) TransactionOption {
67+
return func(options *TransactionOptions) {
68+
options.propagation = propagation
69+
}
70+
}
71+
72+
func WithTimeout(timeout time.Duration) TransactionOption {
73+
return func(options *TransactionOptions) {
74+
options.timeout = timeout
75+
}
76+
}
77+
78+
func WithReadOnly(readOnly bool) TransactionOption {
79+
return func(options *TransactionOptions) {
80+
options.readOnly = readOnly
81+
}
82+
}
83+
84+
func WithIsolation(isolationLevel IsolationLevel) TransactionOption {
85+
return func(options *TransactionOptions) {
86+
options.isolationLevel = isolationLevel
87+
}
88+
}

sql/propagation.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package sql
2+
3+
type Propagation int
4+
5+
const (
6+
PropagationRequired Propagation = iota
7+
PropagationSupports
8+
PropagationMandatory
9+
PropagationNever
10+
PropagationNotSupported
11+
PropagationNested
12+
PropagationRequiredNew
13+
)

sql/register.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package sql
2+
3+
import (
4+
"database/sql"
5+
"database/sql/driver"
6+
)
7+
8+
func Register(name string, driver driver.Driver) {
9+
sql.Register(name, driver)
10+
}

sql/result.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package sql
2+
3+
type Result interface {
4+
LastInsertId() (int64, error)
5+
RowsAffected() (int64, error)
6+
}

0 commit comments

Comments
 (0)