Ejemplo n.º 1
0
void dlgDatabase::OnOK(wxCommandEvent &ev)
{
#ifdef __WXGTK__
	if (!btnOK->IsEnabled())
		return;
#endif
	if (database)
	{
		database->iSetSchemaRestriction(txtSchemaRestr->GetValue().Trim());
		settings->Write(wxString::Format(wxT("Servers/%d/Databases/%s/SchemaRestriction"), database->GetServer()->GetServerIndex(), database->GetName().c_str()), txtSchemaRestr->GetValue().Trim());

		/*
		 * The connection from the database will get disconnected before execution of any
		 * sql statements for the database.
		 *
		 * Hence, we need to hack the execution of the default privileges statements(sqls)
		 * before getting disconnected from this database. So that, these statements will
		 * run against the current database connection, and not against the server connection.
		 */
		// defaultSecurityChanged will be true only for PostgreSQL 9.0 or later
		if (defaultSecurityChanged)
		{
			wxString strDefPrivs = GetDefaultPrivileges();
			if (!executeDDLSql(strDefPrivs))
			{
				EnableOK(true);
				return;
			}
			defaultSecurityChanged = false;
		}
	}
	dlgDefaultSecurityProperty::OnOK(ev);
}
Ejemplo n.º 2
0
wxString dlgSchema::GetSql()
{
	wxString sql, name;
	name = qtIdent(GetName());

	if (schema)
	{
		// edit mode
		AppendNameChange(sql);
		AppendOwnerChange(sql, wxT("SCHEMA ") + name);
	}
	else
	{
		// create mode
		sql = wxT("CREATE SCHEMA ") + name;
		AppendIfFilled(sql, wxT("\n       AUTHORIZATION "), qtIdent(cbOwner->GetValue()));
		sql += wxT(";\n");

	}
	AppendComment(sql, wxT("SCHEMA"), 0, schema);

	sql += GetGrant(wxT("UC"), wxT("SCHEMA ") + name);

	if (connection->BackendMinimumVersion(9, 0) && defaultSecurityChanged)
		sql += GetDefaultPrivileges(name);

	if (seclabelPage && connection->BackendMinimumVersion(9, 1))
		sql += seclabelPage->GetSqlForSecLabels(wxT("SCHEMA"), name);

	return sql;
}
Ejemplo n.º 3
0
// Note: CREATE DATABASE cannot be part of a multi-statement query as of
//       PG83, and never actually would have been transaction-safe prior
//       to then. Therefore, when creating a new database, only the CREATE
//       statement comes from GetSql(), subsequent ALTERs come from GetSql2()
wxString dlgDatabase::GetSql()
{
	wxString sql, name;
	name = GetName();

	if (database)
	{
		// edit mode

		AppendNameChange(sql);
		AppendOwnerChange(sql, wxT("DATABASE ") + qtIdent(name));

		AppendComment(sql, wxT("DATABASE"), 0, database);

		if (seclabelPage && connection->BackendMinimumVersion(9, 2))
			sql += seclabelPage->GetSqlForSecLabels(wxT("DATABASE"), qtIdent(name));

		if (connection->BackendMinimumVersion(8, 4))
		{
			if (cbTablespace->GetCurrentSelection() > 0 && cbTablespace->GetOIDKey() > 0
			        && cbTablespace->GetOIDKey() != database->GetTablespaceOid())
				sql += wxT("ALTER DATABASE ") + qtIdent(name)
				       +  wxT("\n  SET TABLESPACE ") + qtIdent(cbTablespace->GetValue())
				       +  wxT(";\n");
		}
		if (connection->BackendMinimumVersion(8, 1))
		{
			long connLimit;

			if (txtConnLimit->GetValue().IsEmpty())
				connLimit = -1;
			else if (!txtConnLimit->GetValue().ToLong(&connLimit))
				connLimit = database->GetConnectionLimit();

			if (connLimit != database->GetConnectionLimit())
			{
				wxString strConnLimit;
				strConnLimit << connLimit;
				sql += wxT("ALTER DATABASE ") + qtIdent(name)
				       +  wxT("\n  WITH CONNECTION LIMIT = ")
				       +  strConnLimit
				       +  wxT(";\n");
			}
		}

		if (!connection->BackendMinimumVersion(8, 2))
			sql += GetGrant(wxT("CT"), wxT("DATABASE ") + qtIdent(name));
		else
			sql += GetGrant(wxT("CTc"), wxT("DATABASE ") + qtIdent(name));

		wxArrayString vars;
		wxString username;
		wxString varname;
		wxString varvalue;
		size_t index;
		int pos;

		// copy database->GetVariables() into vars
		for (index = 0 ; index < database->GetVariables().GetCount() ; index++)
			vars.Add(database->GetVariables().Item(index));

		// check for changed or added vars
		for (pos = 0 ; pos < lstVariables->GetItemCount() ; pos++)
		{
			wxString newUsr = lstVariables->GetText(pos);
			wxString newVar = lstVariables->GetText(pos, 1);
			wxString newVal = lstVariables->GetText(pos, 2);

			wxString oldVal;

			for (index = 0 ; index < vars.GetCount() ; index += 3)
			{
				username = vars.Item(index);
				varname = vars.Item(index + 1);
				varvalue = vars.Item(index + 2);

				if (newUsr == username && newVar == varname)
				{
					oldVal = varvalue;
					vars.RemoveAt(index);
					vars.RemoveAt(index);
					vars.RemoveAt(index);
					break;
				}
			}
			if (oldVal != newVal)
			{
				if (newUsr.Length() == 0)
					sql += wxT("ALTER DATABASE ") + qtIdent(name);
				else
					sql += wxT("ALTER ROLE ") + newUsr + wxT(" IN DATABASE ") + qtIdent(name);

				if (newVar != wxT("search_path") && newVar != wxT("temp_tablespaces"))
				{
					sql += wxT("\n  SET ") + newVar + wxT(" = '") + newVal + wxT("';\n");
				}
				else
				{
					sql += wxT("\n  SET ") + newVar + wxT(" = ") + newVal + wxT(";\n");
				}
			}
		}

		// check for removed vars
		for (pos = 0 ; pos < (int)vars.GetCount() ; pos += 3)
		{
			username = vars.Item(index);
			varname = vars.Item(index + 1);
			varvalue = vars.Item(index + 2);

			if (username.Length() == 0)
			{
				sql += wxT("ALTER DATABASE ") + qtIdent(name)
				       +  wxT("\n  RESET ") + varname
				       + wxT(";\n");
			}
			else
			{
				sql += wxT("ALTER ROLE ") + username + wxT(" IN DATABASE ") + qtIdent(name)
				       +  wxT("\n  RESET ") + varname + wxT(";\n");
			}
		}

		if (defaultSecurityChanged)
			sql += wxT("\n") + GetDefaultPrivileges();
	}
	else
	{
		// create mode
		sql = wxT("CREATE DATABASE ") + qtIdent(name)
		      + wxT("\n  WITH ENCODING=") + qtDbString(cbEncoding->GetValue());

		AppendIfFilled(sql, wxT("\n       OWNER="), qtIdent(cbOwner->GetValue()));
		AppendIfFilled(sql, wxT("\n       TEMPLATE="), qtIdent(cbTemplate->GetValue()));
		AppendIfFilled(sql, wxT("\n       LOCATION="), txtPath->GetValue());
		if (connection->BackendMinimumVersion(8, 4))
		{
			wxString strCollate = cbCollate->GetValue();
			if (!strCollate.IsEmpty())
				AppendIfFilled(sql, wxT("\n       LC_COLLATE="), qtDbString(strCollate));
			wxString strCType = cbCType->GetValue();
			if (!strCType.IsEmpty())
				AppendIfFilled(sql, wxT("\n       LC_CTYPE="), qtDbString(strCType));
		}
		if (connection->BackendMinimumVersion(8, 1))
		{
			AppendIfFilled(sql, wxT("\n       CONNECTION LIMIT="), (txtConnLimit->GetValue() == wxT("-") ? wxT("-1") : txtConnLimit->GetValue()));
		}
		if (cbTablespace->GetCurrentSelection() > 0 && cbTablespace->GetOIDKey() > 0)
			sql += wxT("\n       TABLESPACE=") + qtIdent(cbTablespace->GetValue());

		sql += wxT(";\n");
	}

	return sql.Trim(false);
}