Changeset 52582cf in tol


Ignore:
Timestamp:
Oct 26, 2008, 10:36:44 AM (18 years ago)
Author:
Víctor de Buen <vdebuen@…>
Branches:
1941-docker-image-to-test, devel, master
Children:
6f22f5e
Parents:
e7bf535
Message:

New methods to handle with cached inserting queries to build optimized SQL scripts

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@81 be4846ae-f051-483a-831f-b64e7d08527f

Location:
tol/stdlib/math/stat/models/bayesian/bysMcmc/bsr/dynhlm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tol/stdlib/math/stat/models/bayesian/bysMcmc/bsr/dynhlm/_db_api.tol

    re7bf535 r52582cf  
    2020  //Enables or disables SQL tracing
    2121  Real doQueryTrace = False;
    22  
     22  //Enables or disables automatic SQL cache
     23  Real allowAutoCache = False;
     24
     25  Struct CachedTableReg
     26  {
     27    Text table_;
     28    Text path_;
     29    Real file_;
     30    Real numReg_
     31  };
     32
     33  Real _autoCache = False;
     34  Text _cacheQueryRootPath = "";
     35  Set  _cachedTables = Copy(Empty);
     36
     37  Real SqlExecScript.NotImplemented(Text filePath)
     38  {
     39    WriteLn("[SqlExecScript.NotImplemented] Sorry assign "
     40      "BysMcmc::Bsr::DynHlm::DBApi::SqlExecScript or disable "+
     41      "BysMcmc::Bsr::DynHlm::DBApi::allowAutoCache","E");
     42    Real Stop
     43  };
     44
     45  Code SqlExecScript = SqlExecScript.NotImplemented;
     46
    2347  //Set of TOL alias for database tables
    2448  Set _.tableName =
    … …  
    5882  Code SqlTableTrace;
    5983  Code SqlExecTrace;
     84  Code SqlCachedTableFile;
     85  Code SqlCachedInsert;
     86  Code SqlStartCachedInsert;
     87  Code SqlStopCachedInsert;
    6088  Code SqlFormatReal;
    6189  Code SqlFormatDate;
  • tol/stdlib/math/stat/models/bayesian/bysMcmc/bsr/dynhlm/_db_api.tools.tol

    re7bf535 r52582cf  
    55//////////////////////////////////////////////////////////////////////////////
    66
     7
    78//////////////////////////////////////////////////////////////////////////////
    89//Set current data base engine to MySQL
    … …  
    1213  Text _.engine := SqlEngine::GesMys;
    1314  0
     15};
     16
     17//////////////////////////////////////////////////////////////////////////////
     18//Trace and execute a non SELECT query returning nothing but success or not
     19Real SqlExecTrace(Text query)
     20//////////////////////////////////////////////////////////////////////////////
     21{
     22  If(doQueryTrace,
     23    WriteLn("[Bsr::DynHlm::DBApi::SqlExecTrace] "+
     24    If(_autoCache,"CACHED","")+" "+
     25    "Query=\n"+query));
     26  DBExecQuery(query)
    1427};
    1528
    … …  
    2740
    2841//////////////////////////////////////////////////////////////////////////////
    29 //Trace and execute a non SELECT query returning nothing but success or not
    30 Real SqlExecTrace(Text query)
    31 //////////////////////////////////////////////////////////////////////////////
    32 {
    33   If(doQueryTrace,
    34     WriteLn("[Bsr::DynHlm::DBApi::SqlExecTrace] Query=\n"+query));
    35   Real DBExecQuery(query);
    36   1
    37 };
    38 
    39 //////////////////////////////////////////////////////////////////////////////
    4042Text SqlFormatDate(Date dte)
    4143//////////////////////////////////////////////////////////////////////////////
    … …  
    5254};
    5355
     56//////////////////////////////////////////////////////////////////////////////
     57//
     58Set SqlCachedTableFile(Text table)
     59//////////////////////////////////////////////////////////////////////////////
     60{
     61  Text id_table = ToName(table);
     62  Real idx = FindIndexByName(_cachedTables,id_table);
     63  If(idx, _cachedTables[idx],
     64  {
     65    Text path = _cacheQueryRootPath+id_table+".sql";
     66    Real file = FOpen(path,"w");
     67    Real FPutText(file,"INSERT INTO "+table+" VALUES");
     68    Set aux = CachedTableReg(table,path,file,0);
     69    Set entry = SetOfSet(Eval(id_table+"=aux"));
     70    Set Append(_cachedTables, entry);
     71    Real SetIndexByName(_cachedTables);
     72    entry[1]
     73  })
     74};
     75
     76//////////////////////////////////////////////////////////////////////////////
     77//
     78Real SqlCachedInsert(Text table, Text values)
     79//////////////////////////////////////////////////////////////////////////////
     80{
     81  If(!_autoCache,
     82  {
     83    SqlExecTrace(
     84    "INSERT INTO "+table+"\n"+
     85    "VALUES("+values+")")
     86  },
     87  {
     88    Set tabReg = SqlCachedTableFile(table);   
     89    Real FPutText(tabReg->file_, If(tabReg->numReg_,",","")+"\n"+
     90      "("+values+")");
     91    Real tabReg->numReg_ := tabReg->numReg_+1;
     92    1
     93  })
     94};
     95
     96//////////////////////////////////////////////////////////////////////////////
     97//Enables automatic cached queries
     98Real SqlStartCachedInsert(Real unused)
     99//////////////////////////////////////////////////////////////////////////////
     100{
     101  If(!allowAutoCache,1,
     102  {
     103    Real _autoCache := True;
     104    Set _cachedTables := Copy(Empty);
     105  //WriteLn("TRACE [SqlStartCachedInsert] 1");
     106    Text _cacheQueryRootPath :=
     107       Replace(TolAppDataPath,"\\","/")+"SqlScript/"+
     108       "tmp."+IntText(Rand(1,999999999))+"/";
     109  //WriteLn("TRACE [SqlStartCachedInsert] 2");
     110    WriteLn("[SqlStartCachedInsert] Storing SQL cache into "+
     111      _cacheQueryRootPath);
     112    Real OSDirMake(_cacheQueryRootPath);
     113  //WriteLn("TRACE [SqlStartCachedInsert] 3");
     114    1
     115  })
     116};
     117
     118//////////////////////////////////////////////////////////////////////////////
     119//Disables automatic cached queries
     120Real SqlStopCachedInsert(Real unused)
     121//////////////////////////////////////////////////////////////////////////////
     122{
     123  Real ok = If(!allowAutoCache,1,
     124  {
     125    Real _autoCache := False;
     126    Real ok.1 = SetMin(EvalSet(_cachedTables, Real(CachedTableReg tabReg)
     127    {
     128      Real FClose(tabReg->file_);
     129      Text query = ReadFile(tabReg->path_);
     130      Real ok.1.1 = SqlExecTrace(query);
     131    //Real ok.1 = SqlExecScript(ReplaceSlash(tabReg->path_));
     132      Real ok.1.2 = OSFilRemove(tabReg->path_);
     133      And(ok.1.1,ok.1.2)
     134    }));
     135    Real ok.2 = OSDirRemove(_cacheQueryRootPath);
     136    Text _cacheQueryRootPath := "";
     137    Set _cachedTables := Copy(Empty);
     138    And(ok.1,ok.2)
     139  });
     140  ok
     141};
    54142
    55143//////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset for help on using the changeset viewer.