Cutting the guts out of a database Greetings, fellow javascripters ! The aim of these lessons is to understand, how a database-program can be coded (explained for the javascript-language). Well, of course, such a javascripted database has its limits ! You know, with javascript you have NO access to your harddisk, so all of the database-entries have to be loaded in your browser. Could take some time, if your database is growing. But this should not bother us too much : our aim is to collect knowledge. And KNOWLEDGE IS POWER. What we learn from these javascript-programs, we will use some day when we start with just another programming language. So : let's rock ! Our first target is a rather simple (but interesting) database, coded by Satadip Dutta . This program can be found at JavaGoodies.com and other places around the web. (nssearch.html). My advice is that you should first grab the script (rename as *.htm) and play a little bit with the program. Get used with it and come back. Finished ? O.k., so let's have a closer look at our database ! Now we know that this is a database for www-links. With this program you can build up a database with your favourite links and make search in the title-field. Let's check the code. REMEMBER : I will not criticize anything of this program, the aim is just to understand how the program does its job. After examing some more of these database-programs we will try to make the best out of them all and write our own database-program. So come on, no time to loose ! The first thing I do when checking an unknown code - no matter which program-language it is written - is do make myself clear, which way the program flows and which global variables I am handling with :
The Code... ....................... ...and my Remarks |
---|
<.SCRIPT language='javascript'> Let's go and start with the global variables : first 6 object-variables are built .. .. with the help of the javascript object 'object' the new-statement creates a new a copy (=instance) of an object title = new Object(); - the title of the actual entry desc = new Object(); - the description of the actual entry links= new Object(); - the URL of the actual entry matched= new Object(); - how often ... the number of matching-the-search-entries keywords= new Object(); - container for the search-items found= new Object(); - the number of entries with our keyword So : our database consists of a number of 'records', every 'record' has 3 entries. As we will see, the only searchable item is the title var temp=0; and one 'normal' variable (a 'working man') title[0]=10 - actual number of database-entries has to be updated every time you add an entry !! keywords[0]=0 the number of keywords after parsing found[0]=0 the number of matches found Here they are : our database-records title[1]="homepage dutta" desc[1]="Inch of Dutta's Life" links[1]="http://dutta.home.ml.org" matched[1]=0 title[2]="yahoo search engine" desc[2]=" the yahoo search engine " links[2]=" http://www.yahoo.com" matched[2]=0 ..... .... title[10]="free email" desc[10]="rocketmail.com" links[10]="http://www.rocketmail.com" matched[10]=0 function search(){ start of a function goes to work after the start of a search will be our next task .... } <./SCRIPT> <./HEAD> <.BODY> <.FORM name="searchengine"> the name of our form : searchengine <.CENTER>Keywords: <.INPUT type = text name ="keywords" value="" maxlength=40> the name of our input : keywords <.INPUT type = button name="go" Value="Go ahead and find" onClick="search()"> start a search <./CENTER> <./FORM> |
O.k., let's resume what we have learned til now : Our database consists of 3 REAL entries : title = new Object() the title which is also the keyword to search desc = new Object() the description links = new Object() the URL Why are these entries built with the 'Object'-object and not with the 'array'-object ? (If you don't know about the array-object, so please be patient for the next part of this writing). The 'Object'-object is the primitive Javascript object type (the grandfather of all the other objects like array, document and so on). All Javascript objects are decended from Object. That is, all Javascript objects have the methods defined for Object.
I did not ask the programmer for the reason of using this object, but I found a possible answer : In Javascript 1.0 there is not yet the 'array'-object. So you have to make a work-around to make use of arrays.
Here I found another explanation : Object is a generic object constructor, and is borrowed from Java. However, JavaScript's Object is a much simplified version of the Java Object class. In Java, Object is used as a superclass, but in JavaScript, Object is used as a simplified means of creating new objects without providing an object constructor function. In JavaScript, an object constructor function is called with the new statement, and the function defines any properties or methods of the object. In the following simplified example, the calling statement instantiates a new object named myObject. The makeObject function creates the object, defining one property ("name") to it. myObject = new makeObject ("my object"); function makeObject(name) { this.name = name; return (this); } The Object constructor saves you the trouble of making a constructor function, and is handy when you don't need or want to define methods and properties of the object when it is created, or when you want to define just one two properties. These following two lines replicate the example above: myObject = new Object(); myObject.name = "my object"; You might use the Object constructor as a quick means to create an object.
So I think, the coder of this progie just used it for compatibility-reasons. To be honest, I would not use this old code anymore. Nowadays Javascript 1.3 is already in use, all of the browsers are supporting Javascript 1.2 (or could it be, that the actual Opera is still hanging on version 1.1 ? - Don't know at the moment). Nevertheless, I don't give a damn about any old javascript-1.0-browsers. With such arguments there would not be any program for Windows 95 (wouldn't that be nice ;) ?) O.k.,, this is old code, we won't use this array-construction anymore, but there is still some code in the function 'search' waiting to be examined. What are we waiting for ?
What happens after you have put in your keyword(s) and started the search :
The Code... ....................... ...and my Remarks |
---|
function search() the one and only function here
{ starting with the declaration of some variables
first :
the keyword(s), transformed to lowercase
var skeyword=document.searchengine.keywords.value.toLowerCase();
var check=1;
var pos=0;
var i=0;
var j=0;
var itemp=0;
var config='';
while (true) LOOP ONE : while
REMEMBER : always TRUE, to leave this loop : 'break'
{ in this loop all the entered search-items are collected
and stored in the 'keywords'-array
if (skeyword.indexOf("+") == -1 )
{
keywords[check]=skeyword;
break;
|
O.k, now everything is clear, in't it ? - Should you have troubles in understanding the loops or what's going on in the program, so why don't you test my little debugging-tool, written in javascript and - IMO - a little bit better than the usual 'alert-method'. But feel free to do what you like. Let's put it together :
Database | creation | content | case-sensitive | multiple-search | sort | search field |
Number 1 | ... = new Object() | title desc links matched found |
no | yes | yes | title |
Number 2 | --- | --- | --- | --- | --- | --- |