Le 23/01/2010 16:48, Guillaume Lelarge a écrit :
> Hi,
> 
> Le 21/01/2010 22:34, Guillaume Lelarge a écrit :
>> [...]
>> It took me one more day than I previously thought. Anyways, here it is.
>> I checked every I could (apart from checking on Windows and Mac OS X
>> because I don't have those in my hotel room...).
>>
>> There is not much to say on this patch. It changes the Role and Database
>> dialogsbut not that much. Should work on 9.0 (obviously), and previous
>> releases.
>>
>> Comments?
>>
> 
> I have one :)
> 
> I have an issue with it. Let's say I changed a database setting for one
> user (ALTER ROLE u1 ON DATABASE db1 SET param=value). When I click OK on
> the dialog, I get back to the browser. The SQL pane of the database db1
> object shows my change, the SQL pane of the user u1 pane doesn't. We
> need a refresh of the user when we change the database, and vice-versa.
> Won't be done today unfortunately.
> 

According to our talk during the wait at the restaurant, I dropped the
user changes. Only the database has a properties' dialog that allows a
user to use the complexe user/database default GUC settings.

Patch attached.

Comments?


-- 
Guillaume.
 http://www.postgresqlfr.org
 http://dalibo.com
diff --git a/pgadmin/ctl/ctlListView.cpp b/pgadmin/ctl/ctlListView.cpp
index 84e17e6..ac46f06 100644
--- a/pgadmin/ctl/ctlListView.cpp
+++ b/pgadmin/ctl/ctlListView.cpp
@@ -105,3 +105,41 @@ void ctlListView::CreateColumns(wxImageList *images, const wxString &left, const
     if (images)
         SetImageList(images, wxIMAGE_LIST_SMALL);
 }
+
+
+void ctlListView::CreateColumns(wxImageList *images, const wxString &str1, const wxString &str2, const wxString &str3, int leftSize)
+{
+    int rightSize;
+    if (leftSize < 0)
+    {
+#ifdef __WXMAC__
+        leftSize = rightSize = (GetParent()->GetSize().GetWidth() - 20)/2;
+#else
+        leftSize = rightSize = GetSize().GetWidth()/2;
+#endif
+    }
+    else
+    {
+        if (leftSize)
+            leftSize = ConvertDialogToPixels(wxPoint(leftSize, 0)).x;
+
+#ifdef __WXMAC__
+        rightSize = (GetParent()->GetSize().GetWidth() - 20) - leftSize;
+#else
+        rightSize = GetClientSize().GetWidth()-leftSize;
+#endif
+    }
+    if (!leftSize)
+    {
+        InsertColumn(0, str1, wxLIST_FORMAT_LEFT, GetClientSize().GetWidth());
+    }
+    else
+    {
+        InsertColumn(0, str1, wxLIST_FORMAT_LEFT, leftSize);
+        InsertColumn(1, str2, wxLIST_FORMAT_LEFT, rightSize/2);
+        InsertColumn(2, str3, wxLIST_FORMAT_LEFT, rightSize/2);
+    }
+
+    if (images)
+        SetImageList(images, wxIMAGE_LIST_SMALL);
+}
diff --git a/pgadmin/dlg/dlgDatabase.cpp b/pgadmin/dlg/dlgDatabase.cpp
index b5a6041..39fa1d5 100644
--- a/pgadmin/dlg/dlgDatabase.cpp
+++ b/pgadmin/dlg/dlgDatabase.cpp
@@ -32,6 +32,7 @@
 
 #define lstVariables    CTRL_LISTVIEW("lstVariables")
 #define cbVarname       CTRL_COMBOBOX2("cbVarname")
+#define cbVarUsername   CTRL_COMBOBOX2("cbVarUsername")
 #define txtValue        CTRL_TEXT("txtValue")
 #define chkValue        CTRL_CHECKBOX("chkValue")
 #define btnAdd          CTRL_BUTTON("wxID_ADD")
@@ -63,6 +64,7 @@ BEGIN_EVENT_TABLE(dlgDatabase, dlgSecurityProperty)
     EVT_BUTTON(wxID_ADD,                            dlgDatabase::OnVarAdd)
     EVT_BUTTON(wxID_REMOVE,                         dlgDatabase::OnVarRemove)
     EVT_TEXT(XRCID("cbVarname"),                    dlgDatabase::OnVarnameSelChange)
+    EVT_TEXT(XRCID("cbVarUsername"),                dlgDatabase::OnVarnameSelChange)
     EVT_COMBOBOX(XRCID("cbVarname"),                dlgDatabase::OnVarnameSelChange)
     EVT_BUTTON(wxID_OK,                             dlgDatabase::OnOK)
     EVT_TEXT(XRCID("cbCollate"),                    dlgDatabase::OnCollateSelChange)
@@ -79,7 +81,7 @@ dlgDatabase::dlgDatabase(pgaFactory *f, frmMain *frame, pgDatabase *node)
 {
     database=node;
     schemaRestrictionOk=true;
-    lstVariables->CreateColumns(0, _("Variable"), _("Value"));
+    lstVariables->CreateColumns(0, _("Username"), _("Variable"), _("Value"));
 
     chkValue->Hide();
 
@@ -108,6 +110,14 @@ int dlgDatabase::Go(bool modal)
     AddGroups();
     AddUsers(cbOwner);
 
+    if (connection->BackendMinimumVersion(8,5))
+    {
+        cbVarUsername->Append(wxT(""));
+        AddUsers(cbVarUsername);
+    }
+    else
+        cbVarUsername->Enable(false);
+
     if (connection->BackendMinimumVersion(8, 0))
     {
         stPath->Hide();
@@ -151,6 +161,11 @@ int dlgDatabase::Go(bool modal)
         delete set;
 
         cbVarname->SetSelection(0);
+
+        if (connection->BackendMinimumVersion(8,5))
+        {
+            cbVarUsername->SetSelection(0);
+        }
     }
 
     if (database)
@@ -168,16 +183,27 @@ int dlgDatabase::Go(bool modal)
         if (readOnly)
         {
             cbVarname->Disable();
+            cbVarUsername->Disable();
             txtValue->Disable();
             btnAdd->Disable();
             btnRemove->Disable();
         }
 
         size_t i;
+        wxString username;
+        wxString varname;
+        wxString varvalue;
         for (i=0 ; i < database->GetVariables().GetCount() ; i++)
         {
-            wxString item=database->GetVariables().Item(i);
-            lstVariables->AppendItem(0, item.BeforeFirst('='), item.AfterFirst('='));
+            wxStringTokenizer tkz(database->GetVariables().Item(i), wxT("="));
+            while (tkz.HasMoreTokens())
+            {
+                username = tkz.GetNextToken();
+                varname = tkz.GetNextToken();
+                varvalue = tkz.GetNextToken();
+            }
+
+            lstVariables->AppendItem(0, username, varname, varvalue);
         }
 
         PrepareTablespace(cbTablespace, database->GetTablespaceOid());
@@ -459,12 +485,13 @@ void dlgDatabase::OnVarSelChange(wxListEvent &ev)
     long pos=lstVariables->GetSelection();
     if (pos >= 0)
     {
-        wxString value=lstVariables->GetText(pos, 1);
-        cbVarname->SetValue(lstVariables->GetText(pos));
+        cbVarUsername->SetValue(lstVariables->GetText(pos));
+        cbVarname->SetValue(lstVariables->GetText(pos, 1));
 
         // We used to raise an OnVarnameSelChange() event here, but
         // at this point the combo box hasn't necessarily updated.
-        int sel = cbVarname->FindString(lstVariables->GetText(pos));
+        wxString value=lstVariables->GetText(pos, 2);
+        int sel = cbVarname->FindString(lstVariables->GetText(pos, 1));
         SetupVarEditor(sel);
 
         txtValue->SetValue(value);
@@ -475,6 +502,7 @@ void dlgDatabase::OnVarSelChange(wxListEvent &ev)
 
 void dlgDatabase::OnVarAdd(wxCommandEvent &ev)
 {
+    wxString username=cbVarUsername->GetValue();
     wxString name=cbVarname->GetValue();
     wxString value;
     if (chkValue->IsShown())
@@ -487,13 +515,39 @@ void dlgDatabase::OnVarAdd(wxCommandEvent &ev)
 
     if (!name.IsEmpty())
     {
-        long pos=lstVariables->FindItem(-1, name);
-        if (pos < 0)
+        bool found = false;
+        long prevpos = -1;
+        for (long item=0; item<lstVariables->GetItemCount(); item++)
         {
-            pos = lstVariables->GetItemCount();
-            lstVariables->InsertItem(pos, name, 0);
+            if (name == lstVariables->GetText(item, 1))
+            {
+                if (username == lstVariables->GetText(item))
+                {
+                    found = true;
+                    lstVariables->SetItem(item, 2, value);
+                }
+                else
+                {
+                    prevpos = item;
+                }
+            }
+        }
+        if (!found)
+        {
+            if (prevpos != -1)
+            {
+                lstVariables->InsertItem(prevpos, username, 1);
+                lstVariables->SetItem(prevpos, 1, name);
+                lstVariables->SetItem(prevpos, 2, value);
+            }
+            else
+            {
+                long pos = lstVariables->GetItemCount();
+                lstVariables->InsertItem(pos, username, 1);
+                lstVariables->SetItem(pos, 1, name);
+                lstVariables->SetItem(pos, 2, value);
+            }
         }
-        lstVariables->SetItem(pos, 1, value);
     }
 	dirtyVars = true;
     CheckChange();
@@ -560,6 +614,9 @@ wxString dlgDatabase::GetSql()
             sql += GetGrant(wxT("CTc"), wxT("DATABASE ") + qtIdent(name));
 
         wxArrayString vars;
+        wxString username;
+        wxString varname;
+        wxString varvalue;
 
         size_t index;
 
@@ -572,36 +629,71 @@ wxString dlgDatabase::GetSql()
         // check for changed or added vars
         for (pos=0 ; pos < cnt ; pos++)
         {
-            wxString newVar=lstVariables->GetText(pos);
-            wxString newVal=lstVariables->GetText(pos, 1);
+            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++)
             {
+                wxStringTokenizer tkz(vars.Item(index), wxT("="));
+                while (tkz.HasMoreTokens())
+                {
+                    username = tkz.GetNextToken();
+                    varname = tkz.GetNextToken();
+                    varvalue = tkz.GetNextToken();
+                }
+
                 wxString var=vars.Item(index);
-                if (var.BeforeFirst('=').IsSameAs(newVar, false))
+                if (newUsr == username && newVar == varname)
                 {
-                    oldVal = var.Mid(newVar.Length()+1);
+                    oldVal = varvalue;
                     vars.RemoveAt(index);
                     break;
                 }
             }
             if (oldVal != newVal)
             {
-                sql += wxT("ALTER DATABASE ") + qtIdent(name)
-                    +  wxT(" SET ") + newVar
-                    +  wxT("='") + newVal
-                    +  wxT("';\n");
+                if (newUsr.Length() == 0)
+                {
+                    sql += wxT("ALTER DATABASE ") + qtIdent(name)
+                        +  wxT(" SET ") + newVar
+                        +  wxT("='") + newVal
+                        +  wxT("';\n");
+                }
+                else
+                {
+                    sql += wxT("ALTER ROLE ") + newUsr + wxT(" IN DATABASE ") + qtIdent(name)
+                        +  wxT(" SET ") + newVar
+                        +  wxT("='") + newVal
+                        +  wxT("';\n");
+                }
             }
         }
         
         // check for removed vars
         for (pos=0 ; pos < (int)vars.GetCount() ; pos++)
         {
-            sql += wxT("ALTER DATABASE ") + qtIdent(name)
-                +  wxT(" RESET ") + vars.Item(pos).BeforeFirst('=')
-                + wxT(";\n");
+            wxStringTokenizer tkz(vars.Item(pos), wxT("="));
+            while (tkz.HasMoreTokens())
+            {
+                username = tkz.GetNextToken();
+                varname = tkz.GetNextToken();
+                varvalue = tkz.GetNextToken();
+            }
+
+            if (username.Length() == 0)
+            {
+                sql += wxT("ALTER DATABASE ") + qtIdent(name)
+                    +  wxT(" RESET ") + varname
+                    + wxT(";\n");
+            }
+            else
+            {
+                sql += wxT("ALTER ROLE ") + username + wxT(" IN DATABASE ") + qtIdent(name)
+                    +  wxT(" RESET ") + varname + wxT(";\n");
+            }
         }
     }
     else
diff --git a/pgadmin/include/ctl/ctlListView.h b/pgadmin/include/ctl/ctlListView.h
index 01c2a5a..4928eaa 100644
--- a/pgadmin/include/ctl/ctlListView.h
+++ b/pgadmin/include/ctl/ctlListView.h
@@ -27,6 +27,7 @@ public:
     wxString GetText(long row, long col=0);
 
     void CreateColumns(wxImageList *images, const wxString &left, const wxString &right, int leftSize=60);
+    void CreateColumns(wxImageList *images, const wxString &str1, const wxString &str2, const wxString &str3, int leftSize=60);
 
     void AddColumn(const wxChar *text, int size=wxLIST_AUTOSIZE_USEHEADER, int format=wxLIST_FORMAT_LEFT);
 
diff --git a/pgadmin/schema/pgDatabase.cpp b/pgadmin/schema/pgDatabase.cpp
index c46bae8..4b153e2 100644
--- a/pgadmin/schema/pgDatabase.cpp
+++ b/pgadmin/schema/pgDatabase.cpp
@@ -406,9 +406,30 @@ wxString pgDatabase::GetSql(ctlTree *browser)
         sql += wxT(";\n");
 
         size_t i;
+        wxString username;
+        wxString varname;
+        wxString varvalue;
         for (i=0 ; i < variables.GetCount() ; i++)
-            sql += wxT("ALTER DATABASE ") + GetQuotedFullIdentifier()
-                + wxT(" SET ") + variables.Item(i).BeforeFirst('=') + wxT("='") + variables.Item(i).AfterFirst('=') + wxT("';\n");
+        {
+            wxStringTokenizer tkz(variables.Item(i), wxT("="));
+            while (tkz.HasMoreTokens())
+            {
+                username = tkz.GetNextToken();
+                varname = tkz.GetNextToken();
+                varvalue = tkz.GetNextToken();
+            }
+
+            if (username.Length() == 0)
+            {
+                sql += wxT("ALTER DATABASE ") + GetQuotedFullIdentifier()
+                    +  wxT(" SET ") + varname + wxT("='") + varvalue + wxT("';\n");
+            }
+            else
+            {
+                sql += wxT("ALTER ROLE ") + username + wxT(" IN DATABASE ") + GetQuotedFullIdentifier()
+                    +  wxT(" SET ") + varname + wxT("='") + varvalue + wxT("';\n");
+            }
+        }
 
 		if (myConn)
 		{
@@ -498,10 +519,29 @@ void pgDatabase::ShowTreeDetail(ctlTree *browser, frmMain *form, ctlListView *pr
             properties->AppendItem(_("Default schema"), defaultSchema);
 
         size_t i;
+        wxString username;
+        wxString varname;
+        wxString varvalue;
         for (i=0 ; i < variables.GetCount() ; i++)
         {
-            wxString item=variables.Item(i);
-            properties->AppendItem(item.BeforeFirst('='), item.AfterFirst('='));
+            wxStringTokenizer tkz(variables.Item(i), wxT("="));
+            while (tkz.HasMoreTokens())
+            {
+                username = tkz.GetNextToken();
+                varname = tkz.GetNextToken();
+                varvalue = tkz.GetNextToken();
+            }
+
+            if (username.Length() == 0)
+            {
+                properties->AppendItem(varname, varvalue);
+            }
+            else
+            {
+                // should we add the parameters for the username?
+                // I don't think so
+                // but if we want this, how will we display that?
+            }
         }
         properties->AppendItem(_("Allow connections?"), GetAllowConnections());
         properties->AppendItem(_("Connected?"), GetConnected());
@@ -568,8 +608,12 @@ pgObject *pgDatabaseFactory::CreateObjects(pgCollection *collection, ctlTree *br
     
     // In 8.5+, database config options are in pg_db_role_setting
     if (collection->GetConnection()->BackendMinimumVersion(8, 5))
+    {
+        wxString setconfig = wxT("SELECT array(select coalesce('''' || rolname || '''', '') || '=' || unnest(setconfig) ")
+           wxT("FROM pg_db_role_setting setting LEFT JOIN pg_roles role ON setting.setrole=role.oid ")
+           wxT("WHERE setdatabase = db.oid)");
         databases = collection->GetServer()->ExecuteSet(
-           wxT("SELECT db.oid, datname, db.dattablespace AS spcoid, spcname, datallowconn, setconfig AS datconfig, datacl, ")
+           wxT("SELECT db.oid, datname, db.dattablespace AS spcoid, spcname, datallowconn, (") + setconfig + wxT(") AS datconfig, datacl, ")
            wxT("pg_encoding_to_char(encoding) AS serverencoding, pg_get_userbyid(datdba) AS datowner,")
            wxT("has_database_privilege(db.oid, 'CREATE') as cancreate, \n")
            wxT("current_setting('default_tablespace') AS default_tablespace, \n")
@@ -578,9 +622,9 @@ pgObject *pgDatabaseFactory::CreateObjects(pgCollection *collection, ctlTree *br
            wxT("  FROM pg_database db\n")
            wxT("  LEFT OUTER JOIN pg_tablespace ta ON db.dattablespace=ta.OID\n")
            wxT("  LEFT OUTER JOIN pg_shdescription descr ON db.oid=descr.objoid\n")
-           wxT("  LEFT OUTER JOIN pg_db_role_setting setting ON (db.oid=setting.setdatabase AND setting.setrole=0)\n")
            + restr +
            wxT(" ORDER BY datname"));
+    }
     else if (collection->GetConnection()->BackendMinimumVersion(8, 0))
         databases = collection->GetServer()->ExecuteSet(
            wxT("SELECT db.oid, datname, db.dattablespace AS spcoid, spcname, datallowconn, datconfig, datacl, ")
@@ -621,6 +665,18 @@ pgObject *pgDatabaseFactory::CreateObjects(pgCollection *collection, ctlTree *br
             database->iSetCreatePrivilege(databases->GetBool(wxT("cancreate")));
             database->iSetComment(databases->GetVal(wxT("description")));
             wxString str=databases->GetVal(wxT("datconfig"));
+            if (!collection->GetConnection()->BackendMinimumVersion(8, 5))
+            {
+                wxString tmp = wxEmptyString;
+                wxStringTokenizer tkz(str.Mid(1, str.Length()-2), wxT(","));
+                while (tkz.HasMoreTokens())
+                {
+                    if (tmp.Length() > 0)
+                        tmp += wxT(",");
+                    tmp += wxT("=") + tkz.GetNextToken();
+                }
+                str = wxT("{") + tmp + wxT("}");
+            }
             if (!str.IsEmpty())
                 FillArray(database->GetVariables(), str.Mid(1, str.Length()-2));
             database->iSetAllowConnections(databases->GetBool(wxT("datallowconn")));
diff --git a/pgadmin/ui/dlgDatabase.xrc b/pgadmin/ui/dlgDatabase.xrc
index cec97bc..2d5129d 100644
--- a/pgadmin/ui/dlgDatabase.xrc
+++ b/pgadmin/ui/dlgDatabase.xrc
@@ -290,6 +290,21 @@
                       <flag>wxALL|wxGROW|wxALIGN_CENTRE</flag>
                       <border>4</border>
                     </object>
+                    <object class="sizeritem">
+                      <object class="wxStaticText" name="stVarUsername">
+                        <label>User Name</label>
+                      </object>
+                      <flag>wxALIGN_CENTER_VERTICAL|wxTOP|wxLEFT|wxRIGHT</flag>
+                      <border>4</border>
+                    </object>
+                    <object class="sizeritem">
+                      <object class="ctlComboBox" name="cbVarUsername">
+                        <content/>
+                        <style>wxCB_DROPDOWN</style>
+                      </object>
+                      <flag>wxEXPAND|wxALIGN_CENTER_VERTICAL|wxTOP|wxLEFT|wxRIGHT</flag>
+                      <border>4</border>
+                    </object>
                   </object>
                   <flag>wxGROW</flag>
                 </object>
-- 
Sent via pgadmin-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgadmin-hackers

Reply via email to