Skip to content

Commit 8d718ce

Browse files
committed
Move gdbc package from codnect.io/procyon to codnect.io/procyon-data
1 parent 353a91a commit 8d718ce

5 files changed

Lines changed: 258 additions & 0 deletions

File tree

gdbc/connection.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package gdbc
2+
3+
import (
4+
"codnect.io/procyon-data/sql"
5+
"context"
6+
dsql "database/sql"
7+
"database/sql/driver"
8+
"errors"
9+
"time"
10+
)
11+
12+
var (
13+
ErrConnectionDoesNotExist = errors.New("this connection has been closed")
14+
)
15+
16+
type Connection struct {
17+
db *dsql.DB
18+
closed bool
19+
}
20+
21+
func (c *Connection) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
22+
if c.closed {
23+
return nil, ErrConnectionDoesNotExist
24+
}
25+
26+
return c.db.ExecContext(ctx, query, args...)
27+
}
28+
29+
func (c *Connection) PrepareContext(ctx context.Context, query string, args ...any) (sql.Statement, error) {
30+
if c.closed {
31+
return nil, ErrConnectionDoesNotExist
32+
}
33+
34+
statement, err := c.db.PrepareContext(ctx, query)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
preparedStatement := sql.NewPreparedStatement(statement)
40+
return preparedStatement, nil
41+
}
42+
43+
func (c *Connection) QueryContext(ctx context.Context, query string, args ...any) (sql.RowSet, error) {
44+
if c.closed {
45+
return nil, ErrConnectionDoesNotExist
46+
}
47+
48+
return c.db.QueryContext(ctx, query, args...)
49+
}
50+
51+
func (c *Connection) QueryRowContext(ctx context.Context, query string, args ...any) (sql.Row, error) {
52+
if c.closed {
53+
return nil, ErrConnectionDoesNotExist
54+
}
55+
56+
return c.db.QueryRowContext(ctx, query, args...), nil
57+
}
58+
59+
func (c *Connection) PingContext(ctx context.Context) error {
60+
if c.closed {
61+
return ErrConnectionDoesNotExist
62+
}
63+
64+
return c.db.PingContext(ctx)
65+
}
66+
67+
func (c *Connection) SetConnMaxLifetime(d time.Duration) error {
68+
if c.closed {
69+
return ErrConnectionDoesNotExist
70+
}
71+
72+
c.db.SetConnMaxLifetime(d)
73+
return nil
74+
}
75+
76+
func (c *Connection) SetConnMaxIdleTime(d time.Duration) error {
77+
if c.closed {
78+
return ErrConnectionDoesNotExist
79+
}
80+
81+
c.db.SetConnMaxIdleTime(d)
82+
return nil
83+
}
84+
85+
func (c *Connection) SetMaxIdleConns(n int) error {
86+
if c.closed {
87+
return ErrConnectionDoesNotExist
88+
}
89+
90+
c.db.SetMaxIdleConns(n)
91+
return nil
92+
}
93+
94+
func (c *Connection) SetMaxOpenConns(n int) error {
95+
if c.closed {
96+
return ErrConnectionDoesNotExist
97+
}
98+
99+
c.db.SetMaxOpenConns(n)
100+
return nil
101+
}
102+
103+
func (c *Connection) BeginTransaction(ctx context.Context) (sql.TransactionStatus, error) {
104+
if c.closed {
105+
return nil, ErrConnectionDoesNotExist
106+
}
107+
108+
return nil, nil
109+
}
110+
111+
func (c *Connection) Driver() driver.Driver {
112+
return c.db.Driver()
113+
}
114+
115+
func (c *Connection) Close() error {
116+
if c.closed {
117+
return ErrConnectionDoesNotExist
118+
}
119+
120+
err := c.db.Close()
121+
if err != nil {
122+
return err
123+
}
124+
125+
c.closed = true
126+
return nil
127+
}
128+
129+
func (c *Connection) IsClosed() bool {
130+
return c.closed
131+
}

gdbc/datasource.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package gdbc
2+
3+
import (
4+
"codnect.io/procyon-data/sql"
5+
dsql "database/sql"
6+
"errors"
7+
"strings"
8+
)
9+
10+
type DataSource struct {
11+
props DataSourceProperties
12+
}
13+
14+
type DataSourceProperties struct {
15+
Url string
16+
Username string
17+
Password string
18+
}
19+
20+
func NewDataSource(props DataSourceProperties) *DataSource {
21+
return &DataSource{
22+
props: props,
23+
}
24+
}
25+
26+
func (ds *DataSource) GetConnection() (sql.Connection, error) {
27+
var (
28+
db *dsql.DB
29+
driverName string
30+
connectionUrl string
31+
err error
32+
)
33+
34+
driverName, connectionUrl, err = ds.parseDataSourceUrl()
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
db, err = dsql.Open(driverName, connectionUrl)
40+
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
return NewConnection(db), nil
46+
}
47+
48+
func (ds *DataSource) parseDataSourceUrl() (string, string, error) {
49+
if len(ds.props.Url) == 0 {
50+
return "", "", errors.New("url must not be empty")
51+
}
52+
53+
src := strings.Split(ds.props.Url, ":")
54+
if len(src) < 3 {
55+
return "", "", errors.New("url format is wrong : " + ds.props.Url)
56+
}
57+
58+
scheme := src[0]
59+
if "gdbc" != scheme {
60+
return "", "", errors.New("driver name must not be empty")
61+
}
62+
63+
driverName := src[1]
64+
if len(driverName) == 0 {
65+
return "", "", errors.New("driver name must not be empty")
66+
}
67+
68+
return driverName, strings.Join(append(src[:1], src[2:]...), ":"), nil
69+
}

gdbc/gdbc.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package gdbc
2+
3+
import "database/sql"
4+
5+
func NewConnection(db *sql.DB) *Connection {
6+
if db == nil {
7+
panic("nil db")
8+
}
9+
10+
return &Connection{
11+
db: db,
12+
}
13+
}

gdbc/operations.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package gdbc
2+
3+
import (
4+
"codnect.io/procyon-data/sql"
5+
"context"
6+
)
7+
8+
type Operations interface {
9+
Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
10+
Prepare(ctx context.Context, query string) (sql.Statement, error)
11+
Query(ctx context.Context, query string, args ...any) (sql.RowSet, error)
12+
QueryRow(ctx context.Context, query string, args ...any) sql.Row
13+
}

gdbc/template.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package gdbc
2+
3+
import (
4+
"codnect.io/procyon-data/sql"
5+
"context"
6+
)
7+
8+
type Template struct {
9+
dataSource sql.DataSource
10+
}
11+
12+
func NewTemplate(dataSource sql.DataSource) *Template {
13+
return &Template{
14+
dataSource: dataSource,
15+
}
16+
}
17+
18+
func (t *Template) Exec(ctx context.Context, query string, args ...any) (sql.Result, error) {
19+
return nil, nil
20+
}
21+
22+
func (t *Template) Prepare(ctx context.Context, query string) (sql.Statement, error) {
23+
return nil, nil
24+
}
25+
26+
func (t *Template) Query(ctx context.Context, query string, args ...any) (sql.RowSet, error) {
27+
return nil, nil
28+
}
29+
30+
func (t *Template) QueryRow(ctx context.Context, query string, args ...any) sql.Row {
31+
return nil
32+
}

0 commit comments

Comments
 (0)