Class Index

Classes


v1.2.1

DBFunctionStore

This object houses the database function information, and is accessible via the DBCatalog#functions property as follows:

	var sqlFunctions = db.catalog.functions;
	

User-defined functions can be added and removed from the catalog, and may be used within SQL statements. Both "scalar" (works on one row of data) and "aggregate" (works on a group of rows) functions can be created. The following example shows how to create a "scalar" function:

	var fctn = function(str, count) {
		var arr = [];
		for (var i = 0; i < count; ++i)
			arr.push(str);
		return arr.join("");
	};
	db.catalog.functions.addScalarFunction("RepeatStr", fctn);
	
	//  Now let's test the function:
	var res = db.query("SELECT repeatstr('foo', 3)");
	window.alert(res.data[0][0]); // displays 'foofoofoo'
	

Unlike tables and data, user-defined functions only have a "persistence scope" of DB.SCOPE_PAGE , and must be recreated every time a browser page is loaded.

Summary
Constructor Attributes Constructor Name and Description
This object houses the database function information.
Method Summary
Method Attributes Method Name and Description
addAggregateFunction (functionName, fctn)
Adds an "aggregate" User-defined function to the database catalog to be used within SQL queries.
addScalarFunction (functionName, fctn)
Adds a "scalar" User-defined function to the database catalog to be used within SQL queries.
dropFunction (functionName)
Drops the specified function from the database catalog.
hasFunction (functionName)
Returns whether a function with the specified name exists in the database catalog.
Detail
DBFunctionStore ()
This object houses the database function information.
Method Detail
addAggregateFunction (functionName, fctn)
Adds an "aggregate" User-defined function to the database catalog to be used within SQL queries. An "aggregate" function work on groups of rows in conjunction with the GROUP BY clause. Aggregate functions can also receive any number of arguments, but each argument will be an array of values corresponding to the number of rows in the grouping set.

The following example shows how to create a "aggregate" function:

	var fctn = function(arr) {
		return arr.join(" ");
	};
	db.catalog.functions.addAggregateFunction("ConcatAll", fctn);
	
	//  Now let's test the function:
	var res = db.query(
		"SELECT id, concatall(word) " +
		" FROM (" +
		" SELECT 1 as id, 'foo' as word" + 
		" UNION SELECT 1, 'bar'" + 
		" UNION SELECT 1, 'baz'" + 
		" ) as tab" + 
		" GROUP BY id"
	);
	window.alert(res.data[0][1]); // displays 'foo bar baz'
	
Parameters:
{string} functionName
The name of the function to add.
{function} fctn
The function to use.
See:
DBFunctionStore#addScalarFunction

addScalarFunction (functionName, fctn)
Adds a "scalar" User-defined function to the database catalog to be used within SQL queries. A "scalar" function is one that works on a single "row", and can receive zero, one, or more arguments.

The following example shows how to create a "scalar" function:

	var fctn = function(str, count) {
		var arr = [];
		for (var i = 0; i < count; ++i)
			arr.push(str);
		return arr.join("");
	};
	db.catalog.functions.addScalarFunction("RepeatStr", fctn);
	
	//  Now let's test the function:
	var res = db.query("SELECT repeatstr('foo', 3)");
	window.alert(res.data[0][0]); // displays 'foofoofoo'
	
Parameters:
{string} functionName
The name of the function to add.
{function} fctn
The function to use.
See:
DBFunctionStore#addAggregateFunction

{boolean} dropFunction (functionName)
Drops the specified function from the database catalog.

Note: This does not participate in a transaction, and will take affect immediately.

Parameters:
{string} functionName
The name of the function to drop.
Returns:
{boolean} Whether the function exists in the database.

{boolean} hasFunction (functionName)
Returns whether a function with the specified name exists in the database catalog.
Parameters:
{string} functionName
The name of the function.
Returns:
{boolean} Whether the function exists in the database.

©2012 Sequel Sphere, LLC.