|
|
Web Programming Using DaoLimin Fu (phoolimin 2009-03-22 This document will not explain how to do web programming, instead, it will just show how to use Dao as a server side scripting language. Server side web scripts usually interact with a web server (such as Apache) through the Common Gateway Interface (CGI) protocol. Web scripts can access the information of a HTTP request using CGI protocol, and decide how to respond to the request. Normally, web scripts also have to query a database, and present the results in a good way to the client browser.
DaoCGI is a module to parse the information of a HTTP request, and presents the information in a structured way (as Dao variables) to Dao web scripts. The CGI module can be loaded by
load DaoCGI;
After loading, there will be several global variables imported into the
current namespace:
<input type="file" name="upload_name" id="filename"/>
and the value is an IO stream containing the uploaded file.A simple CGI program using Dao could be,
load DaoCGI;
stdio.println( 'content-type: text/plain\n' ); stdio.println( 'You are from:', HTTP_ENV[ 'REMOTE_ADDR' ] ); stdio.println( 'Welcome to visit:', HTTP_ENV['REQUEST_URI'] );
Currently Dao only has a module to handle MYSQL database. The way this module handles database is by mapping Dao classes to database tables (class members to table fields), and by using class instances to operate on database tables. For the details, please refer to the Database Handling by DaoDataModel .
It is normally the responsibility of server side web scripts to generate HTML codes to present some information to a web browser. Such information or data are often stored in files or databases, and have to be processed in certain way (model). If the information is going to be presented to a web brower, it is preferrable to achieve certain visual appearance by generating appropriate HTML codes (view). Obviously, it is better to separate model and view, so that it becomes much easier to modify one of them without affecting the other, resulting web applications that are easy to maintain or upgrade. The DaoDataModel suit well the model part of a web application. While for the view part, several string methods are available in Dao to make thing much easier. In the following, I will use an example to explain this. Suppose there is a database table named Friend for an address book with the following information:
id : INTEGER PRIMARY KEY AUTOINCREMENT
name : CHAR(50) phone : CHAR(20) city : CHAR(50) street : VARCHAR(100) and the corresponding Dao class would be:
typedef tuple<id:string,name:string,phone:string,city:string,street:string> friend_t;
Here a tuple type with named items is also defined to be
the basic data structure to interface between the model and view part
of the application.class Friend { my id : INT_PRIMARY_KEY_AUTOINCREMENT; my name : CHAR50; my phone : CHAR20; my city : CHAR50; my street : CHAR100; routine AsTuple() => friend_t { return ( (string) id, name, phone, city, street ); } } Now suppose again, the database is open as,
global model = DataModel( 'dbname', 'host', 'user', 'password' );
Now in the model part, querying the database table is extremely simple. For example, one would like to find all friends in certain city, this can be done as,
routine FriendInCity( city : string ) => list<friend_t>
Obviously this function can also be defined as
a member method of class Friend .{ friends = {}; friend = Friend(); hd = model.Select( Friend ).Where().EQ( 'city', city ); while( hd.Query( friend ) ) friends.append( friend.AsTuple() ); hd.Done(); return friends; } Creating view can also be done in a simple way by using template HTML codes. For example, if the queried friends need to be displayed in a table, one can define the following template,
<tr class="myrowstyle"><td class="mycellstyle">@(id)</td>
The template will define which fields to be displayed, and how is the layout.
The color and font information can also be included in the template,
but it is easier to set them by using Cascade Style Sheet (CSS).<td>@(name)</td><td>@(phone)</td> <td>@(city)</td><td>@(street)</td></tr> The generation of view would be,
routine ViewFriends( friends : list<friend_t>, rowtpl = '' )
Here the method rowtpl.expand() will expand the rowtpl
string and substitute the place-holders by the tuple items of friend
with corresponding item names. A map (associative array) could also be
used in the place of the tuple parameter of expand() .{ html = '<table class="mytablestyle">\n'; for( friend in friends ) html += rowtpl.expand( friend, '@' ); html += '</table>'; return html; } So to query the friends and view them, one could use,
row = '<tr class="myrowstyle"><td class="mycellstyle">@(id)</td>
By changing the template html codes (as well as CSS),
the appearance of the view can be easily changed.<td>@(name)</td><td>@(phone)</td> <td>@(city)</td><td>@(street)</td></tr>'; friends = FriendInCity( 'Beijing' ); view = ViewFriends( friends, row );
view count 1399 times
created at 2009-02-24, 18:55 GMT modified at 2009-03-22, 17:07 GMT |
fu: ... I forgot to say something about the plan for the whole new year in my previous reply. Well, besides w ... (Jan.19,01:40) fu: ... Happy new dragon year (which will start from this sunday)! Actually, it was a busy month (I wish th ... (Jan.18,22:46) ybabel: What's the plan for the new year ? Hello 'vry budy :- ) happy new year (when is the new year for you Fu ?) I saw you come back and comm ... (Jan.18,18:59) |