Skip to content

Commit

Permalink
Fix staticcheck and deadcode linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Jan 7, 2025
1 parent fd9a2cf commit af929d4
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 114 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ debug:
devbox run "cd src && dlv test github.com/BemiHQ/BemiDB"

lint:
devbox run "cd src && go fmt"
devbox run "cd src && go fmt && deadcode . && staticcheck ."

outdated:
devbox run "cd src && go list -u -m -f '{{if and .Update (not .Indirect)}}{{.}}{{end}}' all"
Expand Down
9 changes: 0 additions & 9 deletions src/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ func NewOrderedMap(keyVals [][]string) *OrderedMap {
return orderedMap
}

func (orderedMap *OrderedMap) Get(key string) string {
return orderedMap.valueByKey[key]
}

func (orderedMap *OrderedMap) HasKey(key string) bool {
_, ok := orderedMap.valueByKey[key]
return ok
}

func (orderedMap *OrderedMap) Set(key string, value string) {
if _, ok := orderedMap.valueByKey[key]; !ok {
orderedMap.orderedKeys = append(orderedMap.orderedKeys, key)
Expand Down
2 changes: 1 addition & 1 deletion src/parser_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (utils *ParserUtils) MakeSubselectWithoutRowsNode(tableName string, columns
}

targetList := make([]*pgQuery.Node, len(columns))
for i, _ := range columns {
for i := range columns {
targetList[i] = pgQuery.MakeResTargetNodeWithVal(
utils.MakeAConstBoolNode(false),
0,
Expand Down
21 changes: 9 additions & 12 deletions src/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ func (postgres *Postgres) Run(queryHandler *QueryHandler) {
return // Terminate connection
}

switch message.(type) {
switch message := message.(type) {
case *pgproto3.Query:
postgres.handleSimpleQuery(queryHandler, message.(*pgproto3.Query))
postgres.handleSimpleQuery(queryHandler, message)
case *pgproto3.Parse:
err = postgres.handleExtendedQuery(queryHandler, message.(*pgproto3.Parse))
err = postgres.handleExtendedQuery(queryHandler, message)
if err != nil {
return // Terminate connection
}
Expand Down Expand Up @@ -116,9 +116,8 @@ func (postgres *Postgres) handleExtendedQuery(queryHandler *QueryHandler, parseM
return err
}

switch message.(type) {
switch message := message.(type) {
case *pgproto3.Bind:
message := message.(*pgproto3.Bind)
LogDebug(postgres.config, "Binding query", message.PreparedStatement)
messages, preparedStatement, err = queryHandler.HandleBindQuery(message, preparedStatement)
if err != nil {
Expand All @@ -127,7 +126,6 @@ func (postgres *Postgres) handleExtendedQuery(queryHandler *QueryHandler, parseM
}
postgres.writeMessages(messages...)
case *pgproto3.Describe:
message := message.(*pgproto3.Describe)
LogDebug(postgres.config, "Describing query", message.Name, "("+string(message.ObjectType)+")")
var messages []pgproto3.Message
messages, preparedStatement, err = queryHandler.HandleDescribeQuery(message, preparedStatement)
Expand All @@ -137,7 +135,6 @@ func (postgres *Postgres) handleExtendedQuery(queryHandler *QueryHandler, parseM
}
postgres.writeMessages(messages...)
case *pgproto3.Execute:
message := message.(*pgproto3.Execute)
LogDebug(postgres.config, "Executing query", message.Portal)
messages, err := queryHandler.HandleExecuteQuery(message, preparedStatement)
if err != nil {
Expand Down Expand Up @@ -179,19 +176,19 @@ func (postgres *Postgres) handleStartup() error {
return err
}

switch startupMessage.(type) {
switch startupMessage := startupMessage.(type) {
case *pgproto3.StartupMessage:
params := startupMessage.(*pgproto3.StartupMessage).Parameters
params := startupMessage.Parameters
LogDebug(postgres.config, "BemiDB: startup message", params)

if params["database"] != postgres.config.Database {
postgres.writeError("database " + params["database"] + " does not exist")
return errors.New("Database does not exist")
return errors.New("database does not exist")
}

if postgres.config.User != "" && params["user"] != postgres.config.User && params["user"] != SYSTEM_AUTH_USER {
postgres.writeError("role \"" + params["user"] + "\" does not exist")
return errors.New("Role does not exist")
return errors.New("role does not exist")
}

postgres.writeMessages(
Expand All @@ -209,6 +206,6 @@ func (postgres *Postgres) handleStartup() error {
postgres.handleStartup()
return nil
default:
return errors.New("Unknown startup message")
return errors.New("unknown startup message")
}
}
8 changes: 4 additions & 4 deletions src/query_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (queryHandler *QueryHandler) HandleParseQuery(message *pgproto3.Parse) ([]p
func (queryHandler *QueryHandler) HandleBindQuery(message *pgproto3.Bind, preparedStatement *PreparedStatement) ([]pgproto3.Message, *PreparedStatement, error) {
if message.PreparedStatement != preparedStatement.Name {
LogError(queryHandler.config, "Prepared statement mismatch:", message.PreparedStatement, "instead of", preparedStatement.Name)
return nil, nil, errors.New("Prepared statement mismatch")
return nil, nil, errors.New("prepared statement mismatch")
}

var variables []interface{}
Expand Down Expand Up @@ -274,12 +274,12 @@ func (queryHandler *QueryHandler) HandleDescribeQuery(message *pgproto3.Describe
case 'S': // Statement
if message.Name != preparedStatement.Query {
LogError(queryHandler.config, "Statement mismatch:", message.Name, "instead of", preparedStatement.Query)
return nil, nil, errors.New("Statement mismatch")
return nil, nil, errors.New("statement mismatch")
}
case 'P': // Portal
if message.Name != preparedStatement.Portal {
LogError(queryHandler.config, "Portal mismatch:", message.Name, "instead of", preparedStatement.Portal)
return nil, nil, errors.New("Portal mismatch")
return nil, nil, errors.New("portal mismatch")
}
}

Expand All @@ -300,7 +300,7 @@ func (queryHandler *QueryHandler) HandleDescribeQuery(message *pgproto3.Describe
func (queryHandler *QueryHandler) HandleExecuteQuery(message *pgproto3.Execute, preparedStatement *PreparedStatement) ([]pgproto3.Message, error) {
if message.Portal != preparedStatement.Portal {
LogError(queryHandler.config, "Portal mismatch:", message.Portal, "instead of", preparedStatement.Portal)
return nil, errors.New("Portal mismatch")
return nil, errors.New("portal mismatch")
}

if preparedStatement.Rows == nil {
Expand Down
19 changes: 12 additions & 7 deletions src/query_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/binary"
"reflect"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -975,12 +976,12 @@ func TestHandleDescribeQuery(t *testing.T) {
queryHandler := initQueryHandler()
query := "SELECT usename, passwd FROM pg_shadow WHERE usename=$1"
parseMessage := &pgproto3.Parse{Query: query}
messages, preparedStatement, err := queryHandler.HandleParseQuery(parseMessage)
_, preparedStatement, _ := queryHandler.HandleParseQuery(parseMessage)
bindMessage := &pgproto3.Bind{Parameters: [][]byte{[]byte("bemidb")}}
messages, preparedStatement, err = queryHandler.HandleBindQuery(bindMessage, preparedStatement)
_, preparedStatement, _ = queryHandler.HandleBindQuery(bindMessage, preparedStatement)
message := &pgproto3.Describe{ObjectType: 'P'}

messages, preparedStatement, err = queryHandler.HandleDescribeQuery(message, preparedStatement)
messages, preparedStatement, err := queryHandler.HandleDescribeQuery(message, preparedStatement)

testNoError(t, err)
testMessageTypes(t, messages, []pgproto3.Message{
Expand All @@ -998,14 +999,14 @@ func TestHandleExecuteQuery(t *testing.T) {
queryHandler := initQueryHandler()
query := "SELECT usename, passwd FROM pg_shadow WHERE usename=$1"
parseMessage := &pgproto3.Parse{Query: query}
messages, preparedStatement, err := queryHandler.HandleParseQuery(parseMessage)
_, preparedStatement, _ := queryHandler.HandleParseQuery(parseMessage)
bindMessage := &pgproto3.Bind{Parameters: [][]byte{[]byte("bemidb")}}
messages, preparedStatement, err = queryHandler.HandleBindQuery(bindMessage, preparedStatement)
_, preparedStatement, _ = queryHandler.HandleBindQuery(bindMessage, preparedStatement)
describeMessage := &pgproto3.Describe{ObjectType: 'P'}
messages, preparedStatement, err = queryHandler.HandleDescribeQuery(describeMessage, preparedStatement)
_, preparedStatement, _ = queryHandler.HandleDescribeQuery(describeMessage, preparedStatement)
message := &pgproto3.Execute{}

messages, err = queryHandler.HandleExecuteQuery(message, preparedStatement)
messages, err := queryHandler.HandleExecuteQuery(message, preparedStatement)

testNoError(t, err)
testMessageTypes(t, messages, []pgproto3.Message{
Expand Down Expand Up @@ -1140,3 +1141,7 @@ func testDataRowValues(t *testing.T, dataRowMessage pgproto3.Message, expectedVa
}
}
}

func Uint32ToString(i uint32) string {
return strconv.FormatUint(uint64(i), 10)
}
22 changes: 11 additions & 11 deletions src/query_remapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ func (remapper *QueryRemapper) remapSelectStatement(selectStatement *pgQuery.Sel
if selectStatement.FromClause == nil && selectStatement.Larg != nil && selectStatement.Rarg != nil {
remapper.traceTreeTraversal("UNION left", indentLevel)
leftSelectStatement := selectStatement.Larg
leftSelectStatement = remapper.remapSelectStatement(leftSelectStatement, indentLevel+1) // self-recursion
remapper.remapSelectStatement(leftSelectStatement, indentLevel+1) // self-recursion

remapper.traceTreeTraversal("UNION right", indentLevel)
rightSelectStatement := selectStatement.Rarg
rightSelectStatement = remapper.remapSelectStatement(rightSelectStatement, indentLevel+1) // self-recursion
remapper.remapSelectStatement(rightSelectStatement, indentLevel+1) // self-recursion
}

// JOIN
Expand Down Expand Up @@ -167,7 +167,7 @@ func (remapper *QueryRemapper) remapSelectStatement(selectStatement *pgQuery.Sel
// FROM (SELECT ...)
remapper.traceTreeTraversal("FROM subselect", indentLevel)
subSelectStatement := fromNode.GetRangeSubselect().Subquery.GetSelectStmt()
subSelectStatement = remapper.remapSelectStatement(subSelectStatement, indentLevel+1) // self-recursion
remapper.remapSelectStatement(subSelectStatement, indentLevel+1) // self-recursion
} else if fromNode.GetRangeFunction() != nil {
// FROM PG_FUNCTION()
selectStatement.FromClause[i] = remapper.remapTableFunction(fromNode, indentLevel) // recursive
Expand Down Expand Up @@ -201,12 +201,12 @@ func (remapper *QueryRemapper) remapCaseExpressions(selectStatement *pgQuery.Sel
if subLink := aExpr.Lexpr.GetSubLink(); subLink != nil {
remapper.traceTreeTraversal("CASE WHEN left", indentLevel+1)
subSelect := subLink.Subselect.GetSelectStmt()
subSelect = remapper.remapSelectStatement(subSelect, indentLevel+1)
remapper.remapSelectStatement(subSelect, indentLevel+1)
}
if subLink := aExpr.Rexpr.GetSubLink(); subLink != nil {
remapper.traceTreeTraversal("CASE WHEN right", indentLevel+1)
subSelect := subLink.Subselect.GetSelectStmt()
subSelect = remapper.remapSelectStatement(subSelect, indentLevel+1)
remapper.remapSelectStatement(subSelect, indentLevel+1)
}
}
}
Expand All @@ -215,7 +215,7 @@ func (remapper *QueryRemapper) remapCaseExpressions(selectStatement *pgQuery.Sel
if subLink := whenClause.Result.GetSubLink(); subLink != nil {
remapper.traceTreeTraversal("CASE THEN", indentLevel+1)
subSelect := subLink.Subselect.GetSelectStmt()
subSelect = remapper.remapSelectStatement(subSelect, indentLevel+1)
remapper.remapSelectStatement(subSelect, indentLevel+1)
}
if funcCall := whenClause.Result.GetFuncCall(); funcCall != nil {
remapper.traceTreeTraversal("CASE THEN function", indentLevel+1)
Expand All @@ -229,7 +229,7 @@ func (remapper *QueryRemapper) remapCaseExpressions(selectStatement *pgQuery.Sel
if subLink := caseExpr.Defresult.GetSubLink(); subLink != nil {
remapper.traceTreeTraversal("CASE ELSE", indentLevel+1)
subSelect := subLink.Subselect.GetSelectStmt()
subSelect = remapper.remapSelectStatement(subSelect, indentLevel+1)
remapper.remapSelectStatement(subSelect, indentLevel+1)
}
if funcCall := caseExpr.Defresult.GetFuncCall(); funcCall != nil {
remapper.traceTreeTraversal("CASE ELSE function", indentLevel+1)
Expand Down Expand Up @@ -275,7 +275,7 @@ func (remapper *QueryRemapper) remapTableFunction(fromNode *pgQuery.Node, indent
if funcCallNode == nil {
continue
}
funcCallNode = remapper.remapTableFunctionArgs(funcCallNode, indentLevel+1) // recursive
remapper.remapTableFunctionArgs(funcCallNode, indentLevel+1) // recursive
}
}

Expand Down Expand Up @@ -373,7 +373,7 @@ func (remapper *QueryRemapper) remapJoinExpressions(selectStatement *pgQuery.Sel
leftJoinNode = remapper.remapperTable.RemapTable(leftJoinNode)
} else if leftJoinNode.GetRangeSubselect() != nil {
leftSelectStatement := leftJoinNode.GetRangeSubselect().Subquery.GetSelectStmt()
leftSelectStatement = remapper.remapSelectStatement(leftSelectStatement, indentLevel+1) // parent-recursion
remapper.remapSelectStatement(leftSelectStatement, indentLevel+1) // parent-recursion
}
node.GetJoinExpr().Larg = leftJoinNode

Expand All @@ -385,13 +385,13 @@ func (remapper *QueryRemapper) remapJoinExpressions(selectStatement *pgQuery.Sel
// WHERE
remapper.traceTreeTraversal("WHERE right", indentLevel+1)
qSchemaTable := remapper.parserTable.NodeToQuerySchemaTable(rightJoinNode)
selectStatement = remapper.remapperTable.RemapWhereClauseForTable(qSchemaTable, selectStatement)
remapper.remapperTable.RemapWhereClauseForTable(qSchemaTable, selectStatement)
// TABLE
remapper.traceTreeTraversal("TABLE right", indentLevel+1)
rightJoinNode = remapper.remapperTable.RemapTable(rightJoinNode)
} else if rightJoinNode.GetRangeSubselect() != nil {
rightSelectStatement := rightJoinNode.GetRangeSubselect().Subquery.GetSelectStmt()
rightSelectStatement = remapper.remapSelectStatement(rightSelectStatement, indentLevel+1) // parent-recursion
remapper.remapSelectStatement(rightSelectStatement, indentLevel+1) // parent-recursion
}
node.GetJoinExpr().Rarg = rightJoinNode

Expand Down
4 changes: 2 additions & 2 deletions src/query_remapper_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (remapper *QueryRemapperSelect) RemapSelect(targetNode *pgQuery.Node) *pgQu
remapper.parserSelect.SetDefaultTargetName(targetNode, schemaFunction.Function)
}

functionCall = remapper.remapNestedFunctionCalls(functionCall) // recursive
remapper.remapNestedFunctionCalls(functionCall) // recursive

return targetNode
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (remapper *QueryRemapperSelect) remapNestedFunctionCalls(functionCall *pgQu
remapper.parserFunction.OverrideFunctionCallArg(functionCall, i, constantNode)
}

nestedFunctionCall = remapper.remapNestedFunctionCalls(nestedFunctionCall)
remapper.remapNestedFunctionCalls(nestedFunctionCall)
}

return functionCall
Expand Down
Loading

0 comments on commit af929d4

Please sign in to comment.