Command Package Types
CommandInterface
type CommandInterface interface {
// GetCypher returns the Cypher query string.
GetCypher() string
// GetParams returns the parameters map for the Cypher query.
GetParams() map[string]interface{}
// GetOnSuccess returns the success callback function.
GetOnSuccess() func(interface{})
// GetOnFailure returns the failure callback function.
GetOnFailure() func(error)
}
CommandInterface defines the contract for a cypher command.
AuditedCypherCommand
type AuditedCypherCommand struct {
InnerCommand CommandInterface
Logger *log.Logger
}
AuditedCypherCommand is a decorator that adds logging for Cypher queries. AuditedCypherCommand implements CommandInterface. Fields:
InnerCommand: The wrapped command to execute.
Logger: The logger used for auditing Cypher queries and parameters. Implements:
CommandInterface Behavior:
Logs the Cypher query and parameters before execution. Typical Usage:
logger := log.New(os.Stdout, "AUDIT: ", log.LstdFlags)
auditedCmd := &AuditedCypherCommand{
InnerCommand: baseCommand,
Logger: logger,
}
Example Logs:
AUDIT: Executing Cypher: MATCH (n:Person) RETURN n
AUDIT: With Params: map[string]interface{}{}
SQLCommandTemplate
type SQLCommandTemplate struct {
// SQLCommandTemplate
Query string
Params map[string]interface{}
Args []interface{}
}
SQLCommandTemplate describes a SQL query.
GraphCommandTemplate
type GraphCommandTemplate struct {
// GraphCommandTemplate
Cypher string
Params map[string]interface{}
}
GraphCommandTemplate describes a Neo4j cypher.
CypherCommand
type CypherCommand struct {
// Cypher is the Cypher query string to execute.
Cypher string
// Params are the parameters for the Cypher query.
Params map[string]interface{}
// OnSuccess is a callback invoked with the query results upon successful execution.
OnSuccess func(interface{})
// OnFailure is a callback invoked with an error if the execution fails.
OnFailure func(err error)
}
CypherCommand represents a simple Cypher query with optional callbacks. And implements the Command Interface.
Fields:
Cypher: The Cypher query string.
Params: The parameters for the Cypher query.
OnSuccess: A callback function invoked with query results on success.
OnFailure: A callback function invoked with the error on failure.
Implements:
Typical Usage:
cmd := &CypherCommand{
Cypher: "CREATE (n:Person {name: $name})",
Params: map[string]interface{}{"name": "Alice"},
OnSuccess: func(results interface{}) {
fmt.Println("Query succeeded:", results)
},
OnFailure: func(err error) {
fmt.Println("Query failed:", err)
},
}
ParameterizedCypherCommand
type ParameterizedCypherCommand struct {
BaseCypher string
OnSuccess func(interface{})
OnFailure func(error)
// A function that extracts parameters from the request
ExtractParamsFunc func(r *http.Request) (map[string]interface{}, error)
// Cached parameters after extraction
params map[string]interface{}
}
ParameterizedCypherCommand is a command that extracts parameters dynamically. ParameterizedCypherCommand implements the CommandInterface.
Fields:
BaseCypher: The Cypher query string.
ExtractParamsFunc: A function for extracting parameters, e.g., from an HTTP request.
OnSuccess: A callback invoked with results upon success.
OnFailure: A callback invoked with an error upon failure.
Implements:
Behavior:
Typical Usage:
cmd := &ParameterizedCypherCommand{
BaseCypher: "CREATE (n:Person {name: $name})",
ExtractParamsFunc: JSONBodyExtractor,
OnSuccess: func(results interface{}) {
fmt.Println("Query executed successfully")
},
OnFailure: func(err error) {
fmt.Println("Query failed:", err)
},
}
err := cmd.LoadParamsFromRequest(httpRequest)
ReadCypherCommand
type ReadCypherCommand struct {
Cypher string
Params map[string]interface{}
OnSuccess func(interface{})
OnFailure func(error)
}
ReadCypherCommand is a specialized command for executing read-only Cypher queries. ReadCypherCommand implements the CommandInterface.
Fields:
Cypher: The Cypher query string to execute.
Params: The parameters for the Cypher query.
OnSuccess: A callback invoked with the query results upon success.
OnFailure: A callback invoked with an error if the query fails.
Implements:
Typical Usage:
cmd := &ReadCypherCommand{
Cypher: "MATCH (n:Person) RETURN n",
Params: nil,
OnSuccess: func(results interface{}) {
fmt.Println("Query Results:", results)
},
OnFailure: func(err error) {
fmt.Println("Error occurred:", err)
},
}
SQLCommandInterface
type SQLCommandInterface interface {
GetQuery() string
GetParams() map[string]interface{}
GetArgs() []interface{}
GetOnSuccess() func([]map[string]interface{})
GetOnFailure() func(error)
}
SQLCommandInterface defines the methods required for an SQL command.
SQLCommand
type SQLCommand struct {
Query string
Params map[string]interface{}
Args []interface{}
OnSuccess func([]map[string]interface{})
OnFailure func(error)
}
SQLCommand implements the SQLCommandInterface.
ValidatedCypherCommand
type ValidatedCypherCommand struct {
InnerCommand CommandInterface
ValidateFunc func(map[string]interface{}) error
}
ValidatedCypherCommand is a decorator that adds validation logic to a Cypher command. ValidatedCypherCommand implements the CommandInterface.
Fields:
Implements:
Behavior:
Validates the parameters using ValidateFunc before executing the query.
If validation fails, the OnFailure callback is invoked.
Typical Usage:
validateFunc := func(params map[string]interface{}) error {
if _, ok := params["name"]; !ok {
return fmt.Errorf("missing required parameter: name")
}
return nil
}
validatedCmd := &ValidatedCypherCommand{
InnerCommand: baseCommand,
ValidateFunc: validateFunc,
}
WriteCypherCommand
type WriteCypherCommand struct {
Cypher string
Params map[string]interface{}
OnSuccess func(interface{})
OnFailure func(error)
}
WriteCypherCommand is a specialized command for executing write Cypher queries. WriteCypherCommand implements the CommandInterface.
Fields:
Cypher: The Cypher query string to execute.
Params: The parameters for the Cypher query.
OnSuccess: A callback invoked upon successful execution.
OnFailure: A callback invoked with an error if the execution fails.
Implements:
Typical Usage:
cmd := &WriteCypherCommand{
Cypher: "CREATE (n:Person {name: $name})",
Params: map[string]interface{}{"name": "Bob"},
OnSuccess: func(results interface{}) {
fmt.Println("Node created successfully")
},
OnFailure: func(err error) {
fmt.Println("Failed to create node:", err)
},
}
Last modified: 28 January 2025