Lasso Soft Inc. > Home

[admin_createdatabase]

Linkadmin_createdatabase
AuthorJason Huck
CategoryAdministration
Version8.x
Licensehttp://opensource.org/licenses/artistic-license.php
Posted21 Nov 2006
Updated21 Nov 2006
More by this author...

Description

Creates a new database on the selected host and enables it in SiteAdmin. Requires [host_id] and authentication as a user with permissions on Lasso's internal db's, i.e., with [auth_admin]. Works for any datasource which supports CREATE DATABASE in SQL.

Sample Usage

admin_createdatabase(
	-host='Local MySQL',
	-name='TestDB'
);

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(
	'createdatabase',
	-namespace='admin_',
	-req='host',
	-req='name', -copy,
	-priority='replace',
	-description='Creates a new database on the selected host and enables it in SiteAdmin.'
);
	#name = encode_sql(#name);

	inline( -database='lasso_internal', -sql='SELECT 1');
		// make sure name is unique
		local('sql' = '
			SELECT id 
			FROM security_datasource_databases 
			WHERE name = \'' + #name + '\'
				OR alias = \'' + #name + '\'
		');
		
		inline( -sql=#sql);
			found_count ? return(false);
		/inline;
		
		// get the host id
		local('hostID') = host_id(#host);
	
		// find an enabled database on this host to use for the connection
		local('sql' = '
			SELECT alias, name
			FROM security_datasource_databases
			WHERE id_host = ' + #hostID + '
				AND enabled = \'Y\'
			LIMIT 1
		');
		
		inline( -sql=#sql);
			found_count ?
				local('accessDB') = (field('alias') != '' ? field('alias') | field('name'))
				| return(false);
		/inline;
		
		// create the database
		local('sql' = 'CREATE DATABASE ' + #name);
		
		protect;
			inline( -database=#accessdb, -sql=#sql); /inline;
			
			// get the connector ID for the host so we can refresh it
			local('sql' = '
				SELECT id_datasource 
				FROM security_datasource_hosts 
				WHERE id = ' + #hostID + '
			');
			
			inline( -sql=#sql);
				found_count ? local('connector') = integer(field('id_datasource')) | return(false);
			/inline;
			
			admin_reloaddatasource(#connector);
			
			// enable the new database
			local('sql' = '
				UPDATE security_datasource_databases
				SET enabled = \'Y\'
				WHERE name = \'' + #name + '\'
			');
			
			inline( -sql=#sql); /inline;
			
			return(true);
		
			handle_error;
				return(false);
			/handle_error;
		/protect;
	/inline;
/define_tag;

Related Tags

Comments

06 Feb 2009, Stefan Straakenbroek

Bug fix

You should add another call to admin_reloaddatasource() after you enable the database, in order to make to table contents available to the site process. If you don't do this, the site process will display an HTTP Authentication window when trying to access a table in the database.

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