Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(connector): added Connector interface #43

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions connector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package sqle

import (
"context"
"database/sql"
)

// Connector represents a database connector that provides methods for executing queries and commands.
// Context and Tx both implement this interface.
type Connector interface {
// Query executes a query that returns multiple rows.
// It takes a query string and optional arguments.
// It returns a pointer to a Rows object and an error, if any.
Query(query string, args ...any) (*Rows, error)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code_clarification): Consider documenting the behavior when args is nil or empty.

Clarifying how the function behaves with no arguments could improve the usability and understanding of the API.

Suggested change
Query(query string, args ...any) (*Rows, error)
// Query executes a query that returns multiple rows.
// It takes a query string and optional arguments. If no arguments are provided,
// the query is executed without any parameter substitution.
// It returns a pointer to a Rows object and an error, if any.
Query(query string, args ...any) (*Rows, error)


// QueryBuilder executes a query using a Builder object.
// It takes a context and a Builder object.
// It returns a pointer to a Rows object and an error, if any.
QueryBuilder(ctx context.Context, b *Builder) (*Rows, error)

// QueryContext executes a query that returns multiple rows using a context.
// It takes a context, a query string, and optional arguments.
// It returns a pointer to a Rows object and an error, if any.
QueryContext(ctx context.Context, query string, args ...any) (*Rows, error)

// QueryRow executes a query that returns a single row.
// It takes a query string and optional arguments.
// It returns a pointer to a Row object.
QueryRow(query string, args ...any) *Row
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code_clarification): Specify the return behavior when no rows are found.

It would be beneficial to define whether a nil Row or an error is returned when the query results in no rows, enhancing API predictability.

Suggested change
QueryRow(query string, args ...any) *Row
// QueryRow executes a query that returns a single row.
// It takes a query string and optional arguments.
// It returns a pointer to a Row object or nil if no rows are found.
QueryRow(query string, args ...any) *Row


// QueryRowBuilder executes a query that returns a single row using a Builder object.
// It takes a context and a Builder object.
// It returns a pointer to a Row object.
QueryRowBuilder(ctx context.Context, b *Builder) *Row

// QueryRowContext executes a query that returns a single row using a context.
// It takes a context, a query string, and optional arguments.
// It returns a pointer to a Row object.
QueryRowContext(ctx context.Context, query string, args ...any) *Row

// Exec executes a query that doesn't return any rows.
// It takes a query string and optional arguments.
// It returns a sql.Result object and an error, if any.
Exec(query string, args ...any) (sql.Result, error)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (code_clarification): Clarify the expected behavior of Exec with invalid SQL commands.

Documenting how Exec handles syntax errors or unauthorized commands can help developers understand error handling.

Suggested change
Exec(query string, args ...any) (sql.Result, error)
// Exec executes a query that doesn't return any rows.
// It takes a query string and optional arguments.
// It returns a sql.Result object and an error, if any.
// Errors can include syntax errors in the query or issues like unauthorized commands,
// which will be returned as part of the error object.
Exec(query string, args ...any) (sql.Result, error)


// ExecContext executes a query that doesn't return any rows using a context.
// It takes a context, a query string, and optional arguments.
// It returns a sql.Result object and an error, if any.
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

// ExecBuilder executes a query that doesn't return any rows using a Builder object.
// It takes a context and a Builder object.
// It returns a sql.Result object and an error, if any.
ExecBuilder(ctx context.Context, b *Builder) (sql.Result, error)
}
Loading