SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Sokoban Level Designer : Client Data Access with HTML Applications : FileSave function

The FileSave function is fairly short, so we will be covering it in one go.

function FileSave(){
//Check if there is a file to save
if(DataFile==''){
	alert('No Data File currently loaded');
	return;
	}

//Delete existing data file
ADOConn.Execute('DROP TABLE '+DataFile);

//Create Empty Data File
CreateStr='CREATE TABLE '+DataFile+'
  (FLDA INTEGER,FLDB INTEGER,FLDC TEXT)'; 
ADOConn.Execute(CreateStr);

//Save TDC filter
SaveFilter=TDC1.filter;
TDC1.filter='';
TDC1.Reset();

//Loop through TDC
while(!TDC1.recordset.eof){
	InsertStr='INSERT INTO '+DataFile+' VALUES ('+
	   TDC1.recordset.Fields("FLDA").value+','+
	   TDC1.recordset.Fields("FLDB").value+','+
	   '\''+levels.recordset.Fields("FLDC").value+'\')';
	TDC1.recordset.MoveNext();
	ADOConn.Execute(InsertStr);
	
	}

//Restore TDC filter
TDC1.filter=SaveFilter;
TDC1.Reset();
} //end function FileSave 

The first thing the function does is to see if there is a file to save, by checking whether the global DataFile variable is a null string. The functions next step is rather interesting - it deletes the existing file from the disk by executing a DROP TABLE command. This approach is used because the Text Driver does not support the SQL UPDATE command. Therefore, the table (file) needs to be dropped (deleted), then recreated from scratch.

The function recreates an empty table using the CREATE TABLE command, then saves the current TDC filter before setting it to a null string (No Filter). Then the function loops through the TDC, building an INSERT INTO statement based on the field values in the TDC, and finally executing the SQL statement. Needless to say, you will replace the sample fields shown here with your own schema. Also, remember to enclose the values of text fields (like FLDC above) in quotes, otherwise the INSERT INTO statement will bomb.

If you have been following this discussion closely, you might find it a little weird. The TDC points to the data file that has just been deleted. Yet even after the deletion, the TDC is still reading data from the deleted file! The explanation is simple - the TDC reads the data file just once - when the dataURL property changes. The file is then cached, and all subsequent reads are from the cache, not from the physical file. Thus the TDC is totally unconcerned whether or not the underlying file has been deleted or not.

Finally the function resets the filter of the TDC to it's original value and exits. In the last section, we will look at another short function, FileSaveAs.