-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A lot of golang drivers for different SQL libraries expose a standard `sql.DB` interface. Our backend connector code for ClickHouse, Postgres, MySQL can be unified around this interface. This reduces the code duplication by moving the common code to a new `basic_sql_backend_connector` struct.
- Loading branch information
1 parent
6e6652a
commit b37c655
Showing
6 changed files
with
112 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright Quesma, licensed under the Elastic License 2.0. | ||
// SPDX-License-Identifier: Elastic-2.0 | ||
|
||
package backend_connectors | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
quesma_api "github.com/QuesmaOrg/quesma/quesma/v2/core" | ||
) | ||
|
||
type BasicSqlBackendConnector struct { | ||
connection *sql.DB | ||
} | ||
|
||
type SqlRows struct { | ||
rows *sql.Rows | ||
} | ||
|
||
func (p *SqlRows) Next() bool { | ||
return p.rows.Next() | ||
} | ||
|
||
func (p *SqlRows) Scan(dest ...interface{}) error { | ||
return p.rows.Scan(dest...) | ||
} | ||
|
||
func (p *SqlRows) Close() error { | ||
return p.rows.Close() | ||
} | ||
|
||
func (p *SqlRows) Err() error { | ||
return p.rows.Err() | ||
} | ||
|
||
func (p *BasicSqlBackendConnector) Open() error { | ||
conn, err := initDBConnection() | ||
if err != nil { | ||
return err | ||
} | ||
p.connection = conn | ||
return nil | ||
} | ||
|
||
func (p *BasicSqlBackendConnector) Close() error { | ||
if p.connection == nil { | ||
return nil | ||
} | ||
return p.connection.Close() | ||
} | ||
|
||
func (p *BasicSqlBackendConnector) Ping() error { | ||
return p.connection.Ping() | ||
} | ||
|
||
func (p *BasicSqlBackendConnector) Query(ctx context.Context, query string, args ...interface{}) (quesma_api.Rows, error) { | ||
rows, err := p.connection.QueryContext(ctx, query, args...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &SqlRows{rows: rows}, nil | ||
} | ||
|
||
func (p *BasicSqlBackendConnector) QueryRow(ctx context.Context, query string, args ...interface{}) quesma_api.Row { | ||
return p.connection.QueryRowContext(ctx, query, args...) | ||
} | ||
|
||
func (p *BasicSqlBackendConnector) Exec(ctx context.Context, query string, args ...interface{}) error { | ||
if len(args) == 0 { | ||
_, err := p.connection.ExecContext(ctx, query) | ||
return err | ||
} | ||
_, err := p.connection.ExecContext(ctx, query, args...) | ||
return err | ||
} | ||
|
||
func (p *BasicSqlBackendConnector) Stats() quesma_api.DBStats { | ||
stats := p.connection.Stats() | ||
return quesma_api.DBStats{ | ||
MaxOpenConnections: stats.MaxOpenConnections, | ||
OpenConnections: stats.OpenConnections, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.