StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StDbSql.hh
1 /***************************************************************************
2  *
3  * $Id: StDbSql.hh,v 1.11 2016/05/25 20:17:51 dmitry Exp $
4  *
5  * Author: R. Jeff Porter
6  ***************************************************************************
7  *
8  * Description: Implementation class of StDataBaseI in (My)SQL
9  *
10  ***************************************************************************
11  *
12  * $Log: StDbSql.hh,v $
13  * Revision 1.11 2016/05/25 20:17:51 dmitry
14  * coverity - uninit ctor
15  *
16  * Revision 1.10 2005/11/07 14:46:44 deph
17  * added protoype for function that accepts IN for non contiguous elementIDs
18  *
19  * Revision 1.9 2004/01/15 00:02:25 fisyak
20  * Replace ostringstream => StString, add option for alpha
21  *
22  * Revision 1.8 2003/09/16 22:44:17 porter
23  * got rid of all ostrstream objects; replaced with StString+string.
24  * modified rules.make and added file stdb_streams.h for standalone compilation
25  *
26  * Revision 1.7 2003/04/11 22:47:36 porter
27  * Added a fast multi-row write model specifically needed by the daqEventTag
28  * writer. Speed increased from about 100Hz to ~3000Hz. It is only invoked if
29  * the table is marked as Non-Indexed (daqTags & scalers). For non-indexed tables
30  * which include binary stored data (we don't have any yet), the fast writer has
31  * to invoke a slower buffer so that the rates are a bit slower (~500Hz at 50 rows/insert).
32  *
33  * Revision 1.6 2003/01/10 04:19:20 porter
34  * added feature of getting timestamp list (but no data) for a table.
35  * fixed 2 features sometimes used in online in query-by-whereclause.
36  * removed a stray 'cout' in a routine that is rarely accessed
37  *
38  * Revision 1.5 2002/01/30 15:40:48 porter
39  * changed limits on flavor tag & made defaults retrieving more readable
40  *
41  * Revision 1.4 2001/10/24 04:05:20 porter
42  * added long long type to I/O and got rid of obsolete dataIndex table
43  *
44  * Revision 1.3 2001/04/25 17:19:53 perev
45  * HPcorrs
46  *
47  * Revision 1.2 2001/03/30 18:48:26 porter
48  * modified code to keep Insure from wigging-out on ostrstream functions.
49  * moved some messaging into a StDbSql method.
50  *
51  * Revision 1.1 2001/01/22 18:37:59 porter
52  * Update of code needed in next year running. This update has little
53  * effect on the interface (only 1 method has been changed in the interface).
54  * Code also preserves backwards compatibility so that old versions of
55  * StDbLib can read new table structures.
56  * -Important features:
57  * a. more efficient low-level table structure (see StDbSql.cc)
58  * b. more flexible indexing for new systems (see StDbElememtIndex.cc)
59  * c. environment variable override KEYS for each database
60  * d. StMessage support & clock-time logging diagnostics
61  * -Cosmetic features
62  * e. hid stl behind interfaces (see new *Impl.* files) to again allow rootcint access
63  * f. removed codes that have been obsolete for awhile (e.g. db factories)
64  * & renamed some classes for clarity (e.g. tableQuery became StDataBaseI
65  * and mysqlAccessor became StDbSql)
66  *
67  *
68  **************************************************************************/
69 #ifndef STDATABASE_HH
70 #define STDATABASE_HH
71 
72 #include "StDbTableDescriptor.h"
73 #include "StDbManager.hh"
74 #include "StDataBaseI.hh"
75 #include "MysqlDb.h"
76 #include "StDbBuffer.h"
77 
78 #include <list>
79 #ifdef ST_NO_TEMPLATE_DEF_ARGS
80 typedef list<StDbTableDescriptor*,allocator<StDbTableDescriptor*> > DescList;
81 #else
82 #if !defined(ST_NO_NAMESPACES)
83 using std::list;
84 #endif
85 
86 #ifdef HPUX
87 #define freeze(i) str()
88 #endif
89 
90 typedef list<StDbTableDescriptor*> DescList;
91 #endif
92 
93 #define MAX_EID_INDECES 10
94 
95 class StDbSql : public StDataBaseI {
96 
97 protected:
98 
99  StDbManager* mgr = 0;
100 
101  // A found descriptor list -> can be faster than asking DB if already found
102  DescList mdescriptors;
103 
104  char* mretString = 0; // memory holder for internal string passing
105  int mtableCatalog = 0; // 0==hasn't checked, 1==doesn't have it, 2==has it
106  char* mdefaultEndDateTime = 0;
107  unsigned int mdefaultEndTime = 0;
108 
109  // descriptor tracking methods
110  StDbTableDescriptor* findDescriptor(int structID, int schemaID);
111  StDbTableDescriptor* getDescriptor(int structID, int schemaID);
112  void addDescriptor(StDbTableDescriptor* td);
113  void deleteDescriptors();
114  void deleteDescriptor(int structID, int schemaID);
115 
116  int prepareNode(StDbNode* node);
117  int queryNode(StDbNode* node);
118  bool readNodeInfo(StDbNode* node);
119  bool readConfigNodeInfo(StDbConfigNode* node);
120  bool readTableInfo(StDbTable* table);
121  bool checkValue(const char* colName, const char* colValue);
122  bool checkForNull(const char* src);
123 
124  // use local 'mretString' for building and tracking common query entities
125  char* insertNodeString(StDbNode* node);
126  char* getFlavorQuery(const char* flavor);
127  char* getProdTimeQuery(unsigned int prodTime);
128  char* getElementList(int* elements, int num);
129  char* getElementListIN(int* elements, int num);
130  char* getColumnList(StDbTable* table,char* tableName=0, char* funcName=0);
131  char* getEmptyString();
132 
133  bool hasInstance(StDbTable* table);
134  void checkTableCatalog();
135  char* checkTablePrepForQuery(StDbTable* table, bool checkIndexed=false);
136 
137  void deleteRows(const char* tableName, int* rowID, int nrows);
138  void initEndTime();
139 
140  char* mRetString(StString& rs);
141  int sendMess(const char* a, const char* b, StDbMessLevel m,
142  int lineNum=0, const char* className=" ",
143  const char* methName=" ");
144 
145  /* new */
146  bool checkColumn(const char* tableName, const char* columnName);
147  bool updateEndTime(StDbTable* table, const char* dataTable, unsigned int reqTime);
148  void init();
149 
150  // specific meth for fast multi-row writes of non-indexed tables
151  virtual int WriteDbNoIndex(StDbTable* table, unsigned int storeTime);
152 
153 
154 public:
155 
156  StDbSql(MysqlDb &db, StDbBuffer& buffer);
157  StDbSql(MysqlDb &db, StDbBuffer& buffer,
158  StDbType type, StDbDomain domain);
159  StDbSql(MysqlDb &db, StDbBuffer& buffer,
160  const char* typeName, const char* domainName);
161 
162  virtual ~StDbSql();
163 
164  virtual void use();
165  virtual void close();
166  virtual void clear();
167 
168  virtual int QueryDb(StDbConfigNode* node);
169  virtual int QueryDb(StDbNode* node);
170  virtual int QueryDb(StDbTable* table, unsigned int reqTime);
171  virtual int QueryDb(StDbTable* table, const char* whereClause);
172  virtual unsigned int* QueryDbTimes(StDbTable* table, const char* whereClause,int opt=0);
173  virtual int QueryDbFunction(StDbTable* table, const char* whereClause, char* funcName);
174  virtual int QueryDescriptor(StDbTable* table);
175  virtual int WriteDb(StDbTable* table, unsigned int storeTime);
176  virtual int WriteDb(StDbConfigNode* node, int parentID, int& configID);
177 
178  virtual int storeConfigNode(StDbConfigNode* node);
179  virtual int storeTableNode(StDbTable* table);
180  virtual bool insertNodeRelation(int configID, int parent, int child);
181  virtual bool rollBack(StDbNode* node);
182  virtual bool rollBack(StDbTable* table);
183 
184  virtual unsigned int getUnixTime(const char* time) ;
185  virtual char* getDateTime(unsigned int time) ;
186 
187  virtual int* selectElements(const char* elementName,
188  StDbElementIndex* inval,
189  int& numElements);
190 
191  char* getDataTable(StDbTable* table, unsigned int time);
192  char** getDataTables(StDbTable* table,int& numTables);
193  void setDbUtils(MysqlDb& db, StDbBuffer& buffer);
194  void setDefaultReturnValues(StDbTable* table, unsigned int reqTime);
195  void setDefaultBeginTime(StDbTable* table,unsigned int reqTime);
196  void setDefaultEndTime(StDbTable* table );
197  char** getIndexNames( const char* elementName, int& numIndexes);
198 
199 MysqlDb& Db;
200 StDbBuffer& buff;
201 
202 };
203 
204 inline char* StDbSql::mRetString(StString& rs){
205  if(mretString)delete [] mretString;
206  string srs=rs.str();
207  mretString = new char[srs.length()+1];
208  strcpy(mretString,srs.c_str());
209  return mretString;
210 }
211 
212 inline int StDbSql::sendMess(const char* a, const char* b, StDbMessLevel m, int lineNum, const char* className, const char* methName){
213  if(m==dbMDebug && !(mgr->IsVerbose()))return 0;
214  return mgr->printInfo(a,b,m,lineNum,className,methName);
215 }
216 
217 inline void StDbSql::init() {
218  mretString = 0;
219  mtableCatalog=0;
220  mdefaultEndDateTime = 0;
221 }
222 
223 inline void StDbSql::use() { Db.setDefaultDb(mdbName); };
224 inline void StDbSql::close() { Db.Close(); };
225 inline void StDbSql::clear() { Db.Release(); buff.Raz(); };
226 
227 #endif
228 
229 
230 
231 
232