diff options
| author | Shulhan <ms@kilabit.info> | 2024-03-03 04:59:34 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2024-03-05 14:53:12 +0700 |
| commit | 2fa7605727e90ca323b7b24168632e485d74c583 (patch) | |
| tree | 3533df0fc07ef96b1564736926cc3310994be515 /lib/sql | |
| parent | b921ebfb3e81b367ff24305eb18c5dd073b38635 (diff) | |
| download | pakakeh.go-2fa7605727e90ca323b7b24168632e485d74c583.tar.xz | |
all: comply with linter recommendations #2
HTTP request now implicitly create request with context.
Any false positive related to not closing HTTP response body has been
annotated with "nolint:bodyclose".
In the example code, use consistent "// Output:" comment format, by
prefixing with single space.
Any comment on code now also prefixing with single space.
An error returned without variables now use [errors.New] instead of
[fmt.Errorf].
Any error returned using [fmt.Errorf] now wrapped using "%w" instead of
"%s".
Also, replace error checking using [errors.Is] or [errors.As], instead
of using equal/not-equal operator.
Any statement like "x = x OP y" now replaced with "x OP= y".
Also, swap statement is simplified using "x, y = y, x".
Any switch statement with single case now replaced with if-condition.
Any call to defer on function or program that call [os.Exit], now
replaced by calling the deferred function directly.
Any if-else condition now replaced with switch statement, if possible.
Diffstat (limited to 'lib/sql')
| -rw-r--r-- | lib/sql/client.go | 17 | ||||
| -rw-r--r-- | lib/sql/row.go | 2 |
2 files changed, 12 insertions, 7 deletions
diff --git a/lib/sql/client.go b/lib/sql/client.go index c281dab8..344ed76b 100644 --- a/lib/sql/client.go +++ b/lib/sql/client.go @@ -49,6 +49,7 @@ func NewClient(opts ClientOptions) (cl *Client, err error) { // FetchTableNames return the table names in current database schema sorted // in ascending order. func (cl *Client) FetchTableNames() (tableNames []string, err error) { + var logp = `FetchTableNames` var q, v string switch cl.DriverName { @@ -63,27 +64,31 @@ func (cl *Client) FetchTableNames() (tableNames []string, err error) { ` } - rows, err := cl.DB.Query(q) + var rows *sql.Rows + + rows, err = cl.DB.Query(q) if err != nil { - return nil, fmt.Errorf("FetchTableNames: " + err.Error()) + return nil, fmt.Errorf(`%s: %w`, logp, err) } if len(cl.TableNames) > 0 { cl.TableNames = cl.TableNames[:0] } + defer func() { + _ = rows.Close() + }() for rows.Next() { err = rows.Scan(&v) if err != nil { - _ = rows.Close() - return cl.TableNames, err + return cl.TableNames, fmt.Errorf(`%s: %w`, logp, err) } cl.TableNames = append(cl.TableNames, v) } err = rows.Err() if err != nil { - return nil, err + return nil, fmt.Errorf(`%s: %w`, logp, err) } return cl.TableNames, nil @@ -313,7 +318,7 @@ func (cl *Client) TruncateTable(tableName string) (err error) { _, err = cl.DB.Exec(q) if err != nil { - return fmt.Errorf("TruncateTable %q: %s", tableName, err) + return fmt.Errorf(`TruncateTable %q: %w`, tableName, err) } return nil } diff --git a/lib/sql/row.go b/lib/sql/row.go index 1fdbfb7b..693ee51e 100644 --- a/lib/sql/row.go +++ b/lib/sql/row.go @@ -14,7 +14,7 @@ import ( // the column's value. // This type can be used to create dynamic insert-update fields. // -// DEPRECATED: use [Meta] instead. +// Deprecated: use [Meta] instead. type Row map[string]interface{} // Meta convert the Row into Meta. |
