Lasso Soft Inc. > Home

[fields_convert]

Linkfields_convert
AuthorJason Huck
CategoryDatabase
Version8.x
Licensehttp://opensource.org/licenses/artistic-license.php
Posted14 Mar 2006
Updated14 Mar 2006
More by this author...

Description

This is a shortcut for shoving the results of an inline into your choice of variables. Called without arguments, the tag will create one var for each field returned, named the same as the field. Or, use -name and -type to create a map, array, or pair array containing all the values. Finally, you can use -convert to pass a map of type conversions to the tag and you can override the type of any field. Requires [cast].

Sample Usage

var('sql' = 'SELECT * FROM global_prefs LIMIT 1');

inline(
	-username='xxxxxx',
	-password='xxxxxx',
	-database='lasso_internal',
	-sql=$sql
);
	// with no arguments, creates a var for each field
	fields_convert;
	
	iterate(field_names, local('i'));
		#i + ': ' + (var_defined(#i) ? 'defined' | 'not defined') + '
\n'; /iterate; '
\n'; // specify a name and type to create a map, array, or pair array var('mytypes') = array('array','map','pairarray'); iterate($mytypes, local('t')); 'Type: ' + #t + '

\n'; fields_convert( -name='myrecord', -type=#t); $myrecord; '
\n'; /iterate; // optionally accepts a map of type conversions // either for individual variables... var('conversions') = map( 'id' = integer, 'size' = integer, 'modified' = date ); fields_convert( -convert=$conversions); iterate(field_names, local('f')); #f + ': ' + var(#f)->type + '
\n'; /iterate; '
\n'; // ...or elements within the container type // in this case, an array fields_convert( -name='myrecord', -type='array', -convert=$conversions ); iterate($myrecord, local('f')); #f + ': ' + #f->type + '
\n'; /iterate; '
\n'; // ...a map... fields_convert( -name='myrecord', -type='map', -convert=$conversions ); iterate($myrecord->keys, local('f')); #f + ': ' + $myrecord->find(#f)->type + '
\n'; /iterate; '
\n'; // ...or a pair array... fields_convert( -name='myrecord', -type='pairarray', -convert=$conversions ); iterate($myrecord, local('f')); #f->first + ': ' + #f->second->type + '
\n'; /iterate; '
\n'; /inline;

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.

define_tag(
	'convert',
	-namespace='fields_',
	-optional='type',
	-optional='name',
	-optional='convert',
	-type='map',
	-priority='replace',
	-description='Converts a single row of results into the specified type(s).'
);
	!local_defined('type') ? local('type' = 'string');
	!local_defined('name') ? local('name' = 'result');
	
	select(#type);				
		case('array');
			var(#name) = rows_array->first;
			
			if(local_defined('convert'));					
				iterate(field_names, local('f'));					
					if(#convert->keys->find(#f));
						cast(var(#name)->get(loop_count), -as=#convert->find(#f)->type);
					/if;
				/iterate;
			/if;
			
		case('map');
			var(#name) = map;
			
			iterate(field_names, local('i'));
				var(#name)->insert(#i = field(#i));
			/iterate;

			if(local_defined('convert'));
				iterate(var(#name)->keys, local('f'));
					if(#convert->keys->find(#f));
						cast(var(#name)->find(#f), -as=#convert->find(#f)->type);
					/if;
				/iterate;
			/if;
			
		case('pairarray');				
			var(#name) = array;
			
			iterate(field_names, local('i'));
				var(#name)->insert(
					pair(#i = field(#i))
				);
			/iterate;				

			if(local_defined('convert'));
				iterate(var(#name), local('f'));
					if(#convert->keys->find(#f->first));
						local('v') = #f->second;
						cast(#v, -as=#convert->find(#f->first)->type);
						#f = pair(#f->first = #v);
					/if;
				/iterate;
			/if;
			
		case;				
			iterate(field_names, local('i'));
				var(#i) = field(#i);
				cast(var(#i), -as=#type);
			/iterate;
			
			if(local_defined('convert'));
				iterate(#convert->keys, local('k'));
					cast(var(#k), -as=#convert->find(#k)->type);
				/iterate;
			/if;
			
	/select;
/define_tag;

Related Tags

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