Le 31/10/2010 09:44, Guillaume Lelarge a écrit :
> Le 31/10/2010 00:39, Dave Page a écrit :
>> On Sun, Oct 31, 2010 at 1:56 AM, Guillaume Lelarge
>> <[email protected]> wrote:
>>> Le 30/10/2010 10:25, Dave Page a écrit :
>>>
>>>> Yeah, that's really nasty. I guess we need split the commands at ;.
>>>
>>> Yeah. If it's not between quotes. I don't like it at all, but I don't
>>> see another way of doing it.
>>>
>>>> I guess we should pass a flag down somehow to tell the function that
>>>> executes the query to do that and then we could also potentially get
>>>> rid of the double SQL boxes.  I'm not looking at the code, but I
>>>> suspect that'll be nasty.
>>>>
>>>
>>> We actually aren't required to add such a flag. We can check if the
>>> query contains "ALTER TYPE", and "ADD AFTER" or "ADD BEFORE".
>>
>> That's knowledge I'd rather avoid hardwiring into the lower level
>> machinery here.
>>
> 
> So do I. I tried a few things yesterday. Changing the apply() and
> GetSql() parameters imply to change all GetSql for all dlg* source code.
> That will be quite an invasive patch.
> 

I've done the "split-the-queries" function. Seems to work great, but
still doesn't cover dollar quoting. Anyway, it's less ugly than I
thought. The interesting part is dlgProperty::SplitQueries(). Would love
to get comments :)


-- 
Guillaume
 http://www.postgresql.fr
 http://dalibo.com
diff --git a/pgadmin/dlg/dlgProperty.cpp b/pgadmin/dlg/dlgProperty.cpp
index ece5ea3..5f8b4b0 100644
--- a/pgadmin/dlg/dlgProperty.cpp
+++ b/pgadmin/dlg/dlgProperty.cpp
@@ -799,6 +799,7 @@ void dlgProperty::ShowObject()
 
 bool dlgProperty::apply(const wxString &sql, const wxString &sql2)
 {
+    wxString tmp;
     pgConn *myConn = connection;
 
     if (GetDisconnectFirst())
@@ -809,37 +810,22 @@ bool dlgProperty::apply(const wxString &sql, const wxString &sql2)
 
     if (!sql.IsEmpty())
     {
-        wxString tmp;
-        if (cbClusterSet && cbClusterSet->GetSelection() > 0)
-        {
-            replClientData *data=(replClientData*)cbClusterSet->GetClientData(cbClusterSet->GetSelection());
+        wxArrayString queries;
 
-            if (data->majorVer > 1 || (data->majorVer == 1 && data->minorVer >= 2))
-            {
-                tmp = wxT("SELECT ") + qtIdent(data->cluster)
-                    + wxT(".ddlscript_prepare(") + NumToStr(data->setId) + wxT(", 0);\n")
-                    + wxT("SELECT ") + qtIdent(data->cluster)
-                    + wxT(".ddlscript_complete(") + NumToStr(data->setId) + wxT(", ")
-                    + qtDbString(sql) + wxT(", 0);\n");
-            }
-            else
+        queries= SplitQueries(BuildSql(sql));
+
+        for (size_t index = 0; index < queries.GetCount(); index++)
+        {
+            tmp = queries.Item(index);
+            if (!myConn->ExecuteVoid(tmp))
             {
-                tmp = wxT("SELECT ") + qtIdent(data->cluster)
-                    + wxT(".ddlscript(") + NumToStr(data->setId) + wxT(", ")
-                    + qtDbString(sql) + wxT(", 0);\n");
+                // error message is displayed inside ExecuteVoid
+                return false;
             }
-        }
-        else
-            tmp = sql;
 
-        if (!myConn->ExecuteVoid(tmp))
-        {
-            // error message is displayed inside ExecuteVoid
-            return false;
+            if (database)
+                database->AppendSchemaChange(tmp);
         }
-
-        if (database)
-            database->AppendSchemaChange(tmp);
     }
 
     // Process the second SQL statement. This is primarily only used by
@@ -847,28 +833,7 @@ bool dlgProperty::apply(const wxString &sql, const wxString &sql2)
     // PostgreSQL 8.3+
     if (!sql2.IsEmpty())
     {
-        wxString tmp;
-        if (cbClusterSet && cbClusterSet->GetSelection() > 0)
-        {
-            replClientData *data=(replClientData*)cbClusterSet->GetClientData(cbClusterSet->GetSelection());
-
-            if (data->majorVer > 1 || (data->majorVer == 1 && data->minorVer >= 2))
-            {
-                tmp = wxT("SELECT ") + qtIdent(data->cluster)
-                    + wxT(".ddlscript_prepare(") + NumToStr(data->setId) + wxT(", 0);\n")
-                    + wxT("SELECT ") + qtIdent(data->cluster)
-                    + wxT(".ddlscript_complete(") + NumToStr(data->setId) + wxT(", ")
-                    + qtDbString(sql2) + wxT(", 0);\n");
-            }
-            else
-            {
-                tmp = wxT("SELECT ") + qtIdent(data->cluster)
-                    + wxT(".ddlscript(") + NumToStr(data->setId) + wxT(", ")
-                    + qtDbString(sql2) + wxT(", 0);\n");
-            }
-        }
-        else
-            tmp = sql2;
+        tmp = BuildSql(sql2);
 
         if (!myConn->ExecuteVoid(tmp))
         {
@@ -891,6 +856,86 @@ bool dlgProperty::apply(const wxString &sql, const wxString &sql2)
 }
 
 
+wxString dlgProperty::BuildSql(const wxString &sql)
+{
+    wxString tmp;
+
+    if (cbClusterSet && cbClusterSet->GetSelection() > 0)
+    {
+        replClientData *data=(replClientData*)cbClusterSet->GetClientData(cbClusterSet->GetSelection());
+
+        if (data->majorVer > 1 || (data->majorVer == 1 && data->minorVer >= 2))
+        {
+            tmp = wxT("SELECT ") + qtIdent(data->cluster)
+                + wxT(".ddlscript_prepare(") + NumToStr(data->setId) + wxT(", 0);\n")
+                + wxT("SELECT ") + qtIdent(data->cluster)
+                + wxT(".ddlscript_complete(") + NumToStr(data->setId) + wxT(", ")
+                + qtDbString(sql) + wxT(", 0);\n");
+        }
+        else
+        {
+            tmp = wxT("SELECT ") + qtIdent(data->cluster)
+                + wxT(".ddlscript(") + NumToStr(data->setId) + wxT(", ")
+                + qtDbString(sql) + wxT(", 0);\n");
+        }
+    }
+    else
+        tmp = sql;
+
+    return tmp;
+}
+
+
+wxArrayString dlgProperty::SplitQueries(const wxString &sql)
+{
+    wxArrayString queries;
+    wxString query;
+    wxString c;
+
+    bool antislash = false;
+    bool quote_string = false;
+    bool doublequote_string = false;
+
+    for (size_t item = 0; item < sql.Length(); item++)
+    {
+        c = sql.GetChar(item);
+
+        if (c == wxT("\\"))
+            antislash = true;
+
+        if (c == wxT("'"))
+        {
+            if (antislash)
+                antislash = false;
+            else if (quote_string)
+                quote_string = false;
+            else if (!doublequote_string)
+                quote_string = true;
+        }
+
+        if (c == wxT("\""))
+        {
+            if (antislash)
+                antislash = false;
+            else if (doublequote_string)
+                doublequote_string = false;
+            else if (!quote_string)
+                doublequote_string = true;
+        }
+
+        query = query + c;
+
+        if (c == wxT(";") && !antislash && !quote_string && !doublequote_string)
+        {
+            queries.Add(query);
+            query = wxEmptyString;
+        }
+    }
+
+    return queries;
+}
+
+
 void dlgProperty::OnApply(wxCommandEvent &ev)
 {
     if (!IsUpToDate())
diff --git a/pgadmin/include/dlg/dlgProperty.h b/pgadmin/include/dlg/dlgProperty.h
index 682cb33..0e02a20 100644
--- a/pgadmin/include/dlg/dlgProperty.h
+++ b/pgadmin/include/dlg/dlgProperty.h
@@ -161,6 +161,8 @@ protected:
 private:
     bool tryUpdate(wxTreeItemId collectionItem);
     bool apply(const wxString &sql, const wxString &sql2);
+    wxString BuildSql(const wxString &sql);
+    wxArrayString SplitQueries(const wxString &sql);
 
     DECLARE_EVENT_TABLE()
 };
-- 
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