// @(#)root/sql:$Id$
// Author: Sergey Linev  20/11/2005

/*************************************************************************
 * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TSQLStructure
#define ROOT_TSQLStructure


/////////////////////////////////////////////////////////////////////////
//                                                                     //
// TSQLStructure is special class, used in TSQLBuffer for data convers //
//                                                                     //
/////////////////////////////////////////////////////////////////////////



#ifndef ROOT_TNamed
#include "TNamed.h"
#endif

#ifndef ROOT_TObjArray
#include "TObjArray.h"
#endif

#ifndef ROOT_TAttAxis
#include "TAttAxis.h"
#endif

#ifdef Bool
#undef Bool
#endif
#ifdef True
#undef True
#endif
#ifdef False
#undef False
#endif


class TStreamerInfo;
class TStreamerInfo;
class TStreamerElement;
class TSQLFile;
class TSqlRegistry;
class TSqlRawBuffer;
class TSQLObjectData;
class TSQLClassInfo;
class TBufferSQL2;

class TSQLColumnData : public TObject {
    
protected:   
   TString     fName;             //!  name of the table column
   TString     fType;             //!  type of the table column
   TString     fValue;            //!  value of the table column
   Bool_t      fNumeric;          //!  for numeric quotes (double quotes) are not required
public:
   TSQLColumnData();
   TSQLColumnData(const char* name,   
                  const char* sqltype, 
                  const char* value, 
                  Bool_t numeric);

   TSQLColumnData(const char* name, Long64_t value);
   virtual ~TSQLColumnData();
  
   virtual const char* GetName() const { return fName.Data(); }
   const char* GetType() const { return fType.Data(); }
   const char* GetValue() const { return fValue.Data(); }
   Bool_t IsNumeric() const { return fNumeric; }
  
   ClassDef(TSQLColumnData, 1); // Single SQL column data.
};

//______________________________________________________________________

class TSQLTableData : public TObject {
  
protected:
   TSQLFile*      fFile;           //!
   TSQLClassInfo* fInfo;           //!
   TObjArray      fColumns;        //! collection of columns
   TObjArray*     fColInfos;       //! array with TSQLClassColumnInfo, used later for TSQLClassInfo
   
   TString DefineSQLName(const char* fullname);
   Bool_t HasSQLName(const char* sqlname);
   
public:
   TSQLTableData(TSQLFile* f = 0, TSQLClassInfo* info = 0);
   virtual ~TSQLTableData();

   void AddColumn(const char* name, Long64_t value);
   void AddColumn(const char* name, 
                  const char* sqltype, 
                  const char* value, 
                  Bool_t numeric);
   
   TObjArray* TakeColInfos();
   
   Int_t GetNumColumns();
   const char* GetColumn(Int_t n);
   Bool_t IsNumeric(Int_t n);
   
   ClassDef(TSQLTableData, 1); // Collection of columns data for single SQL table
};

//______________________________________________________________________

class TSQLStructure : public TObject {
protected:   

   Bool_t           CheckNormalClassPair(TSQLStructure* vers, TSQLStructure* info);

   Long64_t         FindMaxObjectId();
   void             PerformConversion(TSqlRegistry* reg, TSqlRawBuffer* blobs, const char* topname, Bool_t useblob = kFALSE);
   Bool_t           StoreObject(TSqlRegistry* reg, Long64_t objid, TClass* cl, Bool_t registerobj = kTRUE);
   Bool_t           StoreObjectInNormalForm(TSqlRegistry* reg);
   Bool_t           StoreClassInNormalForm(TSqlRegistry* reg);
   Bool_t           StoreElementInNormalForm(TSqlRegistry* reg, TSQLTableData* columns);
   Bool_t           TryConvertObjectArray(TSqlRegistry* reg, TSqlRawBuffer* blobs);

   Bool_t           StoreTObject(TSqlRegistry* reg);
   Bool_t           StoreTString(TSqlRegistry* reg);
   Bool_t           RecognizeTString(const char* &value);

   TSQLStructure*   fParent;     //!
   Int_t            fType;       //!
   const void*      fPointer;    //!
   TString          fValue;      //!
   Int_t            fArrayIndex; //!
   Int_t            fRepeatCnt;  //!
   TObjArray        fChilds;     //!

public:
   TSQLStructure();
   virtual ~TSQLStructure();
  
   TSQLStructure*   GetParent() const { return fParent; }
   void             SetParent(TSQLStructure* p) { fParent = p; }
   Int_t            NumChilds() const;
   TSQLStructure*   GetChild(Int_t n) const;
 
   void             SetType(Int_t typ) { fType = typ; }
   Int_t            GetType() const { return fType; }
   
   // this part requried for writing to SQL tables
   void             SetObjectRef(Long64_t refid, const TClass* cl);
   void             SetObjectPointer(Long64_t ptrid);
   void             SetVersion(const TClass* cl, Int_t version = -100);
   void             SetClassStreamer(const TClass* cl);
   void             SetStreamerInfo(const TStreamerInfo* info);
   void             SetStreamerElement(const TStreamerElement* elem, Int_t number);
   void             SetCustomClass(const TClass* cl, Version_t version);
   void             SetCustomElement(TStreamerElement* elem);
   void             SetValue(const char* value, const char* tname = 0);
   void             SetArrayIndex(Int_t indx, Int_t cnt=1);
   void             SetArray(Int_t sz = -1);
   void             ChangeValueOnly(const char* value);
   
   TClass*          GetObjectClass() const;
   TClass*          GetVersionClass() const;
   TStreamerInfo*   GetStreamerInfo() const;
   TStreamerElement* GetElement() const;
   Int_t            GetElementNumber() const;
   TClass*          GetCustomClass() const;
   Version_t        GetCustomClassVersion() const;
   Bool_t           GetClassInfo(TClass* &cl, Version_t &version);
   const char*      GetValueType() const;
   const char*      GetValue() const;
   Int_t            GetArrayIndex() const { return fArrayIndex; }
   Int_t            GetRepeatCounter() const { return fRepeatCnt; }
  
   void             Add(TSQLStructure* child);
   void             AddVersion(const TClass* cl, Int_t version = -100);
   void             AddValue(const char* value, const char* tname = 0);
   void             ChildArrayIndex(Int_t index, Int_t cnt = 1);

   // this is part specially for reading of sql tables
  
   Long64_t         DefineObjectId(Bool_t recursive = kTRUE);
  
   void             SetObjectData(TSQLObjectData* objdata);
   void             AddObjectData(TSQLObjectData* objdata);
   TSQLObjectData*  GetObjectData(Bool_t search = false);
  
   virtual void     Print(Option_t* option = "") const;
   void             PrintLevel(Int_t level) const;
  
   Bool_t           ConvertToTables(TSQLFile* f, Long64_t keyid, TObjArray* cmds);
  
   Int_t            LocateElementColumn(TSQLFile* f, TBufferSQL2* buf, TSQLObjectData* data);

   static Bool_t    UnpackTObject(TSQLFile* f, TBufferSQL2* buf, TSQLObjectData* data, Long64_t objid, Int_t clversion);
   static Bool_t    UnpackTString(TSQLFile* f, TBufferSQL2* buf, TSQLObjectData* data, Long64_t objid, Int_t clversion);
   static Bool_t    IsNumericType(Int_t typ);
   static const char* GetSimpleTypeName(Int_t typ);
   static TString   MakeArrayIndex(TStreamerElement* elem, Int_t n);
   static Int_t     DefineElementColumnType(TStreamerElement* elem, TSQLFile* f);
   static TString   DefineElementColumnName(TStreamerElement* elem, TSQLFile* f, Int_t indx = 0);
   static void      AddStrBrackets(TString &s, const char* quote);
  
   enum ESQLTypes {
      kSqlObject       = 10001,
      kSqlPointer      = 10002,
      kSqlVersion      = 10003,
      kSqlStreamerInfo = 10004,
      kSqlClassStreamer= 10005,
      kSqlElement      = 10006,
      kSqlValue        = 10007,
      kSqlArray        = 10008,
      kSqlObjectData   = 10009,
      kSqlCustomClass  = 10010,
      kSqlCustomElement= 10011
   };
   
   enum ESQLColumns {
      kColUnknown      = 0,
      kColSimple       = 1,
      kColSimpleArray  = 2,
      kColParent       = 3,
      kColObject       = 4,
      kColObjectArray  = 5,
      kColNormObject   = 6,
      kColNormObjectArray = 7,
      kColObjectPtr    = 8,
      kColTString      = 9,
      kColRawData      = 10
   };
   
   enum ESQLIdType {
      kIdTable    = 0,
      kIdRawTable = 1,
      kIdColumn   = 2
   }; 
       
  
   ClassDef(TSQLStructure, 1); // Table/structure description used internally by YBufferSQL.
};

// text constants, used in SQL I/O

namespace sqlio {
    
   extern Long64_t atol64(const char* value);
    
   extern const Int_t Ids_NullPtr;
   extern const Int_t Ids_RootDir;
   extern const Int_t Ids_TSQLFile;
   extern const Int_t Ids_StreamerInfos;
   extern const Int_t Ids_FirstKey;
   extern const Int_t Ids_FirstObject;
    
   extern const char* ObjectRef;
   extern const char* ObjectRef_Arr;
   extern const char* ObjectPtr;
   extern const char* ObjectInst;
   extern const char* Version;
   extern const char* TObjectUniqueId;
   extern const char* TObjectBits;
   extern const char* TObjectProcessId;
   extern const char* TStringValue;
   extern const char* IndexSepar;
   extern const char* RawSuffix;
   extern const char* ParentSuffix;
   extern const char* ObjectSuffix;
   extern const char* PointerSuffix;
   extern const char* StrSuffix;
   extern const char* LongStrPrefix;
    
   extern const char* Array;
   extern const char* Bool;
   extern const char* Char;
   extern const char* Short;
   extern const char* Int;
   extern const char* Long;
   extern const char* Long64;
   extern const char* Float;
   extern const char* Double;
   extern const char* UChar;
   extern const char* UShort;
   extern const char* UInt;
   extern const char* ULong;
   extern const char* ULong64;
   extern const char* CharStar;
   extern const char* True;
   extern const char* False;
    
   extern const char* KeysTable;
   extern const char* KeysTableIndex;
   extern const char* KT_Name;
   extern const char* KT_Title;
   extern const char* KT_Datetime;
   extern const char* KT_Cycle;
   extern const char* KT_Class;

   extern const char* DT_Create;
   extern const char* DT_Modified;
   extern const char* DT_UUID;
    
   extern const char* ObjectsTable;
   extern const char* ObjectsTableIndex;
   extern const char* OT_Class;
   extern const char* OT_Version;
   
   extern const char* IdsTable;
   extern const char* IdsTableIndex;
   extern const char* IT_TableID;
   extern const char* IT_SubID;
   extern const char* IT_Type;
   extern const char* IT_FullName;
   extern const char* IT_SQLName;
   extern const char* IT_Info;
    
   extern const char* BT_Field;
   extern const char* BT_Value;
    
   extern const char* StringsTable;
   extern const char* ST_Value;
    
   extern const char* ConfigTable;
   extern const char* CT_Field;
   extern const char* CT_Value;
    
   extern const char* cfg_Version;
   extern const char* cfg_UseSufixes;
   extern const char* cfg_ArrayLimit;
   extern const char* cfg_TablesType;
   extern const char* cfg_UseTransactions;
   extern const char* cfg_UseIndexes;
   extern const char* cfg_LockingMode;
   extern const char* cfg_ModifyCounter;
}

#endif
 TSQLStructure.h:1
 TSQLStructure.h:2
 TSQLStructure.h:3
 TSQLStructure.h:4
 TSQLStructure.h:5
 TSQLStructure.h:6
 TSQLStructure.h:7
 TSQLStructure.h:8
 TSQLStructure.h:9
 TSQLStructure.h:10
 TSQLStructure.h:11
 TSQLStructure.h:12
 TSQLStructure.h:13
 TSQLStructure.h:14
 TSQLStructure.h:15
 TSQLStructure.h:16
 TSQLStructure.h:17
 TSQLStructure.h:18
 TSQLStructure.h:19
 TSQLStructure.h:20
 TSQLStructure.h:21
 TSQLStructure.h:22
 TSQLStructure.h:23
 TSQLStructure.h:24
 TSQLStructure.h:25
 TSQLStructure.h:26
 TSQLStructure.h:27
 TSQLStructure.h:28
 TSQLStructure.h:29
 TSQLStructure.h:30
 TSQLStructure.h:31
 TSQLStructure.h:32
 TSQLStructure.h:33
 TSQLStructure.h:34
 TSQLStructure.h:35
 TSQLStructure.h:36
 TSQLStructure.h:37
 TSQLStructure.h:38
 TSQLStructure.h:39
 TSQLStructure.h:40
 TSQLStructure.h:41
 TSQLStructure.h:42
 TSQLStructure.h:43
 TSQLStructure.h:44
 TSQLStructure.h:45
 TSQLStructure.h:46
 TSQLStructure.h:47
 TSQLStructure.h:48
 TSQLStructure.h:49
 TSQLStructure.h:50
 TSQLStructure.h:51
 TSQLStructure.h:52
 TSQLStructure.h:53
 TSQLStructure.h:54
 TSQLStructure.h:55
 TSQLStructure.h:56
 TSQLStructure.h:57
 TSQLStructure.h:58
 TSQLStructure.h:59
 TSQLStructure.h:60
 TSQLStructure.h:61
 TSQLStructure.h:62
 TSQLStructure.h:63
 TSQLStructure.h:64
 TSQLStructure.h:65
 TSQLStructure.h:66
 TSQLStructure.h:67
 TSQLStructure.h:68
 TSQLStructure.h:69
 TSQLStructure.h:70
 TSQLStructure.h:71
 TSQLStructure.h:72
 TSQLStructure.h:73
 TSQLStructure.h:74
 TSQLStructure.h:75
 TSQLStructure.h:76
 TSQLStructure.h:77
 TSQLStructure.h:78
 TSQLStructure.h:79
 TSQLStructure.h:80
 TSQLStructure.h:81
 TSQLStructure.h:82
 TSQLStructure.h:83
 TSQLStructure.h:84
 TSQLStructure.h:85
 TSQLStructure.h:86
 TSQLStructure.h:87
 TSQLStructure.h:88
 TSQLStructure.h:89
 TSQLStructure.h:90
 TSQLStructure.h:91
 TSQLStructure.h:92
 TSQLStructure.h:93
 TSQLStructure.h:94
 TSQLStructure.h:95
 TSQLStructure.h:96
 TSQLStructure.h:97
 TSQLStructure.h:98
 TSQLStructure.h:99
 TSQLStructure.h:100
 TSQLStructure.h:101
 TSQLStructure.h:102
 TSQLStructure.h:103
 TSQLStructure.h:104
 TSQLStructure.h:105
 TSQLStructure.h:106
 TSQLStructure.h:107
 TSQLStructure.h:108
 TSQLStructure.h:109
 TSQLStructure.h:110
 TSQLStructure.h:111
 TSQLStructure.h:112
 TSQLStructure.h:113
 TSQLStructure.h:114
 TSQLStructure.h:115
 TSQLStructure.h:116
 TSQLStructure.h:117
 TSQLStructure.h:118
 TSQLStructure.h:119
 TSQLStructure.h:120
 TSQLStructure.h:121
 TSQLStructure.h:122
 TSQLStructure.h:123
 TSQLStructure.h:124
 TSQLStructure.h:125
 TSQLStructure.h:126
 TSQLStructure.h:127
 TSQLStructure.h:128
 TSQLStructure.h:129
 TSQLStructure.h:130
 TSQLStructure.h:131
 TSQLStructure.h:132
 TSQLStructure.h:133
 TSQLStructure.h:134
 TSQLStructure.h:135
 TSQLStructure.h:136
 TSQLStructure.h:137
 TSQLStructure.h:138
 TSQLStructure.h:139
 TSQLStructure.h:140
 TSQLStructure.h:141
 TSQLStructure.h:142
 TSQLStructure.h:143
 TSQLStructure.h:144
 TSQLStructure.h:145
 TSQLStructure.h:146
 TSQLStructure.h:147
 TSQLStructure.h:148
 TSQLStructure.h:149
 TSQLStructure.h:150
 TSQLStructure.h:151
 TSQLStructure.h:152
 TSQLStructure.h:153
 TSQLStructure.h:154
 TSQLStructure.h:155
 TSQLStructure.h:156
 TSQLStructure.h:157
 TSQLStructure.h:158
 TSQLStructure.h:159
 TSQLStructure.h:160
 TSQLStructure.h:161
 TSQLStructure.h:162
 TSQLStructure.h:163
 TSQLStructure.h:164
 TSQLStructure.h:165
 TSQLStructure.h:166
 TSQLStructure.h:167
 TSQLStructure.h:168
 TSQLStructure.h:169
 TSQLStructure.h:170
 TSQLStructure.h:171
 TSQLStructure.h:172
 TSQLStructure.h:173
 TSQLStructure.h:174
 TSQLStructure.h:175
 TSQLStructure.h:176
 TSQLStructure.h:177
 TSQLStructure.h:178
 TSQLStructure.h:179
 TSQLStructure.h:180
 TSQLStructure.h:181
 TSQLStructure.h:182
 TSQLStructure.h:183
 TSQLStructure.h:184
 TSQLStructure.h:185
 TSQLStructure.h:186
 TSQLStructure.h:187
 TSQLStructure.h:188
 TSQLStructure.h:189
 TSQLStructure.h:190
 TSQLStructure.h:191
 TSQLStructure.h:192
 TSQLStructure.h:193
 TSQLStructure.h:194
 TSQLStructure.h:195
 TSQLStructure.h:196
 TSQLStructure.h:197
 TSQLStructure.h:198
 TSQLStructure.h:199
 TSQLStructure.h:200
 TSQLStructure.h:201
 TSQLStructure.h:202
 TSQLStructure.h:203
 TSQLStructure.h:204
 TSQLStructure.h:205
 TSQLStructure.h:206
 TSQLStructure.h:207
 TSQLStructure.h:208
 TSQLStructure.h:209
 TSQLStructure.h:210
 TSQLStructure.h:211
 TSQLStructure.h:212
 TSQLStructure.h:213
 TSQLStructure.h:214
 TSQLStructure.h:215
 TSQLStructure.h:216
 TSQLStructure.h:217
 TSQLStructure.h:218
 TSQLStructure.h:219
 TSQLStructure.h:220
 TSQLStructure.h:221
 TSQLStructure.h:222
 TSQLStructure.h:223
 TSQLStructure.h:224
 TSQLStructure.h:225
 TSQLStructure.h:226
 TSQLStructure.h:227
 TSQLStructure.h:228
 TSQLStructure.h:229
 TSQLStructure.h:230
 TSQLStructure.h:231
 TSQLStructure.h:232
 TSQLStructure.h:233
 TSQLStructure.h:234
 TSQLStructure.h:235
 TSQLStructure.h:236
 TSQLStructure.h:237
 TSQLStructure.h:238
 TSQLStructure.h:239
 TSQLStructure.h:240
 TSQLStructure.h:241
 TSQLStructure.h:242
 TSQLStructure.h:243
 TSQLStructure.h:244
 TSQLStructure.h:245
 TSQLStructure.h:246
 TSQLStructure.h:247
 TSQLStructure.h:248
 TSQLStructure.h:249
 TSQLStructure.h:250
 TSQLStructure.h:251
 TSQLStructure.h:252
 TSQLStructure.h:253
 TSQLStructure.h:254
 TSQLStructure.h:255
 TSQLStructure.h:256
 TSQLStructure.h:257
 TSQLStructure.h:258
 TSQLStructure.h:259
 TSQLStructure.h:260
 TSQLStructure.h:261
 TSQLStructure.h:262
 TSQLStructure.h:263
 TSQLStructure.h:264
 TSQLStructure.h:265
 TSQLStructure.h:266
 TSQLStructure.h:267
 TSQLStructure.h:268
 TSQLStructure.h:269
 TSQLStructure.h:270
 TSQLStructure.h:271
 TSQLStructure.h:272
 TSQLStructure.h:273
 TSQLStructure.h:274
 TSQLStructure.h:275
 TSQLStructure.h:276
 TSQLStructure.h:277
 TSQLStructure.h:278
 TSQLStructure.h:279
 TSQLStructure.h:280
 TSQLStructure.h:281
 TSQLStructure.h:282
 TSQLStructure.h:283
 TSQLStructure.h:284
 TSQLStructure.h:285
 TSQLStructure.h:286
 TSQLStructure.h:287
 TSQLStructure.h:288
 TSQLStructure.h:289
 TSQLStructure.h:290
 TSQLStructure.h:291
 TSQLStructure.h:292
 TSQLStructure.h:293
 TSQLStructure.h:294
 TSQLStructure.h:295
 TSQLStructure.h:296
 TSQLStructure.h:297
 TSQLStructure.h:298
 TSQLStructure.h:299
 TSQLStructure.h:300
 TSQLStructure.h:301
 TSQLStructure.h:302
 TSQLStructure.h:303
 TSQLStructure.h:304
 TSQLStructure.h:305
 TSQLStructure.h:306
 TSQLStructure.h:307
 TSQLStructure.h:308
 TSQLStructure.h:309
 TSQLStructure.h:310
 TSQLStructure.h:311
 TSQLStructure.h:312
 TSQLStructure.h:313
 TSQLStructure.h:314
 TSQLStructure.h:315
 TSQLStructure.h:316
 TSQLStructure.h:317
 TSQLStructure.h:318
 TSQLStructure.h:319
 TSQLStructure.h:320
 TSQLStructure.h:321
 TSQLStructure.h:322
 TSQLStructure.h:323
 TSQLStructure.h:324
 TSQLStructure.h:325
 TSQLStructure.h:326
 TSQLStructure.h:327
 TSQLStructure.h:328
 TSQLStructure.h:329
 TSQLStructure.h:330
 TSQLStructure.h:331
 TSQLStructure.h:332
 TSQLStructure.h:333
 TSQLStructure.h:334
 TSQLStructure.h:335
 TSQLStructure.h:336
 TSQLStructure.h:337
 TSQLStructure.h:338
 TSQLStructure.h:339
 TSQLStructure.h:340
 TSQLStructure.h:341