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;
}
Beispiel #2
0
wxString pgOperator::GetSql(ctlTree *browser)
{
	if (sql.IsNull())
	{
		sql = wxT("-- Operator: ") + GetQuotedFullIdentifier() + wxT("(") + GetOperands() + wxT(")\n\n")
		      + wxT("-- DROP OPERATOR ") + GetQuotedFullIdentifier()
		      + wxT("(") + GetOperands() + wxT(");\n\n")
		      wxT("CREATE OPERATOR ") + GetQuotedFullIdentifier()
		      + wxT("(\n  PROCEDURE = ") + GetOperatorFunction();
		AppendIfFilled(sql, wxT(",\n  LEFTARG = "), qtTypeIdent(GetLeftType()));
		AppendIfFilled(sql, wxT(",\n  RIGHTARG = "), qtTypeIdent(GetRightType()));
		AppendIfFilled(sql, wxT(",\n  COMMUTATOR = "), GetCommutator());
		AppendIfFilled(sql, wxT(",\n  RESTRICT = "), GetRestrictFunction());
		AppendIfFilled(sql, wxT(",\n  JOIN = "), GetJoinFunction());
		if (GetHashJoins()) sql += wxT(",\n  HASHES");
		if (GetMergeJoins()) sql += wxT(",\n  MERGES");

		if (!GetDatabase()->BackendMinimumVersion(8, 3))
		{
			AppendIfFilled(sql, wxT(",\n  SORT1 = "), GetLeftSortOperator());
			AppendIfFilled(sql, wxT(",\n  SORT2 = "), GetRightSortOperator());
			AppendIfFilled(sql, wxT(",\n  LTCMP = "), GetLessOperator());
			AppendIfFilled(sql, wxT(",\n  GTCMP = "), GetGreaterOperator());
		}

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

		if (!GetComment().IsNull())
			sql += wxT("COMMENT ON OPERATOR ") + GetQuotedFullIdentifier()
			       + wxT("(") + GetOperands() + wxT(") IS ")
			       + qtDbString(GetComment()) + wxT(";\n");
	}

	return sql;
}
Beispiel #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;
}
Beispiel #4
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;
}
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;
}
Beispiel #6
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;
}
Beispiel #7
0
wxString pgType::GetSql(ctlTree *browser)
{
	if (sql.IsNull())
	{
		sql = wxT("-- Type: ") + GetQuotedFullIdentifier() + wxT("\n\n")
		      + wxT("-- DROP TYPE ") + GetQuotedFullIdentifier() + wxT(";")
		      + wxT("\n\nCREATE TYPE ") + GetQuotedFullIdentifier();
		if (GetTypeClass() == TYPE_COMPOSITE)
		{
			sql += wxT(" AS\n   (");
			sql += GetQuotedTypesList();
		}
		else if (GetTypeClass() == TYPE_ENUM)
		{
			sql += wxT(" AS ENUM\n   (");
			sql += GetQuotedLabelList();
		}
		else
		{
			sql += wxT("\n   (INPUT=") + qtIdent(GetInputFunction())
			       + wxT(", OUTPUT=") + qtIdent(GetOutputFunction());
			AppendIfFilled(sql, wxT(", DEFAULT="), qtDbString(GetDefault()));
			if (!GetElement().IsNull())
			{
				sql += wxT(",\n       ELEMENT=") + GetElement()
				       + wxT(", DELIMITER='") + GetDelimiter() + wxT("'");
			}
			sql += wxT(",\n       INTERNALLENGTH=") + NumToStr(GetInternalLength())
			       + wxT(", ALIGNMENT=" + GetAlignment()
			             + wxT(", STORAGE=") + GetStorage());
			if (GetConnection()->BackendMinimumVersion(8, 3))
			{
				if (GetTypmodinFunction() != wxEmptyString && GetTypmodoutFunction() != wxEmptyString)
				{
					sql += wxT(",\n       TYPMOD_IN=") + GetTypmodinFunction()
					       + wxT(", TYPMOD_OUT=") + GetTypmodoutFunction();
				}
				else if (GetTypmodinFunction() != wxEmptyString)
					sql += wxT(",\n       TYPMOD_IN=") + GetTypmodinFunction();
				else if (GetTypmodoutFunction() != wxEmptyString)
					sql += wxT(",\n       TYPMOD_OUT=") + GetTypmodoutFunction();
			}
			if (GetConnection()->BackendMinimumVersion(9, 1) && GetCollatable())
			{
				sql += wxT(",\n       COLLATABLE=true");
			}
		}
		sql += wxT(");\n")
		       + GetOwnerSql(8, 0)
		       + GetCommentSql();

		if (GetConnection()->BackendMinimumVersion(9, 1))
			sql += GetSeqLabelsSql();
	}

	return sql;
}
Beispiel #8
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;
}
Beispiel #9
0
wxString pgDomain::GetSql(ctlTree *browser)
{
    if (sql.IsNull())
    {
        sql = wxT("-- Domain: ") + GetQuotedFullIdentifier() + wxT("\n\n")
              + wxT("-- DROP DOMAIN ") + GetQuotedFullIdentifier() + wxT(";")
              + wxT("\n\nCREATE DOMAIN ") + GetQuotedFullIdentifier()
              + wxT("\n  AS ") + GetQuotedBasetype();
        if (GetCollationOid() > 0)
            sql += wxT("\n  COLLATE ") + GetQuotedCollation();
        AppendIfFilled(sql, wxT("\n  DEFAULT "), GetDefault());
        // CONSTRAINT Name Dont know where it's stored, may be omitted anyway
        if (notNull)
            sql += wxT("\n  NOT NULL");

        // Get a count of the constraints.
        int consCount = 0;
        pgCollection *constraints = browser->FindCollection(checkFactory, GetId());
        if (constraints)
        {
            constraints->ShowTreeDetail(browser);
            treeObjectIterator consIt(browser, constraints);

            pgObject *data;

            while ((data = consIt.GetNextObject()) != 0)
            {
                data->ShowTreeDetail(browser);

                sql += wxT("\n  CONSTRAINT ") + data->GetQuotedIdentifier()
                       + wxT(" ") + data->GetTypeName().Upper()
                       + wxT(" ") ;

                switch (data->GetMetaType())
                {
                case PGM_CHECK:
                    sql += wxT("(") + ((pgCheck *)data)->GetDefinition() + wxT(")");
                    if (GetDatabase()->BackendMinimumVersion(9, 2) && !((pgCheck *)data)->GetValid())
                        sql += wxT(" NOT VALID");
                    break;
                }
            }
        }

        sql += wxT(";\n")
               + GetOwnerSql(7, 4)
               + GetCommentSql();

        if (GetConnection()->BackendMinimumVersion(9, 1))
            sql += GetSeqLabelsSql();
    }

    return sql;
}
Beispiel #10
0
wxString dlgColumn::GetDefinition()
{
	wxString sql;
	sql = GetQuotedTypename(cbDatatype->GetGuessedSelection());
	if (!cbCollation->GetValue().IsEmpty() && cbCollation->GetValue() != wxT("pg_catalog.\"default\""))
		sql += wxT(" COLLATE ") + cbCollation->GetValue();
	if (chkNotNull->GetValue())
		sql += wxT(" NOT NULL");

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

	return sql;
}
Beispiel #11
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;
}
Beispiel #12
0
wxString pgUser::GetSql(ctlTree *browser)
{
    if (sql.IsNull())
    {
        sql = wxT("-- User: "******"\n\n")
              + wxT("-- DROP USER ") + GetQuotedFullIdentifier() + wxT(";")
              + wxT("\n\nCREATE USER ") + GetQuotedIdentifier()
              + wxT("\n  WITH SYSID ") + NumToStr(userId);
        if (GetPassword() != wxT("********"))
            AppendIfFilled(sql, wxT("\n  ENCRYPTED PASSWORD "), qtDbString(GetPassword()));
        sql += wxT("\n ");
        if (GetCreateDatabase())    sql += wxT(" CREATEDB");
        else                        sql += wxT(" NOCREATEDB");
        if (GetUpdateCatalog())     sql += wxT(" CREATEUSER");
        else                        sql += wxT(" NOCREATEUSER");
        if (GetAccountExpires().IsValid())
            AppendIfFilled(sql, wxT(" VALID UNTIL "), qtDbString(DateToAnsiStr(GetAccountExpires())));
        sql += wxT(";\n");

        size_t index;
        for (index = 0 ; index < configList.GetCount() ; index++)
        {
            if (configList.Item(index).BeforeFirst('=') != wxT("search_path") &&
                    configList.Item(index).BeforeFirst('=') != wxT("temp_tablespaces"))
                sql += wxT("ALTER USER ") + GetQuotedIdentifier()
                       + wxT(" SET ") + configList.Item(index).BeforeFirst('=') + wxT("='") + configList.Item(index).AfterFirst('=') + wxT("';\n");
            else
                sql += wxT("ALTER USER ") + GetQuotedIdentifier()
                       + wxT(" SET ") + configList.Item(index).BeforeFirst('=') + wxT("=") + configList.Item(index).AfterFirst('=') + wxT(";\n");
        }
        for (index = 0 ; index < groupsIn.GetCount() ; index++)
            sql += wxT("ALTER GROUP ") + qtIdent(groupsIn.Item(index))
                   +  wxT(" ADD USER ") + GetQuotedIdentifier() + wxT(";\n");

    }
    return sql;
}
Beispiel #13
0
wxString pgTextSearchParser::GetSql(ctlTree *browser)
{
	if (sql.IsNull())
	{
		sql = wxT("-- Text Search Parser: ") + GetFullIdentifier() + wxT("\n\n")
		      + wxT("-- DROP TEXT SEARCH PARSER ") + GetFullIdentifier() + wxT("\n\n")
		      + wxT("CREATE TEXT SEARCH PARSER ") + GetFullIdentifier() + wxT(" (")
		      + wxT("\n  START = ") + qtTypeIdent(GetStart())
		      + wxT(",\n  GETTOKEN = ") + qtTypeIdent(GetGettoken())
		      + wxT(",\n  END = ") + qtTypeIdent(GetEnd())
		      + wxT(",\n  LEXTYPES = ") + qtTypeIdent(GetLextypes());

		AppendIfFilled(sql, wxT(",\n  HEADLINE = "), GetHeadline());

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

		if (!GetComment().IsNull())
			sql += wxT("COMMENT ON TEXT SEARCH PARSER ") + GetFullIdentifier()
			       + wxT(" IS ") + qtDbString(GetComment()) + wxT(";\n");
	}

	return sql;
}
wxString dlgIndexConstraint::GetDefinition()
{
	wxString sql = wxEmptyString;

	if (cbIndex->GetCurrentSelection() > 0)
	{
		sql += wxT(" USING INDEX ") + qtIdent(cbIndex->GetValue());
	}
	else
	{
		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)
			sql += wxT(" USING INDEX TABLESPACE ") + qtIdent(cbTablespace->GetValue());

		if (chkDeferrable->GetValue())
		{
			sql += wxT(" DEFERRABLE");
			if (chkDeferred->GetValue())
				sql += wxT(" INITIALLY DEFERRED");
		}

		if (txtWhere->GetValue().Length() > 0)
			sql += wxT(" WHERE (") + txtWhere->GetValue() + wxT(")");
	}

	return sql;
}
wxString pgOperatorClass::GetSql(ctlTree *browser)
{
	if (sql.IsNull())
	{
		sql = wxT("-- Operator Class: ") + GetName() + wxT("\n\n")
		      + wxT("-- DROP OPERATOR CLASS ") + GetQuotedFullIdentifier() + wxT(" USING ") + GetAccessMethod() + wxT(";")
		      + wxT("\n\nCREATE OPERATOR CLASS ") + GetQuotedFullIdentifier();
		if (GetOpcDefault())
			sql += wxT(" DEFAULT");
		sql += wxT("\n   FOR TYPE ") + GetInType()
		       +  wxT(" USING ") + GetAccessMethod()
		       +  wxT(" AS");
		unsigned int i;
		bool needComma = false;

		for (i = 0 ; i < operators.Count() ; i++)
		{
			if (needComma)
				sql += wxT(",");

			sql += wxT("\n   OPERATOR ") + operators.Item(i);
			needComma = true;
		}
		for (i = 0 ; i < functions.Count() ; i++)
		{
			if (needComma)
				sql += wxT(",");

			sql += wxT("\n   FUNCTION ") + quotedFunctions.Item(i);
			needComma = true;
		}
		AppendIfFilled(sql, wxT("\n   STORAGE "), GetKeyType());
		sql += wxT(";\n");
	}

	return sql;
}
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;
}
Beispiel #17
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;
}
Beispiel #18
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;
}
Beispiel #19
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;
}
Beispiel #20
0
wxString pgRole::GetSql(ctlTree *browser)
{
    if (sql.IsNull())
    {
        sql = wxT("-- Role: ") + GetName() + wxT("\n\n")
            + wxT("-- DROP ROLE ") + GetQuotedFullIdentifier() + wxT(";")
            + wxT("\n\nCREATE ROLE ") + GetQuotedIdentifier();

        if (GetCanLogin())
        {
            sql += wxT(" LOGIN");
            if (GetPassword() != wxT("********") && !GetPassword().IsEmpty())
                AppendIfFilled(sql, wxT("\n  ENCRYPTED PASSWORD "), qtDbString(GetPassword()));
        }
        sql += wxT("\n ");
        if (this->GetSuperuser())   sql += wxT(" SUPERUSER");
        else                        sql += wxT(" NOSUPERUSER");
        if (GetInherits())          sql += wxT(" INHERIT");
        else                        sql += wxT(" NOINHERIT");
        if (GetCreateDatabase())    sql += wxT(" CREATEDB");
        else                        sql += wxT(" NOCREATEDB");
        if (GetCreateRole())        sql += wxT(" CREATEROLE");
        else                        sql += wxT(" NOCREATEROLE");
        if (server->GetConnection()->BackendMinimumVersion(9, 1))
        {
            if (GetReplication())       sql += wxT(" REPLICATION");
            else                        sql += wxT(" NOREPLICATION");
        }
        if (GetConnectionLimit() > 0)
                                    sql += wxT(" CONNECTION LIMIT ") + NumToStr(GetConnectionLimit());
        if (GetAccountExpires().IsValid())
        AppendIfFilled(sql, wxT(" VALID UNTIL "), qtDbString(DateToAnsiStr(GetAccountExpires())));
        if (GetRolQueueName().Length() > 0)
            AppendIfFilled(sql, wxT(" RESOURCE QUEUE "), GetRolQueueName());
        sql +=wxT(";\n");

        if (this->GetSuperuser() && !GetUpdateCatalog())
            sql += wxT("UPDATE pg_authid SET rolcatupdate=false WHERE rolname=") + qtDbString(GetIdentifier()) + wxT(";\n");

        size_t index;
        for (index=0 ; index < configList.GetCount() ; index++)
        {
            if (configList.Item(index).BeforeFirst('=') != wxT("search_path") &&
                configList.Item(index).BeforeFirst('=') != wxT("temp_tablespaces"))
                sql += wxT("ALTER ROLE ") + GetQuotedIdentifier()
                    + wxT(" SET ") + configList.Item(index).BeforeFirst('=') + wxT("='") + configList.Item(index).AfterFirst('=') + wxT("';\n");
            else
                sql += wxT("ALTER ROLE ") + GetQuotedIdentifier()
                    + wxT(" SET ") + configList.Item(index).BeforeFirst('=') + wxT("=") + configList.Item(index).AfterFirst('=') + wxT(";\n");
        }
        for (index=0 ; index < rolesIn.GetCount() ; index++)
        {
            wxString role=rolesIn.Item(index);
            bool admin=false;
            if (role.Right(PGROLE_ADMINOPTION_LEN) == PGROLE_ADMINOPTION)
            {
                admin=true;
                role=role.Left(role.Length()-PGROLE_ADMINOPTION_LEN);
            }
            sql += wxT("GRANT ") + qtIdent(role)
                +  wxT(" TO ") + GetQuotedIdentifier();

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

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

        if (!GetComment().IsNull())
        {
            sql += wxT("COMMENT ON ROLE ") + GetQuotedFullIdentifier() + wxT(" IS ")
                +  qtDbString(GetComment()) + wxT(";\n");
        }

    }
    return sql;
}
Beispiel #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);
}
Beispiel #22
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;
}
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;
}
Beispiel #25
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;
}
Beispiel #26
0
wxString pgType::GetSql(ctlTree *browser)
{
	if (sql.IsNull())
	{
		sql = wxT("-- Type: ") + GetQuotedFullIdentifier() + wxT("\n\n")
		      + wxT("-- DROP TYPE ") + GetQuotedFullIdentifier() + wxT(";")
		      + wxT("\n\nCREATE TYPE ") + GetQuotedFullIdentifier();
		if (GetTypeClass() == TYPE_COMPOSITE)
		{
			sql += wxT(" AS\n   (");
			sql += GetQuotedTypesList();
		}
		else if (GetTypeClass() == TYPE_ENUM)
		{
			sql += wxT(" AS ENUM\n   (");
			sql += GetQuotedLabelList();
		}
		else if (GetTypeClass() == TYPE_RANGE)
		{
			sql += wxT(" AS RANGE\n   (")
			       wxT("SUBTYPE=") + rngsubtypestr;
			if (!rngsubopcstr.IsEmpty())
				sql += wxT(",\n    SUBTYPE_OPCLASS=") + rngsubopcstr;
			if (!rngcollationstr.IsEmpty())
				sql += wxT(",\n    COLLATION=") + rngcollationstr;
			if (!rngcanonical.IsEmpty())
				sql += wxT(",\n    CANONICAL=") + rngcanonical;
			if (!rngsubdiff.IsEmpty())
				sql += wxT(",\n    SUBTYPE_DIFF=") + rngsubdiff;
		}
		else
		{
			sql += wxT("\n   (INPUT=") + qtIdent(GetInputFunction())
			       + wxT(",\n       OUTPUT=") + qtIdent(GetOutputFunction());
			if (GetConnection()->BackendMinimumVersion(7, 4))
			{
				if (!GetReceiveFunction().IsEmpty())
				{
					sql += wxT(",\n       RECEIVE=") + GetReceiveFunction();
				}
				if (!GetSendFunction().IsEmpty())
				{
					sql += wxT(",\n       SEND=") + GetSendFunction();
				}
			}
			if (GetConnection()->BackendMinimumVersion(8, 3))
			{
				if (!GetTypmodinFunction().IsEmpty())
					sql += wxT(",\n       TYPMOD_IN=") + GetTypmodinFunction();
				if (!GetTypmodoutFunction().IsEmpty())
					sql += wxT(",\n       TYPMOD_OUT=") + GetTypmodoutFunction();
				if (GetAnalyzeFunction() != wxEmptyString)
					sql += wxT(",\n       ANALYZE=") + GetAnalyzeFunction();
			}
			if (GetConnection()->BackendMinimumVersion(8, 4))
			{
				sql += wxT(",\n       CATEGORY=") + qtDbString(GetCategory());
				if (GetPrefered())
					sql += wxT(",\n       PREFERRED=true");
			}
			if (GetPassedByValue())
				sql += wxT(",\n    PASSEDBYVALUE");
			AppendIfFilled(sql, wxT(", DEFAULT="), qtDbString(GetDefault()));
			if (!GetElement().IsNull())
			{
				sql += wxT(",\n       ELEMENT=") + GetElement()
				       + wxT(", DELIMITER='") + GetDelimiter() + wxT("'");
			}
			sql += wxT(",\n       INTERNALLENGTH=") + NumToStr(GetInternalLength())
			       + wxT(", ALIGNMENT=" + GetAlignment()
			             + wxT(", STORAGE=") + GetStorage());
			if (GetConnection()->BackendMinimumVersion(9, 1) && GetCollatable())
			{
				sql += wxT(",\n       COLLATABLE=true");
			}
			if (GetConnection()->BackendMinimumVersion(9, 1) && GetCollatable())
			{
				sql += wxT(",\n       COLLATABLE=true");
			}
		}
		sql += wxT(");\n")
		       + GetOwnerSql(8, 0)
		       + GetCommentSql();

		if (GetConnection()->BackendMinimumVersion(9, 1))
			sql += GetSeqLabelsSql();
	}

	return sql;
}
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;
}