library: libSQL #include "TSQLFile.h" |
TSQLFile
class description - header file - source file - inheritance tree (.pdf)
private:
void operator=(const TSQLFile&)
protected:
void AddIdEntry(Long64_t tableid, Int_t subid, Int_t type, const char* name, const char* sqlname, const char* info)
TString CodeLongString(Long64_t objid, Int_t strid)
void CreateBasicTables()
Bool_t CreateClassTable(TSQLClassInfo* sqlinfo, TObjArray* colinfos)
Bool_t CreateRawTable(TSQLClassInfo* sqlinfo)
Long64_t DefineNextKeyId()
TString DefineTableName(const char* clname, Int_t version, Bool_t rawtable)
void DeleteKeyFromDB(Long64_t keyid)
virtual Long64_t DirCreateEntry(TDirectory*)
virtual Int_t DirReadKeys(TDirectory*)
virtual void DirWriteHeader(TDirectory*)
virtual void DirWriteKeys(TDirectory*)
TSQLClassInfo* FindSQLClassInfo(const char* clname, Int_t version)
TSQLClassInfo* FindSQLClassInfo(const TClass* cl)
TKeySQL* FindSQLKey(TDirectory* dir, Long64_t keyid)
TSQLResult* GetBlobClassData(Long64_t objid, TSQLClassInfo* sqlinfo)
TSQLStatement* GetBlobClassDataStmt(Long64_t objid, TSQLClassInfo* sqlinfo)
Int_t GetLocking()
Bool_t GetLongString(Long64_t objid, Int_t strid, TString& value)
TSQLResult* GetNormalClassData(Long64_t objid, TSQLClassInfo* sqlinfo)
TSQLResult* GetNormalClassDataAll(Long64_t minobjid, Long64_t maxobjid, TSQLClassInfo* sqlinfo)
Bool_t HasTable(const char* name)
void IncrementModifyCounter()
void InitSqlDatabase(Bool_t create)
Int_t IsLongStringCode(Long64_t objid, const char* value)
Bool_t IsReadAccess()
Bool_t IsTablesExists()
Bool_t IsWriteAccess()
Bool_t ProduceClassSelectQuery(TStreamerInfo* info, TSQLClassInfo* sqlinfo, TString& columns, TString& tables, Int_t& tablecnt)
Bool_t ReadConfigurations()
TObject* ReadSpecialObject(Long64_t keyid, TObject* obj = 0)
void ReadSQLClassInfos()
TSQLClassInfo* RequestSQLClassInfo(const char* clname, Int_t version)
TSQLClassInfo* RequestSQLClassInfo(const TClass* cl)
void SaveToDatabase()
void SetLocking(Int_t mode)
Bool_t SQLApplyCommands(TObjArray* cmds)
const char* SQLBigTextType() const
Bool_t SQLCanStatement()
Bool_t SQLCommit()
const char* SQLCompatibleType(Int_t typ) const
const char* SQLDatetimeType() const
const char* SQLDefaultTableType() const
void SQLDeleteAllTables()
void SQLDeleteStatement(TSQLStatement* stmt)
const char* SQLDirIdColumn() const
const char* SQLIdentifierQuote() const
const char* SQLIntType() const
const char* SQLKeyIdColumn() const
Int_t SQLMaxIdentifierLength()
Long64_t SQLMaximumValue(const char* tablename, const char* columnname)
const char* SQLNameSeparator() const
const char* SQLObjectIdColumn() const
Bool_t SQLObjectInfo(Long64_t objid, TString& clname, Version_t& version)
TObjArray* SQLObjectsInfo(Long64_t keyid)
TSQLResult* SQLQuery(const char* cmd, Int_t flag = 0, Bool_t* res = 0)
const char* SQLRawIdColumn() const
Bool_t SQLRollback()
const char* SQLSmallTextType() const
Int_t SQLSmallTextTypeLimit() const
Bool_t SQLStartTransaction()
TSQLStatement* SQLStatement(const char* cmd, Int_t bufsize = 1000)
const char* SQLStrIdColumn() const
Bool_t SQLTestTable(const char* tablename)
const char* SQLValueQuote() const
Long64_t StoreObjectInTables(Long64_t keyid, const void* obj, const TClass* cl)
Int_t StreamKeysForDirectory(TDirectory* dir, Bool_t doupdate, Long64_t specialkeyid = -1, TKeySQL** specialkey = 0)
virtual Int_t SysClose(Int_t)
virtual Int_t SysOpen(const char*, Int_t, UInt_t)
virtual Int_t SysRead(Int_t, void*, Int_t)
virtual Long64_t SysSeek(Int_t, Long64_t, Int_t)
virtual Int_t SysStat(Int_t, Long_t*, Long64_t*, Long_t*, Long_t*)
virtual Int_t SysSync(Int_t)
virtual Int_t SysWrite(Int_t, const void*, Int_t)
Bool_t UpdateKeyData(TKeySQL* key)
Bool_t VerifyLongStringTable()
Long64_t VerifyObjectTable()
Bool_t WriteKeyData(TKeySQL* key)
Bool_t WriteSpecialObject(Long64_t keyid, TObject* obj, const char* name, const char* title)
public:
TSQLFile()
TSQLFile(const char* dbname, Option_t* option = "read", const char* user = "user", const char* pass = "pass")
virtual ~TSQLFile()
static TClass* Class()
virtual void Close(Option_t* option = "")
Bool_t Commit()
virtual TKey* CreateKey(TDirectory* mother, const TObject* obj, const char* name, Int_t bufsize)
virtual TKey* CreateKey(TDirectory* mother, const void* obj, const TClass* cl, const char* name, Int_t bufsize)
virtual void DrawMap(const char* = "*", Option_t* = "")
virtual void FillBuffer(char*&)
virtual void Flush()
Int_t GetArrayLimit() const
const char* GetDataBaseName() const
virtual Long64_t GetEND() const
virtual Int_t GetErrno() const
virtual Int_t GetNbytesFree() const
virtual Int_t GetNbytesInfo() const
virtual Int_t GetNfree() const
Int_t GetQuerisCounter() const
virtual Long64_t GetSeekFree() const
virtual Long64_t GetSeekInfo() const
virtual Long64_t GetSize() const
virtual TList* GetStreamerInfoList()
const char* GetTablesType() const
Int_t GetUseIndexes() const
Bool_t GetUseSuffixes() const
Int_t GetUseTransactions() const
virtual TClass* IsA() const
Bool_t IsMySQL() const
Bool_t IsODBC() const
virtual Bool_t IsOpen() const
Bool_t IsOracle() const
virtual void MakeFree(Long64_t, Long64_t)
virtual void MakeProject(const char*, const char* = "*", Option_t* = "new")
TString MakeSelectQuery(TClass* cl)
virtual void Map()
virtual void Paint(Option_t* = "")
virtual void Print(Option_t* = "") const
virtual Bool_t ReadBuffer(char*, Int_t)
virtual void ReadFree()
virtual Int_t Recover()
virtual Int_t ReOpen(Option_t* mode)
virtual void ResetErrno() const
Bool_t Rollback()
virtual void Seek(Long64_t, TFile::ERelativeTo = kBeg)
void SetArrayLimit(Int_t limit = 20)
virtual void SetEND(Long64_t)
void SetTablesType(const char* table_type)
void SetUseIndexes(Int_t use_type = kIndexesBasic)
void SetUseSuffixes(Bool_t on = kTRUE)
void SetUseTransactions(Int_t mode = kTransactionsAuto)
virtual void ShowMembers(TMemberInspector& insp, char* parent)
virtual Int_t Sizeof() const
void SkipArrayLimit()
void StartLogFile(const char* fname)
Bool_t StartTransaction()
void StopLogFile()
virtual void Streamer(TBuffer& b)
void StreamerNVirtual(TBuffer& b)
virtual void UseCache(Int_t = 10, Int_t = 0)
virtual Int_t Write(const char* = "0", Int_t = 0, Int_t = 0)
virtual Int_t Write(const char* = "0", Int_t = 0, Int_t = 0) const
virtual Bool_t WriteBuffer(const char*, Int_t)
virtual void WriteFree()
virtual void WriteHeader()
virtual void WriteStreamerInfo()
protected:
TSQLServer* fSQL ! interface to SQL database
TList* fSQLClassInfos ! list of SQL class infos
Bool_t fUseSuffixes ! use suffixes in column names like fValue:Int_t or fObject:pointer
Int_t fSQLIOversion ! version of SQL I/O which is stored in configurations
Int_t fArrayLimit ! limit for array size. when array bigger, its content converted to raw format
Bool_t fCanChangeConfig ! variable indicates can be basic configuration changed or not
TString fTablesType ! type, used in CREATE TABLE statements
Int_t fUseTransactions ! use transaction statements for writing data into the tables
Int_t fUseIndexes ! use indexes for tables: 0 - off, 1 - only for basic tables, 2 + normal class tables, 3 - all tables
Int_t fModifyCounter ! indicates how many changes was done with database tables
Int_t fQuerisCounter ! how many query was applied
const char** fBasicTypes ! pointer on list of basic types specific for currently connected SQL server
const char** fOtherTypes ! pointer on list of other SQL types like TEXT or blob
TString fUserName ! user name, used to access objects from database
ofstream* fLogFile ! log file with SQL statements
Bool_t fIdsTableExists ! indicate if IdsTable exists
Int_t fStmtCounter ! count numbers of active statements
public:
static const TSQLFile::ELockingKinds kLockFree
static const TSQLFile::ELockingKinds kLockBusy
static const TSQLFile::ETransactionKinds kTransactionsOff
static const TSQLFile::ETransactionKinds kTransactionsAuto
static const TSQLFile::ETransactionKinds kTransactionsUser
static const TSQLFile::EIndexesKinds kIndexesNone
static const TSQLFile::EIndexesKinds kIndexesBasic
static const TSQLFile::EIndexesKinds kIndexesClass
static const TSQLFile::EIndexesKinds kIndexesAll
________________________________________________________________________
The main motivation for the TSQLFile development is to have
"transparent" access to SQL data base via standard TFile interface.
The main approach that each class (but not each object) has one or two tables
with names like $(CLASSNAME)_ver$(VERSION) and $(CLASSNAME)_raw$(VERSION)
For example: TAxis_ver8 or TList_raw5
Second kind of tables appears, when some of class members can not be converted to
normalized form or when class has custom streamer.
For instance, for TH1 class two tables are required: TH1_ver4 and TH1_raw4
Most of memebers are stored in TH1_ver4 table columnwise, and only memeber:
Double_t* fBuffer; //[fBufferSize]
can not be represented as column while size of array is not known apriory.
Therefore, fBuffer will be written as list of values in TH1_raw4 table.
All objects, stored in the DB, will be registered in table "ObjectsTable".
In this there are following columns:
"key:id" - key identifier to which belong object
"obj:id" - object identifier
"Class" - object class name
"Version" - object class version
Data in each "ObjectsTable" row uniqly identify, in which table
and which column object is stored.
In normal situation all class data should be sorted columnwise.
Up to now following member are supported:
1) Basic data types
Here is everything clear. Column SQL type will be as much as possible
close to the original type of value.
2) Fixed array of basic data types
In this case n columns like fArr[0], fArr[1] and so on will be created.
If there is multidimensional array, names will be fArr2[1][2][1] and so on
3) Parent class
In this case version of parent class is stored and
data of parent class will be stored with the same obj:id in corrspondent table.
There is a special case, when parent store nothing (this is for instance TQObject).
In that case just -1 is written to avoid any extra checks if table exist or not.
4) Object as data member.
In that case object is saved in normal way to data base and column
will contain id of this object.
5) Pointer on object
Same as before. In case if object was already stored, just its id
will be placed in the column. For NULL pointer 0 is used.
6) TString
Now column with limited width like VARCAHR(255) in MySQL is used.
Later this will be improved to support maximum possible strings
7) Anything else.
Data will be converted to raw format and saved in _streamer_ table.
Each row supplied with obj:id and row:id, where row:id indicates
data, corresponding to this particular data member, and column
will contain this raw:id
All conversion to SQL statements are done with help of TSQLStructure class.
This is special hierarchical structure wich internally is very similar
to XML structures. TBufferSQL2 creates these structures, when object
data is streamed by ROOT and only afterwards all SQL statements will be produced
and applied all together.
When data is reading, TBufferSQL2 will produce requests to database
during unstreaming of object data.
Optionally (default this options on) name of column includes
suffix which indicates type of column. For instance:
*:parent - parent class, column contain class version
*:object - other object, column contain object id
*:rawdata - raw data, column contains id of raw data from _streamer_ table
*:Int_t - column with integer value
Use TSQLFile::SetUseSuffixes(kFALSE) to disable suffixes usage.
This and several other options can be changed only when
TSQLFile created with options "CREATE" or "RECREATE" and only before
first write operation. These options are:
SetUseSuffixes() - suffix usage in column names (default - on)
SetArrayLimit() - defines maximum array size, which can
has column for each element (default 20)
SetTablesType() - table type name in MySQL database (default "InnoDB")
SetUseIndexes() - usage of indexes in database (default kIndexesBasic)
Normally these functions should be called immidiately after TSQLFile constructor.
When objects data written to database, by default START TRANSACTION/COMMIT
SQL commands are used before and after data storage. If TSQLFile detects
any problems, ROLLBACK command will be used to restore
previous state of data base. If transactions not supported by SQL server,
they can be disabled by SetUseTransactions(kTransactionsOff). Or user
can take responsibility to use transactions function to hime
By default only indexes for basic tables are created.
In most cases usage of indexes increase perfomance to data reading,
but it also can increase time of writing data to database.
There are several modes of index usage available in SetUseIndexes() method
There is MakeSelectQuery(TClass*) method, which
produces SELECT statement to get objects data of specified class.
Difference from simple statement like:
mysql> SELECT * FROM TH1I_ver1
that not only data for that class, but also data from parent classes
will be extracted from other tables and combined in single result table.
Such select query can be usufull for external access to objects data.
Up to now MySQL 4.1 and Oracle 9i were tested.
Some extra work is required for other SQL databases.
Hopefully, this should be straigthforward.
Known problems and open questions.
1) TTree is not supported by TSQLFile. There is independent development
of TTreeSQL class, which allows to store trees directly in SQL database
2) TClonesArray is store objects in raw format,
which can not be accessed outside ROOT.
This will be changed later.
3) TDirectory cannot work. Hopefully, will (changes in ROOT basic I/O is required)
4) Streamer infos are not written to file, therefore schema evolution
is not yet supported. All eforts are done to enable this feature in
the near future
Example how TSQLFile can be used:
example of a session saving data to a SQL data base
=====================================================
const char* dbname = "mysql://host.domain:3306/dbname";
const char* username = "username";
const char* userpass = "userpass";
// Clean data base and create primary tables
TSQLFile* f = new TSQLFile(dbname, "recreate", username, userpass);
// Write with standard I/O functions
arr->Write("arr", TObject::kSingleKey);
h1->Write("histo");
// Close connection to DB
delete f;
example of a session read data from SQL data base
=====================================================
// Open database again in read-only mode
TSQLFile* f = new TSQLFile(dbname, "open", username, userpass);
// Show list of keys
f->ls();
// Read stored object, again standard ROOT I/O
TH1* h1 = (TH1*) f->Get("histo");
if (h1!=0) { h1->SetDirectory(0); h1->Draw(); }
TObject* obj = f->Get("arr");
if (obj!=0) obj->Print("*");
// close connection to DB
delete f;
The "SQL I/O" package is currently under development.
Any bug reports and suggestions are welcome.
Author: S.Linev, GSI Darmstadt, S.Linev@gsi.de
______________________________________________________________________________
TSQLFile(const char* dbname, Option_t* option, const char* user, const char* pass)
Connects to SQL server with provided arguments.
If the constructor fails in any way IsZombie() will
return true. Use IsOpen() to check if the file is (still) open.
If option = NEW or CREATE create a ROOT tables in database
if the tables already exists connection is
not opened.
= RECREATE create completely new tables. Any existing tables
will be deleted
= UPDATE open an existing database for writing.
If data base open by other TSQLFile instance for writing,
write access will be rejected
= BREAKLOCK Special case when lock was not correctly released
by TSQLFile instance. This may happen if program crashed when
TSQLFile was open with write access mode.
= READ or OPEN open an existing data base for reading.
For more details see comments for TFile::TFile() constructor
For a moment TSQLFile does not support TTree objects and subdirectories
void StartLogFile(const char* fname)
start logging of all SQL statements in specified file
Bool_t IsODBC()
checks, if ODBC driver used for database connection
void SetUseSuffixes(Bool_t on)
enable/disable uasge of suffixes in columns names
can be changed before first object is saved into file
void SetArrayLimit(Int_t limit)
Defines maximum number of columns for array representation
If array size bigger than limit, array data will be converted to raw format
This is usefull to prevent tables with very big number of columns
If limit==0, all arrays will be stored in raw format
If limit<0, all array values will be stored in column form
Default value is 20
void SetTablesType(const char* tables_type)
Defines tables type, which is used in CREATE TABLE statements
Now is only used for MySQL database, where following types are supported:
"BDB", "HEAP", "ISAM", "InnoDB", "MERGE", "MRG_MYISAM", "MYISAM"
Default for TSQLFile is "InnoDB". For more detailes see MySQL docs.
Bool_t StartTransaction()
Start user transaction.
This can be usesfull, when big number of objects should be stored in
data base and commitment required only if all operations were succesfull.
In that case in the end of all operations method Commit() should be
called. If operation on user-level is looks like not successfull,
method Rollback() will return database data and TSQLFile instance to
previous state.
In MySQL not all tables types support transaction mode of operation.
See SetTablesType() method for details .
void SetUseIndexes(Int_t use_type)
Specify usage of indexes for data tables
kIndexesNone = 0 - no indexes are used
kIndexesBasic = 1 - indexes used only for keys list and
objects list tables (default)
kIndexesClass = 2 - index also created for every normal class table
kIndexesAll = 3 - index created for every table, including _streamer_ tables
Indexes in general should increase speed of access to objects data,
but they required more operations and more disk space on server side
const char* GetDataBaseName()
Return name of data base on the host
For Oracle always return 0
Bool_t WriteSpecialObject(Long64_t keyid, TObject* obj, const char* name, const char* title)
write special kind of object like streamer infos or file itself
keys for that objects should exist in tables but not indicated in list of keys,
therefore users can not get them with TDirectory::Get() method
TList* GetStreamerInfoList()
Read back streamer infos from database
List of streamer infos is always stored with key:id 0,
which is not shown in normal keys list
void SaveToDatabase()
save data which is not yet in Database
Typically this is streamerinfos structures or
void CreateBasicTables()
Creates initial tables in database
This is table with configurations and table with keys
Function called once when first object is stored to the file.
void IncrementModifyCounter()
Update value of modify counter in config table
Modify counter used to indicate that something was changed in database.
It will be used when multiple instances of TSQLFile for the same data base
will be connected.
TString MakeSelectQuery(TClass* cl)
Produce SELECT statement which can be used to get all data
of class cl in one SELECT statement
This statement also can be used to create VIEW by command like
mysql> CREATE VIEW TH1I_view AS $CLASSSELECT$
Where $CLASSSELECT$ argument should be produced by call
f->MakeSelectQuery(TH1I::Class());
VIEWs supported by latest MySQL 5 and Oracle
TSQLResult* SQLQuery(const char* cmd, Int_t flag, Bool_t* ok)
submits query to SQL server
if flag==0, result is not interesting and will be deleted
if flag==1, return result of submitted query
if flag==2, results is may be necessary for long time
Oracle plugin do not support working with several TSQLResult
objects, therefore explicit deep copy will be produced
If ok!=0, it will contains kTRUE is Query was successfull, otherwise kFALSE
Long64_t SQLMaximumValue(const char* tablename, const char* columnname)
Returns maximum value, found in specified columnname of table tablename
Column type should be numeric
void DeleteKeyFromDB(Long64_t keyid)
remove key with specified id from keys table
also removes all objects data, related to this table
void AddIdEntry(Long64_t tableid, Int_t subid, Int_t type, const char* name, const char* sqlname, const char* info)
Add entry into IdsTable, where all tables names and columns names are listed
Long64_t VerifyObjectTable()
Checks that objects table is exists
If not, table will be created
Returns maximum value for existing objects id
Author: Sergey Linev 20/11/2005
Last update: root/sql:$Name: $:$Id: TSQLFile.cxx,v 1.15 2006/06/27 14:36:27 brun Exp $
Copyright (C) 1995-2005, Rene Brun and Fons Rademakers. *
ROOT page - Class index - Class Hierarchy - Top of the page
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.