Lasso Soft Inc. > Home

[mysql_fields]

Linkmysql_fields
AuthorJason Huck
CategoryDatabase
Version8.5.x
LicensePublic Domain
Posted05 Feb 2008
Updated15 Feb 2008
More by this author...

Description

This custom type runs a "SHOW FIELDS IN ..." query against the database and table specified in the enclosing inline and returns the results as a map, where the key for each item is the field name and the value is a map of the type, null, key, default, and extra values. The values for an individual field can be returned by calling it directly by name. The value of an individual setting for a given field can be returned by passing it as a param. See below for examples. The tag also includes an alias for "mysql_columns." Must be used within an inline and only works with MySQL datasources.

Sample Usage

inline( ...params... );
    // return all settings for all fields
    mysql_fields;

    // return settings for a single field
    mysql_fields->first_name;

    // return a single setting for a single field
    mysql_fields->first_name( -type);
/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_type(
	'fields',
	-namespace='mysql_',
	-prototype,
	-description='For MySQL, returns details about the table used in the current SELECT inline.'
);
	local('data' = map);

	define_tag('oncreate');
		local('query') = action_statement;

		// remove whole line comments
		#query = string_replaceregexp(
			#query, 
			-find='\\s*#.*', 
			-replace=''
		);
		
		#query->trim&removetrailing(';');
		
		// remove LIMIT
		#query = string_replaceregexp(
			#query, 
			-find='(?im)\\sLIMIT\\s+\\d+(,\\d+)?\\s*$', 
			-replace=''
		);

		if(#query->beginswith('SELECT') && lasso_datasourceismysql(database_name));
			local('sql' = '
			   DROP TEMPORARY TABLE IF EXISTS TEMP_COLUMNFINDER;
			   
			   CREATE TEMPORARY TABLE TEMP_COLUMNFINDER
			   ' + #query + '
			   LIMIT 0;
			   
			   SHOW FULL COLUMNS FROM TEMP_COLUMNFINDER;\				
			');
		
			inline( -sql=#sql);
				rows;
					local('tmp') = map(
					   'type' 		= field('Type'),
					   'collation'	= field('Collation'),
					   'null'		= field('Null'),
					   'key'		= field('Key'),
					   'default'	= field('Default'),
					   'extra'		= field('Extra'),
					   'privileges'	= field('Privileges'),
					   'comment'	= field('Comment')
					);

					self->'data'->insert(field('Field') = #tmp);
				/rows;
			/inline;
		/if;
	/define_tag;

	define_tag('_unknowntag');
		local('field') = @self->'data'->find(tag_name);
	
		if(#field->isa('map'));
			!params->size ? return(#field);
			local('key') = params->first->removeleading('-')&;
			return(#field->find(#key));
		/if;
	/define_tag;

	define_tag('onconvert');
		return(self->'data');
	/define_tag;
/define_type;

namespace_import('mysql_fields','mysql_columns');

Comments

15 Feb 2008, Jason Huck

Updated

This tag has been updated to work with complex queries by creating a temp table based on the original query. It also returns additional information about each column. Thanks to Bil Corry for a few enhancements.

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