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.
Parameters
-host
string, required
The name or alias of the host on which to create the new database.
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;