Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

XML and Scripting Languages

By Parand Tony Darugar
2005-05-18


Retrieving the rules

Our buy/sell rules are stored in the following table:


CREATE TABLE rules (
symbol CHAR(5),
field CHAR(8),
value CHAR(16),
action CHAR(5)
);

symbol is the stock symbol. field describes which field will be used in the criterion (in this case either price or volume). value is the value of field which would trigger an action. action describes the type of action to take (in this case either buy or sell).

Thus the following row from the rules table:



INSERT INTO rules VALUES ("IBM", "price", "120.0", "buy");

means if the price of the IBM stock is greater than 120, issue a buy order. And



INSERT INTO rules VALUES ("MSFT", "volume", "65000000", "sell");

means if the trading volume of Microsoft stock is over 65000000, issue a sell order.

Retrieving these rules from the database is a simple matter using the Perl DBI/DBD extensions. The connection to the database can be created at the start of processing, and kept open until the end. For each stock, the applicable rules can be retrieved by selecting from the rules tables based on the stock symbol.

The tag stock_quotes is the outermost tag, meaning its start will trigger the first handler callback, and its end the last. This provides the perfect place for establishing and closing the database connection.



sub stock_quotes {
use DBI;

$dsn = "DBI:mysql:database=test;";
$::state::dbh = DBI->connect($dsn);
}

sub stock_quotes_ {
$::state::dbh->disconnect();
}

The rules can be retrieved by selecting based on the stock symbol:



my $sth = $::state::dbh->prepare("select * from rules where symbol='$symbol'");
$sth->execute();

while (my $ref = $sth->fetchrow_hashref()) {
# Act on the retrieved rules
}
$sth->finish();


Tutorial Pages:
» Converting XML to HTML
» Simple substitution
» Function-based substitution
» Tree-based processing
» Active XML documents
» Storing tag contents
» Retrieving the rules
» Acting on the rules
» Next steps
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» Starting with XML
» Performing Client-Side XSL Transformations
» Create a Google Sitemap for your Web Site
» Parsing Comma-Separated Values
» XML Security Suite: Increasing the Security of E-Business
» Servlets and XML: Made for Each Other