00001 // $Header: /bes/bes/BossCvs/Calibration/rdbModel/src/Tables/Column.cxx,v 1.1.1.1 2005/10/17 06:10:53 maqm Exp $ 00002 00003 #include "rdbModel/Tables/Column.h" 00004 #include "rdbModel/Tables/Datatype.h" 00005 #include "rdbModel/Tables/Table.h" 00006 #include "facilities/Timestamp.h" 00007 00008 #include <algorithm> 00009 namespace rdbModel { 00010 00011 Column::~Column() { 00012 delete m_type; 00013 } 00014 00015 Enum* Column::getEnum() const {return m_type->getEnum();} 00016 00017 const std::string& Column::getTableName() const { 00018 return m_myTable->getName(); 00019 } 00020 00021 bool Column::okValue(const std::string& val, bool set) const { 00022 // auto increment and datetime values are established by rdbms 00023 if (set) { 00024 00025 if ( (m_from == FROMautoIncrement) || 00026 (m_from == FROMnow)) return false; 00027 } 00028 00029 return m_type->okValue(val); 00030 } 00031 00032 bool Column::isCompatible(const Column* otherCol) const { 00033 return m_type->isCompatible(otherCol->m_type); 00034 } 00035 00036 00037 bool Column::isAutoIncrement() const { 00038 return (m_from == FROMautoIncrement); 00039 } 00040 00041 bool Column::interpret(const std::string& interpType, std::string& val) { 00042 // Currently only interpretation is for timestamp-like columns. 00043 // Value of interpType must be "time" and val must be "NOW". 00044 // In this case, substitute ascii current time 00045 if (interpType.compare(std::string("time")) != 0) return false; 00046 00047 Datatype::TYPES dtype = m_type->getType(); 00048 if ((dtype != Datatype::TYPEdatetime) && 00049 (dtype != Datatype::TYPEtimestamp)) { 00050 return false; 00051 } 00052 if (val.compare(std::string("NOW")) != 0) return false; 00053 00054 val = facilities::Timestamp().getString(); 00055 return true; 00056 } 00057 00058 Visitor::VisitorState Column::accept(Visitor* v) { 00059 00060 Visitor::VisitorState state = v->visitColumn(this); 00061 if (state == Visitor::VBRANCHDONE) return Visitor::VCONTINUE; 00062 return state; 00063 } 00064 00065 void Row::rowSort() { 00066 if (m_sorted) return; 00067 00068 FieldValCompare cmp; 00069 std::sort(m_fields.begin(), m_fields.end(), cmp); 00070 m_sorted = true; 00071 } 00072 00073 FieldVal* Row::find(std::string colname) { 00074 unsigned nField = m_fields.size(); 00075 unsigned minI = 0; 00076 unsigned maxI = nField; 00077 00078 unsigned guess = maxI/2; 00079 unsigned oldGuess = nField; 00080 00081 int cmp = colname.compare(m_fields[guess].m_colname); 00082 00083 while (cmp != 0) { 00084 if (guess == oldGuess) return 0; // not found 00085 00086 if (cmp < 0 ) { // thing we tried is > colName, so decrease maxI 00087 maxI = guess; 00088 } else { // thing we tried is > colName, so increase minI 00089 minI = guess; 00090 } 00091 oldGuess = guess; 00092 guess = (minI + maxI)/2; 00093 cmp = colname.compare(m_fields[guess].m_colname); 00094 } 00095 return &m_fields[guess]; 00096 } 00097 00098 void Row::regroup(std::vector<std::string>& colNames, 00099 std::vector<std::string>& colVals, 00100 std::vector<std::string>& nullCols) const { 00101 unsigned nFields = m_fields.size(); 00102 colNames.reserve(nFields); 00103 colVals.reserve(nFields); 00104 00105 for (unsigned i = 0; i < nFields; i++) { 00106 if (m_fields[i].m_null) { 00107 nullCols.push_back(m_fields[i].m_colname); 00108 } 00109 else { 00110 colNames.push_back(m_fields[i].m_colname); 00111 colVals.push_back(m_fields[i].m_val); 00112 } 00113 } 00114 } 00115 00116 }