Concept Table meta data i.e. data about the table itself More...
#include <TDbiTableMetaData.hxx>
Classes | |
struct | ColumnAttributes |
Column attributes. More... | |
Public Member Functions | |
TDbiTableMetaData (const string &tableName="Unknown") | |
virtual | ~TDbiTableMetaData () |
string | Sql () const |
Return SQL string to create table. | |
string | TableName () const |
Bool_t | HasEpoch () const |
UInt_t | NumCols () const |
const TDbiFieldType & | ColFieldType (Int_t colNum) const |
UInt_t | ColFieldConcept (Int_t colNum) const |
Bool_t | ColIsNullable (Int_t colNum) const |
Bool_t | ColMustDelimit (Int_t colNum) const |
string | ColName (Int_t colNum) const |
void | SetFromSql (const string &sql) |
Recreate from SQL used to create table. | |
Protected Member Functions | |
void | Clear () |
..................................................................... | |
void | SetColIsNullable (Int_t colNum, Bool_t isNullable=true) |
void | SetColName (const string &name, Int_t colNum) |
void | SetColFieldType (const TDbiFieldType &type, Int_t colNum) |
Private Types | |
enum | { MAXCOL = 1000 } |
Sanity check: limit number of columns. More... | |
Private Member Functions | |
void | ExpandTo (UInt_t colNum) |
const ColumnAttributes & | GetAttributes (Int_t colNum) const |
Return a column attributes (will be dummy entry if requesting invalid column). | |
ColumnAttributes & | SetAttributes (Int_t colNum) |
Return a setable column attributes (will be dummy entry if requesting invalid column). | |
Static Private Member Functions | |
static string | GetToken (const char *&strPtr) |
Use to parse table creation SQL - move to UtilString? | |
Private Attributes | |
std::vector< ColumnAttributes > | fColAttr |
Column attributes indexed by column (starting from 0). | |
UInt_t | fNumCols |
Number of columns. | |
string | fTableName |
Table name (either XXX or XXXVLD). | |
Static Private Attributes | |
static ColumnAttributes | fgDummy |
Dummy attributes (used when requesting invalid column). | |
Friends | |
class | TDbiDBProxy |
Concept Table meta data i.e. data about the table itself
Purpose To provide information on the names and types of columns Contact: A.Finch@lancaster.ac.uk to transient TDbiRowStream objects.
Definition at line 35 of file TDbiTableMetaData.hxx.
anonymous enum [private] |
Sanity check: limit number of columns.
Definition at line 84 of file TDbiTableMetaData.hxx.
00084 { MAXCOL = 1000};
TDbiTableMetaData::TDbiTableMetaData | ( | const string & | tableName = "Unknown" |
) |
TDbiTableMetaData::~TDbiTableMetaData | ( | ) | [virtual] |
Definition at line 56 of file TDbiTableMetaData.cxx.
References SK_DBI_Trace.
00056 { 00057 // 00058 // 00059 // Purpose: Destructor 00060 00061 00062 SK_DBI_Trace( "Destroying TDbiTableMetaData" << " "); 00063 00064 }
void TDbiTableMetaData::Clear | ( | ) | [protected] |
.....................................................................
Definition at line 68 of file TDbiTableMetaData.cxx.
References fColAttr, and fNumCols.
Referenced by SetFromSql(), and TDbiDBProxy::StoreMetaData().
00068 { 00069 // 00070 // 00071 // Purpose: Clear out existing data (apart from table name) 00072 // 00073 // Contact: N. West 00074 00075 fColAttr.clear(); 00076 fNumCols = 0; 00077 00078 }
UInt_t TDbiTableMetaData::ColFieldConcept | ( | Int_t | colNum | ) | const [inline] |
Definition at line 58 of file TDbiTableMetaData.hxx.
References TDbiTableMetaData::ColumnAttributes::Concept, and GetAttributes().
Referenced by TDbiInRowStream::RowAsCsv().
00058 { return GetAttributes(colNum).Concept;}
const TDbiFieldType& TDbiTableMetaData::ColFieldType | ( | Int_t | colNum | ) | const [inline] |
Definition at line 57 of file TDbiTableMetaData.hxx.
References GetAttributes(), and TDbiTableMetaData::ColumnAttributes::Type.
Referenced by TDbiInRowStream::AsString(), TDbiRowStream::ColFieldType(), TDbiRowStream::CurColFieldType(), TDbiInRowStream::RowAsCsv(), SetFromSql(), and Sql().
00057 { return GetAttributes(colNum).Type;}
Bool_t TDbiTableMetaData::ColIsNullable | ( | Int_t | colNum | ) | const [inline] |
Definition at line 59 of file TDbiTableMetaData.hxx.
References GetAttributes(), and TDbiTableMetaData::ColumnAttributes::IsNullable.
Referenced by Sql().
00059 { return GetAttributes(colNum).IsNullable;}
Bool_t TDbiTableMetaData::ColMustDelimit | ( | Int_t | colNum | ) | const [inline] |
Definition at line 60 of file TDbiTableMetaData.hxx.
References GetAttributes(), and TDbiTableMetaData::ColumnAttributes::MustDelimit.
Referenced by TDbiInRowStream::RowAsCsv().
00060 { return GetAttributes(colNum).MustDelimit;}
string TDbiTableMetaData::ColName | ( | Int_t | colNum | ) | const [inline] |
Definition at line 61 of file TDbiTableMetaData.hxx.
References GetAttributes(), and TDbiTableMetaData::ColumnAttributes::Name.
Referenced by TDbiRowStream::ColName(), TDbiRowStream::CurColName(), HasEpoch(), SetFromSql(), and Sql().
00061 { return GetAttributes(colNum).Name;}
void TDbiTableMetaData::ExpandTo | ( | UInt_t | colNum | ) | [private] |
Definition at line 82 of file TDbiTableMetaData.cxx.
References fColAttr, fNumCols, and MAXCOL.
Referenced by SetAttributes().
00082 { 00083 // 00084 // 00085 // Purpose: Expand table if required to specied colNum 00086 // 00087 // Arguments: 00088 // colNum in Column number to expand to (1,2...). 00089 // 00090 // Return: 00091 // 00092 // Contact: N. West 00093 // 00094 // Specification:- 00095 // ============= 00096 // 00097 // o 00098 00099 // Program Notes:- 00100 // ============= 00101 00102 // None. 00103 00104 assert ( colNum < MAXCOL ); 00105 while ( fNumCols < colNum ) { 00106 fColAttr.push_back(ColumnAttributes()); 00107 ++fNumCols; 00108 } 00109 }
const TDbiTableMetaData::ColumnAttributes & TDbiTableMetaData::GetAttributes | ( | Int_t | colNum | ) | const [private] |
Return a column attributes (will be dummy entry if requesting invalid column).
Definition at line 113 of file TDbiTableMetaData.cxx.
References fColAttr, fgDummy, fNumCols, and TDbiTableMetaData::ColumnAttributes::SetDefault().
Referenced by ColFieldConcept(), ColFieldType(), ColIsNullable(), ColMustDelimit(), ColName(), and SetAttributes().
00113 { 00114 00115 // Return a column attributes (will be dummy entry if requesting invalid column) 00116 00117 if ( colNum > 0 && colNum <= (signed) fNumCols ) return fColAttr[colNum-1]; 00118 fgDummy.SetDefault(); 00119 return fgDummy; 00120 00121 }
string TDbiTableMetaData::GetToken | ( | const char *& | strPtr | ) | [static, private] |
Use to parse table creation SQL - move to UtilString?
Definition at line 125 of file TDbiTableMetaData.cxx.
Referenced by SetFromSql().
00125 { 00126 // 00127 // 00128 // Purpose: Skip spaces and return next token from string and move pointer on. 00129 00130 string token; 00131 00132 // Skip white space and quit if at EOS. 00133 while ( isspace(*strPtr) ) ++strPtr; 00134 if ( *strPtr == 0 ) return token; 00135 00136 // Collect the first character whatever it is. 00137 char firstChar = *strPtr++; 00138 token = firstChar; 00139 if ( ! isalnum(firstChar) && firstChar != '_' ) return token; 00140 00141 // Collect more if collecting alphanumeric + underscore string 00142 while ( isalnum(*strPtr) || *strPtr == '_' ) token += *strPtr++; 00143 return token; 00144 00145 }
Bool_t TDbiTableMetaData::HasEpoch | ( | ) | const [inline] |
Definition at line 52 of file TDbiTableMetaData.hxx.
References ColName(), and NumCols().
Referenced by TDbiRowStream::HasEpoch(), and TDbiDBProxy::HasEpoch().
UInt_t TDbiTableMetaData::NumCols | ( | ) | const [inline] |
Definition at line 53 of file TDbiTableMetaData.hxx.
References fNumCols.
Referenced by HasEpoch(), TDbiRowStream::NumCols(), and Sql().
00053 { return fNumCols;}
TDbiTableMetaData::ColumnAttributes & TDbiTableMetaData::SetAttributes | ( | Int_t | colNum | ) | [private] |
Return a setable column attributes (will be dummy entry if requesting invalid column).
Definition at line 149 of file TDbiTableMetaData.cxx.
References ExpandTo(), and GetAttributes().
Referenced by SetColFieldType(), SetColIsNullable(), and SetColName().
00149 { 00150 00151 // Return a column attributes (will be dummy entry if requesting invalid column) 00152 00153 this->ExpandTo(colNum); 00154 // Using const metho so must cast away constness. 00155 return const_cast<TDbiTableMetaData::ColumnAttributes&>(this->GetAttributes(colNum)); 00156 00157 }
void TDbiTableMetaData::SetColFieldType | ( | const TDbiFieldType & | type, | |
Int_t | colNum | |||
) | [protected] |
Definition at line 162 of file TDbiTableMetaData.cxx.
References TDbiTableMetaData::ColumnAttributes::Concept, TDbiFieldType::GetConcept(), TDbi::kChar, TDbi::kDate, TDbi::kString, TDbiTableMetaData::ColumnAttributes::MustDelimit, SetAttributes(), and TDbiTableMetaData::ColumnAttributes::Type.
Referenced by SetFromSql(), and TDbiDBProxy::StoreMetaData().
00163 { 00164 // 00165 // 00166 // Purpose: Define field type for specified column. 00167 // 00168 // Arguments: 00169 // fieldType in Column field type. 00170 // colNum in Column number (1,2...) 00171 // 00172 00173 ColumnAttributes& attrib(this->SetAttributes(colNum)); 00174 UInt_t concept = fieldType.GetConcept(); 00175 attrib.Type = fieldType; 00176 attrib.MustDelimit = concept == TDbi::kString || concept == TDbi::kDate || concept == TDbi::kChar; 00177 attrib.Concept = concept; 00178 00179 }
void TDbiTableMetaData::SetColIsNullable | ( | Int_t | colNum, | |
Bool_t | isNullable = true | |||
) | [inline, protected] |
Definition at line 74 of file TDbiTableMetaData.hxx.
References TDbiTableMetaData::ColumnAttributes::IsNullable, and SetAttributes().
Referenced by SetFromSql(), and TDbiDBProxy::StoreMetaData().
00074 { SetAttributes(colNum).IsNullable = isNullable;}
void TDbiTableMetaData::SetColName | ( | const string & | name, | |
Int_t | colNum | |||
) | [inline, protected] |
Definition at line 75 of file TDbiTableMetaData.hxx.
References TDbiTableMetaData::ColumnAttributes::Name, and SetAttributes().
Referenced by SetFromSql(), and TDbiDBProxy::StoreMetaData().
00075 { SetAttributes(colNum).Name = name;}
void TDbiTableMetaData::SetFromSql | ( | const string & | sql | ) |
Recreate from SQL used to create table.
Definition at line 183 of file TDbiTableMetaData.cxx.
References TDbiFieldType::AsString(), DbiDetector::AsString(), Clear(), ColFieldType(), ColName(), fTableName, GetToken(), SetColFieldType(), SetColIsNullable(), SetColName(), SK_DBI_Log, SK_DBI_Severe, and SK_DBI_Warn.
00183 { 00184 // 00185 // 00186 // Purpose: Reconstruct this object using SQL to create table. 00187 00188 00189 TString SqlUpper(sql); 00190 SqlUpper.ToUpper(); 00191 00192 const char* strPtr = SqlUpper.Data(); 00193 00194 string token1(TDbiTableMetaData::GetToken(strPtr)); 00195 string token2(TDbiTableMetaData::GetToken(strPtr)); 00196 string token3(TDbiTableMetaData::GetToken(strPtr)); 00197 string token4(TDbiTableMetaData::GetToken(strPtr)); 00198 00199 if ( token1 != "CREATE" || token2 != "TABLE" || token4 != "(" ) { 00200 SK_DBI_Severe( "Cannot recreate: SQL " << SqlUpper 00201 << " does not start CREATE TABLE ... (" << " "); 00202 return; 00203 } 00204 00205 this->Clear(); 00206 fTableName = token3; 00207 SK_DBI_Log( "Recreating TDbiTableMetaData for table " << fTableName << " "); 00208 00209 // Loop processing column specifications. 00210 Int_t col = 0; 00211 00212 string delim; 00213 while ( delim != ")" ) { 00214 string name = TDbiTableMetaData::GetToken(strPtr); 00215 00216 // Deal with INDEX and PRIMARY KEY 00217 if ( name == "INDEX" || name == "KEY" || name == "PRIMARY" ) { 00218 if ( name == "PRIMARY" || name == "KEY" )delim = TDbiTableMetaData::GetToken(strPtr); 00219 delim = TDbiTableMetaData::GetToken(strPtr); 00220 if ( delim == "(" ) while ( delim != ")" ) delim = TDbiTableMetaData::GetToken(strPtr); 00221 delim = TDbiTableMetaData::GetToken(strPtr); 00222 continue; 00223 } 00224 00225 // Collect name and type. 00226 ++col; 00227 this->SetColName(name,col); 00228 this->SetColIsNullable(col); 00229 00230 string type = TDbiTableMetaData::GetToken(strPtr); 00231 int precision = 0; 00232 delim = TDbiTableMetaData::GetToken(strPtr); 00233 if ( delim == "(" ) { 00234 delim = TDbiTableMetaData::GetToken(strPtr); 00235 istringstream is(delim); 00236 is >> precision; 00237 delim = TDbiTableMetaData::GetToken(strPtr); 00238 delim = TDbiTableMetaData::GetToken(strPtr); 00239 } 00240 TDbiFieldType ft(type,precision); 00241 this->SetColFieldType(ft,col); 00242 SK_DBI_Log( " Column: " << col << " name " << this->ColName(col) 00243 << " type " << this->ColFieldType(col).AsString() 00244 << " precision " << precision << " "); 00245 00246 // Collect optional qualifiers. 00247 00248 while ( delim != "," && delim != ")" ) { 00249 string opt2 = TDbiTableMetaData::GetToken(strPtr); 00250 if ( delim == "NOT" && opt2 == "NULL") { 00251 this->SetColIsNullable(col,false); 00252 delim = TDbiTableMetaData::GetToken(strPtr); 00253 } 00254 else if ( delim == "PRIMARY" && opt2 == "KEY") { 00255 delim = TDbiTableMetaData::GetToken(strPtr); 00256 } 00257 else if ( delim == "AUTO_INCREMENT") { 00258 delim = opt2; 00259 } 00260 else if ( delim == "UNSIGNED") { 00261 delim = opt2; 00262 } 00263 else { 00264 SK_DBI_Warn( "Column: " << col << " name " << name << " type " << ft.AsString() 00265 << " ignoring unknown option: " << delim << " "); 00266 delim = opt2; 00267 } 00268 } 00269 } 00270 00271 }
string TDbiTableMetaData::Sql | ( | ) | const |
Return SQL string to create table.
Definition at line 276 of file TDbiTableMetaData.cxx.
References TDbiFieldType::AsSQLString(), ColFieldType(), ColIsNullable(), ColName(), fTableName, TDbiString::GetString(), and NumCols().
Referenced by TDbiSqlValPacket::SetMetaData().
00276 { 00277 // 00278 // 00279 // Purpose: Return SQL string to create table. 00280 // 00281 // Return: SQL command to create required table. 00282 00283 Bool_t mainTable = fTableName.substr(fTableName.size()-3,3) != "VLD"; 00284 00285 string tableName = fTableName; 00286 TDbiString sql; 00287 sql.GetString() = ""; 00288 sql << "create table " << tableName << "("; 00289 00290 00291 // Assemble columns. 00292 00293 int numCols = this->NumCols(); 00294 for(int i=1; i<= numCols; i++) { 00295 00296 sql << this->ColName(i) << " " ; 00297 sql << this->ColFieldType(i).AsSQLString(); 00298 00299 if( this->ColName(i) == "SEQNO" 00300 && ! mainTable ) sql << " not null primary key" ; 00301 00302 else if( ! this->ColIsNullable(i) 00303 || this->ColName(i) == "SEQNO" 00304 || this->ColName(i) == "ROW_COUNTER" 00305 ) sql << " not null" ; 00306 00307 if (i < numCols) sql << ", "; 00308 00309 } 00310 00311 // Deal with key/index/constraint. 00312 00313 // Nothing to do for the special xxxSEQNO tables. 00314 if ( fTableName == "GLOBALSEQNO" || fTableName == "LOCALSEQNO" ) { 00315 sql << ")"; 00316 } 00317 00318 else { 00319 if ( mainTable ) { 00320 sql << ", primary key (SEQNO,ROW_COUNTER)"; 00321 } 00322 else { 00323 sql << ", key TIMESTART (TIMESTART), key TIMEEND (TIMEEND)"; 00324 } 00325 sql << ")"; 00326 } 00327 00328 return sql.GetString(); 00329 00330 }
string TDbiTableMetaData::TableName | ( | ) | const [inline] |
Definition at line 51 of file TDbiTableMetaData.hxx.
References fTableName.
Referenced by TDbiDBProxy::StoreMetaData(), TDbiRowStream::TableName(), and TDbiRowStream::TableNameTc().
00051 { return fTableName; }
friend class TDbiDBProxy [friend] |
Definition at line 38 of file TDbiTableMetaData.hxx.
std::vector<ColumnAttributes> TDbiTableMetaData::fColAttr [private] |
Column attributes indexed by column (starting from 0).
Definition at line 118 of file TDbiTableMetaData.hxx.
Referenced by Clear(), ExpandTo(), and GetAttributes().
ColumnAttributes TDbiTableMetaData::fgDummy [static, private] |
Dummy attributes (used when requesting invalid column).
Definition at line 106 of file TDbiTableMetaData.hxx.
Referenced by GetAttributes().
UInt_t TDbiTableMetaData::fNumCols [private] |
Number of columns.
Definition at line 121 of file TDbiTableMetaData.hxx.
Referenced by Clear(), ExpandTo(), GetAttributes(), and NumCols().
string TDbiTableMetaData::fTableName [private] |
Table name (either XXX or XXXVLD).
Definition at line 124 of file TDbiTableMetaData.hxx.
Referenced by SetFromSql(), Sql(), and TableName().