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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
|
/*********************************************************************************
NixNote - An open-source client for the Evernote service.
Copyright (C) 2013 Randy Baumgarte
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***********************************************************************************/
#include "linkednotebooktable.h"
#include "sql/notetable.h"
#include "sql/configstore.h"
#include "sql/notebooktable.h"
#include "sql/sharednotebooktable.h"
#include "sql/nsqlquery.h"
#include "global.h"
// Generic constructor
LinkedNotebookTable::LinkedNotebookTable(DatabaseConnection *db)
{
this->db = db;
}
// Given a notebook's name as a std::string, we return the lid
qint32 LinkedNotebookTable::findByName(string &name) {
NotebookTable ntable(db);
return ntable.findByName(name);
}
// Given a notebook's name as a QString, we return the lid
qint32 LinkedNotebookTable::findByName(QString &name) {
string n = name.toStdString();
return findByName(n);
}
// Synchronize a new notebook with what is in the database. We basically
// just delete the old one & give it a new entry
qint32 LinkedNotebookTable::sync(LinkedNotebook ¬ebook) {
return sync(0, notebook);
}
// Synchronize a new notebook with what is in the database. We basically
// just delete the old one & give it a new entry
qint32 LinkedNotebookTable::sync(qint32 lid, LinkedNotebook ¬ebook) {
qint32 lastUSN = 0;
NotebookTable ntable(db);
SharedNotebookTable stable(db);
if (lid == 0 && notebook.shareKey.isSet()) {
lid = stable.findByShareKey(notebook.shareKey);
}
if (lid == 0 && notebook.uri.isSet()) {
lid = ntable.findByUri(notebook.uri);
}
if (lid > 0) {
lastUSN = getLastUpdateSequenceNumber(lid);
// Delete the old record
NSqlQuery query(db);
db->lockForWrite();
query.prepare("Delete from DataStore where lid=:lid and key>=3200 and key<3300");
query.bindValue(":lid", lid);
query.exec();
query.finish();
db->unlock();
SharedNotebookTable stable(db);
stable.expunge(lid);
}
if (lid == 0) {
lid = ntable.getLid(notebook.guid);
if (lid == 0) {
ConfigStore cs(db);
lid = cs.incrementLidCounter();
NotebookTable ntable(db);
// Build the dummy notebook entry
Notebook book;
book.guid = notebook.guid;
book.name = notebook.shareName;
book.updateSequenceNum = notebook.updateSequenceNum;
book.published = true;
Publishing publishing;
publishing.uri = notebook.uri;
book.publishing = publishing;
if (notebook.stack.isSet()) {
book.stack = notebook.stack;
}
ntable.sync(lid, book);
}
}
add(lid, notebook, false);
if (lastUSN > 0) {
setLastUpdateSequenceNumber(lid, lastUSN);
}
return lid;
}
// Given a notebook's GUID, we return the LID
qint32 LinkedNotebookTable::getLid(QString guid) {
NSqlQuery query(db);
db->lockForRead();
query.prepare("Select lid from DataStore where key=:key and data=:data");
query.bindValue(":data", guid);
query.bindValue(":key", LINKEDNOTEBOOK_GUID);
query.exec();
qint32 retval = 0;
if (query.next())
retval = query.value(0).toInt();
query.finish();
db->unlock();
return retval;
}
// Given a notebook's GUID, we return the LID
qint32 LinkedNotebookTable::getLid(string guid) {
QString s(QString::fromStdString(guid));
return getLid(s);
}
// Add a new notebook to the database
qint32 LinkedNotebookTable::add(qint32 l, LinkedNotebook &t, bool isDirty) {
ConfigStore cs(db);
qint32 lid = l;
if (lid == 0) {
lid = cs.incrementLidCounter();
}
NSqlQuery query(db);
NSqlQuery query2(db);
db->lockForWrite();
query.prepare("Insert into DataStore (lid, key, data) values (:lid, :key, :data)");
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_GUID);
QString linkedguid = "";
if (t.guid.isSet())
linkedguid = t.guid;
query.bindValue(":data", linkedguid);
query.exec();
if (t.username.isSet()) {
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_USERNAME);
QString username = t.username;
query.bindValue(":data",username);
query.exec();
}
if (t.shardId.isSet()) {
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_SHARD_ID);
QString shardid = t.shardId;
query.bindValue(":data",shardid);
query.exec();
}
if (t.shareKey.isSet()) {
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_SHARE_KEY);
QString sharekey = t.shareKey;
query.bindValue(":data", sharekey);
query.exec();
}
if (t.uri.isSet()) {
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_URI);
QString uri = t.uri;
query.bindValue(":data", uri);
query.exec();
}
if (t.updateSequenceNum.isSet()) {
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_UPDATE_SEQUENCE_NUMBER);
qint32 usn = t.updateSequenceNum;
query.bindValue(":data", usn);
query.exec();
}
if (t.noteStoreUrl.isSet()) {
QString url = t.noteStoreUrl;
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_NOTE_STORE_URL);
query.bindValue(":data", url);
query.exec();
}
if (t.webApiUrlPrefix.isSet()) {
query.bindValue(":lid", lid);
QString api = t.webApiUrlPrefix;
query.bindValue(":key", LINKEDNOTEBOOK_WEB_API_URL_PREFIX);
query.bindValue(":data", api);
query.exec();
}
if (t.stack.isSet()) {
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_STACK);
QString stack = t.stack;
query.bindValue(":data", stack);
query.exec();
}
if (t.businessId.isSet()) {
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_BUSINESS_ID);
qint32 businessid = t.businessId;
query.bindValue(":data", businessid);
query.exec();
}
if (t.shareName.isSet()) {
query.bindValue(":lid", lid);
QString sharename = t.shareName;
query.bindValue(":key", LINKEDNOTEBOOK_SHARE_NAME);
query.bindValue(":data", sharename);
query.exec();
query2.prepare("Update datastore set data=:name where key=:key and lid=:lid");
query2.bindValue(":name", sharename);
query2.bindValue(":key", NOTEBOOK_NAME);
query2.bindValue(":lid", lid);
query2.exec();
query2.prepare("Update notetable set notebook=:name where lid=:lid");
query2.bindValue(":name", sharename);
query2.bindValue(":lid", lid);
query2.exec();
query2.finish();
}
if (isDirty) {
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_IS_DIRTY);
query.bindValue(":data", true);
query.exec();
}
query.finish();
db->unlock();
return lid;
}
// Return a notebook structure given the LID
bool LinkedNotebookTable::get(LinkedNotebook ¬ebook, qint32 lid) {
NSqlQuery query(db);
db->lockForRead();
query.prepare("Select key, data from DataStore where lid=:lid");
query.bindValue(":lid", lid);
query.exec();
if (query.size() == 0)
return false;
while (query.next()) {
qint32 key = query.value(0).toInt();
switch (key) {
case (LINKEDNOTEBOOK_GUID):
notebook.guid = query.value(1).toString();
break;
case (LINKEDNOTEBOOK_UPDATE_SEQUENCE_NUMBER):
notebook.updateSequenceNum = query.value(1).toInt();
break;
case (LINKEDNOTEBOOK_SHARE_NAME):
notebook.shareName = query.value(1).toString();
break;
case (LINKEDNOTEBOOK_SHARE_KEY):
notebook.shareKey = query.value(1).toString();
break;
case (LINKEDNOTEBOOK_USERNAME):
notebook.username = query.value(1).toString();
break;
case (LINKEDNOTEBOOK_SHARD_ID):
notebook.shardId = query.value(1).toString();
break;
case (LINKEDNOTEBOOK_URI):
notebook.uri = query.value(1).toString();
break;
case (LINKEDNOTEBOOK_NOTE_STORE_URL):
notebook.noteStoreUrl = query.value(1).toString();
break;
case (LINKEDNOTEBOOK_WEB_API_URL_PREFIX):
notebook.webApiUrlPrefix = query.value(1).toString();
break;
case (LINKEDNOTEBOOK_STACK):
notebook.stack = query.value(1).toString();
break;
case (LINKEDNOTEBOOK_BUSINESS_ID):
notebook.businessId = query.value(1).toInt();
break;
}
}
query.finish();
db->unlock();
return true;
}
// Return a notebook given the GUID
bool LinkedNotebookTable::get(LinkedNotebook ¬ebook, QString guid) {
qint32 lid = getLid(guid);
return get(notebook, lid);
}
// Return a notebook given the GUID as a std::string
bool LinkedNotebookTable::get(LinkedNotebook ¬ebook, string guid) {
qint32 lid = getLid(guid);
return get(notebook, lid);
}
// Get a list of all notebooks
qint32 LinkedNotebookTable::getAll(QList<qint32> &books) {
NSqlQuery query(db);
db->lockForRead();
query.prepare("select distinct lid from DataStore where key=:key");
query.bindValue(":key", LINKEDNOTEBOOK_GUID);
query.exec();
while (query.next()) {
books.append(query.value(0).toInt());
}
query.finish();
db->unlock();
return books.size();
}
// Get all notebooks for a particular stack
qint32 LinkedNotebookTable::getStack(QList<qint32> &retval, QString &stack){
NSqlQuery query(db);
db->lockForRead();
query.prepare("select distinct lid from DataStore where key=:key and data=:stack");
query.bindValue(":key", LINKEDNOTEBOOK_STACK);
query.bindValue(":stack", stack);
query.exec();
while (query.next()) {
retval.append(query.value(0).toInt());
}
query.finish();
db->unlock();
return retval.size();
}
// Get the guid for a particular lid
bool LinkedNotebookTable::getGuid(QString &retval, qint32 lid){
NSqlQuery query(db);
db->lockForRead();
query.prepare("select data from DataStore where key=:key and lid=:lid");
query.bindValue(":key", LINKEDNOTEBOOK_GUID);
query.bindValue(":lid", lid);
query.exec();
while (query.next()) {
retval = query.value(0).toString();
query.finish();
db->unlock();
return true;
}
query.finish();
db->unlock();
return false;
}
bool LinkedNotebookTable::findGuidByName(QString &retval, QString &guid) {
NotebookTable ntable(db);
return ntable.findGuidByName(retval, guid);
}
bool LinkedNotebookTable::update(LinkedNotebook ¬ebook, bool isDirty) {
LinkedNotebook oldBook;
qint32 lid = getLid(notebook.guid);
get(oldBook, lid);
if (lid <= 0)
return false;
expunge(lid);
add(lid, notebook, isDirty);
// Rename anything in the note list
QString oldname = "";
QString newname = "";
if (notebook.shareName.isSet())
newname = notebook.shareName;
if (oldBook.shareName.isSet())
oldname = oldBook.shareName;
if (oldname != newname) {
NSqlQuery query(db);
db->lockForWrite();
query.prepare("Update notetable set notebook=:name where notebooklid=:lid");
query.bindValue(":name", newname);
query.bindValue(":lid", lid);
query.exec();
query.finish();
db->unlock();
}
return true;
}
void LinkedNotebookTable::expunge(qint32 lid) {
NSqlQuery query(db);
db->lockForWrite();
query.prepare("delete from DataStore where lid=:lid");
query.bindValue(":lid", lid);
query.exec();
query.finish();
db->unlock();
}
void LinkedNotebookTable::expunge(string guid) {
int lid = this->getLid(guid);
this->expunge(lid);
}
void LinkedNotebookTable::expunge(QString guid) {
int lid = this->getLid(guid);
NoteTable ntable(db);
// Remove any notes
QList<qint32> notes;
ntable.findNotesByNotebook(notes, lid);
for (int i=0; i<notes.size(); i++) {
ntable.expunge(notes[i]);
}
// Expunge the actual notebook
this->expunge(lid);
}
void LinkedNotebookTable::renameStack(QString oldName, QString newName) {
QList<qint32> lids;
findByStack(lids, oldName);
NSqlQuery query(db);
db->lockForWrite();
query.prepare("Update Datastore set data=:newname where key=:key and data=:oldname");
query.bindValue(":newname", newName);
query.bindValue(":key", LINKEDNOTEBOOK_STACK);
query.bindValue(":oldname", oldName);
query.exec();
query.finish();
db->unlock();
}
void LinkedNotebookTable::findByStack(QList<qint32> &lids, QString stackName) {
NSqlQuery query(db);
db->lockForRead();
query.prepare("Select lid from DataStore where key=:key and data=:name");
query.bindValue(":key", LINKEDNOTEBOOK_STACK);
query.bindValue(":name", stackName);
query.exec();
while(query.next()) {
lids.append(query.value(0).toInt());
}
query.finish();
db->unlock();
}
bool LinkedNotebookTable::isDeleted(qint32 lid) {
NotebookTable ntable(db);
return ntable.isDeleted(lid);
}
void LinkedNotebookTable::getStacks(QStringList &stacks) {
NSqlQuery query(db);
db->lockForRead();
query.prepare("Select distinct data from DataStore where key=:key");
query.bindValue(":key", LINKEDNOTEBOOK_STACK);
query.exec();
while (query.next()) {
stacks.append(query.value(0).toString());
}
query.finish();
db->unlock();
}
bool LinkedNotebookTable::isStacked(qint32 lid) {
NSqlQuery query(db);
db->lockForRead();
query.prepare("Select data from DataStore where lid=:lid and key=:key");
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_STACK);
query.exec();
bool retval = false;
if (query.next())
retval = true;
query.finish();
db->unlock();
return retval;
}
void LinkedNotebookTable::removeFromStack(qint32 lid) {
NSqlQuery query(db);
db->lockForWrite();
query.prepare("delete from DataStore where lid=:lid and key=:key");
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_STACK);
query.exec();
query.finish();
db->unlock();
}
qint32 LinkedNotebookTable::getLastUpdateSequenceNumber(qint32 lid) {
NSqlQuery query(db);
db->lockForRead();
query.prepare("select data from datastore where lid=:lid and key=:key");
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_LAST_USN);
query.exec();
qint32 retval = 0;
if (query.next()) {
retval = query.value(0).toInt();
}
query.finish();
db->unlock();
return retval;
}
void LinkedNotebookTable::setLastUpdateSequenceNumber(qint32 lid, qint32 lastUSN) {
NSqlQuery query(db);
db->lockForWrite();
query.prepare("delete from datastore where lid=:lid and key=:key");
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_LAST_USN);
query.exec();
query.prepare("insert into datastore (lid, key, data) values (:lid, :key, :data)");
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_LAST_USN);
query.bindValue(":data", lastUSN);
query.exec();
query.finish();
db->unlock();
}
bool LinkedNotebookTable::exists(qint32 lid) {
NSqlQuery query(db);
db->lockForRead();
query.prepare("select lid from datastore where lid=:lid and key=:key");
query.bindValue(":lid", lid);
query.bindValue(":key", LINKEDNOTEBOOK_SHARE_NAME);
query.exec();
bool retval = false;
if (query.next())
retval = true;
query.finish();
db->unlock();
return retval;
}
|