1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
|
/* Copyright (C) 2005 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
// NOTE: shared with OSX
#include "MYXResultSet.h"
class MYXResultSet::RSChange
{
public:
MYX_RS_ACTION *action;
unsigned int column;
bool has_error;
RSChange(const MYX_RS_ACTION &act, int col)
: column(col)
{
action= myx_query_create_action(act.action, act.row, act.column,
act.new_value_length, act.new_value);
has_error= false;
}
RSChange(MYX_RS_ACTION *act, int col)
: action(act), column(col)
{
has_error= false;
}
~RSChange()
{
if (action)
{
g_free(action->new_value);
action->new_value= NULL;
myx_query_free_action(action);
}
}
void add_to_resultset(MYX_RESULTSET *rs)
{
myx_query_add_action(rs, action);
g_free(action);
action= NULL;
}
inline MYX_RS_ACTION_TYPE get_type() { return action->action; };
inline size_t get_size() { return action->new_value_length; };
inline char *get_value() { return action->new_value; };
inline unsigned int get_row() { return action->row; };
};
MYXResultSet::MYXResultSet(MYX_RESULTSET *rs, const MYXResultSetCallbacks &cbacks)
: _resultset(rs), _callbacks(cbacks), _new_rows(0), _placeholder(false)
{
}
MYXResultSet::~MYXResultSet()
{
for (std::list<RSChange*>::iterator it= _changes.begin(); it != _changes.end(); ++it)
delete *it;
}
unsigned int MYXResultSet::add_row()
{
unsigned int row= get_row_count();
_new_rows++;
if (_callbacks.row_added)
(*_callbacks.row_added)(this, row);
return row;
}
void MYXResultSet::delete_row(unsigned int row)
{
MYX_RS_ACTION *action;
if (row < _resultset->rows_num + _new_rows - 1)
{
action= myx_query_create_action(MYX_RSA_DELETE, row, NULL, 0, NULL);
RSChange *change= new RSChange(action, 0);
_changes.push_front(change);
}
}
bool MYXResultSet::row_has_changes(unsigned int row)
{
for (std::list<RSChange*>::const_iterator it= _changes.begin();
it != _changes.end(); ++it)
{
if ((*it)->get_row() == row)
return true;
}
return false;
}
MYX_RS_COLUMN_TYPE MYXResultSet::get_column_type(unsigned int column)
{
if (_resultset->columns_num <= column)
return _resultset->columns[column].column_type;
else
return MYX_RSCT_INTEGER;
}
void MYXResultSet::set_placeholder_enabled(bool flag)
{
if (_placeholder != flag)
{
_placeholder= flag;
if (_placeholder)
add_row();
else
{
int i= _resultset->rows_num + _new_rows - 1;
while (i >= (int)_resultset->rows_num)
{
if (row_has_changes(i))
{
break;
}
if (_callbacks.row_deleted)
(*_callbacks.row_deleted)(this, i);
_new_rows--;
i--;
}
}
}
}
MYXResultSet::RSChange* MYXResultSet::find_change(unsigned int row, unsigned int column) const
{
for (std::list<RSChange*>::const_iterator it= _changes.begin();
it != _changes.end(); ++it)
{
if ((*it)->get_row() == row && ((*it)->column == column ||
(*it)->get_type() == MYX_RSA_DELETE))
return *it;
}
return 0;
}
MYXRSEditStatus MYXResultSet::get_edit_status(unsigned int row, unsigned int column) const
{
if (row >= _resultset->rows_num)
{
if (row == _resultset->rows_num + _new_rows - 1 && _placeholder)
return MESPlaceHolder;
else
{
RSChange *change= find_change(row, 0);
if (change && change->get_type() == MYX_RSA_DELETE)
return MESDeleted;
else
return MESAdded;
}
}
else if (!_resultset->rows[row].fields)
return MESAdded;
else
{
RSChange *change= find_change(row, column);
if (change && change->get_type() == MYX_RSA_DELETE)
return MESDeleted;
else if (change)
return MESChanged;
else
return MESUnchanged;
}
}
MYXRSCompareStatus MYXResultSet::get_compare_status(unsigned int row, unsigned int column)
{
switch (_resultset->rows[row].diff&MYX_RD_MASK)
{
case MYX_RD_OTHER_ONLY:
return MCSOnlyInOther;
case MYX_RD_THIS_ONLY:
return MCSOnlyInThis;
case MYX_RD_DIFFERS:
// check column specific differences
if ((_resultset->rows[row].diff>>4) & (1<<column))
return MCSDiffers;
default:
return MCSMatch;
}
}
bool MYXResultSet::isNull(unsigned int row, unsigned int column)
{
char *dummy= NULL;
size_t dummys;
if (!get(row, column, dummy, dummys))
return false;
return dummy == NULL;
}
bool MYXResultSet::get(unsigned int row, unsigned int column,
char *&value, size_t &size) const
{
RSChange *change= find_change(row, column);
if (change)
{
if (change->get_type() == MYX_RSA_DELETE)
{
if (_resultset->rows[row].fields && row < _resultset->rows_num)
{
size= _resultset->rows[row].fields[column].value_length;
value= _resultset->rows[row].fields[column].value;
}
else
{
size= change->get_size();
value= change->get_value();
}
}
else
{
size= change->get_size();
value= change->get_value();
}
}
else
{
if (_resultset->rows && _resultset->rows[row].fields && row < _resultset->rows_num)
{
size= _resultset->rows[row].fields[column].value_length;
value= _resultset->rows[row].fields[column].value;
}
else
{
size= 0;
value= NULL;
return false;
}
}
return true;
}
void MYXResultSet::set(unsigned int row, unsigned int column,
const char *value, size_t size)
{
RSChange *change= find_change(row, column);
// check if same value as last change (or if it's deleted)
if (change && (change->get_type() == MYX_RSA_DELETE ||
(change->get_size() == size &&
memcmp(change->get_value(), value, size)==0)))
return;
// check if same value as in resultset
if (row < _resultset->rows_num)
{
if (_resultset->rows[row].fields == NULL)
return;
// if (_resultset->rows[row].fields[column].value_length == size
// && memcmp(_resultset->rows[row].fields[column].value, value, size)==0)
// return;
}
if (row == _resultset->rows_num + _new_rows - 1 && _placeholder)
add_row();
MYX_RS_ACTION *action= myx_query_create_action(row >= _resultset->rows_num ? MYX_RSA_ADD : MYX_RSA_UPDATE,
row, _resultset->columns+column,
size, value);
_changes.push_front(new RSChange(action, column));
}
bool MYXResultSet::get_row_editable(unsigned int row) const
{
RSChange *change= find_change(row, 0);
if (change && change->get_type() == MYX_RSA_DELETE)
return false;
return true;
}
unsigned int MYXResultSet::get_row_count() const
{
return _resultset->rows_num + _new_rows;
}
void MYXResultSet::pre_commit()
{
// go through all actions requested and leave only those that
// should actually be executed
std::list<RSChange*>::iterator jter, iter= _changes.begin();
while (iter != _changes.end())
{
RSChange *change= *iter;
_changes.erase(iter);
// locate all previous changes to the same cell and remove them
jter= _changes.begin();
while (jter != _changes.end())
{
std::list<RSChange*>::iterator jnext= jter;
RSChange *ch= *jter;
++jnext;
if (ch->get_row() == change->get_row() &&
(change->get_type() == MYX_RSA_DELETE ||
change->column == ch->column))
{
_changes.erase(jter);
}
jter= jnext;
}
// do not add DELETEs for rows that were newly added
if ((change->get_row() < _resultset->rows_num && _resultset->rows[change->get_row()].fields!=NULL) ||
change->get_type() != MYX_RSA_DELETE)
change->add_to_resultset(_resultset);
else
{
// go through all actions and decrement the row index of
// the new items that come after this one
jter= _changes.begin();
while (++jter != _changes.end())
{
RSChange *ch= *jter;
if (ch->action->row >= _resultset->rows_num
&& ch->action->row > change->action->row)
--ch->action->row;
}
}
iter= _changes.begin();
}
}
void MYXResultSet::pack()
{
if (_callbacks.row_deleted)
{
for (unsigned int i= 0; i < _resultset->rows_num; i++)
{
if (!_resultset->rows[i].fields)
{
(*_callbacks.row_deleted)(this, _resultset->rows_num-i-1);
}
}
}
myx_query_pack_resultset(_resultset);
}
void MYXResultSet::post_commit(bool failed)
{
_new_rows= 0;
if (_resultset->actions)
{
myx_query_update_resultset(_resultset);
if (_resultset->actions)
{
if (failed)
{
// add back the actions that failed
unsigned int i;
for (i= 0; i < _resultset->actions->actions_num; i++)
{
if (_resultset->actions->actions[i].status == MYX_RSAS_NEW
|| _resultset->actions->actions[i].status == MYX_RSAS_FAILED)
{
if (_resultset->actions->actions[i].row >= _resultset->rows_num)
{
if (_new_rows < _resultset->actions->actions[i].row - (_resultset->rows_num-1))
{
g_message("recycle row %i", _resultset->actions->actions[i].row - (_resultset->rows_num-1));
_new_rows= _resultset->actions->actions[i].row - (_resultset->rows_num-1);
}
}
_changes.push_front(new RSChange(_resultset->actions->actions[i], _resultset->actions->actions[i].column-_resultset->columns));
}
}
g_free(_resultset->actions->actions);
g_free(_resultset->actions);
_resultset->actions= NULL;
}
}
}
if (!failed)
{
myx_query_discard_actions(_resultset);
pack();
}
}
void MYXResultSet::discard_changes()
{
_new_rows= 0;
for (std::list<RSChange*>::iterator it= _changes.begin(); it != _changes.end(); ++it)
delete *it;
_changes.clear();
if (_resultset->actions)
myx_query_discard_actions(_resultset);
myx_query_pack_resultset(_resultset);
}
bool MYXResultSet::has_changes()
{
if (_changes.begin() == _changes.end())
return false;
return true;
}
|