Prev | Current Page 662 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

Several such functions are commonly made available
within a SQL server??™s core functionality set. A few such commonly implemented
functions, known as aggregate functions, include sum(), max(), and min(). However,
you might require a custom aggregate function not otherwise available within the
server??™s default capabilities. SQLite compensates for this by offering the ability to create
your own. The function used to register your custom aggregate functions is
sqlite_create_aggregate(). Its prototype follows:
boolean sqlite_create_aggregate(resource dbh, string func, mixed step_func,
mixed final_func [, int num_args])
Actually it registers two functions: step_func, which is called on every row of the
query target, and final_func, which is used to return the aggregate value back to the
caller. Once registered, you can call final_func within the caller by the alias func. The
optional num_args parameter specifies the number of parameters the aggregate function
should take. Although the SQLite parser attempts to discern the number if this parameter
is omitted, you should always include it for clarity??™s sake.
Consider an example. Building on the salary conversion example from the previous
section, suppose you want to calculate the total amount of gold employees could
collectively purchase:
// Define gold's current price-per-ounce
define("PPO",400);
// Create the aggregate function
function total_salary(&$total,$salary)
{
$total += $salary;
}
// Create the aggregate finalization function
function convert_to_gold(&$total)
{
return $total / PPO;
}
590 CHAPTER 22 ?–  SQLITE
// Connect to the SQLite database
$sqldb = sqlite_open("corporate.


Pages:
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674