Skip to content

Commit

Permalink
add option for specifying the template database during database creat…
Browse files Browse the repository at this point in the history
…ion. prompted by changes in 8.4 wrt encoding rules changes, but this should work on all versions. Verified against 8.3 and 8.4
  • Loading branch information
Robert Treat committed Jul 3, 2009
1 parent 50ac93d commit 3914822
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
27 changes: 26 additions & 1 deletion all_db.php
Expand Up @@ -163,9 +163,13 @@ function doCreate($msg = '') {
else
$_POST['formEncoding'] = '';
}
if (!isset($_POST['formTemplate'])) $_POST['formTemplate'] = 'template1';
if (!isset($_POST['formSpc'])) $_POST['formSpc'] = '';
if (!isset($_POST['formComment'])) $_POST['formComment'] = '';

// Fetch a list of databases in the cluster
$templatedbs = $data->getDatabases(false);

// Fetch all tablespaces from the database
if ($data->hasTablespaces()) $tablespaces = $data->getTablespaces();

Expand All @@ -174,6 +178,27 @@ function doCreate($msg = '') {
echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n";
echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"",
htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>\n";

echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strtemplatedb']}</th>\n";
echo "\t\t<td class=\"data1\">\n";
echo "\t\t\t<select name=\"formTemplate\">\n";
// Always offer template0 and template1
echo "\t\t\t\t<option value=\"template0\"",
($_POST['formTemplate'] == 'template0') ? ' selected="selected"' : '', ">template0</option>\n";
echo "\t\t\t\t<option value=\"template1\"",
($_POST['formTemplate'] == 'template1') ? ' selected="selected"' : '', ">template1</option>\n";
while (!$templatedbs->EOF) {
$dbname = htmlspecialchars($templatedbs->fields['datname']);
if ($dbname != 'template1') {
// filter out for $conf[show_system] users so we dont get duplicates
echo "\t\t\t\t<option value=\"{$dbname}\"",
($dbname == $_POST['formTemplate']) ? ' selected="selected"' : '', ">{$dbname}</option>\n";
}
$templatedbs->moveNext();
}
echo "\t\t\t</select>\n";
echo "\t\t</td>\n\t</tr>\n";

echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strencoding']}</th>\n";
echo "\t\t<td class=\"data1\">\n";
echo "\t\t\t<select name=\"formEncoding\">\n";
Expand Down Expand Up @@ -233,7 +258,7 @@ function doSaveCreate() {
// Check that they've given a name and a definition
if ($_POST['formName'] == '') doCreate($lang['strdatabaseneedsname']);
else {
$status = $data->createDatabase($_POST['formName'], $_POST['formEncoding'], $_POST['formSpc'], $_POST['formComment']);
$status = $data->createDatabase($_POST['formName'], $_POST['formEncoding'], $_POST['formSpc'], $_POST['formComment'], $_POST['formTemplate']);
if ($status == 0) {
$_reload_browser = true;
doDefault($lang['strdatabasecreated']);
Expand Down
11 changes: 5 additions & 6 deletions classes/database/Postgres.php
Expand Up @@ -545,17 +545,16 @@ function getDefaultWithOid() {
* @return -1 tablespace error
* @return -2 comment error
*/
function createDatabase($database, $encoding, $tablespace = '', $comment = '') {
function createDatabase($database, $encoding, $tablespace = '', $comment = '', $template = 'template1') {
$this->fieldClean($database);
$this->clean($encoding);
$this->fieldClean($tablespace);
$this->fieldClean($comment);
$this->fieldClean($template);

if ($encoding == '') {
$sql = "CREATE DATABASE \"{$database}\"";
} else {
$sql = "CREATE DATABASE \"{$database}\" WITH ENCODING='{$encoding}'";
}
$sql = "CREATE DATABASE \"{$database}\" WITH TEMPLATE=\"{$template}\"";

if ($encoding != '') $sql .= " ENCODING='{$encoding}'";

if ($tablespace != '' && $this->hasTablespaces()) $sql .= " TABLESPACE \"{$tablespace}\"";

Expand Down
1 change: 1 addition & 0 deletions lang/english.php
Expand Up @@ -369,6 +369,7 @@
$lang['strdatabasealtered'] = 'Database altered.';
$lang['strdatabasealteredbad'] = 'Database alter failed.';
$lang['strspecifydatabasetodrop'] = 'You must specify at least one database to drop.';
$lang['strtemplatedb'] = 'Template';

// Views
$lang['strview'] = 'View';
Expand Down
1 change: 1 addition & 0 deletions lang/recoded/english.php
Expand Up @@ -369,6 +369,7 @@
$lang['strdatabasealtered'] = 'Database altered.';
$lang['strdatabasealteredbad'] = 'Database alter failed.';
$lang['strspecifydatabasetodrop'] = 'You must specify at least one database to drop.';
$lang['strtemplatedb'] = 'Template';

// Views
$lang['strview'] = 'View';
Expand Down

0 comments on commit 3914822

Please sign in to comment.