Added ability to set the connection's database in SetConnectionInfo function. Implemented SELECT command to allow TCP connections to select a different database.

This commit is contained in:
Kelvin Mwinuka
2024-06-24 09:06:07 +08:00
parent 21aabda04d
commit dc9b33bc15
3 changed files with 66 additions and 4 deletions

View File

@@ -119,14 +119,31 @@ func handleHello(params internal.HandlerFuncParams) ([]byte, error) {
}
// Set the connection details.
params.SetConnectionInfo(params.Connection, options.protocol, options.clientname)
connectionInfo := params.GetConnectionInfo(params.Connection)
params.SetConnectionInfo(params.Connection, options.clientname, options.protocol, connectionInfo.Database)
// Get the new connection details and server info to return to the client.
serverInfo := params.GetServerInfo()
connectionInfo := params.GetConnectionInfo(params.Connection)
connectionInfo = params.GetConnectionInfo(params.Connection)
return buildHelloResponse(serverInfo, connectionInfo), nil
}
func handleSelect(params internal.HandlerFuncParams) ([]byte, error) {
if len(params.Command) != 2 {
return nil, errors.New(constants.WrongArgsResponse)
}
database, err := strconv.Atoi(params.Command[1])
if err != nil {
return nil, err
}
connectionInfo := params.GetConnectionInfo(params.Connection)
params.SetConnectionInfo(params.Connection, connectionInfo.Name, connectionInfo.Protocol, database)
return []byte(constants.OkResponse), nil
}
func Commands() []internal.Command {
return []internal.Command{
{
@@ -193,5 +210,20 @@ Otherwise, the server will return "PONG".`,
},
HandlerFunc: handleHello,
},
{
Command: "select",
Module: constants.ConnectionModule,
Categories: []string{constants.FastCategory, constants.ConnectionCategory},
Description: `(SELECT index) Change the logical database that the current connection is operating from.`,
Sync: false,
KeyExtractionFunc: func(cmd []string) (internal.KeyExtractionFuncResult, error) {
return internal.KeyExtractionFuncResult{
Channels: make([]string, 0),
ReadKeys: make([]string, 0),
WriteKeys: make([]string, 0),
}, nil
},
HandlerFunc: handleSelect,
},
}
}