[fields_convert]

Description

Link: [fields_convert]
Author: Jason Huck
Category: Database
Version: 8.x
License:
Posted: Mar. 14, 2006
Updated: Jan. 01, 0001
More by this author...
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].

Parameters

-type string, optional Tells the tag to create either a map, array, or pairarray from the field values.
-name string, optional The name of the variable to create to hold the field values when -type is used. If omitted, uses 'result.'
-convert map, optional Shortcut to override the default type conversion for a given field. Accepts a map of field names to data types.

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') + '<br>\n';
	/iterate;
	
	'<hr>\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 + '<br><br>\n';
		fields_convert( -name='myrecord', -type=#t);
		$myrecord;
		'<hr>\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 + '<br>\n';
	/iterate;
	
	'<hr>\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 + '<br>\n';
	/iterate;
	
	'<hr>\n';


	// ...a map...
	fields_convert(
		-name='myrecord',
		-type='map',
		-convert=$conversions
	);
	
	iterate($myrecord->keys, local('f'));
		#f + ': ' + $myrecord->find(#f)->type + '<br>\n';
	/iterate;
	
	'<hr>\n';


	// ...or a pair array...
	fields_convert(
		-name='myrecord',
		-type='pairarray',
		-convert=$conversions
	);
	
	iterate($myrecord, local('f'));
		#f->first + ': ' + #f->second->type + '<br>\n';
	/iterate;
	
	'<hr>\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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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

none

Email:


Password:



Newest

Most Popular