00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 #include "DbObjects.h"
00091
00092
00093
00094
00095
00096
00097
00098
00099
00103 DOSnapshot::DOSnapshot(DOConnection* pConnection,
00104 const char* tableName)
00105 : DOQuery(pConnection)
00106 , m_pNewData(NULL)
00107 , m_LastInsertId(0)
00108 {
00109 ConstructorInclude();
00110
00111 CString q = "select * from ";
00112 q += tableName;
00113 Query (q);
00114 }
00115
00116
00120 DOSnapshot::~DOSnapshot()
00121 {
00122 DestructorInclude();
00123 if (m_pNewData) {
00124 unsigned int i, nfld = GetNumFields ();
00125 for (i = 0; i < nfld; i ++) {
00126 delete m_pNewData [i];
00127 m_pNewData [i] = NULL;
00128 }
00129 delete[] m_pNewData;
00130 }
00131
00132
00133 }
00134
00135
00142 void DOSnapshot::AddNew()
00143 {
00144 unsigned int i, nfld = GetNumFields ();
00145 if (!m_pNewData) {
00146 m_pNewData = new CString* [nfld];
00147 for (i = 0; i < nfld; i ++) {
00148 m_pNewData [i] = NULL;
00149 }
00150 } else {
00151 for (i = 0; i < nfld; i ++) {
00152 delete m_pNewData [i];
00153 m_pNewData [i] = NULL;
00154 }
00155 }
00156 }
00157
00158
00165 void DOSnapshot::CommitAdd(bool refresh, bool delayedAllowed)
00166 {
00167 CString query;
00168 if (refresh || !delayedAllowed) query = "insert into ";
00169 else query = "insert delayed into ";
00170 query += m_query.Mid (m_query.ReverseFind (' ') + 1);
00171 query += "(";
00172 bool first = true;
00173 DOQuery::QueryFieldIterator iFld(this);
00174 while (++iFld)
00175 {
00176 if (m_pNewData [iFld->GetNumber ()]) {
00177 if (!first) query += ", ";
00178 else first = false;
00179 query += iFld->GetName ();
00180 }
00181 }
00182 query += ") values (";
00183 iFld.Reset ();
00184 first = true;
00185 while (++iFld)
00186 {
00187 if (m_pNewData [iFld->GetNumber ()]) {
00188 if (!first) query += ", ";
00189 else first = false;
00190 if (!iFld->isNum ()) {
00191 query += "'";
00192 }
00193 query += *(m_pNewData [iFld->GetNumber ()]);
00194 if (!iFld->isNum ()) {
00195 query += "'";
00196 }
00197 }
00198 }
00199 query += ")";
00200 mysql_query (GetConnection()->GetHdl(), query);
00201 if (refresh || !delayedAllowed) {
00202 m_LastInsertId = (int) mysql_insert_id (GetConnection()->GetHdl());
00203 } else {
00204 m_LastInsertId = 0;
00205 }
00206 if (refresh) Query (m_query);
00207 }
00208
00209
00219 void DOSnapshot::CommitEdit(bool refresh)
00220 {
00221 CString query = "update low_priority ";
00222 query += m_query.Mid (m_query.ReverseFind (' ') + 1);
00223 query += " set ";
00224 CString where, sets;
00225 bool first = true;
00226 DOQuery::QueryFieldIterator iFld(this);
00227 while (++iFld)
00228 {
00229 if (m_pNewData [iFld->GetNumber ()] &&
00230 *(m_pNewData [iFld->GetNumber ()]) != iFld->GetValue ()) {
00231 if (!first) sets += ", ";
00232 else first = false;
00233 sets += iFld->GetName ();
00234 sets += '=';
00235 if (!iFld->isNum ()) {
00236 sets += "'";
00237 }
00238 sets += *(m_pNewData [iFld->GetNumber ()]);
00239 if (!iFld->isNum ()) {
00240 sets += "'";
00241 }
00242 } else if (!m_pNewData [iFld->GetNumber ()] && !iFld->isNull ()) {
00243 if (!first) sets += ", ";
00244 else first = false;
00245 sets += iFld->GetName ();
00246 sets += "=NULL";
00247 }
00248 if (iFld->isPriKey ()) {
00249 if (where.IsEmpty ()) {
00250 where = " where (";
00251 } else {
00252 where += " and (";
00253 }
00254 where += iFld->GetName ();
00255 where += '=';
00256 if (!iFld->isNum ()) {
00257 where += "'";
00258 }
00259 where += iFld->GetValue ();
00260 if (!iFld->isNum ()) {
00261 where += "'";
00262 }
00263 where += ")";
00264 }
00265 }
00266 if (!first && (!where.IsEmpty () || GetNumRows () == 1)) {
00267 mysql_query (GetConnection()->GetHdl(), query + sets + where);
00268 if (refresh) Query (m_query);
00269 }
00270 }
00271
00272
00279 void DOSnapshot::Edit()
00280 {
00281 unsigned int nfld = GetNumFields ();
00282 if (!m_pNewData) {
00283 m_pNewData = new CString* [nfld];
00284 unsigned int i;
00285 for (i = 0; i < nfld; i ++) {
00286 m_pNewData [i] = NULL;
00287 }
00288 }
00289 DOQuery::QueryFieldIterator iFld(this);
00290 while (++iFld)
00291 {
00292 if (iFld->isNull()) {
00293 delete m_pNewData [iFld->GetNumber ()];
00294 m_pNewData [iFld->GetNumber ()] = NULL;
00295 } else {
00296 if (!m_pNewData [iFld->GetNumber ()]) {
00297 m_pNewData [iFld->GetNumber ()] = new CString;
00298 }
00299 *(m_pNewData [iFld->GetNumber ()]) = iFld->GetValue ();
00300 }
00301 }
00302 }
00303
00304
00308 void DOSnapshot::Let(const char* fldName, const char* value)
00309 {
00310 Let (FindQueryField(fldName)->GetNumber (), value);
00311 }
00312
00313
00317 void DOSnapshot::Let(const unsigned int fldNum, const char* value)
00318 {
00319 if (!m_pNewData) return;
00320 if (fldNum >= GetNumFields ()) return;
00321 if (value && !m_pNewData [fldNum]) m_pNewData [fldNum] = new CString;
00322 if (value) {
00323 *(m_pNewData [fldNum]) = value;
00324 } else {
00325 delete m_pNewData [fldNum];
00326 m_pNewData [fldNum] = NULL;
00327 }
00328 }
00329
00330
00334 void DOSnapshot::Let(const char* fldName, int value)
00335 {
00336 CString num;
00337 num.Format ("%d", value);
00338 Let (fldName, num);
00339 }
00340
00341
00345 void DOSnapshot::Let(const unsigned int fldNum, int value)
00346 {
00347 CString num;
00348 num.Format ("%d", value);
00349 Let (fldNum, num);
00350 }
00351
00352
00353
00354
00358 void DOSnapshot::ConstructorInclude()
00359 {
00360 }
00361
00362
00366 void DOSnapshot::DestructorInclude()
00367 {
00368 }
00369
00370
00371
00372
00373
00374
00375
00376