00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/data/RecordSet.h>
00018 #include <spl/data/ColumnTypes.h>
00019
00020 IColumn::IColumn(const String& name, const int maxlen)
00021 : m_name(name), m_maxlen(maxlen), m_currow(-1)
00022 {
00023 }
00024
00025 IColumn::~IColumn()
00026 {
00027 }
00028
00029 #if defined(DEBUG)
00030 void IColumn::CheckMem() const
00031 {
00032 m_name.CheckMem();
00033 }
00034
00035 void IColumn::ValidateMem() const
00036 {
00037 m_name.ValidateMem();
00038 }
00039 #endif
00040
00041 RecordSet::RecordSet()
00042 : m_columnIdx(), m_columns()
00043 {
00044 }
00045
00046 RecordSet::RecordSet(const RecordSet& rs)
00047 : m_columnIdx(), m_columns()
00048 {
00049 *this = rs;
00050 }
00051
00052 RecordSet::~RecordSet()
00053 {
00054 Clear();
00055 }
00056
00057 RecordSet& RecordSet::operator =(const RecordSet& rs)
00058 {
00059 int count = rs.m_columns.Count();
00060 for ( int x = 0; x < count; x++ )
00061 {
00062 IColumn *col = rs.m_columns.ElementAt(x)->Clone();
00063 m_columns.Add(col);
00064 m_columnIdx.Set(col->Name(), col);
00065 }
00066
00067 return *this;
00068 }
00069
00070 void RecordSet::Clear()
00071 {
00072 m_columnIdx.Clear();
00073
00074 int count = m_columns.Count();
00075 for ( int x = 0; x < count; x++ )
00076 {
00077 delete m_columns.ElementAt(x);
00078 }
00079 m_columns.Clear();
00080 }
00081
00082 void RecordSet::BeginIter()
00083 {
00084 int count = m_columns.Count();
00085 #ifdef DEBUG
00086 int rowcount = (count > 0) ? m_columns.ElementAt(0)->Count() : 0;
00087 #endif
00088 for ( int x = 0; x < count; x++ )
00089 {
00090 m_columns.ElementAt(x)->BeginInter();
00091 ASSERT(rowcount == m_columns.ElementAt(x)->Count());
00092 }
00093 }
00094
00095 bool RecordSet::Next()
00096 {
00097 int count = m_columns.Count();
00098 for ( int x = 0; x < count; x++ )
00099 {
00100 if ( ! m_columns.ElementAt(x)->Next() )
00101 {
00102 return false;
00103 }
00104 }
00105 return true;
00106 }
00107
00108 void RecordSet::SetCurrentRow(const int row)
00109 {
00110 int count = m_columns.Count();
00111 for ( int x = 0; x < count; x++ )
00112 {
00113 m_columns.ElementAt(x)->SeekRow(row);
00114 }
00115 }
00116
00117 void RecordSet::SetColumns( RecordSet& rs )
00118 {
00119 ASSERT( rs.ColumnCount() == 0 );
00120 int count = m_columns.Count();
00121 for ( int x = 0; x < count; x++ )
00122 {
00123 IColumn *col = m_columns.ElementAt(x);
00124 rs.DefineColumn( col->Name().GetChars(), col->Type(), col->MaxLength() );
00125 }
00126 }
00127
00128 void RecordSet::DefineColumn(const String& name, int type, int fieldmaxlen)
00129 {
00130 IColumn *col;
00131 switch ( type )
00132 {
00133 case DbSqlType::SQL_TYPE_INT8:
00134 col = new Int8Column(name);
00135 break;
00136 case DbSqlType::SQL_TYPE_INT16:
00137 col = new Int16Column(name);
00138 break;
00139 case DbSqlType::SQL_TYPE_INT32:
00140 col = new Int32Column(name);
00141 break;
00142 case DbSqlType::SQL_TYPE_INT64:
00143 col = new Int64Column(name);
00144 break;
00145 case DbSqlType::SQL_TYPE_DECIMAL:
00146 col = new DecimalColumn(name, fieldmaxlen);
00147 break;
00148 case DbSqlType::SQL_TYPE_FLOAT32:
00149 col = new Float32Column(name);
00150 break;
00151 case DbSqlType::SQL_TYPE_FLOAT64:
00152 col = new Float64Column(name);
00153 break;
00154 case DbSqlType::SQL_TYPE_FLAG:
00155 col = new BitColumn(name);
00156 break;
00157 case DbSqlType::SQL_TYPE_TIMESTAMP:
00158 col = new TimeStampColumn(name);
00159 break;
00160 case DbSqlType::SQL_TYPE_DATE:
00161 col = new DateColumn(name);
00162 break;
00163 case DbSqlType::SQL_TYPE_DATETIME:
00164 col = new DateTimeColumn(name);
00165 break;
00166 case DbSqlType::SQL_TYPE_CHAR:
00167 col = new CharColumn(name, fieldmaxlen);
00168 break;
00169 case DbSqlType::SQL_TYPE_VARCHAR:
00170 col = new VarCharColumn(name, fieldmaxlen);
00171 break;
00172 case DbSqlType::SQL_TYPE_BLOB:
00173 throw new NotImplementedException();
00174 break;
00175 default:
00176 throw new InvalidArgumentException("Unknown SQL type");
00177 }
00178 m_columns.Add(col);
00179 m_columnIdx.Set(name, col);
00180 }
00181
00182 #if defined(DEBUG)
00183 void RecordSet::CheckMem() const
00184 {
00185 m_columnIdx.CheckMem();
00186 m_columns.CheckMem();
00187
00188 int count = m_columns.Count();
00189 for ( int x = 0; x < count; x++ )
00190 {
00191 IColumn *col = m_columns.ElementAt(x);
00192 DEBUG_NOTE_MEM( col );
00193 col->CheckMem();
00194 }
00195 }
00196
00197 void RecordSet::ValidateMem() const
00198 {
00199 m_columnIdx.ValidateMem();
00200 m_columns.ValidateMem();
00201
00202 int count = m_columns.Count();
00203 for ( int x = 0; x < count; x++ )
00204 {
00205 IColumn *col = m_columns.ElementAt(x);
00206 ASSERT_PTR( col );
00207 col->ValidateMem();
00208 }
00209 }
00210 #endif