-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from gramian/main
Refactor and expand SQL Section
- Loading branch information
Showing
8 changed files
with
127 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
[[Custom-Functions]] | ||
=== Custom Functions | ||
|
||
The SQL engine can be extended with custom functions written with a scripting language or via Java. | ||
|
||
[discrete] | ||
|
||
==== Database's function | ||
|
||
Look at the <<Database,Database Interface>> page. | ||
|
||
|
||
[discrete] | ||
|
||
==== Custom Functions in SQL | ||
|
||
Custom functions can be defined via SQL using the following command: | ||
|
||
---- | ||
DEFINE FUNCTION <library>.<name> "<body>" [PARAMETERS [<parameter>,*]] LANGUAGE <language> | ||
---- | ||
|
||
* `<library>` A name space to group functions. | ||
* `<name>` The function's name. | ||
* `<body>` The function's body encapsuled in a string in the chosen language's syntax. | ||
* `[<parameter>,*]` A list of parameter identifiers used in the function body. For functions without parameters omit the `PARAMETERS []` block instead of supplying an empty array. | ||
* `<language>` Either `js` (Javascript) or `sql`. | ||
|
||
NOTE: To invoke a custom function, the function identifier (`library.function`) must be enclosed with back ticks. | ||
|
||
NOTE: The return value in a custom SQL function is determined by the projection named `'result'`. | ||
|
||
*Examples* | ||
|
||
[source,sql] | ||
---- | ||
DEFINE FUNCTION my.fma 'return a + b * c' PARAMETERS [a,b,c] LANGUAGE js; | ||
SELECT `my.fma`(1,2,3); | ||
---- | ||
|
||
[source,sql] | ||
---- | ||
DEFINE FUNCTION the.answer 'SELECT "fourty-two" AS result' LANGUAGE sql; | ||
SELECT `the.answer`(); | ||
---- | ||
|
||
''' | ||
|
||
A custom function can be deleted via SQL using the following command: | ||
|
||
---- | ||
DELETE FUNCTION <library>.<name> | ||
---- | ||
|
||
* `<library>` A name space to group functions. | ||
* `<name>` The function's name. | ||
|
||
*Example* | ||
|
||
[source,sql] | ||
---- | ||
DELETE FUNCTION extra.tsum | ||
---- | ||
|
||
[discrete] | ||
|
||
==== Custom Functions in Java | ||
|
||
Before to use them in your queries you need to register: | ||
|
||
[source,java] | ||
---- | ||
// REGISTER 'BIGGER' FUNCTION WITH FIXED 2 PARAMETERS (MIN/MAX=2) | ||
SQLEngine.getInstance().registerFunction("bigger", | ||
new SQLFunctionAbstract("bigger", 2, 2) { | ||
public String getSyntax() { | ||
return "bigger(<first>, <second>)"; | ||
} | ||
public Object execute(Object[] iParameters) { | ||
if (iParameters[0] == null || iParameters[1] == null) | ||
// CHECK BOTH EXPECTED PARAMETERS | ||
return null; | ||
if (!(iParameters[0] instanceof Number) || !(iParameters[1] instanceof Number)) | ||
// EXCLUDE IT FROM THE RESULT SET | ||
return null; | ||
// USE DOUBLE TO AVOID LOSS OF PRECISION | ||
final double v1 = ((Number) iParameters[0]).doubleValue(); | ||
final double v2 = ((Number) iParameters[1]).doubleValue(); | ||
return Math.max(v1, v2); | ||
} | ||
public boolean aggregateResults() { | ||
return false; | ||
} | ||
}); | ||
---- | ||
|
||
Now you can execute it: | ||
|
||
[source,java] | ||
---- | ||
Resultset result = database.command("sql", "SELECT FROM Account WHERE bigger( salary, 10 ) > 10"); | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters