Lasso Soft Inc. > Home

[object_generation]

Linkobject_generation
AuthorRichard Moore
CategoryUtility
Version9.x
LicensePublic Domain
Posted18 Jun 2012
Updated18 Jun 2012
More by this author...

Description

This file can be used to generate an object file from a mySQL database table. It effectively turns a table into an object.

e.g if you have a mySQL table called "customers" it wil craete an Object called customers.

Sample Usage

In the code below

var('tsdb') = 'revman'; // This is the mySQL database
var(t) = 'revenue_schedules'; // This is the name of the mySQL table
var(ta) = 'revenue_schedules_archive'; // This is an archive table we use to track deleted transactions


To use you will need to replace the database name to your own database so it can read the mySQL files

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.






Lasso Programming: CSS/HTML Menu



 {
local(sqlcode) = 'show full columns from ' + #t;
local(fieldarray) = array;
inline(-database=$tsdb,-sql=#sqlcode,-maxrows='all',-inlinename='typelist');
	records;
		if(string(field('type'))->beginswith('int('));
				#fieldarray->insert(pair(field('field') = 'integer'));
			else(string(field('type'))->beginswith('tiny'));
				#fieldarray->insert(pair(field('field') = 'integer'));
			else(string(field('type'))->beginswith('varch'));
				#fieldarray->insert(pair(field('field') = 'string'));
			else(string(field('type'))->beginswith('date'));
				#fieldarray->insert(pair(field('field') = 'date'));
			else(string(field('type'))->beginswith('time'));
				#fieldarray->insert(pair(field('field') = 'date'));
			else(string(field('type'))->beginswith('float'));
				#fieldarray->insert(pair(field('field') = 'decimal'));
			else(string(field('type'))->beginswith('decimal'));
				#fieldarray->insert(pair(field('field') = 'decimal'));
			else(string(field('type'))->beginswith('double'));
				#fieldarray->insert(pair(field('field') = 'decimal'));
		/if;
	/records;
/inline;


local('content') = " type { ";
#content->append('\r\t');
#content->append('\r\t');

#content->append('/* This code creates a ' + #t + '2 object */');
#content->append('\r\t');
#content->append('/* This created on ' + date + ' */');

#content->append('\r\t');
#content->append('/* Copyright of TennisSource.Net. All Rights Reserved */');
#content->append('\r\t');
#content->append('\r\t');
#content->append("data");
#content->append('\r\t');
#content->append("private table::string = '" + #t + "',");
#content->append('\r\t');
#content->append("private table_archive::string = '" + #ta + "',");
#content->append('\r\t');
#content->append("public error::string,");
#content->append('\r\t');
#content->append("public code::string");
#content->append('\r\t');
	iterate(#fieldarray,var(i));
#content->append(",public " + $i->first + "::" + $i->second);
#content->append('\r\t');
	/iterate; 
#content->append('\r\t');
#content->append('\r\t');
#content->append('/* This creates an object from an id if it exists using ' + #t + '(id) */');
#content->append('\r\t');
#content->append('\r\t');
#content->append("public onCreate(id::integer)=> {");
#content->append('\r\t\t');	
#content->append(".id = 0;");
#content->append('\r\t\t');
#content->append("local('sql') = 'select * from ' + .table + ' where id = ' + #id;");
#content->append('\r\t\t');
#content->append("inline(-database=#tsdb,-sql=#sql);");
		iterate(#fieldarray,var(i));
		#content->append('\r\t\t\t');
#content->append(".'" + $i->first + "' = " + $i->second + "(field('" + $i->first + "'));");
		/iterate;
				#content->append('\r\t\t\t');
#content->append(".'code' = string(action_statement);");
#content->append('\r\t\t\t');
#content->append(".'error' = string(error_currenterror);");
#content->append('\r\t\t');
#content->append("/inline;");
#content->append('\r\t');
#content->append("} ");
			#content->append('\r\t');
			#content->append('\r\t');
			#content->append('/* This creates an unpopulated object  using ' + #t + '() */');
#content->append('\r\t');
			#content->append('/* This is an example of multiple dispatch */');
#content->append('\r\t');
#content->append('\r\t');
#content->append("public onCreate()=> {");
			iterate(#fieldarray,var(i));
			#content->append('\r\t\t');
#content->append(".'" + $i->first + "' = " + $i->second + ";");
			/iterate;
			#content->append('\r\t\t');
#content->append(".'code' = string;");
			#content->append('\r\t\t');
#content->append(".'error' = string;");
			#content->append('\r\t');
#content->append("}"); 
#content->append('\r\t');
#content->append('\r\t');
			#content->append('/* This creates an archive entry object  using ' + #t + '->archive() */');
#content->append('\r\t');
			#content->append('/* A table defiend above is expected */');
#content->append('\r\t');
#content->append('\r\t');
#content->append("public archive() => {");	
#content->append('\r\t\t');
#content->append("inline(");
#content->append('\r\t\t\t');	
#content->append("-database = #tsdb,");	
#content->append('\r\t\t\t');
#content->append("-add,");	
#content->append('\r\t\t\t');
#content->append("-keyfield = 'id',");	
#content->append('\r\t\t\t');
#content->append("-table = .table_archive");	
#content->append('\r\t\t\t');
			iterate(#fieldarray,var(i));
#content->append(",.'" + $i->first + "' = " + $i->second);	
#content->append('\r\t\t\t');
			/iterate;	
		
		
#content->append(");");
#content->append('\r\t\t\t');
#content->append(".id=keyfield_value;");	
#content->append('\r\t\t\t');
#content->append(".'code' = string(action_statement);");	
#content->append('\r\t\t\t');
#content->append(".'error' = string(error_currenterror);");	
#content->append('\r\t\t\t');
#content->append("/inline;");	
#content->append('\r\t\t');
#content->append("}");	
#content->append('\r\t');	
#content->append('\r\t');
#content->append('/* This saves or updates the object using ' + #t + '->save() */');
#content->append('\r\t');
#content->append('\r\t');
#content->append("public save() => {");		
#content->append('\r\t\t');	
#content->append("inline(");	
#content->append('\r\t\t');	
#content->append("-database = #tsdb,");	
#content->append('\r\t\t\t');	
#content->append(".id > 0 ? '-update' | '-add',");	
#content->append('\r\t\t\t');	
#content->append("-keyfield = 'id',");	
#content->append('\r\t\t\t');	
#content->append("-keyvalue = .id,");	
#content->append('\r\t\t\t');	
#content->append("-table = .table");	
			iterate(#fieldarray,var(i));
#content->append('\r\t\t\t');	
#content->append(",'" + $i->first + "' = ." + $i->first);	
			/iterate;			
#content->append('\r\t\t\t');				
#content->append(");");
#content->append('\r\t');	
#content->append(".id=keyfield_value;");
#content->append('\r\t\t\t');	
#content->append(".'code' = string(action_statement);");
#content->append('\r\t\t\t');	
#content->append(".'error' = string(error_currenterror);");
#content->append('\r\t\t');	
#content->append("/inline;");
#content->append('\r\t');	
#content->append("}");
#content->append('\r\t');	
#content->append('\r\t');
#content->append('/* This clones an the object using ' + #t + '->clone() */');
#content->append('\r\t');
#content->append('/* A save will be required to hold the data */');
#content->append('\r\t');
#content->append('\r\t');
#content->append("public clone() => {");	
#content->append('\r\t');	
#content->append(".id = 0;");
#content->append('\r\t');	
#content->append("}");
#content->append('\r\t');		
#content->append('\r\t');	
#content->append('/* This deletes an the object data using ' + #t + '->delete() */');
#content->append('\r\t');
#content->append('/* An archive shhould be performed first if needed */');
#content->append('\r\t');
#content->append('\r\t');
#content->append("public delete() => {");
#content->append('\r\t\t');	
#content->append("inline(");
#content->append('\r\t\t\t');
#content->append("-database = #tsdb,");
#content->append('\r\t\t\t');
#content->append("-delete,");
#content->append('\r\t\t\t');
#content->append("-keyfield = 'id',");
#content->append('\r\t\t\t');
#content->append("-keyvalue = .id,");
#content->append('\r\t\t\t');
#content->append("-table = .table,");
#content->append('\r\t\t\t');
#content->append(");");
#content->append('\r\t\t\t');
#content->append(".'code' = string(action_statement);");
#content->append('\r\t\t\t');
#content->append(".'error' = string(error_currenterror);");
#content->append('\r\t\t');
#content->append("/inline;");
#content->append('\r\t\t');
#content->append("}");
#content->append('\r\t');
#content->append('\r\t');
#content->append('/* This outputs the values of the object using ' + #t + '->values()  as a table */');
#content->append('\r\t');
#content->append('\r\t');
#content->append("public values() => {	");
#content->append('\r\t\t');
#content->append("local(vals)=string('');");
#content->append('\r\t\t');
   		iterate(#fieldarray,local(i));
#content->append("#vals->append('');");
#content->append('\r\t\t');
   		/iterate;
#content->append("#vals->append('
');"); #content->append('\r\t\t'); #content->append("#vals->append('" + #i->first + "');"); #content->append('\r\t\t'); #content->append("#vals->append('');"); #content->append('\r\t\t'); #content->append("#vals->append(." + #i->first + ");"); #content->append('\r\t\t'); #content->append("#vals->append('
');"); #content->append('\r\t\t'); #content->append("return(#vals);"); #content->append('\r\t'); #content->append("}"); #content->append('\r\t'); #content->append('\r\t'); #content->append('/* This is the end of the ' + #t + ' object */'); #content->append('\r\t'); #content->append('/* ----------------------------- */'); #content->append('\r\t'); #content->append('\r\t'); #content->append("} \r\t ?>"); local(my_file) = file('/' + #t + '.lasso'); // #my_file->doWithClose => { #my_file->openWrite() #my_file->writeString(#content) file_currenterror; // } return(file_currenterror + '. The file ' + #t + '.lasso' + ' has been created'); } create_object($t,$ta) ?>

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft