mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-19 14:24:43 +08:00
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:
@@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user