Ignore:
Timestamp:
11/01/06 08:29:46 (18 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/msql/MSQLServer.cc

    r7940 r8185  
    11/* ======================================================================== *\
     2! $Name: not supported by cvs2svn $:$Id: MSQLServer.cc,v 1.13 2006-11-01 08:29:45 tbretz Exp $
     3! --------------------------------------------------------------------------
    24!
    35! *
     
    1820!   Author(s): Thomas Bretz 2/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
    1921!
    20 !   Copyright: MAGIC Software Development, 2000-2004
     22!   Copyright: MAGIC Software Development, 2000-2006
    2123!
    2224!
     
    4749#include <TH1.h>
    4850#include <TEnv.h>
     51#include <TPRegexp.h>
    4952
    5053#include <TSQLResult.h>
     
    519522}
    520523
    521 TSQLResult *MSQLServer::GetTables(const char *dbname, const char *wild) /*FOLD00*/
    522 {
    523     return fType==kIsServer && fServ ? fServ->GetTables(dbname, wild) : NULL;
    524 }
    525 
    526 TSQLResult *MSQLServer::GetColumns(const char *dbname, const char *table, const char *wild) /*FOLD00*/
    527 {
    528     return fType==kIsServer && fServ ? fServ->GetColumns(dbname, table, wild) : NULL;
     524TSQLResult *MSQLServer::GetTables(const char *wild, const char *dbname) /*FOLD00*/
     525{
     526    return fType==kIsServer && fServ ? fServ->GetTables(dbname?dbname:fDataBase.Data(), wild) : NULL;
     527}
     528
     529TSQLResult *MSQLServer::GetColumns(const char *table, const char *wild, const char *dbname) /*FOLD00*/
     530{
     531    return fType==kIsServer && fServ ? fServ->GetColumns(dbname?dbname:fDataBase.Data(), table, wild) : NULL;
    529532}
    530533
     
    604607    }
    605608    else
    606         SetBit(kZombie);
     609        fType = kIsZombie;
    607610
    608611    fList.SetOwner();
     
    749752}
    750753
     754// --------------------------------------------------------------------------
     755//
     756// Return the name of the (first) column with a primary key
     757//
     758TString MSQLServer::GetPrimaryKey(const char *table)
     759{
     760    TSQLResult *res = GetColumns(table);
     761    if (!res)
     762        return "";
     763
     764    TString rc;
     765
     766    TSQLRow *row = 0;
     767    while ((row=res->Next()))
     768    {
     769        TString key = (*row)[3];
     770        if (key!="PRI")
     771            continue;
     772
     773        rc = (*row)[0];
     774        break;
     775    }
     776
     777    delete res;
     778    return rc;
     779}
     780
     781// --------------------------------------------------------------------------
     782//
     783// Searches in the text for patterns like "Table.Column". If such a pettern
     784// is found the primary key of the table is requested a "LEFT JOIN"
     785// with this Table is added ON the identity of the primary key of Table
     786// with the given table.
     787//
     788TString MSQLServer::GetJoins(const char *table, const TString text)
     789{
     790    Int_t p=0;
     791
     792    TString mods;
     793    TArrayI pos;
     794
     795    // Find all Table.Column expression. Because also floating point
     796    // numbers can contain a dot the result has to be checked carefully
     797    TString joins;
     798    TPRegexp reg = TPRegexp("\\w+[.]\\w+");
     799    while (1)
     800    {
     801        // Check whether expression is found
     802        if (reg.Match(text, mods, p, 130, &pos)==0)
     803            break;
     804
     805        // Get expression from text
     806        const TString expr = text(pos[0], pos[1]-pos[0]);
     807        p = pos[1];
     808
     809        if (expr.IsFloat())
     810            continue;
     811
     812        const TString tab = expr(0, expr.First('.'));
     813        const TString var = expr(expr.First('.')+1, expr.Length());
     814
     815        // If the table found is the primary table itself skip it.
     816        if (tab==table)
     817            continue;
     818
     819        // If this join has already be set, skip it.
     820        if (joins.Contains(Form(" %s ", tab.Data())))
     821            continue;
     822
     823        // Now get the primary key of the table to be joined
     824        const TString prim = GetPrimaryKey(tab);
     825        if (prim.IsNull())
     826            continue;
     827
     828        joins += Form("LEFT JOIN %s ON %s.%s=%s.%s ", tab.Data(),
     829                      table, prim.Data(), tab.Data(), prim.Data());
     830    }
     831
     832    if (!joins.IsNull())
     833        joins += " ";
     834
     835    return joins;
     836}
     837
    751838void MSQLServer::RecursiveRemove(TObject *obj)
    752839{
Note: See TracChangeset for help on using the changeset viewer.