Skip to content

Commit

Permalink
chore: staticcheck cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
proullon committed Jan 9, 2024
1 parent aac44c7 commit a212ff6
Show file tree
Hide file tree
Showing 17 changed files with 81 additions and 178 deletions.
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func prettyPrintHeader(row []string) {
fmt.Println()
for i, r := range row {
if i != 0 {
line += fmt.Sprintf(" | ")
line += " | "
}
line += fmt.Sprintf("%-6s", r)
}
Expand Down
2 changes: 1 addition & 1 deletion driver/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *Conn) Prepare(query string) (driver.Stmt, error) {
// Implemented for Conn interface
func (c *Conn) Close() error {
if c.tx != nil {
c.tx.Rollback()
_ = c.tx.Rollback()
c.tx = nil
}

Expand Down
11 changes: 5 additions & 6 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ func TestInsertTable(t *testing.T) {
t.Fatalf("sql.Exec: Error: %s\n", err)
}

res, err := db.Exec("INSERT INTO account ('id', 'email') VALUES (1, 'foo@bar.com')")
_, err = db.Exec("INSERT INTO account ('id', 'email') VALUES (1, 'foo@bar.com')")
if err != nil {
t.Fatalf("Cannot insert into table account: %s", err)
}

res, err = db.Exec("INSERT INTO account ('id', 'email') VALUES (2, 'roger@gmail.com')")
res, err := db.Exec("INSERT INTO account ('id', 'email') VALUES (2, 'roger@gmail.com')")
if err != nil {
t.Fatalf("Cannot insert into table account: %s", err)
}
Expand Down Expand Up @@ -1298,7 +1298,7 @@ func TestFloat(t *testing.T) {
t.Fatalf("Cannot run UPDATE query with AND: %s\n", err)
}

db.QueryRow(`SELECT age FROM user WHERE surname = 'Demo'`).Scan(&age)
err = db.QueryRow(`SELECT age FROM user WHERE surname = 'Demo'`).Scan(&age)
if err != nil {
t.Fatalf("Cannot load negative age: %s", err)
}
Expand Down Expand Up @@ -1513,8 +1513,7 @@ func TestInsertSingle(t *testing.T) {

var breed string
var name string
var funny bool
funny = true
var funny bool = true

err = row.Scan(&breed, &name, &funny)
if err != nil {
Expand All @@ -1529,7 +1528,7 @@ func TestInsertSingle(t *testing.T) {
t.Fatalf("Expected funny=false, got true")
}

result, err = db.Exec("INSERT INTO cat (breed, name, funny) VALUES ('indeterminate', 'Kuhura', true)")
_, err = db.Exec("INSERT INTO cat (breed, name, funny) VALUES ('indeterminate', 'Kuhura', true)")
if err != nil {
t.Fatalf("Cannot insert into table account: %s", err)
}
Expand Down
9 changes: 0 additions & 9 deletions driver/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@ type Result struct {
rowsAffected int64
}

func newResult(lastInsertedID int64, rowsAffected int64) *Result {
r := &Result{
lastInsertedID: lastInsertedID,
rowsAffected: rowsAffected,
}

return r
}

// LastInsertId returns the database's auto-generated ID
// after, for example, an INSERT into a table with primary
// key.
Expand Down
68 changes: 0 additions & 68 deletions driver/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@ package ramsql

import (
"database/sql/driver"
"errors"
"fmt"
"io"

"github.com/proullon/ramsql/engine/agnostic"
"github.com/proullon/ramsql/engine/executor"
)

// Rows implements the sql/driver Rows interface
type Rows struct {
columns []string
tuples []*agnostic.Tuple
tx *executor.Tx
idx int
end int
}
Expand All @@ -40,21 +37,6 @@ func (r *Rows) Columns() []string {

// Close closes the rows iterator.
func (r *Rows) Close() error {

/*
if r.rowsChannel == nil {
return nil
}
_, ok := <-r.rowsChannel
if !ok {
return nil
}
// Tels UnlimitedRowsChannel to close itself
//r.rowsChannel <- []string{}
r.rowsChannel = nil
*/
return nil
}

Expand Down Expand Up @@ -85,54 +67,4 @@ func (r *Rows) Next(dest []driver.Value) (err error) {
}

return nil
/*
value, ok := <-r.rowsChannel
if !ok {
r.rowsChannel = nil
return io.EOF
}
if len(dest) < len(value) {
return fmt.Errorf("slice too short (%d slots for %d values)", len(dest), len(value))
}
for i, v := range value {
if v == "<nil>" {
dest[i] = nil
continue
}
switch v.(type) {
case string:
val, _ := v.(string)
// TODO: make rowsChannel send virtualRows,
// so we have the type and don't blindy try to parse date here
if t, err := parser.ParseDate(val); err == nil {
dest[i] = *t
} else {
dest[i] = []byte(val)
}
default:
dest[i] = v
}
}
return nil
*/
}

func (r *Rows) setColumns(columns []string) {
r.columns = columns
}

func assignValue(s string, v driver.Value) error {
dest, ok := v.(*string)
if !ok {
err := errors.New("cannot assign value")
return err
}

*dest = s
return nil
}
6 changes: 3 additions & 3 deletions driver/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ func TestCheckAttributes(t *testing.T) {
}

query = `SELECT * FROM account WHERE nonexisting_attribute = 2`
rows, err := db.Query(query)
_, err = db.Query(query)
if err == nil {
t.Errorf("expected an error trying to make a comparison with a non existing attribute")
}

query = `SELECT id, nonexisting_attribute FROM account WHERE id = 2`
rows, err = db.Query(query)
rows, err := db.Query(query)
if err == nil {
t.Errorf("expected an error trying to select a non existin attribute")
t.Errorf("expected an error trying to select a non existing attribute")
}
_ = rows
}
3 changes: 0 additions & 3 deletions engine/agnostic/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ func ToInstance(value, typeName string) (any, error) {
}
return value, nil
}

return nil, fmt.Errorf("cannot convert %v to instance of type %s", value, typeName)
}

func parseDate(data string) (time.Time, error) {
Expand Down Expand Up @@ -208,7 +206,6 @@ func parseDate(data string) (time.Time, error) {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

func NewRandString(n int) Defaulter {
rand.Seed(time.Now().UnixNano())

f := func() any {
sb := strings.Builder{}
Expand Down
17 changes: 4 additions & 13 deletions engine/agnostic/predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,6 @@ func (s *GroupBySorter) Exec() ([]string, []*list.Element, error) {
return nil, nil, err
}

var idxs []int
for _, a := range s.attrs {
for i, c := range cols {
if c == a || c == s.rel+"."+a {
idxs = append(idxs, i)
}
}
}

return cols, res, nil
}

Expand Down Expand Up @@ -705,7 +696,7 @@ func (s *StarSelector) Select(cols []string, in []*list.Element) (out []*Tuple,

// if only 1 relation, can return directly
for i, c := range cols {
if strings.Contains(c, ".") == false {
if !strings.Contains(c, ".") {
out = make([]*Tuple, len(in))
for i, e := range in {
t, ok := e.Value.(*Tuple)
Expand Down Expand Up @@ -1203,9 +1194,9 @@ func (sn *SelectorNode) Exec() ([]string, []*list.Element, error) {
t := &Tuple{values: make([]any, l)}
var tidx int
// for each selector returned Tuple
for x, _ := range outs {
for x := range outs {
// concatenate values to unified tuple
for y, _ := range outs[x][i].values {
for y := range outs[x][i].values {
t.values[tidx] = outs[x][i].values[y]
tidx++
}
Expand Down Expand Up @@ -2040,7 +2031,7 @@ func NewUpdaterNode(relation *Relation, changes *list.List, values map[string]an
indexes: relation.indexes,
}

for k, _ := range values {
for k := range values {
u.attrs = append(u.attrs, strings.ToLower(k))
}
return u
Expand Down
6 changes: 1 addition & 5 deletions engine/agnostic/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ func (s SeqScanSrc) String() string {
}

func (s *SeqScanSrc) HasNext() bool {
if s.e != nil {
return true
}

return false
return s.e != nil
}

func (s *SeqScanSrc) Next() *list.Element {
Expand Down
26 changes: 7 additions & 19 deletions engine/agnostic/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@ func (t *Transaction) Rollback() {
case ValueChange:
c := b.Value.(ValueChange)
t.rollbackValueChange(c)
break
case RelationChange:
c := b.Value.(RelationChange)
t.rollbackRelationChange(c)
break
}
t.changes.Remove(b)
}
Expand Down Expand Up @@ -130,11 +128,7 @@ func (t *Transaction) CheckRelation(schemaName, relName string) bool {
}

_, err = s.Relation(relName)
if err != nil {
return false
}

return true
return err == nil
}

func (t *Transaction) CreateRelation(schemaName, relName string, attributes []Attribute, pk []string) error {
Expand Down Expand Up @@ -185,11 +179,7 @@ func (t *Transaction) CheckSchema(schemaName string) bool {
}

_, err := t.e.schema(schemaName)
if err != nil {
return false
}

return true
return err == nil
}

func (t *Transaction) CreateSchema(schemaName string) error {
Expand Down Expand Up @@ -442,7 +432,7 @@ func (t *Transaction) Insert(schema, relation string, values map[string]any) (*T
}

// if values map is not empty, then an non existing attribute was specified
for k, _ := range values {
for k := range values {
return nil, t.abort(fmt.Errorf("attribute %s does not exist in relation %s", k, relation))
}

Expand Down Expand Up @@ -641,7 +631,7 @@ func (t *Transaction) Plan(schema string, selectors []Selector, p Predicate, joi

// append sorters
// GroupBy must contains both selector node and last join to compute arithmetic on all groups
if sorters != nil && len(sorters) > 0 {
if len(sorters) > 0 {
sort.Sort(Sorters(sorters))
var src Node
for i, s := range sorters {
Expand All @@ -651,11 +641,10 @@ func (t *Transaction) Plan(schema string, selectors []Selector, p Predicate, joi
src = sorters[i-1]
}

switch s.(type) {
switch s := s.(type) {
case *GroupBySorter:
gb, _ := s.(*GroupBySorter)
gb.SetNode(src)
gb.SetSelector(n)
s.SetNode(src)
s.SetSelector(n)
default:
s.SetNode(src)
}
Expand Down Expand Up @@ -757,7 +746,6 @@ func PrintQueryPlan(n Node, depth int, printer func(fmt string, varargs ...any))

if printer == nil {
printer = log.Debug
return
}

indent := ""
Expand Down
Loading

0 comments on commit a212ff6

Please sign in to comment.