summaryrefslogtreecommitdiff
path: root/storage/connect/tabpivot.cpp
diff options
context:
space:
mode:
authorOlivier Bertrand <bertrandop@gmail.com>2013-05-22 13:35:21 +0200
committerOlivier Bertrand <bertrandop@gmail.com>2013-05-22 13:35:21 +0200
commitaebcd56ca912979c92a32513def44aa121d82bb2 (patch)
tree08f3b33040d32d0cb4b397371988746636d4169d /storage/connect/tabpivot.cpp
parent41bda068a5affa1f8b483784df532c5a3ee8dcfd (diff)
downloadmariadb-git-aebcd56ca912979c92a32513def44aa121d82bb2.tar.gz
- Changing CONNECT version number and date
modified: storage/connect/ha_connect.cc storage/connect/mysql-test/connect/r/xml.result - Testing default pivot columns for srcdef Fixing order by MariaDB bug for pivot tables modified: storage/connect/tabmysql.cpp storage/connect/tabmysql.h storage/connect/tabpivot.cpp storage/connect/tabpivot.h
Diffstat (limited to 'storage/connect/tabpivot.cpp')
-rw-r--r--storage/connect/tabpivot.cpp151
1 files changed, 73 insertions, 78 deletions
diff --git a/storage/connect/tabpivot.cpp b/storage/connect/tabpivot.cpp
index 867689626f9..916a3c2584e 100644
--- a/storage/connect/tabpivot.cpp
+++ b/storage/connect/tabpivot.cpp
@@ -162,6 +162,42 @@ PCOL TDBPIVOT::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
} // end of MakeCol
/***********************************************************************/
+/* Find default fonction and pivot columns. */
+/***********************************************************************/
+bool TDBPIVOT::FindDefaultColumns(PGLOBAL g)
+ {
+ PCOLDEF cdp;
+ PTABDEF defp = Tdbp->GetDef();
+
+ if (!Fncol) {
+ for (cdp = defp->GetCols(); cdp; cdp = cdp->GetNext())
+ if (!Picol || stricmp(Picol, cdp->GetName()))
+ Fncol = cdp->GetName();
+
+ if (!Fncol) {
+ strcpy(g->Message, MSG(NO_DEF_FNCCOL));
+ return true;
+ } // endif Fncol
+
+ } // endif Fncol
+
+ if (!Picol) {
+ // Find default Picol as the last one not equal to Fncol
+ for (cdp = defp->GetCols(); cdp; cdp = cdp->GetNext())
+ if (stricmp(Fncol, cdp->GetName()))
+ Picol = cdp->GetName();
+
+ if (!Picol) {
+ strcpy(g->Message, MSG(NO_DEF_PIVOTCOL));
+ return true;
+ } // endif Picol
+
+ } // endif Picol
+
+ return false;
+ } // end of FindDefaultColumns
+
+/***********************************************************************/
/* Prepare the source table Query. */
/***********************************************************************/
bool TDBPIVOT::GetSourceTable(PGLOBAL g)
@@ -174,69 +210,45 @@ bool TDBPIVOT::GetSourceTable(PGLOBAL g)
if (!(Tdbp = GetSubTable(g, ((PPIVOTDEF)To_Def)->Tablep, true)))
return true;
- if (!Tdbp->IsView()) {
+ if (!GBdone) {
+ char *colist;
PCOLDEF cdp;
- PTABDEF defp = Tdbp->GetDef();
- if (!Fncol) {
- for (cdp = defp->GetCols(); cdp; cdp = cdp->GetNext())
- if (!Picol || stricmp(Picol, cdp->GetName()))
- Fncol = cdp->GetName();
-
- if (!Fncol) {
- strcpy(g->Message, MSG(NO_DEF_FNCCOL));
- return true;
- } // endif Fncol
-
- } // endif Fncol
-
- if (!Picol) {
- // Find default Picol as the last one not equal to Fncol
- for (cdp = defp->GetCols(); cdp; cdp = cdp->GetNext())
- if (!Fncol || stricmp(Fncol, cdp->GetName()))
- Picol = cdp->GetName();
-
- if (!Picol) {
- strcpy(g->Message, MSG(NO_DEF_PIVOTCOL));
- return true;
- } // endif Picol
-
- } // endif Picol
-
- if (!GBdone) {
- char *colist;
+ if (FindDefaultColumns(g))
+ return true;
- // Locate the suballocated colist (size is not known yet)
- *(colist = (char*)PlugSubAlloc(g, NULL, 0)) = 0;
+ // Locate the suballocated colist (size is not known yet)
+ *(colist = (char*)PlugSubAlloc(g, NULL, 0)) = 0;
- // Make the column list
- for (cdp = To_Def->GetCols(); cdp; cdp = cdp->GetNext())
- if (!cdp->GetOffset())
- strcat(strcat(colist, cdp->GetName()), ", ");
+ // Make the column list
+ for (cdp = To_Def->GetCols(); cdp; cdp = cdp->GetNext())
+ if (!cdp->GetOffset())
+ strcat(strcat(colist, cdp->GetName()), ", ");
- // Add the Pivot column at the end of the list
- strcat(colist, Picol);
+ // Add the Pivot column at the end of the list
+ strcat(colist, Picol);
- // Now we know how much was suballocated
- PlugSubAlloc(g, NULL, strlen(colist));
+ // Now we know how much was suballocated
+ PlugSubAlloc(g, NULL, strlen(colist));
- // Locate the source string (size is not known yet)
- Tabsrc = (char*)PlugSubAlloc(g, NULL, 0);
+ // Locate the source string (size is not known yet)
+ Tabsrc = (char*)PlugSubAlloc(g, NULL, 0);
- // Start making the definition
- strcat(strcat(strcpy(Tabsrc, "SELECT "), colist), ", ");
+ // Start making the definition
+ strcat(strcat(strcpy(Tabsrc, "SELECT "), colist), ", ");
- // Make it suitable for Pivot by doing the group by
- strcat(strcat(Tabsrc, Function), "(");
- strcat(strcat(strcat(Tabsrc, Fncol), ") "), Fncol);
- strcat(strcat(Tabsrc, " FROM "), Tabname);
- strcat(strcat(Tabsrc, " GROUP BY "), colist);
+ // Make it suitable for Pivot by doing the group by
+ strcat(strcat(Tabsrc, Function), "(");
+ strcat(strcat(strcat(Tabsrc, Fncol), ") "), Fncol);
+ strcat(strcat(Tabsrc, " FROM "), Tabname);
+ strcat(strcat(Tabsrc, " GROUP BY "), colist);
- // Now we know how much was suballocated
- PlugSubAlloc(g, NULL, strlen(Tabsrc));
- } // endif !GBdone
+ if (Tdbp->IsView()) // Until MariaDB bug is fixed
+ strcat(strcat(Tabsrc, " ORDER BY "), colist);
- } // endif IsView
+ // Now we know how much was suballocated
+ PlugSubAlloc(g, NULL, strlen(Tabsrc));
+ } // endif !GBdone
} else if (!Tabsrc) {
strcpy(g->Message, MSG(SRC_TABLE_UNDEF));
@@ -263,6 +275,10 @@ bool TDBPIVOT::GetSourceTable(PGLOBAL g)
bool TDBPIVOT::MakePivotColumns(PGLOBAL g)
{
if (!Tdbp->IsView()) {
+ // This was not done yet if GBdone is true
+ if (FindDefaultColumns(g))
+ return true;
+
// Now it is time to allocate the pivot and function columns
if (!(Fcolp = Tdbp->ColDB(g, Fncol, 0))) {
// Function column not found in table
@@ -309,36 +325,15 @@ bool TDBPIVOT::MakeViewColumns(PGLOBAL g)
} else
tdbp = (PTDBMY)Tdbp;
- if (!Fncol || !Picol) {
- strcpy(g->Message, "Missing Function or Pivot column");
+ if (!Fncol && !(Fncol = tdbp->FindFieldColumn(Picol))) {
+ strcpy(g->Message, MSG(NO_DEF_FNCCOL));
return true;
- } // endif
-#if 0
- if (!Fncol) {
- for (crp = qryp->Colresp; crp; crp = crp->Next)
- if (!Picol || stricmp(Picol, crp->Name))
- Fncol = crp->Name;
-
- if (!Fncol) {
- strcpy(g->Message, MSG(NO_DEF_FNCCOL));
- return true;
- } // endif Fncol
-
} // endif Fncol
- if (!Picol) {
- // Find default Picol as the last one not equal to Fncol
- for (crp = qryp->Colresp; crp; crp = crp->Next)
- if (!Fncol || stricmp(Fncol, crp->Name))
- Picol = crp->Name;
-
- if (!Picol) {
- strcpy(g->Message, MSG(NO_DEF_PIVOTCOL));
- return true;
- } // endif Picol
-
+ if (!Picol && !(Picol = tdbp->FindFieldColumn(Fncol))) {
+ strcpy(g->Message, MSG(NO_DEF_PIVOTCOL));
+ return true;
} // endif Picol
-#endif // 0
// Now it is time to allocate the pivot and function columns
if (!(Fcolp = tdbp->MakeFieldColumn(g, Fncol)))