Esempio n. 1
0
wxString dlgExtTable::GetSql()
{
    wxString sql, name=GetName();


    if (extTable)
    {
        // edit mode

        if (name != extTable->GetName())
        {
            sql += wxT("ALTER TABLE ") + extTable->GetQuotedFullIdentifier()
                +  wxT(" RENAME TO ") + qtIdent(name) + wxT(";\n");
        }
    }

    if (!extTable || txtSqlBox->GetText() != oldDefinition)
    {
        sql += wxT("CREATE EXTERNAL TABLE ") + schema->GetQuotedPrefix() + qtIdent(name) + wxT("\n")
            + txtSqlBox->GetText()
            + wxT(";\n");
    }

    if (extTable)
        AppendOwnerChange(sql, wxT("TABLE ") + schema->GetQuotedPrefix() + qtIdent(name));
    else
        AppendOwnerNew(sql, wxT("TABLE ") + schema->GetQuotedPrefix() + qtIdent(name));


    sql +=  GetGrant(wxT("r"), wxT("TABLE ") + schema->GetQuotedPrefix() + qtIdent(name));

    AppendComment(sql, wxT("TABLE"), schema, extTable);
    return sql;
}
Esempio 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;
}
Esempio n. 3
0
wxString dlgRule::GetSql()
{
    wxString sql, name = GetName();


    if (!rule || didChange())
    {
        sql += wxT("CREATE OR REPLACE RULE ") + qtIdent(name)
               + wxT(" AS\n   ON ") + rbxEvent->GetStringSelection()
               + wxT(" TO ") + table->GetQuotedFullIdentifier();
        AppendIfFilled(sql, wxT("\n   WHERE ") , txtCondition->GetValue());

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

        if (chkDoInstead->GetValue())
            sql += wxT("INSTEAD ");

        if (txtSqlBox->GetTextLength())
        {
            sql += wxT("\n") + txtSqlBox->GetText().Strip(wxString::both);
            if (sql.Right(1) != wxT(";"))
                sql += wxT(";");
        }
        else
            sql += wxT("NOTHING;");

        sql += wxT("\n");
    }
    AppendComment(sql, wxT("RULE ") + qtIdent(name)
                  + wxT(" ON ") + table->GetQuotedFullIdentifier(), rule);
    return sql;
}
wxString dlgTextSearchTemplate::GetSql()
{
	wxString sql;
	wxString objname;

	if (tmpl)
	{
		// edit mode
		objname = schema->GetQuotedPrefix() + qtIdent(GetName());
		AppendNameChange(sql, wxT("TEXT SEARCH TEMPLATE ") + tmpl->GetQuotedFullIdentifier());
		AppendSchemaChange(sql, wxT("TEXT SEARCH TEMPLATE ") + objname);
	}
	else
	{
		// create mode
		objname = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());
		sql = wxT("CREATE TEXT SEARCH TEMPLATE ")
		      + objname
		      + wxT(" (");

		AppendIfFilled(sql, wxT("\n   INIT="), cbInit->GetValue());
		if (cbInit->GetValue().Length() > 0)
			sql += wxT(",");
		AppendIfFilled(sql, wxT("\n   LEXIZE="), cbLexize->GetValue());

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

	}

	AppendComment(sql, wxT("TEXT SEARCH TEMPLATE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), tmpl);

	return sql;
}
Esempio n. 5
0
wxString dlgTextSearchParser::GetSql()
{
	wxString sql;
	wxString objname;

	if (parser)
	{
		// edit mode
		objname = schema->GetQuotedPrefix() + qtIdent(GetName());
		AppendNameChange(sql, wxT("TEXT SEARCH PARSER ") + parser->GetQuotedFullIdentifier());
		AppendSchemaChange(sql, wxT("TEXT SEARCH PARSER ") + objname);
	}
	else
	{
		// create mode
		objname = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());
		sql = wxT("CREATE TEXT SEARCH PARSER ")
		      + objname
		      + wxT(" (")
		      + wxT("\n  START = ") + cbStart->GetValue()
		      + wxT(",\n  GETTOKEN = ") + cbGetToken->GetValue()
		      + wxT(",\n  END = ") + cbEnd->GetValue()
		      + wxT(",\n  LEXTYPES = ") + cbLextypes->GetValue();

		AppendIfFilled(sql, wxT(",\n   HEADLINE="), cbHeadline->GetValue());

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

	}
	AppendComment(sql, wxT("TEXT SEARCH PARSER ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), parser);

	return sql;
}
Esempio n. 6
0
wxString dlgCast::GetSql()
{
	wxString sql;

	if (cast)
	{
		// edit mode
	}
	else
	{
		// create mode
		sql = wxT("CREATE CAST (") + cbSourceType->GetValue()
		      + wxT(" AS ") + cbTargetType->GetValue()
		      + wxT(")\n   ");
		if (cbFunction->GetCurrentSelection() > 0)
			sql += wxT("WITH FUNCTION ") + functions.Item(cbFunction->GetCurrentSelection())
			       +  wxT("(") + cbSourceType->GetValue() + wxT(")");
		else
			sql += wxT("WITHOUT FUNCTION");

		if (chkImplicit->GetValue())
			sql += wxT("\n   AS IMPLICIT");

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

	}
	AppendComment(sql, wxT("CAST (") + cbSourceType->GetValue()
	              + wxT(" AS ") + cbTargetType->GetValue() + wxT(")"), cast);

	return sql;
}
Esempio n. 7
0
wxString dlgCheck::GetSql()
{
	wxString sql;
	wxString name = GetName();

	if (!check)
	{
		sql = wxT("ALTER TABLE ") + table->GetQuotedFullIdentifier()
		      + wxT("\n  ADD");
		AppendIfFilled(sql, wxT(" CONSTRAINT "), qtIdent(name));
		sql += wxT("\n  CHECK ") + GetDefinition()
		       + wxT(";\n");
	}
	else
	{
		if (check->GetName() != name)
		{
			sql = wxT("ALTER TABLE ") + table->GetQuotedFullIdentifier()
			      + wxT("\n  RENAME CONSTRAINT ") + qtIdent(check->GetName())
			      + wxT(" TO ") + qtIdent(name) + wxT(";\n");
		}
		if (connection->BackendMinimumVersion(9, 2) && !check->GetValid() && !chkDontValidate->GetValue())
		{
			sql += wxT("ALTER TABLE ") + table->GetQuotedFullIdentifier()
			       + wxT("\n  VALIDATE CONSTRAINT ") + qtIdent(name) + wxT(";\n");
		}
	}

	if (!name.IsEmpty())
		AppendComment(sql, wxT("CONSTRAINT ") + qtIdent(name)
		              + wxT(" ON ") + table->GetQuotedFullIdentifier(), check);
	return sql;
}
Esempio n. 8
0
wxString dlgForeignServer::GetSql()
{
	wxString sql, name;
	name = txtName->GetValue();

	if (foreignserver)
	{
		// edit mode
		AppendNameChange(sql, wxT("SERVER ") + qtIdent(foreignserver->GetName()));

		if (txtVersion->GetValue() != foreignserver->GetVersion())
		{
			sql = wxT("ALTER SERVER ") + qtIdent(name)
			      + wxT("\n  VERSION ") + qtDbString(txtVersion->GetValue()) + wxT(";\n");
		}

		wxString sqloptions = GetOptionsSql();
		if (sqloptions.Length() > 0)
		{
			sql += wxT("ALTER SERVER ") + name
			       + wxT("\n  OPTIONS (") + sqloptions + wxT(");\n");
		}

		AppendOwnerChange(sql, wxT("SERVER ") + qtIdent(name));
	}
	else
	{
		// create mode
		sql = wxT("CREATE SERVER ") + qtIdent(name);
		if (!(txtType->GetValue()).IsEmpty())
			sql += wxT("\n   TYPE ") + qtDbString(txtType->GetValue());
		if (!(txtVersion->GetValue()).IsEmpty())
			sql += wxT("\n   VERSION ") + qtDbString(txtVersion->GetValue());
		sql += wxT("\n   FOREIGN DATA WRAPPER ") + qtIdent(foreigndatawrapper->GetName());

		// check for options
		if (lstOptions->GetItemCount() > 0)
		{
			wxString options = wxEmptyString;
			for (int pos = 0 ; pos < lstOptions->GetItemCount() ; pos++)
			{
				if (options.Length() > 0)
					options += wxT(", ");

				options += lstOptions->GetText(pos, 0)
				           + wxT(" '") + lstOptions->GetText(pos, 1) + wxT("' ");
			}
			sql += wxT("\n  OPTIONS (") + options + wxT(")");
		}

		sql += wxT(";\n");
		AppendOwnerNew(sql, wxT("SERVER ") + qtIdent(name));
	}

	sql += GetGrant(wxT("U"), wxT("SERVER ") + qtIdent(name));
	AppendComment(sql, wxT("SERVER"), 0, foreignserver);

	return sql;
}
Esempio n. 9
0
wxString dlgSynonym::GetSql()
{
	wxString sql;

	if (!synonymSchema)
	{

		sql = wxT("CREATE OR REPLACE PUBLIC SYNONYM ") + qtIdent(txtName->GetValue()) + wxT("\n FOR ");

		if (cbTargetSchema->GetValue() != wxEmptyString)
			sql += qtIdent(cbTargetSchema->GetValue()) + wxT(".");

		sql += qtIdent(cbTargetObject->GetValue()) + wxT(";\n");

		AppendComment(sql, wxT("PUBLIC SYNONYM ") + qtIdent(txtName->GetValue()), synonym);
	}
	else
	{
		wxString createSql, commentSql;
		if (synonymSchema->GetName() == wxT("public"))
		{
			createSql = wxT("CREATE OR REPLACE PUBLIC SYNONYM ");
			commentSql = wxT("PUBLIC SYNONYM ");
		}
		else
		{
			createSql = wxT("CREATE OR REPLACE SYNONYM ") + qtIdent(synonymSchema->GetName()) + wxT(".");
			commentSql = wxT("PRIVATE SYNONYM ");
		}

		sql = createSql + qtIdent(txtName->GetValue()) + wxT("\n FOR ");

		if (cbTargetSchema->GetValue() != wxEmptyString)
			sql += qtIdent(cbTargetSchema->GetValue()) + wxT(".");

		sql += qtIdent(cbTargetObject->GetValue()) + wxT(";\n");

		AppendComment(sql, commentSql + qtIdent(txtName->GetValue()), synonym);
	}

	return sql;
}
Esempio n. 10
0
wxString dlgForeignKey::GetSql()
{
	wxString sql;
	wxString name = GetName();

	if (!foreignKey)
	{
		sql = wxT("ALTER TABLE ") + table->GetQuotedFullIdentifier()
		      + wxT("\n  ADD");
		AppendIfFilled(sql, wxT(" CONSTRAINT "), qtIdent(name));
		sql += wxT(" FOREIGN KEY ") + GetDefinition()
		       + wxT(";\n");
	}
	else
	{
		if (foreignKey->GetName() != name)
		{
			sql = wxT("ALTER TABLE ") + table->GetQuotedFullIdentifier()
			      + wxT("\n  RENAME CONSTRAINT ") + qtIdent(foreignKey->GetName())
			      + wxT(" TO ") + qtIdent(name) + wxT(";\n");
		}
		if (connection->BackendMinimumVersion(9, 1) && !foreignKey->GetValid() && !chkDontValidate->GetValue())
		{
			sql += wxT("ALTER TABLE ") + table->GetQuotedFullIdentifier()
			       + wxT("\n  VALIDATE CONSTRAINT ") + qtIdent(name) + wxT(";\n");
		}
	}

	if (!name.IsEmpty())
		AppendComment(sql, wxT("CONSTRAINT ") + qtIdent(name)
		              + wxT(" ON ") + table->GetQuotedFullIdentifier(), foreignKey);

	if (chkAutoIndex->GetValue())
	{
		sql += wxT("CREATE INDEX ") + qtIdent(txtIndexName->GetValue())
		       +  wxT("\n  ON ") + table->GetQuotedFullIdentifier()
		       +  wxT("(");

		int pos;
		for (pos = 0 ; pos < lstColumns->GetItemCount() ; pos++)
		{
			if (pos)
				sql += wxT(", ");

			sql += qtIdent(lstColumns->GetText(pos));
		}

		sql += wxT(");\n");
	}
	return sql;
}
Esempio n. 11
0
wxString dlgDatabase::GetSql2()
{
	wxString sql, name;
	name = GetName();

	// We only use GetSql2() in the CREATE case
	if (!database)
	{
		if (connection->BackendMinimumVersion(8, 2))
			AppendComment(sql, wxT("DATABASE"), 0, database);

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

		int cnt = lstVariables->GetItemCount();
		int pos;

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

			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");
			}
		}
		if (seclabelPage && connection->BackendMinimumVersion(9, 2))
			sql += seclabelPage->GetSqlForSecLabels(wxT("DATABASE"), qtIdent(name));
	}

	return sql;
}
Esempio n. 12
0
wxString dlgCollation::GetSql()
{
	wxString sql;
	wxString name;

	if (collation)
	{
		// edit mode
		name = schema->GetQuotedPrefix() + qtIdent(GetName());;
		AppendNameChange(sql, wxT("COLLATION ") + collation->GetQuotedFullIdentifier());
		AppendOwnerChange(sql, wxT("COLLATION ") + name);
		AppendSchemaChange(sql, wxT("COLLATION ") + name);
	}
	else
	{
		// create mode
		name = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());

		sql = wxT("CREATE COLLATION ") + name;
		if (cbCollation->GetValue().IsEmpty())
		{
			if (txtLocale->GetValue().IsEmpty())
			{
				sql += wxT("(LC_COLLATE=") + qtDbString(txtLcCollate->GetValue())
				       +  wxT(", LC_CTYPE=") + qtDbString(txtLcCtype->GetValue())
				       +  wxT(")");
			}
			else
			{
				sql += wxT("(LOCALE=") + qtDbString(txtLocale->GetValue()) + wxT(")");
			}
		}
		else
		{
			sql += wxT(" FROM ") + cbCollation->GetValue();
		}
		sql += wxT(";\n");

		AppendOwnerNew(sql, wxT("COLLATION ") + schema->GetQuotedPrefix() + qtIdent(name));
	}
	AppendComment(sql, wxT("COLLATION ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), collation);

	return sql;
}
Esempio n. 13
0
wxString dlgLanguage::GetSql()
{
	wxString sql, name;
	name = cbName->GetValue();

	if (language)
	{
		// edit mode
		if (name != language->GetName())
			sql += wxT("ALTER LANGUAGE ") + qtIdent(language->GetName())
			       +  wxT("\n  RENAME TO ") + qtIdent(name) + wxT(";\n");
		if (connection->BackendMinimumVersion(8, 3))
			AppendOwnerChange(sql, wxT("LANGUAGE ") + qtIdent(name));
	}
	else
	{
		// create mode
		if (connection->BackendMinimumVersion(8, 1) && cbName->FindString(name) >= 0)
		{
			sql = wxT("CREATE LANGUAGE ") + qtIdent(name) + wxT(";\n");
		}
		else
		{
			sql = wxT("CREATE ");
			if (chkTrusted->GetValue())
				sql += wxT("TRUSTED ");
			sql += wxT("LANGUAGE ") + qtIdent(name) + wxT("\n   HANDLER ") + qtIdent(cbHandler->GetValue());
			if (connection->BackendMinimumVersion(9, 0))
				AppendIfFilled(sql, wxT("\n   INLINE "), qtIdent(cbInline->GetValue()));
			AppendIfFilled(sql, wxT("\n   VALIDATOR "), qtIdent(cbValidator->GetValue()));
			sql += wxT(";\n");
		}
		if (connection->BackendMinimumVersion(8, 3))
			AppendOwnerNew(sql, wxT("LANGUAGE ") + qtIdent(name));
	}

	sql += GetGrant(wxT("U"), wxT("LANGUAGE ") + qtIdent(name));
	AppendComment(sql, wxT("LANGUAGE ") + qtIdent(name), 0, language);

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

	return sql;
}
Esempio n. 14
0
wxString dlgCheck::GetSql()
{
    wxString sql;
    wxString name = GetName();

    if (!check)
    {
        sql = wxT("ALTER ") + object->GetTypeName().Upper() + wxT(" ") + object->GetQuotedFullIdentifier()
              + wxT("\n  ADD");
        if (name.Length() > 0)
        {
            sql += wxT(" CONSTRAINT ") + qtIdent(name) + wxT("\n ");
        }
        sql += wxT(" CHECK ");
        sql += GetDefinition();
        if (connection->BackendMinimumVersion(9, 2) && chkNoInherit->GetValue())
        {
            sql += wxT(" NO INHERIT");
        }
        sql += wxT(";\n");
    }
    else
    {
        if (check->GetName() != name)
        {
            sql = wxT("ALTER ") + object->GetTypeName().Upper() + wxT(" ") + object->GetQuotedFullIdentifier()
                  + wxT("\n  RENAME CONSTRAINT ") + qtIdent(check->GetName())
                  + wxT(" TO ") + qtIdent(name) + wxT(";\n");
        }
        if (connection->BackendMinimumVersion(9, 2) && !check->GetValid() && !chkDontValidate->GetValue())
        {
            sql += wxT("ALTER ") + object->GetTypeName().Upper() + wxT(" ") + object->GetQuotedFullIdentifier()
                   + wxT("\n  VALIDATE CONSTRAINT ") + qtIdent(name) + wxT(";\n");
        }
    }

    if (!name.IsEmpty())
        AppendComment(sql, wxT("CONSTRAINT ") + qtIdent(name)
                      + wxT(" ON ") + object->GetQuotedFullIdentifier(), check);
    return sql;
}
Esempio n. 15
0
wxString dlgTextSearchDictionary::GetSql()
{
    wxString sql;
    wxString objname=schema->GetQuotedPrefix() + qtIdent(GetName());

    if (dict)
    {
        // edit mode
        AppendNameChange(sql);
        AppendOwnerChange(sql, wxT("TEXT SEARCH DICTIONARY ") + objname);

        wxString sqloptions = GetOptionsSql();
        if (sqloptions.Length() > 0)
        {
            sql += wxT("ALTER TEXT SEARCH DICTIONARY ") + objname
                   + wxT(" (") + sqloptions + wxT(")");
        }
    }
    else
    {
        // create mode
        sql = wxT("CREATE TEXT SEARCH DICTIONARY ")
              + schema->GetQuotedPrefix() + GetName()
              + wxT(" (")
              + wxT("\n  TEMPLATE = ") + cbTemplate->GetValue();

        // check for options
        for (int pos=0 ; pos < lstOptions->GetItemCount() ; pos++)
        {
            sql += wxT(", ") + lstOptions->GetText(pos, 0)
                   + wxT("=") + lstOptions->GetText(pos, 1);
        }

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

    }
    AppendComment(sql, wxT("TEXT SEARCH DICTIONARY ") + objname, dict);

    return sql;
}
wxString dlgTablespace::GetSql2()
{
    wxString sql;
    wxString name=GetName();

    if (!tablespace)
    {
        sql += GetGrant(wxT("C"), wxT("TABLESPACE ") + qtIdent(name));
        AppendComment(sql, wxT("TABLESPACE"), 0, tablespace);

        // check for changed or added vars
        for (int pos=0 ; pos < lstVariables->GetItemCount() ; pos++)
        {
            sql += wxT("ALTER TABLESPACE ") + qtIdent(name)
                +  wxT(" SET (") + lstVariables->GetText(pos)
                +  wxT("=") + lstVariables->GetText(pos, 1)
                +  wxT(");\n");
        }
    }

    return sql;
}
Esempio n. 17
0
wxString dlgIndexConstraint::GetSql()
{
	wxString sql;
	wxString name = GetName();

	if (!index)
	{
		sql = wxT("ALTER TABLE ") + table->GetQuotedFullIdentifier()
		      + wxT("\n  ADD");
		AppendIfFilled(sql, wxT(" CONSTRAINT "), qtIdent(name));

		sql += wxT(" ") + wxString(factory->GetTypeName()).Upper() + wxT(" ") + GetDefinition()
		       + wxT(";\n");
	}
	else
	{
		if (connection->BackendMinimumVersion(8, 0) && cbTablespace->GetOIDKey() != index->GetTablespaceOid())
		{
			sql += wxT("ALTER INDEX ") + index->GetSchema()->GetQuotedIdentifier() + wxT(".") + qtIdent(name)
			       +  wxT("\n  SET TABLESPACE ") + qtIdent(cbTablespace->GetValue())
			       + wxT(";\n");
		}

		if (txtFillFactor->GetValue().Trim().Length() > 0 && txtFillFactor->GetValue() != index->GetFillFactor())
		{
			sql += wxT("ALTER INDEX ") + index->GetSchema()->GetQuotedIdentifier() + wxT(".") + qtIdent(name)
			       +  wxT("\n  SET (FILLFACTOR=")
			       +  txtFillFactor->GetValue() + wxT(");\n");
		}
	}

	if (!name.IsEmpty())
		AppendComment(sql, wxT("CONSTRAINT ") + qtIdent(name)
		              + wxT(" ON ") + table->GetQuotedFullIdentifier(), index);

	return sql;
}
Esempio n. 18
0
wxString dlgPackage::GetSql()
{
	wxString sql;
	wxString qtName = schema->GetQuotedSchemaPrefix(schema->GetName()) + qtIdent(txtName->GetValue());

	if (!package || (package && txtHeader->GetText() != package->GetHeaderInner()))
	{
		if (package)
			sql = wxT("DROP PACKAGE BODY ") + qtName + wxT(";\n\n");

		sql += wxT("CREATE OR REPLACE PACKAGE ") + qtName + wxT("\nIS\n");
		sql += txtHeader->GetText();
		sql += wxT("\nEND ") + qtIdent(txtName->GetValue()) + wxT(";\n\n");
	}

	if (!package || (package && txtBody->GetText() != package->GetBodyInner())
	        || (package && txtHeader->GetText() != package->GetHeaderInner()))
	{
		if (!txtBody->GetText().Trim().IsEmpty())
		{
			sql += wxT("CREATE OR REPLACE PACKAGE BODY ") + qtName + wxT("\nIS\n");
			sql += txtBody->GetText().Trim().Trim(false);
			sql += wxT("\nEND ") + qtIdent(txtName->GetValue()) + wxT(";\n\n");
		}
		else
		{
			if (package && !package->GetBodyInner().Trim().IsEmpty())
				sql = wxT("DROP PACKAGE BODY ") + qtName + wxT(";\n\n");
		}
	}

	sql += GetGrant(wxT("X"), wxT("PACKAGE ") + qtName);

	AppendComment(sql, wxT("PACKAGE"), schema, package);

	return sql;
}
Esempio n. 19
0
wxString dlgForeignKey::GetSql()
{
    wxString sql;
    wxString name=GetName();

    if (!foreignKey)
    {
        sql = wxT("ALTER TABLE ") + table->GetQuotedFullIdentifier()
            + wxT(" ADD");
        AppendIfFilled(sql, wxT(" CONSTRAINT "), qtIdent(name));
        sql +=wxT(" FOREIGN KEY ") + GetDefinition()
            + wxT(";\n");
    }
    if (!name.IsEmpty())
        AppendComment(sql, wxT("CONSTRAINT ") + qtIdent(name) 
            + wxT(" ON ") + table->GetQuotedFullIdentifier(), foreignKey);

    if (chkAutoIndex->GetValue())
    {
        sql += wxT("CREATE INDEX ") + qtIdent(txtIndexName->GetValue())
            +  wxT(" ON ") + table->GetQuotedFullIdentifier()
            +  wxT("(");

        int pos;
        for (pos=0 ; pos < lstColumns->GetItemCount() ; pos++)
        {
            if (pos)
                sql += wxT(", ");

            sql += qtIdent(lstColumns->GetText(pos));
        }

        sql += wxT(");\n");
    }
    return sql;
}
Esempio n. 20
0
wxString dlgForeignDataWrapper::GetSql()
{
	wxString sql, name;
	name = txtName->GetValue();

	if (fdw)
	{
		// edit mode
		sql = wxEmptyString;

		AppendNameChange(sql);

		if (cbHandler->GetValue() != fdw->GetHandlerProc())
		{
			if (cbHandler->GetValue().IsEmpty())
				sql += wxT("ALTER FOREIGN DATA WRAPPER ") + qtIdent(name)
				       + wxT("\n   NO HANDLER;\n");
			else
				sql += wxT("ALTER FOREIGN DATA WRAPPER ") + qtIdent(name)
				       + wxT("\n   HANDLER ") + qtIdent(cbHandler->GetValue())
				       + wxT(";\n");
		}

		if (cbValidator->GetValue() != fdw->GetValidatorProc())
		{
			if (cbValidator->GetValue().IsEmpty())
				sql += wxT("ALTER FOREIGN DATA WRAPPER ") + qtIdent(name)
				       + wxT("\n   NO VALIDATOR;\n");
			else
				sql += wxT("ALTER FOREIGN DATA WRAPPER ") + qtIdent(name)
				       + wxT("\n   VALIDATOR ") + qtIdent(cbValidator->GetValue())
				       + wxT(";\n");
		}

		wxString sqloptions = GetOptionsSql();
		if (sqloptions.Length() > 0)
		{
			sql += wxT("ALTER FOREIGN DATA WRAPPER ") + name
			       + wxT(" OPTIONS (") + sqloptions + wxT(");");
		}

		AppendOwnerChange(sql, wxT("FOREIGN DATA WRAPPER ") + qtIdent(name));
	}
	else
	{
		// create mode
		sql = wxT("CREATE FOREIGN DATA WRAPPER ") + qtIdent(name);
		AppendIfFilled(sql, wxT("\n   HANDLER "), qtIdent(cbHandler->GetValue()));
		AppendIfFilled(sql, wxT("\n   VALIDATOR "), qtIdent(cbValidator->GetValue()));

		// check for options
		if (lstOptions->GetItemCount() > 0)
		{
			wxString options = wxEmptyString;
			for (int pos = 0 ; pos < lstOptions->GetItemCount() ; pos++)
			{
				if (options.Length() > 0)
					options += wxT(", ");

				options += lstOptions->GetText(pos, 0)
				           + wxT(" '") + lstOptions->GetText(pos, 1) + wxT("' ");
			}
			sql += wxT("\n  OPTIONS (") + options + wxT(")");
		}

		sql += wxT(";\n");
		AppendOwnerNew(sql, wxT("FOREIGN DATA WRAPPER ") + qtIdent(name));
	}

	sql += GetGrant(wxT("U"), wxT("FOREIGN DATA WRAPPER ") + qtIdent(name));
	AppendComment(sql, wxT("FOREIGN DATA WRAPPER"), 0, fdw);

	return sql;
}
Esempio n. 21
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);
}
Esempio n. 22
0
wxString dlgRole::GetSql()
{
	int pos;
	wxString sql;
	wxString name = GetName();

	wxString passwd = txtPasswd->GetValue();
	bool createDB = chkCreateDB->GetValue(),
	     createRole = chkCreateRole->GetValue(),
	     superuser = chkSuperuser->GetValue(),
	     inherits = chkInherits->GetValue(),
	     canLogin = chkCanLogin->GetValue(),
	     replication = chkReplication->GetValue();

	if (role)
	{
		// Edit Mode

		AppendNameChange(sql, wxT("ROLE ") + role->GetQuotedFullIdentifier());


		wxString options;
		if (canLogin != role->GetCanLogin())
		{
			if (canLogin)
				options = wxT(" LOGIN");
			else
				options = wxT(" NOLOGIN");
		}
		if (canLogin && !passwd.IsEmpty())
			options += wxT(" ENCRYPTED PASSWORD ") + qtDbString(connection->EncryptPassword(name, passwd));

		if (createDB != role->GetCreateDatabase() || createRole != role->GetCreateRole()
		        || superuser != role->GetSuperuser() || inherits != role->GetInherits()
		        || replication != role->GetReplication())
		{
			options += wxT("\n ");

			if (superuser != role->GetSuperuser())
			{
				if (superuser)
					options += wxT(" SUPERUSER");
				else
					options += wxT(" NOSUPERUSER");
			}
			if (inherits != role->GetInherits())
			{
				if (inherits)
					options += wxT(" INHERIT");
				else
					options += wxT(" NOINHERIT");
			}
			if (createDB != role->GetCreateDatabase())
			{
				if (createDB)
					options += wxT(" CREATEDB");
				else
					options += wxT(" NOCREATEDB");
			}
			if (createRole != role->GetCreateRole())
			{
				if (createRole)
					options += wxT(" CREATEROLE");
				else
					options += wxT(" NOCREATEROLE");
			}
			if (connection->BackendMinimumVersion(9, 1))
			{
				if (replication != role->GetReplication())
				{
					if (replication)
						options += wxT(" REPLICATION");
					else
						options += wxT(" NOREPLICATION");
				}
			}
		}
		if (!datValidUntil->GetValue().IsValid() || DateToStr(datValidUntil->GetValue() + timValidUntil->GetValue()) != DateToStr(role->GetAccountExpires()))
		{
			if (datValidUntil->GetValue().IsValid())
				options += wxT("\n   VALID UNTIL ") + qtDbString(DateToAnsiStr(datValidUntil->GetValue() + timValidUntil->GetValue()));
			else if (!role->GetIsValidInfinity() && role->GetAccountExpires().GetValue() != -1)
				options += wxT("\n   VALID UNTIL 'infinity'");
		}

		if (txtConnectionLimit->GetValue().Length() == 0)
		{
			if (role->GetConnectionLimit() != -1)
			{
				options += wxT(" CONNECTION LIMIT -1");
			}
		}
		else
		{
			if (txtConnectionLimit->GetValue() != NumToStr(role->GetConnectionLimit()))
			{
				options += wxT(" CONNECTION LIMIT ") + txtConnectionLimit->GetValue();
			}
		}

		if (!options.IsNull())
			sql += wxT("ALTER ROLE ") + qtIdent(name) + options + wxT(";\n");

		if (!connection->BackendMinimumVersion(9, 5) &&
				chkUpdateCat->GetValue() != role->GetUpdateCatalog())
		{
			if (!connection->HasPrivilege(wxT("Table"), wxT("pg_authid"), wxT("update")))
				sql += wxT(" -- Can't update 'UpdateCatalog privilege: can't write to pg_authid.\n")
				       wxT("-- ");

			sql += wxT("UPDATE pg_authid SET rolcatupdate=") + BoolToStr(chkUpdateCat->GetValue())
			       + wxT(" WHERE OID=") + role->GetOidStr() + wxT(";\n");
		}
		int cnt = lbRolesIn->GetCount();
		wxArrayString tmpRoles = role->GetRolesIn();

		// check for added roles
		for (pos = 0 ; pos < cnt ; pos++)
		{
			wxString roleName = lbRolesIn->GetString(pos);

			int index = tmpRoles.Index(roleName);
			if (index >= 0)
			{
				// role membership unchanged
				tmpRoles.RemoveAt(index);
			}
			else
			{
				bool admin = false;
				if (roleName.Right(PGROLE_ADMINOPTION_LEN) == PGROLE_ADMINOPTION)
				{
					admin = true;
					roleName = roleName.Left(roleName.Length() - PGROLE_ADMINOPTION_LEN);
				}
				else
				{
					// new role membership without admin option
					index = tmpRoles.Index(roleName + PGROLE_ADMINOPTION);
					if (index >= 0)
					{
						// old membership with admin option
						tmpRoles.RemoveAt(index);
						sql += wxT("REVOKE ADMIN OPTION FOR ") + qtIdent(roleName)
						       + wxT(" FROM ") + qtIdent(name) + wxT(";\n");
						continue;
					}
				}

				index = tmpRoles.Index(roleName);
				if (index >= 0)
				{
					// admin option added to existing membership
					tmpRoles.RemoveAt(index);
				}

				sql += wxT("GRANT ") + qtIdent(roleName)
				       +  wxT(" TO ") + qtIdent(name);

				if (admin)
					sql += wxT(" WITH ADMIN OPTION");

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

		// check for removed roles
		for (pos = 0 ; pos < (int)tmpRoles.GetCount() ; pos++)
		{
			sql += wxT("REVOKE ") + qtIdent(tmpRoles.Item(pos))
			       +  wxT(" FROM ") + qtIdent(name) + wxT(";\n");
		}
	}
	else
	{
		// Create Mode
		sql = wxT(
		          "CREATE ROLE ") + qtIdent(name);
		if (canLogin)
		{
			sql += wxT(" LOGIN");
			if (!passwd.IsEmpty())
				sql += wxT(" ENCRYPTED PASSWORD ") + qtDbString(connection->EncryptPassword(name, passwd));
		}

		if (createDB || createRole || !inherits || superuser)
			sql += wxT("\n ");
		if (superuser)
			sql += wxT(" SUPERUSER");
		if (!inherits)
			sql += wxT(" NOINHERIT");
		if (createDB)
			sql += wxT(" CREATEDB");
		if (createRole)
			sql += wxT(" CREATEROLE");
		if (connection->BackendMinimumVersion(9, 1))
		{
			if (replication)
				sql += wxT(" REPLICATION");
		}
		if (datValidUntil->GetValue().IsValid())
			sql += wxT("\n   VALID UNTIL ") + qtDbString(DateToAnsiStr(datValidUntil->GetValue() + timValidUntil->GetValue()));
		else
			sql += wxT("\n   VALID UNTIL 'infinity'");

		if (txtConnectionLimit->GetValue().Length() > 0)
		{
			sql += wxT(" CONNECTION LIMIT ") + txtConnectionLimit->GetValue();
		}

		int cnt = lbRolesIn->GetCount();
		wxString grants;

		if (cnt)
		{
			wxString roleName;

			for (pos = 0 ; pos < cnt ; pos++)
			{
				bool admin = false;
				roleName = lbRolesIn->GetString(pos);
				if (roleName.Right(PGROLE_ADMINOPTION_LEN) == PGROLE_ADMINOPTION)
				{
					roleName = roleName.Left(roleName.Length() - PGROLE_ADMINOPTION_LEN);
					admin = true;

				}
				grants += wxT("GRANT ") + qtIdent(roleName)
				          +  wxT(" TO ") + qtIdent(name);

				if (admin)
					grants += wxT(" WITH ADMIN OPTION;\n");
				else
					grants += wxT(";\n");
			}
		}
		sql += wxT(";\n") + grants;

		if (superuser && !chkUpdateCat->GetValue() &&
			!connection->BackendMinimumVersion(9, 5))
			sql += wxT("UPDATE pg_authid SET rolcatupdate=false WHERE rolname=") + qtDbString(name) + wxT(";\n");
	}

	wxArrayString vars;
	wxString dbname;
	wxString parameter;
	wxString value;

	size_t index;

	if (role)
	{
		for (index = 0 ; index < role->GetVariables().GetCount() ; index++)
			vars.Add(role->GetVariables().Item(index));
	}

	int cnt = lstVariables->GetItemCount();

	// check for changed or added vars
	for (pos = 0 ; pos < cnt ; pos++)
	{
		wxString newDb = 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)
		{
			dbname = vars.Item(index);
			parameter = vars.Item(index + 1);
			value = vars.Item(index + 2);

			if (newDb == dbname && newVar == parameter)
			{
				oldVal = value;
				vars.RemoveAt(index);
				vars.RemoveAt(index);
				vars.RemoveAt(index);
				break;
			}
		}
		if (oldVal != newVal)
		{
			if (newDb.Length() == 0)
				sql += wxT("ALTER ROLE ") + qtIdent(name);
			else
				sql += wxT("ALTER ROLE ") + qtIdent(name) + wxT(" IN DATABASE ") + newDb;

			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)
	{
		dbname = vars.Item(pos);
		parameter = vars.Item(pos + 1);
		value = vars.Item(pos + 2);

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

	AppendComment(sql, wxT("ROLE"), 0, role);

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

	return sql;
}
wxString dlgTablespace::GetSql()
{
    wxString sql;
    wxString name=GetName();    

    if (tablespace)
    {
        // Edit Mode

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

        sql += GetGrant(wxT("C"), wxT("TABLESPACE ") + qtIdent(name));
        AppendComment(sql, wxT("TABLESPACE"), 0, tablespace);

        wxArrayString vars;

        size_t index;

        for (index = 0 ; index < tablespace->GetVariables().GetCount() ; index++)
            vars.Add(tablespace->GetVariables().Item(index));

        int cnt=lstVariables->GetItemCount();
        int pos;

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

            wxString oldVal;

            for (index=0 ; index < vars.GetCount() ; index++)
            {
                wxString var=vars.Item(index);
                if (var.BeforeFirst('=').IsSameAs(newVar, false))
                {
                    oldVal = var.Mid(newVar.Length()+1);
                    vars.RemoveAt(index);
                    break;
                }
            }
            if (oldVal != newVal)
            {
                sql += wxT("ALTER TABLESPACE ") + qtIdent(name)
                    +  wxT(" SET (") + newVar
                    +  wxT("=") + newVal
                    +  wxT(");\n");
            }
        }
        
        // check for removed vars
        for (pos=0 ; pos < (int)vars.GetCount() ; pos++)
        {
            sql += wxT("ALTER TABLESPACE ") + qtIdent(name)
                +  wxT(" RESET (") + vars.Item(pos).BeforeFirst('=')
                + wxT(");\n");
        }
    }
    else
    {
        // Create Mode
        sql = wxT("CREATE TABLESPACE ") + qtIdent(name);
        AppendIfFilled(sql, wxT(" OWNER "), qtIdent(cbOwner->GetValue()));
        sql += wxT(" LOCATION ") + qtDbString(txtLocation->GetValue())
            +  wxT(";\n");
    }


    return sql;
}
wxString dlgIndex::GetSql()
{
    wxString sql;

    if (table)
    {
        wxString name=GetName();
        if (!index)
        {
            sql = wxT("CREATE ");
            if (chkUnique->GetValue())
                sql += wxT("UNIQUE ");

            sql += wxT("INDEX ");

            if (chkConcurrent->GetValue())
                sql += wxT("CONCURRENTLY ");

            sql += qtIdent(name);

            sql += wxT("\n   ON ") + table->GetQuotedFullIdentifier();

            if (cbType->GetCurrentSelection() > 0)
                AppendIfFilled(sql, wxT(" USING "), cbType->GetValue());

            sql += wxT(" (") + GetColumns()
                + wxT(")");

            if (txtFillFactor)
            {
                if (connection->BackendMinimumVersion(8, 2) && txtFillFactor->GetValue().Length() > 0)
                    sql += wxT("\n  WITH (FILLFACTOR=") + txtFillFactor->GetValue() + wxT(")");
            }

            if (cbTablespace->GetOIDKey() > 0)
                AppendIfFilled(sql, wxT("\n  TABLESPACE "), qtIdent(cbTablespace->GetValue()));

            AppendIfFilled(sql, wxT(" WHERE "), txtWhere->GetValue());
            sql +=  wxT(";\n");
        }
        else
        {
            if (connection->BackendMinimumVersion(8, 2) && txtFillFactor->GetValue().Length() > 0)
                sql += wxT("ALTER INDEX ") + qtIdent(index->GetSchema()->GetName()) + wxT(".")
                    + qtIdent(index->GetName()) +  wxT(" SET (FILLFACTOR=")
                    + txtFillFactor->GetValue() + wxT(");\n");

            if(connection->BackendMinimumVersion(8, 0))
            {
                if (index->GetName() != txtName->GetValue() &&
                    !txtName->GetValue().IsEmpty())
                    sql += wxT("ALTER INDEX ") + qtIdent(index->GetSchema()->GetName()) + wxT(".")
                        + qtIdent(index->GetName()) +  wxT(" RENAME TO ")
                        + qtIdent(txtName->GetValue()) + wxT(";\n");

                if (cbTablespace->GetOIDKey() != index->GetTablespaceOid())
                    sql += wxT("ALTER INDEX ") + qtIdent(index->GetSchema()->GetName()) + wxT(".") + qtIdent(name) 
                        +  wxT(" SET TABLESPACE ") + qtIdent(cbTablespace->GetValue())
                        +  wxT(";\n");
            }
        }
        if (connection->BackendMinimumVersion(7, 4))
        {
            if (index && index->GetIsClustered() && !chkClustered->GetValue())
                sql += wxT("ALTER TABLE ") + table->GetQuotedFullIdentifier()
                    +  wxT(" SET WITHOUT CLUSTER;\n");
            else if (chkClustered->GetValue() && (!index || !index->GetIsClustered()))
                sql += wxT("ALTER TABLE ") + table->GetQuotedFullIdentifier()
                    +  wxT(" CLUSTER ON ") + qtIdent(name) + wxT(";\n");
        }

        AppendComment(sql, wxT("INDEX"), table->GetSchema(), index);
    }
    return sql;
}
Esempio n. 25
0
wxString dlgOperator::GetSql()
{
	wxString sql, name;

	if (oper)
	{
		// edit mode
		name = oper->GetQuotedFullIdentifier()
		       + wxT("(") + oper->GetOperands() + wxT(")");

		AppendOwnerChange(sql, wxT("OPERATOR ") + name);
		AppendSchemaChange(sql, wxT("OPERATOR ") + name);
		name = qtIdent(cbSchema->GetValue()) + wxT(".") + GetName()
		       + wxT("(") + oper->GetOperands() + wxT(")");
	}
	else
	{
		// create mode
		name = qtIdent(cbSchema->GetValue()) + wxT(".") + GetName() + wxT("(");
		if (cbLeftType->GetGuessedSelection() > 0)
			name += GetQuotedTypename(cbLeftType->GetGuessedSelection());
		else
			name += wxT("NONE");
		name += wxT(", ");
		if (cbRightType->GetGuessedSelection() > 0)
			name += GetQuotedTypename(cbRightType->GetGuessedSelection());
		else
			name += wxT("NONE");
		name += wxT(")");


		sql = wxT("CREATE OPERATOR ") + qtIdent(cbSchema->GetValue()) + wxT(".") + GetName()
		      + wxT("(\n   PROCEDURE=") + procedures.Item(cbProcedure->GetGuessedSelection());

		AppendIfFilled(sql, wxT(",\n   LEFTARG="), GetQuotedTypename(cbLeftType->GetGuessedSelection()));
		AppendIfFilled(sql, wxT(",\n   RIGHTARG="), GetQuotedTypename(cbRightType->GetGuessedSelection()));
		AppendIfFilled(sql, wxT(",\n   COMMUTATOR="), cbCommutator->GetValue().Trim());
		AppendIfFilled(sql, wxT(",\n   NEGATOR="), cbNegator->GetValue().Trim());

		if (cbLeftType->GetGuessedSelection() > 0 && cbRightType->GetGuessedSelection() > 0)
		{
			if (cbRestrict->GetCurrentSelection() > 0)
				sql += wxT(",\n   RESTRICT=") + procedures.Item(cbRestrict->GetCurrentSelection() - 1);
			if (cbJoin->GetCurrentSelection() > 0)
				sql += wxT(",\n   JOIN=") + procedures.Item(cbJoin->GetCurrentSelection() - 1);

			if (!connection->BackendMinimumVersion(8, 3))
			{
				AppendFilledOperator(sql, wxT(",\n   SORT1="), cbLeftSort);
				AppendFilledOperator(sql, wxT(",\n   SORT2="), cbRightSort);
				AppendFilledOperator(sql, wxT(",\n   LTCMP="), cbLess);
				AppendFilledOperator(sql, wxT(",\n   GTCMP="), cbGreater);
			}

			if (chkCanMerge->GetValue() || chkCanHash->GetValue())
			{
				sql += wxT(",\n   ");
				if (chkCanHash->GetValue())
				{
					if (chkCanMerge->GetValue())
						sql += wxT("HASHES, MERGES");
					else
						sql += wxT("HASHES");
				}
				else if (chkCanMerge->GetValue())
					sql += wxT("MERGES");
			}
		}
		sql += wxT(");\n");
		AppendOwnerChange(sql, wxT("OPERATOR ") + name);
	}
	AppendComment(sql, wxT("OPERATOR ") + name, oper);

	return sql;
}
Esempio n. 26
0
wxString dlgDomain::GetSql()
{
	wxString sql, name;

	if (domain)
	{
		// edit mode
		name = GetName();

		if (txtName->GetValue() != domain->GetName())
		{

	        if (connection->BackendMinimumVersion(9, 2))
                AppendNameChange(sql, wxT("DOMAIN ") + domain->GetQuotedFullIdentifier());
            else
                AppendNameChange(sql, wxT("TYPE ") + domain->GetQuotedFullIdentifier());
		}
		if (chkNotNull->GetValue() != domain->GetNotNull())
		{
			sql += wxT("ALTER DOMAIN ") + domain->GetQuotedFullIdentifier();
			if (chkNotNull->GetValue())
				sql += wxT("\n  SET NOT NULL;\n");
			else
				sql += wxT("\n  DROP NOT NULL;\n");
		}
		if (txtDefault->GetValue() != domain->GetDefault())
		{
			sql += wxT("ALTER DOMAIN ") + domain->GetQuotedFullIdentifier();
			if (txtDefault->GetValue().IsEmpty())
				sql += wxT("\n  DROP DEFAULT;\n");
			else
				sql += wxT("\n  SET DEFAULT ") + txtDefault->GetValue() + wxT(";\n");
		}
		if (txtCheck->GetValue() != domain->GetCheck())
		{
			if (!domain->GetCheck().IsEmpty())
				sql += wxT("ALTER DOMAIN ") + schema->GetQuotedPrefix() + qtIdent(name)
				       + wxT(" DROP CONSTRAINT ") + qtIdent(domain->GetCheckConstraintName());

			if (!txtCheck->GetValue().IsEmpty())
			{
				sql += wxT("ALTER DOMAIN ") + schema->GetQuotedPrefix() + qtIdent(name)
				       + wxT(" ADD ");
				if (!domain->GetCheck().IsEmpty())
					sql += wxT("CONSTRAINT ") + qtIdent(domain->GetCheckConstraintName());
				sql += wxT("\n   CHECK (") + txtCheck->GetValue() + wxT(")");
				if (chkDontValidate->GetValue())
					sql += wxT(" NOT VALID");
			}
		}
		if (chkDontValidate->IsEnabled() && !domain->GetValid() && !chkDontValidate->GetValue())
		{
			sql += wxT("ALTER DOMAIN ") + schema->GetQuotedPrefix() + qtIdent(name)
			       + wxT(" VALIDATE CONSTRAINT ") + qtIdent(domain->GetCheckConstraintName()) + wxT(";\n");
		}
		AppendOwnerChange(sql, wxT("DOMAIN ") + domain->GetQuotedFullIdentifier());
		AppendSchemaChange(sql, wxT("DOMAIN ") + domain->GetQuotedFullIdentifier());
	}
	else
	{
		// create mode
		name = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());
		sql = wxT("CREATE DOMAIN ") + name
		      + wxT("\n   AS ") + GetQuotedTypename(cbDatatype->GetGuessedSelection());

		if (!cbCollation->GetValue().IsEmpty() && cbCollation->GetValue() != wxT("pg_catalog.\"default\""))
			sql += wxT("\n   COLLATE ") + cbCollation->GetValue();

		if (chkDontValidate->GetValue())
			sql += wxT(";\nALTER DOMAIN ") + name + wxT(" ADD ");

		AppendIfFilled(sql, wxT("\n   DEFAULT "), txtDefault->GetValue());
		if (chkNotNull->GetValue())
			sql += wxT("\n   NOT NULL");
		if (!txtCheck->GetValue().IsEmpty())
			sql += wxT("\n   CHECK (") + txtCheck->GetValue() + wxT(")");

		if (chkDontValidate->GetValue())
			sql += wxT(" NOT VALID");

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

		AppendOwnerNew(sql, wxT("DOMAIN ") + name);
	}

	AppendComment(sql, wxT("DOMAIN ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), domain);

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

	return sql;
}
Esempio n. 27
0
wxString dlgDomain::GetSql()
{
	wxString sql, name;
	int pos;
	wxString definition;
	int index = -1;
	wxArrayString tmpDef = previousConstraints;
	wxString tmpsql = wxEmptyString;

	if (domain)
	{
		// edit mode
		name = GetName();

		if (txtName->GetValue() != domain->GetName())
		{

			if (connection->BackendMinimumVersion(9, 2))
				AppendNameChange(sql, wxT("DOMAIN ") + domain->GetQuotedFullIdentifier());
			else
				AppendNameChange(sql, wxT("TYPE ") + domain->GetQuotedFullIdentifier());
		}
		if (chkNotNull->GetValue() != domain->GetNotNull())
		{
			sql += wxT("ALTER DOMAIN ") + domain->GetQuotedFullIdentifier();
			if (chkNotNull->GetValue())
				sql += wxT("\n  SET NOT NULL;\n");
			else
				sql += wxT("\n  DROP NOT NULL;\n");
		}
		if (txtDefault->GetValue() != domain->GetDefault())
		{
			sql += wxT("ALTER DOMAIN ") + domain->GetQuotedFullIdentifier();
			if (txtDefault->GetValue().IsEmpty())
				sql += wxT("\n  DROP DEFAULT;\n");
			else
				sql += wxT("\n  SET DEFAULT ") + txtDefault->GetValue() + wxT(";\n");
		}

		// Build a temporary list of ADD CONSTRAINTs, and fixup the list to remove
		for (pos = 0; pos < lstConstraints->GetItemCount() ; pos++)
		{
			wxString conname = qtIdent(lstConstraints->GetItemText(pos));
			definition = conname;
			definition += wxT(" CHECK ") + constraintsDefinition.Item(pos);
			index = tmpDef.Index(definition);
			if (index >= 0)
				tmpDef.RemoveAt(index);
			else
			{
				tmpsql += wxT("ALTER DOMAIN ") + domain->GetQuotedFullIdentifier()
				          +  wxT("\n  ADD");
				if (!conname.IsEmpty())
					tmpsql += wxT(" CONSTRAINT ");

				tmpsql += definition + wxT(";\n");
			}
		}

		// Add the DROP CONSTRAINTs...
		for (index = 0 ; index < (int)tmpDef.GetCount() ; index++)
		{
			definition = tmpDef.Item(index);
			if (definition[0U] == '"')
				definition = definition.Mid(1).BeforeFirst('"');
			else
				definition = definition.BeforeFirst(' ');
			sql += wxT("ALTER DOMAIN ") + domain->GetQuotedFullIdentifier()
			       + wxT("\n  DROP CONSTRAINT ") + qtIdent(definition) + wxT(";\n");

		}

		// Add the ADD CONSTRAINTs...
		sql += tmpsql;

		AppendOwnerChange(sql, wxT("DOMAIN ") + domain->GetQuotedFullIdentifier());
		AppendSchemaChange(sql, wxT("DOMAIN ") + domain->GetQuotedFullIdentifier());
	}
	else
	{
		// create mode
		name = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());
		sql = wxT("CREATE DOMAIN ") + name
		      + wxT("\n   AS ") + GetQuotedTypename(cbDatatype->GetGuessedSelection());

		if (!cbCollation->GetValue().IsEmpty() && cbCollation->GetValue() != wxT("pg_catalog.\"default\""))
			sql += wxT("\n   COLLATE ") + cbCollation->GetValue();

		AppendIfFilled(sql, wxT("\n   DEFAULT "), txtDefault->GetValue());

		if (chkNotNull->GetValue())
			sql += wxT("\n   NOT NULL");

		for (pos = 0 ; pos < lstConstraints->GetItemCount() ; pos++)
		{
			wxString name = lstConstraints->GetItemText(pos);
			wxString definition = constraintsDefinition.Item(pos);
			if (!name.IsEmpty())
				sql += wxT("\n   CONSTRAINT ") + qtIdent(name) + wxT(" CHECK ") + definition;
			else
				sql += wxT("\n   CHECK ") + definition;
		}

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

		AppendOwnerNew(sql, wxT("DOMAIN ") + name);
	}

	AppendComment(sql, wxT("DOMAIN ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), domain);

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

	return sql;
}
Esempio n. 28
0
wxString dlgSequence::GetSql()
{
	wxString sql, name;

	if (sequence)
	{
		// edit mode
		name = GetName();

		if (connection->BackendMinimumVersion(8, 3))
			AppendNameChange(sql, wxT("SEQUENCE ") + sequence->GetQuotedFullIdentifier());
		else
			AppendNameChange(sql, wxT("TABLE ") + sequence->GetQuotedFullIdentifier());

		if (connection->BackendMinimumVersion(8, 4))
			AppendOwnerChange(sql, wxT("SEQUENCE ") + schema->GetQuotedPrefix() + qtIdent(name));
		else
			AppendOwnerChange(sql, wxT("TABLE ") + schema->GetQuotedPrefix() + qtIdent(name));

		// This is where things get hairy. Per some thought by Horvath Gabor,
		// we need to adjust the min/max sequence values, and the the current
		// value per the rules:
		//
		// 1 Any ALTER SEQUENCE MIN/MAXVALUE statements that widen the range
		// 2 SETVAL
		// 3 Any ALTER SEQUENCE MIN/MAXVALUE statements that narrow the range.
		//
		// We'll change any other options at the end.
		wxString tmp;

		// MIN/MAX changes that widen the range.
		if (connection->BackendMinimumVersion(7, 4))
		{
			tmp = wxEmptyString;
			if (txtMin->GetValue().IsEmpty())
				tmp += wxT("\n   NO MINVALUE");
			else if (StrToLongLong(txtMin->GetValue()) < sequence->GetMinValue())
				tmp += wxT("\n   MINVALUE ") + txtMin->GetValue();

			if (txtMax->GetValue().IsEmpty())
				tmp += wxT("\n   NO MAXVALUE");
			else if (StrToLongLong(txtMax->GetValue()) > sequence->GetMaxValue())
				tmp += wxT("\n   MAXVALUE ") + txtMax->GetValue();

			if (!tmp.IsEmpty())
			{
				sql += wxT("ALTER SEQUENCE ") + schema->GetQuotedPrefix() + qtIdent(name)
				       +  tmp + wxT(";\n");
			}
		}

		// The new sequence value
		if (txtStart->GetValue() != sequence->GetLastValue().ToString())
			sql += wxT("SELECT setval('") + qtIdent(schema->GetName()) + wxT(".") + qtIdent(name)
			       +  wxT("', ") + txtStart->GetValue()
			       +  wxT(", true);\n");

		// Min/Max changes that narrow the ranges, as well as other changes.
		if (connection->BackendMinimumVersion(7, 4))
		{
			tmp = wxEmptyString;
			if (txtIncrement->GetValue() != sequence->GetIncrement().ToString())
				tmp += wxT("\n   INCREMENT ") + txtIncrement->GetValue();

			if ((!txtMin->GetValue().IsEmpty()) && StrToLongLong(txtMin->GetValue()) > sequence->GetMinValue())
				tmp += wxT("\n   MINVALUE ") + txtMin->GetValue();

			if ((!txtMax->GetValue().IsEmpty()) && StrToLongLong(txtMax->GetValue()) < sequence->GetMaxValue())
				tmp += wxT("\n   MAXVALUE ") + txtMax->GetValue();

			if (txtCache->GetValue() != sequence->GetCacheValue().ToString())
				tmp += wxT("\n   CACHE ") + txtCache->GetValue();

			if (chkCycled->GetValue() != sequence->GetCycled())
			{
				if (chkCycled->GetValue())
					tmp += wxT("\n   CYCLE");
				else
					tmp += wxT("\n   NO CYCLE");
			}

			if (!tmp.IsEmpty())
			{
				sql += wxT("ALTER SEQUENCE ") + schema->GetQuotedPrefix() + qtIdent(name)
				       +  tmp + wxT(";\n");
			}

			if (connection->BackendMinimumVersion(8, 1))
				AppendSchemaChange(sql,  wxT("SEQUENCE ") + schema->GetQuotedPrefix() + qtIdent(name));
		}
	}
	else
	{
		name = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());

		// create mode
		sql = wxT("CREATE SEQUENCE ") + name;
		if (chkCycled->GetValue())
			sql += wxT(" CYCLE");
		AppendIfFilled(sql, wxT("\n   INCREMENT "), txtIncrement->GetValue());
		AppendIfFilled(sql, wxT("\n   START "), txtStart->GetValue());
		AppendIfFilled(sql, wxT("\n   MINVALUE "), txtMin->GetValue());
		AppendIfFilled(sql, wxT("\n   MAXVALUE "), txtMax->GetValue());
		AppendIfFilled(sql, wxT("\n   CACHE "), txtCache->GetValue());
		sql += wxT(";\n");

		if (cbOwner->GetGuessedSelection() > 0)
		{
			if (connection->BackendMinimumVersion(8, 4))
			{
				AppendOwnerChange(sql, wxT("SEQUENCE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));
			}
			else
			{
				AppendOwnerChange(sql, wxT("TABLE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));
			}
		}
	}

	if (!connection->BackendMinimumVersion(8, 2))
		sql +=  GetGrant(wxT("arwdRxt"), wxT("TABLE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));
	else
		sql +=  GetGrant(wxT("rwU"), wxT("TABLE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));

	AppendComment(sql, wxT("SEQUENCE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), sequence);

	if (seclabelPage && connection->BackendMinimumVersion(9, 1))
		sql += seclabelPage->GetSqlForSecLabels(wxT("SEQUENCE"), qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));

	return sql;
}
Esempio n. 29
0
wxString dlgTrigger::GetSql()
{
	wxString sql;
	wxString name = GetName();

	if (trigger)
	{
		if (name != trigger->GetName())
			sql = wxT("ALTER TRIGGER ") + trigger->GetQuotedIdentifier() + wxT(" ON ") + table->GetQuotedFullIdentifier()
			      + wxT("\n  RENAME TO ") + qtIdent(name) + wxT(";\n\n");
	}

	if (!trigger ||
	        (cbFunction->GetValue() == wxString::Format(wxT("<%s>"), _("Inline EDB-SPL"))
	         && (
	             txtBody->GetText() != trigger->GetSource() ||
	             chkRow->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_ROW) ||
	             chkInsert->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_INSERT ? true : false) ||
	             chkUpdate->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_UPDATE ? true : false) ||
	             chkDelete->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_DELETE ? true : false) ||
	             chkTruncate->GetValue() != (trigger->GetTriggerType() & TRIGGER_TYPE_TRUNCATE ? true : false) ||
	             rdbFires->GetSelection() != (trigger->GetTriggerType() & TRIGGER_TYPE_BEFORE ? 0 : TRIGGER_TYPE_INSTEAD ? 2 : 1)
	         )
	        )
	   )
	{
		if (cbFunction->GetValue() == wxString::Format(wxT("<%s>"), _("Inline EDB-SPL")))
			sql += wxT("CREATE OR REPLACE TRIGGER ");
		else if (chkConstraint->GetValue())
			sql += wxT("CREATE CONSTRAINT TRIGGER ");
		else
			sql += wxT("CREATE TRIGGER ");
		sql += qtIdent(name);

		if (rdbFires->GetSelection() == 1)
			sql += wxT(" AFTER");
		else if (rdbFires->GetSelection() == 2)
			sql += wxT(" INSTEAD OF");
		else
			sql += wxT(" BEFORE");
		int actionCount = 0;
		if (chkInsert->GetValue())
		{
			if (actionCount++)
				sql += wxT(" OR");
			sql += wxT(" INSERT");
		}
		if (chkUpdate->GetValue())
		{
			if (actionCount++)
				sql += wxT(" OR");
			sql += wxT(" UPDATE");
			if (lstColumns->GetItemCount() > 0)
				sql += wxT(" OF ") + GetColumns();
		}
		if (chkDelete->GetValue())
		{
			if (actionCount++)
				sql += wxT(" OR");
			sql += wxT(" DELETE");
		}
		if (chkTruncate->GetValue())
		{
			if (actionCount++)
				sql += wxT(" OR");
			sql += wxT(" TRUNCATE");
		}
		sql += wxT("\n   ON ") + table->GetQuotedFullIdentifier();
		if (chkDeferrable->GetValue())
		{
			sql += wxT(" DEFERRABLE");
			if (chkDeferred->GetValue())
				sql += wxT(" INITIALLY DEFERRED");
		}
		sql += wxT(" FOR EACH ");
		if (chkRow->GetValue())
			sql += wxT("ROW");
		else
			sql += wxT("STATEMENT");

		if (connection->BackendMinimumVersion(8, 5) && !txtWhen->GetValue().IsEmpty())
			sql += wxT("\n   WHEN (") + txtWhen->GetValue() + wxT(")");

		if (cbFunction->GetValue() != wxString::Format(wxT("<%s>"), _("Inline EDB-SPL")))
		{
			sql += wxT("\n   EXECUTE PROCEDURE ") + cbFunction->GetValue()
			       + wxT("(") + txtArguments->GetValue()
			       + wxT(");\n");
		}
		else
		{
			sql += wxT("\n") + txtBody->GetText();
			if (!sql.Trim().EndsWith(wxT(";")))
				sql = sql.Trim() + wxT(";");
			sql += wxT("\n");
		}
	}
	AppendComment(sql, wxT("TRIGGER ") + qtIdent(GetName())
	              + wxT(" ON ") + table->GetQuotedFullIdentifier(), trigger);

	return sql;
}
Esempio n. 30
0
wxString dlgView::GetSql()
{
	wxString sql;
	wxString name;
	wxString withoptions = wxEmptyString;
	bool editQuery = false;

	if (view)
	{
		// edit mode
		name = GetName();

		if (name != view->GetName())
		{
			if (connection->BackendMinimumVersion(8, 3))
			{
				if (connection->BackendMinimumVersion(9, 3))
				{
					if (view->GetMaterializedView())
					{
						AppendNameChange(sql, wxT("MATERIALIZED VIEW ") + view->GetQuotedFullIdentifier());
						editQuery = true;
					}
					else
						AppendNameChange(sql, wxT("VIEW ") + view->GetQuotedFullIdentifier());
				}
				else
					AppendNameChange(sql, wxT("VIEW ") + view->GetQuotedFullIdentifier());
			}
			else
				AppendNameChange(sql, wxT("TABLE ") + view->GetQuotedFullIdentifier());
		}

		if (connection->BackendMinimumVersion(8, 4) && cbSchema->GetName() != view->GetSchema()->GetName())
		{
			if (connection->BackendMinimumVersion(9, 3))
			{
				if (view->GetMaterializedView())
				{
					AppendSchemaChange(sql, wxT("MATERIALIZED VIEW " + qtIdent(view->GetSchema()->GetName()) + wxT(".") + qtIdent(name)));
					editQuery = true;
				}
				else
					AppendSchemaChange(sql, wxT("VIEW " + qtIdent(view->GetSchema()->GetName()) + wxT(".") + qtIdent(name)));
			}
			else
				AppendSchemaChange(sql, wxT("VIEW " + qtIdent(view->GetSchema()->GetName()) + wxT(".") + qtIdent(name)));
		}
		else
			AppendSchemaChange(sql, wxT("TABLE " + qtIdent(view->GetSchema()->GetName()) + wxT(".") + qtIdent(name)));
	}

	name = qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName());
	if (!view || txtSqlBox->GetText().Trim(true).Trim(false) != oldDefinition.Trim(true).Trim(false))
	{
		if (editQuery)
		{
			// Delete the materialized view query
			sql += wxT("DROP MATERIALIZED VIEW ") + name + wxT(";");
		}

		// Check if user creates the materialized view
		if (!chkMaterializedView->GetValue())
		{
			sql += wxT("CREATE OR REPLACE VIEW ") + name;

			if (connection->BackendMinimumVersion(9, 2) && chkSecurityBarrier->GetValue())
				withoptions += wxT("security_barrier=true");
			if (connection->BackendMinimumVersion(9, 4) && cbCheckOption->GetSelection() > 0)
			{
				if (withoptions.Length() > 0)
					withoptions += wxT(", ");
				withoptions += wxT("check_option=") + cbCheckOption->GetValue().Lower();
			}

			if (withoptions.Length() > 0)
				sql += wxT(" WITH (") + withoptions + wxT(")");

			sql += wxT(" AS\n")
			       + txtSqlBox->GetText().Trim(true).Trim(false)
			       + wxT(";\n");
		}
		else if (connection->BackendMinimumVersion(9, 3) && chkMaterializedView->GetValue())
		{
			sql += wxT("CREATE MATERIALIZED VIEW ") + name;

			// Add the parameter of tablespace and storage parameter to create the materilized view
			if (txtFillFactor->GetValue().Trim().Length() > 0 || chkVacEnabled->GetValue() == true || chkToastVacEnabled->GetValue() == true)
			{
				bool fillFactorFlag, toastTableFlag;
				fillFactorFlag = false;
				toastTableFlag = false;

				sql += wxT("\nWITH (");

				if (txtFillFactor->GetValue().Trim().Length() > 0)
				{
					sql += wxT("\n  FILLFACTOR = ") + txtFillFactor->GetValue();
					fillFactorFlag = true;
				}

				bool valChanged = false;
				wxString newVal;
				wxString resetStr;

				if (connection->BackendMinimumVersion(9, 3) && chkCustomVac->GetValue())
				{
					FillAutoVacuumParameters(sql, resetStr, wxT("autovacuum_enabled"), BoolToStr(chkVacEnabled->GetValue()));

					if (!fillFactorFlag)
					{
						int position = sql.Find(',', true);
						if (position != wxNOT_FOUND)
							sql.Remove(position, 1);
						toastTableFlag = true;
					}

					newVal =  AppendNum(valChanged, txtBaseVac, tableVacBaseThr);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("autovacuum_vacuum_threshold"), newVal);
					}

					newVal = AppendNum(valChanged, txtBaseAn, tableAnlBaseThr);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("autovacuum_analyze_threshold"), newVal);
					}

					newVal = AppendNum(valChanged, txtFactorVac, tableVacFactor);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("autovacuum_vacuum_scale_factor"), newVal);
					}

					newVal = AppendNum(valChanged, txtFactorAn, tableAnlFactor);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("autovacuum_analyze_scale_factor"), newVal);
					}

					newVal = AppendNum(valChanged, txtVacDelay, tableCostDelay);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("autovacuum_vacuum_cost_delay"), newVal);
					}

					newVal = AppendNum(valChanged, txtVacLimit, tableCostLimit);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("autovacuum_vacuum_cost_limit"), newVal);
					}

					newVal = AppendNum(valChanged, txtFreezeMinAge, tableFreezeMinAge);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("autovacuum_freeze_min_age"), newVal);
					}

					newVal = AppendNum(valChanged, txtFreezeMaxAge, tableFreezeMaxAge);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("autovacuum_freeze_max_age"), newVal);
					}

					newVal = AppendNum(valChanged, txtFreezeTableAge, tableFreezeTableAge);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("autovacuum_freeze_table_age"), newVal);
					}
				}

				if (connection->BackendMinimumVersion(9, 3) && chkCustomToastVac->GetValue())
				{
					FillAutoVacuumParameters(sql, resetStr, wxT("toast.autovacuum_enabled"), BoolToStr(chkToastVacEnabled->GetValue()));

					if (!fillFactorFlag && !toastTableFlag)
					{
						int position = sql.Find(',', true);
						if (position != wxNOT_FOUND)
							sql.Remove(position, 1);
					}

					newVal =  AppendNum(valChanged, txtBaseToastVac, toastTableVacBaseThr);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("toast.autovacuum_vacuum_threshold"), newVal);
					}

					newVal = AppendNum(valChanged, txtFactorToastVac, toastTableVacFactor);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("toast.autovacuum_vacuum_scale_factor"), newVal);
					}

					newVal = AppendNum(valChanged, txtToastVacDelay, toastTableCostDelay);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("toast.autovacuum_vacuum_cost_delay"), newVal);
					}

					newVal = AppendNum(valChanged, txtToastVacLimit, toastTableCostLimit);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("toast.autovacuum_vacuum_cost_limit"), newVal);
					}

					newVal = AppendNum(valChanged, txtToastFreezeMinAge, toastTableFreezeMinAge);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("toast.autovacuum_freeze_min_age"), newVal);
					}

					newVal = AppendNum(valChanged, txtToastFreezeMaxAge, toastTableFreezeMaxAge);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("toast.autovacuum_freeze_max_age"), newVal);
					}

					newVal = AppendNum(valChanged, txtToastFreezeTableAge, toastTableFreezeTableAge);
					if (valChanged)
					{
						valChanged = false;
						FillAutoVacuumParameters(sql, resetStr, wxT("toast.autovacuum_freeze_table_age"), newVal);
					}
				}

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

			if (cboTablespace->GetCurrentSelection() > 0 && cboTablespace->GetOIDKey() > 0)
				sql += wxT("\nTABLESPACE ") + qtIdent(cboTablespace->GetValue());

			wxString sqlDefinition;
			bool tmpLoopFlag = true;
			sqlDefinition = txtSqlBox->GetText().Trim(true).Trim(false);

			// Remove semicolon from the end of the string
			while(tmpLoopFlag)
			{
				int length = sqlDefinition.Len();
				int position = sqlDefinition.Find(';', true);
				if ((position != wxNOT_FOUND) && (position = (length - 1)))
					sqlDefinition.Remove(position, 1);
				else
					tmpLoopFlag = false;
			}

			sql += wxT(" AS\n")
			       + sqlDefinition;

			if (chkMatViewWithData->GetValue())
				sql += wxT("\n WITH DATA;\n");
			else
				sql += wxT("\n WITH NO DATA;\n");
		}
	}
	else if (view)
	{
		if (!chkMaterializedView->GetValue())
		{
			if (connection->BackendMinimumVersion(9, 2))
			{
				if (chkSecurityBarrier->GetValue() && view->GetSecurityBarrier() != wxT("true"))
					sql += wxT("ALTER VIEW ") + name + wxT("\n  SET (security_barrier=true);\n");
				else if (!chkSecurityBarrier->GetValue() && view->GetSecurityBarrier() == wxT("true"))
					sql += wxT("ALTER VIEW ") + name + wxT("\n  SET (security_barrier=false);\n");
			}

			if (connection->BackendMinimumVersion(9, 4)
			        && cbCheckOption->GetValue().Lower().Cmp(view->GetCheckOption()) != 0)
			{
				if (cbCheckOption->GetValue().Cmp(wxT("No")) == 0)
					sql += wxT("ALTER VIEW ") + name + wxT(" RESET (check_option);\n");
				else
					sql += wxT("ALTER VIEW ") + name + wxT("\n  SET (check_option=") + cbCheckOption->GetValue().Lower() + wxT(");\n");
			}

			if (withoptions.Length() > 0)
				sql += wxT(" WITH (") + withoptions + wxT(")");
		}
		else if (connection->BackendMinimumVersion(9, 3) && chkMaterializedView->GetValue())
		{
			if (txtFillFactor->GetValue() != view->GetFillFactor())
			{
				// If fill factor value get changed then set the new value
				if (txtFillFactor->GetValue().Trim().Length() > 0)
				{
					sql += wxT("ALTER MATERIALIZED VIEW ") + name
					       +  wxT("\n  SET (FILLFACTOR=")
					       +  txtFillFactor->GetValue() + wxT(");\n");
				}
				else
				{
					// If fill factor value get changed and value is not blank then do the reset
					sql += wxT("ALTER MATERIALIZED VIEW ") + name
					       +  wxT(" RESET(\n")
					       wxT("  FILLFACTOR\n")
					       wxT(");\n");
				}
			}

			bool isPopulatedFlag = false;

			if (view->GetIsPopulated().Cmp(wxT("t")) == 0)
				isPopulatedFlag = true;

			if (chkMatViewWithData->GetValue() != isPopulatedFlag)
			{
				// If checkbox is checked then set WITH NO DATA
				if (isPopulatedFlag)
				{
					sql += wxT("REFRESH MATERIALIZED VIEW ") + name
					       +  wxT(" WITH NO DATA;\n");
				}
				else
				{
					sql += wxT("REFRESH MATERIALIZED VIEW ") + name
					       +  wxT(" WITH DATA;\n");
				}
			}

			// Altered the storage parameters for the materialized view?
			if (!chkCustomVac->GetValue())
			{
				if (hasVacuum)
				{
					sql += wxT("ALTER MATERIALIZED VIEW ") + name
					       +  wxT(" RESET(\n")
					       wxT("  autovacuum_enabled,\n")
					       wxT("  autovacuum_vacuum_threshold,\n")
					       wxT("  autovacuum_analyze_threshold,\n")
					       wxT("  autovacuum_vacuum_scale_factor,\n")
					       wxT("  autovacuum_analyze_scale_factor,\n")
					       wxT("  autovacuum_vacuum_cost_delay,\n")
					       wxT("  autovacuum_vacuum_cost_limit,\n")
					       wxT("  autovacuum_freeze_min_age,\n")
					       wxT("  autovacuum_freeze_max_age,\n")
					       wxT("  autovacuum_freeze_table_age\n")
					       wxT(");\n");
				}
			}
			else
			{
				wxString vacStr;
				bool changed = (chkVacEnabled->GetValue() != tableVacEnabled);

				bool valChanged = false;
				wxString newVal;
				wxString setStr;
				wxString resetStr;

				if (changed)
				{
					FillAutoVacuumParameters(setStr, resetStr, wxT("autovacuum_enabled"), BoolToStr(chkVacEnabled->GetValue()));
				}
				newVal =  AppendNum(valChanged, txtBaseVac, tableVacBaseThr);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("autovacuum_vacuum_threshold"), newVal);
				}

				newVal = AppendNum(valChanged, txtBaseAn, tableAnlBaseThr);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("autovacuum_analyze_threshold"), newVal);
				}

				newVal = AppendNum(valChanged, txtFactorVac, tableVacFactor);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("autovacuum_vacuum_scale_factor"), newVal);
				}

				newVal = AppendNum(valChanged, txtFactorAn, tableAnlFactor);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("autovacuum_analyze_scale_factor"), newVal);
				}

				newVal = AppendNum(valChanged, txtVacDelay, tableCostDelay);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("autovacuum_vacuum_cost_delay"), newVal);
				}

				newVal = AppendNum(valChanged, txtVacLimit, tableCostLimit);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("autovacuum_vacuum_cost_limit"), newVal);
				}

				newVal = AppendNum(valChanged, txtFreezeMinAge, tableFreezeMinAge);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("autovacuum_freeze_min_age"), newVal);
				}

				newVal = AppendNum(valChanged, txtFreezeMaxAge, tableFreezeMaxAge);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("autovacuum_freeze_max_age"), newVal);
				}

				newVal = AppendNum(valChanged, txtFreezeTableAge, tableFreezeTableAge);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("autovacuum_freeze_table_age"), newVal);
				}

				if (!setStr.IsEmpty())
				{
					vacStr = wxT("ALTER MATERIALIZED VIEW ") + name + setStr + wxT("\n);\n");;
					changed = true;
				}
				if (!resetStr.IsEmpty())
				{
					vacStr += wxT("ALTER MATERIALIZED VIEW ") + name + resetStr + wxT("\n);\n");;
					changed = true;
				}
				if (changed)
					sql += vacStr;
			}

			if (!chkCustomToastVac->GetValue())
			{
				if (toastTableHasVacuum)
				{
					sql += wxT("ALTER MATERIALIZED VIEW ") + name
					       +  wxT(" RESET(\n")
					       wxT("  toast.autovacuum_enabled,\n")
					       wxT("  toast.autovacuum_vacuum_threshold,\n")
					       wxT("  toast.autovacuum_analyze_threshold,\n")
					       wxT("  toast.autovacuum_vacuum_scale_factor,\n")
					       wxT("  toast.autovacuum_analyze_scale_factor,\n")
					       wxT("  toast.autovacuum_vacuum_cost_delay,\n")
					       wxT("  toast.autovacuum_vacuum_cost_limit,\n")
					       wxT("  toast.autovacuum_freeze_min_age,\n")
					       wxT("  toast.autovacuum_freeze_max_age,\n")
					       wxT("  toast.autovacuum_freeze_table_age\n")
					       wxT(");\n");
				}
			}
			else
			{
				wxString vacStr;
				bool changed = (chkToastVacEnabled->GetValue() != toastTableVacEnabled);
				bool valChanged = false;
				wxString newVal;
				wxString setStr;
				wxString resetStr;
				if (changed)
				{
					FillAutoVacuumParameters(setStr, resetStr, wxT("toast.autovacuum_enabled"), BoolToStr(chkToastVacEnabled->GetValue()));
				}
				newVal =  AppendNum(valChanged, txtBaseToastVac, toastTableVacBaseThr);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("toast.autovacuum_vacuum_threshold"), newVal);
				}

				newVal = AppendNum(valChanged, txtFactorToastVac, toastTableVacFactor);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("toast.autovacuum_vacuum_scale_factor"), newVal);
				}

				newVal = AppendNum(valChanged, txtToastVacDelay, toastTableCostDelay);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("toast.autovacuum_vacuum_cost_delay"), newVal);
				}

				newVal = AppendNum(valChanged, txtToastVacLimit, toastTableCostLimit);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("toast.autovacuum_vacuum_cost_limit"), newVal);
				}

				newVal = AppendNum(valChanged, txtToastFreezeMinAge, toastTableFreezeMinAge);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("toast.autovacuum_freeze_min_age"), newVal);
				}

				newVal = AppendNum(valChanged, txtToastFreezeMaxAge, toastTableFreezeMaxAge);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("toast.autovacuum_freeze_max_age"), newVal);
				}

				newVal = AppendNum(valChanged, txtToastFreezeTableAge, toastTableFreezeTableAge);
				if (valChanged)
				{
					valChanged = false;
					FillAutoVacuumParameters(setStr, resetStr, wxT("toast.autovacuum_freeze_table_age"), newVal);
				}

				if (!setStr.IsEmpty())
				{
					vacStr = wxT("ALTER MATERIALIZED VIEW ") + name + setStr + wxT("\n);\n");
					changed = true;
				}
				if (!resetStr.IsEmpty())
				{
					vacStr += wxT("ALTER MATERIALIZED VIEW ") + name + resetStr + wxT("\n);\n");
					changed = true;
				}
				if (changed)
					sql += vacStr;
			}

			if (cboTablespace->GetOIDKey() != view->GetTablespaceOid())
			{
				sql += wxT("ALTER MATERIALIZED VIEW ") + name
				       +  wxT("\n  SET TABLESPACE ") + qtIdent(cboTablespace->GetValue())
				       + wxT(";\n");
			}
		}
	}

	if (view)
		AppendOwnerChange(sql, wxT("TABLE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));
	else
		AppendOwnerNew(sql, wxT("TABLE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));


	sql +=  GetGrant(wxT("arwdRxt"), wxT("TABLE ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));

	if (connection->BackendMinimumVersion(9, 3) && chkMaterializedView->GetValue())
		AppendComment(sql, wxT("MATERIALIZED VIEW ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), view);
	else
		AppendComment(sql, wxT("VIEW ") + qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()), view);

	if (seclabelPage && connection->BackendMinimumVersion(9, 1))
		sql += seclabelPage->GetSqlForSecLabels(wxT("VIEW"), qtIdent(cbSchema->GetValue()) + wxT(".") + qtIdent(GetName()));

	return sql;
}