diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-10-11 13:58:23 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-10-11 13:58:23 +0000 |
commit | a2b6f08c9396ecf3eb24d0d8d3a9ab9d5eca12b0 (patch) | |
tree | d690569acfa8bec45996bebbf12fc19e31034dc4 /src/psycstore | |
parent | 432aeb0adc4e681a0fc8d5865b6ce4742638784d (diff) | |
download | gnunet-a2b6f08c9396ecf3eb24d0d8d3a9ab9d5eca12b0.tar.gz gnunet-a2b6f08c9396ecf3eb24d0d8d3a9ab9d5eca12b0.zip |
check rvalue for mysql_prepare
Diffstat (limited to 'src/psycstore')
-rw-r--r-- | src/psycstore/plugin_psycstore_mysql.c | 398 |
1 files changed, 188 insertions, 210 deletions
diff --git a/src/psycstore/plugin_psycstore_mysql.c b/src/psycstore/plugin_psycstore_mysql.c index 73f55186b..9af0e4687 100644 --- a/src/psycstore/plugin_psycstore_mysql.c +++ b/src/psycstore/plugin_psycstore_mysql.c | |||
@@ -240,7 +240,7 @@ mysql_trace (void *cls, const char *sql) | |||
240 | * @param dbh handle to the database | 240 | * @param dbh handle to the database |
241 | * @param sql SQL statement, UTF-8 encoded | 241 | * @param sql SQL statement, UTF-8 encoded |
242 | * @param stmt set to the prepared statement | 242 | * @param stmt set to the prepared statement |
243 | * @return 0 on success | 243 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure |
244 | */ | 244 | */ |
245 | static int | 245 | static int |
246 | mysql_prepare (struct GNUNET_MYSQL_Context *mc, | 246 | mysql_prepare (struct GNUNET_MYSQL_Context *mc, |
@@ -250,14 +250,19 @@ mysql_prepare (struct GNUNET_MYSQL_Context *mc, | |||
250 | *stmt = GNUNET_MYSQL_statement_prepare (mc, | 250 | *stmt = GNUNET_MYSQL_statement_prepare (mc, |
251 | sql); | 251 | sql); |
252 | 252 | ||
253 | LOG(GNUNET_ERROR_TYPE_DEBUG, | 253 | if (NULL == *stmt) |
254 | "Prepared `%s' / %p\n", sql, stmt); | 254 | { |
255 | if(NULL == *stmt) | 255 | LOG (GNUNET_ERROR_TYPE_ERROR, |
256 | LOG(GNUNET_ERROR_TYPE_ERROR, | 256 | _("Error preparing SQL query: %s\n %s\n"), |
257 | _("Error preparing SQL query: %s\n %s\n"), | 257 | mysql_stmt_error (GNUNET_MYSQL_statement_get_stmt (*stmt)), |
258 | mysql_stmt_error (GNUNET_MYSQL_statement_get_stmt (*stmt)), sql); | 258 | sql); |
259 | 259 | return GNUNET_SYSERR; | |
260 | return 0; | 260 | } |
261 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
262 | "Prepared `%s' / %p\n", | ||
263 | sql, | ||
264 | stmt); | ||
265 | return GNUNET_OK; | ||
261 | } | 266 | } |
262 | 267 | ||
263 | 268 | ||
@@ -359,211 +364,184 @@ database_setup (struct Plugin *plugin) | |||
359 | #undef STMT_RUN | 364 | #undef STMT_RUN |
360 | 365 | ||
361 | /* Prepare statements */ | 366 | /* Prepare statements */ |
362 | mysql_prepare (plugin->mc, | 367 | #define PREP(stmt,handle) \ |
363 | "BEGIN", | 368 | if (GNUNET_OK != mysql_prepare (plugin->mc, stmt, handle)) \ |
364 | &plugin->transaction_begin); | 369 | { \ |
365 | 370 | GNUNET_break (0); \ | |
366 | mysql_prepare (plugin->mc, | 371 | return GNUNET_SYSERR; \ |
367 | "COMMIT", | 372 | } |
368 | &plugin->transaction_commit); | 373 | PREP ("BEGIN", |
369 | 374 | &plugin->transaction_begin); | |
370 | mysql_prepare (plugin->mc, | 375 | PREP ("COMMIT", |
371 | "ROLLBACK;", | 376 | &plugin->transaction_commit); |
372 | &plugin->transaction_rollback); | 377 | PREP ("ROLLBACK;", |
373 | 378 | &plugin->transaction_rollback); | |
374 | mysql_prepare (plugin->mc, | 379 | PREP ("INSERT IGNORE INTO channels (pub_key) VALUES (?);", |
375 | "INSERT IGNORE INTO channels (pub_key) VALUES (?);", | 380 | &plugin->insert_channel_key); |
376 | &plugin->insert_channel_key); | 381 | PREP ("INSERT IGNORE INTO slaves (pub_key) VALUES (?);", |
377 | 382 | &plugin->insert_slave_key); | |
378 | mysql_prepare (plugin->mc, | 383 | PREP ("INSERT INTO membership\n" |
379 | "INSERT IGNORE INTO slaves (pub_key) VALUES (?);", | 384 | " (channel_id, slave_id, did_join, announced_at,\n" |
380 | &plugin->insert_slave_key); | 385 | " effective_since, group_generation)\n" |
381 | 386 | "VALUES ((SELECT id FROM channels WHERE pub_key = ?),\n" | |
382 | mysql_prepare (plugin->mc, | 387 | " (SELECT id FROM slaves WHERE pub_key = ?),\n" |
383 | "INSERT INTO membership\n" | 388 | " ?, ?, ?, ?);", |
384 | " (channel_id, slave_id, did_join, announced_at,\n" | 389 | &plugin->insert_membership); |
385 | " effective_since, group_generation)\n" | 390 | PREP ("SELECT did_join FROM membership\n" |
386 | "VALUES ((SELECT id FROM channels WHERE pub_key = ?),\n" | 391 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
387 | " (SELECT id FROM slaves WHERE pub_key = ?),\n" | 392 | " AND slave_id = (SELECT id FROM slaves WHERE pub_key = ?)\n" |
388 | " ?, ?, ?, ?);", | 393 | " AND effective_since <= ? AND did_join = 1\n" |
389 | &plugin->insert_membership); | 394 | "ORDER BY announced_at DESC LIMIT 1;", |
390 | 395 | &plugin->select_membership); | |
391 | mysql_prepare (plugin->mc, | 396 | |
392 | "SELECT did_join FROM membership\n" | 397 | PREP ("INSERT IGNORE INTO messages\n" |
393 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | 398 | " (channel_id, hop_counter, signature, purpose,\n" |
394 | " AND slave_id = (SELECT id FROM slaves WHERE pub_key = ?)\n" | 399 | " fragment_id, fragment_offset, message_id,\n" |
395 | " AND effective_since <= ? AND did_join = 1\n" | 400 | " group_generation, multicast_flags, psycstore_flags, data)\n" |
396 | "ORDER BY announced_at DESC LIMIT 1;", | 401 | "VALUES ((SELECT id FROM channels WHERE pub_key = ?),\n" |
397 | &plugin->select_membership); | 402 | " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", |
398 | 403 | &plugin->insert_fragment); | |
399 | mysql_prepare (plugin->mc, | 404 | |
400 | "INSERT IGNORE INTO messages\n" | 405 | PREP ("UPDATE messages\n" |
401 | " (channel_id, hop_counter, signature, purpose,\n" | 406 | "SET psycstore_flags = psycstore_flags | ?\n" |
402 | " fragment_id, fragment_offset, message_id,\n" | 407 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
403 | " group_generation, multicast_flags, psycstore_flags, data)\n" | 408 | " AND message_id = ? AND fragment_offset = 0;", |
404 | "VALUES ((SELECT id FROM channels WHERE pub_key = ?),\n" | 409 | &plugin->update_message_flags); |
405 | " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);", | 410 | |
406 | &plugin->insert_fragment); | 411 | PREP ("SELECT hop_counter, signature, purpose, fragment_id,\n" |
407 | 412 | " fragment_offset, message_id, group_generation,\n" | |
408 | mysql_prepare (plugin->mc, | 413 | " multicast_flags, psycstore_flags, data\n" |
409 | "UPDATE messages\n" | 414 | "FROM messages\n" |
410 | "SET psycstore_flags = psycstore_flags | ?\n" | 415 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
411 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | 416 | " AND ? <= fragment_id AND fragment_id <= ?;", |
412 | " AND message_id = ? AND fragment_offset = 0;", | 417 | &plugin->select_fragments); |
413 | &plugin->update_message_flags); | ||
414 | |||
415 | mysql_prepare (plugin->mc, | ||
416 | "SELECT hop_counter, signature, purpose, fragment_id,\n" | ||
417 | " fragment_offset, message_id, group_generation,\n" | ||
418 | " multicast_flags, psycstore_flags, data\n" | ||
419 | "FROM messages\n" | ||
420 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | ||
421 | " AND ? <= fragment_id AND fragment_id <= ?;", | ||
422 | &plugin->select_fragments); | ||
423 | 418 | ||
424 | /** @todo select_messages: add method_prefix filter */ | 419 | /** @todo select_messages: add method_prefix filter */ |
425 | mysql_prepare (plugin->mc, | 420 | PREP ("SELECT hop_counter, signature, purpose, fragment_id,\n" |
426 | "SELECT hop_counter, signature, purpose, fragment_id,\n" | 421 | " fragment_offset, message_id, group_generation,\n" |
427 | " fragment_offset, message_id, group_generation,\n" | 422 | " multicast_flags, psycstore_flags, data\n" |
428 | " multicast_flags, psycstore_flags, data\n" | 423 | "FROM messages\n" |
429 | "FROM messages\n" | 424 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
430 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | 425 | " AND ? <= message_id AND message_id <= ?\n" |
431 | " AND ? <= message_id AND message_id <= ?\n" | 426 | "LIMIT ?;", |
432 | "LIMIT ?;", | 427 | &plugin->select_messages); |
433 | &plugin->select_messages); | 428 | |
434 | 429 | PREP ("SELECT * FROM\n" | |
435 | mysql_prepare (plugin->mc, | 430 | "(SELECT hop_counter, signature, purpose, fragment_id,\n" |
436 | "SELECT * FROM\n" | 431 | " fragment_offset, message_id, group_generation,\n" |
437 | "(SELECT hop_counter, signature, purpose, fragment_id,\n" | 432 | " multicast_flags, psycstore_flags, data\n" |
438 | " fragment_offset, message_id, group_generation,\n" | 433 | " FROM messages\n" |
439 | " multicast_flags, psycstore_flags, data\n" | 434 | " WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
440 | " FROM messages\n" | 435 | " ORDER BY fragment_id DESC\n" |
441 | " WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | 436 | " LIMIT ?)\n" |
442 | " ORDER BY fragment_id DESC\n" | 437 | "ORDER BY fragment_id;", |
443 | " LIMIT ?)\n" | 438 | &plugin->select_latest_fragments); |
444 | "ORDER BY fragment_id;", | ||
445 | &plugin->select_latest_fragments); | ||
446 | 439 | ||
447 | /** @todo select_latest_messages: add method_prefix filter */ | 440 | /** @todo select_latest_messages: add method_prefix filter */ |
448 | mysql_prepare (plugin->mc, | 441 | PREP ("SELECT hop_counter, signature, purpose, fragment_id,\n" |
449 | "SELECT hop_counter, signature, purpose, fragment_id,\n" | 442 | " fragment_offset, message_id, group_generation,\n" |
450 | " fragment_offset, message_id, group_generation,\n" | 443 | " multicast_flags, psycstore_flags, data\n" |
451 | " multicast_flags, psycstore_flags, data\n" | 444 | "FROM messages\n" |
452 | "FROM messages\n" | 445 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
453 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | 446 | " AND message_id IN\n" |
454 | " AND message_id IN\n" | 447 | " (SELECT message_id\n" |
455 | " (SELECT message_id\n" | 448 | " FROM messages\n" |
456 | " FROM messages\n" | 449 | " WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
457 | " WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | 450 | " GROUP BY message_id\n" |
458 | " GROUP BY message_id\n" | 451 | " ORDER BY message_id\n" |
459 | " ORDER BY message_id\n" | 452 | " DESC LIMIT ?)\n" |
460 | " DESC LIMIT ?)\n" | 453 | "ORDER BY fragment_id;", |
461 | "ORDER BY fragment_id;", | 454 | &plugin->select_latest_messages); |
462 | &plugin->select_latest_messages); | 455 | |
463 | 456 | PREP ("SELECT hop_counter, signature, purpose, fragment_id,\n" | |
464 | mysql_prepare (plugin->mc, | 457 | " fragment_offset, message_id, group_generation,\n" |
465 | "SELECT hop_counter, signature, purpose, fragment_id,\n" | 458 | " multicast_flags, psycstore_flags, data\n" |
466 | " fragment_offset, message_id, group_generation,\n" | 459 | "FROM messages\n" |
467 | " multicast_flags, psycstore_flags, data\n" | 460 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
468 | "FROM messages\n" | 461 | " AND message_id = ? AND fragment_offset = ?;", |
469 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | 462 | &plugin->select_message_fragment); |
470 | " AND message_id = ? AND fragment_offset = ?;", | 463 | |
471 | &plugin->select_message_fragment); | 464 | PREP ("SELECT fragment_id, message_id, group_generation\n" |
472 | 465 | "FROM messages\n" | |
473 | mysql_prepare (plugin->mc, | 466 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
474 | "SELECT fragment_id, message_id, group_generation\n" | 467 | "ORDER BY fragment_id DESC LIMIT 1;", |
475 | "FROM messages\n" | 468 | &plugin->select_counters_message); |
476 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | 469 | |
477 | "ORDER BY fragment_id DESC LIMIT 1;", | 470 | PREP ("SELECT max_state_message_id\n" |
478 | &plugin->select_counters_message); | 471 | "FROM channels\n" |
479 | 472 | "WHERE pub_key = ? AND max_state_message_id IS NOT NULL;", | |
480 | mysql_prepare (plugin->mc, | 473 | &plugin->select_counters_state); |
481 | "SELECT max_state_message_id\n" | 474 | |
482 | "FROM channels\n" | 475 | PREP ("UPDATE channels\n" |
483 | "WHERE pub_key = ? AND max_state_message_id IS NOT NULL;", | 476 | "SET max_state_message_id = ?\n" |
484 | &plugin->select_counters_state); | 477 | "WHERE pub_key = ?;", |
485 | 478 | &plugin->update_max_state_message_id); | |
486 | mysql_prepare (plugin->mc, | 479 | |
487 | "UPDATE channels\n" | 480 | PREP ("UPDATE channels\n" |
488 | "SET max_state_message_id = ?\n" | 481 | "SET state_hash_message_id = ?\n" |
489 | "WHERE pub_key = ?;", | 482 | "WHERE pub_key = ?;", |
490 | &plugin->update_max_state_message_id); | 483 | &plugin->update_state_hash_message_id); |
491 | 484 | ||
492 | mysql_prepare (plugin->mc, | 485 | PREP ("REPLACE INTO state\n" |
493 | "UPDATE channels\n" | 486 | " (channel_id, name, value_current, value_signed)\n" |
494 | "SET state_hash_message_id = ?\n" | 487 | "SELECT new.channel_id, new.name,\n" |
495 | "WHERE pub_key = ?;", | 488 | " new.value_current, old.value_signed\n" |
496 | &plugin->update_state_hash_message_id); | 489 | "FROM (SELECT (SELECT id FROM channels WHERE pub_key = ?)\n" |
497 | 490 | " AS channel_id,\n" | |
498 | mysql_prepare (plugin->mc, | 491 | " ? AS name, ? AS value_current) AS new\n" |
499 | "REPLACE INTO state\n" | 492 | "LEFT JOIN (SELECT channel_id, name, value_signed\n" |
500 | " (channel_id, name, value_current, value_signed)\n" | 493 | " FROM state) AS old\n" |
501 | "SELECT new.channel_id, new.name,\n" | 494 | "ON new.channel_id = old.channel_id AND new.name = old.name;", |
502 | " new.value_current, old.value_signed\n" | 495 | &plugin->insert_state_current); |
503 | "FROM (SELECT (SELECT id FROM channels WHERE pub_key = ?)\n" | 496 | |
504 | " AS channel_id,\n" | 497 | PREP ("DELETE FROM state\n" |
505 | " ? AS name, ? AS value_current) AS new\n" | 498 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
506 | "LEFT JOIN (SELECT channel_id, name, value_signed\n" | 499 | " AND (value_current IS NULL OR length(value_current) = 0)\n" |
507 | " FROM state) AS old\n" | 500 | " AND (value_signed IS NULL OR length(value_signed) = 0);", |
508 | "ON new.channel_id = old.channel_id AND new.name = old.name;", | 501 | &plugin->delete_state_empty); |
509 | &plugin->insert_state_current); | 502 | |
510 | 503 | PREP ("UPDATE state\n" | |
511 | mysql_prepare (plugin->mc, | 504 | "SET value_signed = value_current\n" |
512 | "DELETE FROM state\n" | 505 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);", |
513 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | 506 | &plugin->update_state_signed); |
514 | " AND (value_current IS NULL OR length(value_current) = 0)\n" | 507 | |
515 | " AND (value_signed IS NULL OR length(value_signed) = 0);", | 508 | PREP ("DELETE FROM state\n" |
516 | &plugin->delete_state_empty); | 509 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);", |
517 | 510 | &plugin->delete_state); | |
518 | mysql_prepare (plugin->mc, | 511 | |
519 | "UPDATE state\n" | 512 | PREP ("INSERT INTO state_sync (channel_id, name, value)\n" |
520 | "SET value_signed = value_current\n" | 513 | "VALUES ((SELECT id FROM channels WHERE pub_key = ?), ?, ?);", |
521 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);", | 514 | &plugin->insert_state_sync); |
522 | &plugin->update_state_signed); | 515 | |
523 | 516 | PREP ("INSERT INTO state\n" | |
524 | mysql_prepare (plugin->mc, | 517 | " (channel_id, name, value_current, value_signed)\n" |
525 | "DELETE FROM state\n" | 518 | "SELECT channel_id, name, value, value\n" |
526 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);", | 519 | "FROM state_sync\n" |
527 | &plugin->delete_state); | 520 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);", |
528 | 521 | &plugin->insert_state_from_sync); | |
529 | mysql_prepare (plugin->mc, | 522 | |
530 | "INSERT INTO state_sync (channel_id, name, value)\n" | 523 | PREP ("DELETE FROM state_sync\n" |
531 | "VALUES ((SELECT id FROM channels WHERE pub_key = ?), ?, ?);", | 524 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);", |
532 | &plugin->insert_state_sync); | 525 | &plugin->delete_state_sync); |
533 | 526 | ||
534 | mysql_prepare (plugin->mc, | 527 | PREP ("SELECT value_current\n" |
535 | "INSERT INTO state\n" | 528 | "FROM state\n" |
536 | " (channel_id, name, value_current, value_signed)\n" | 529 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
537 | "SELECT channel_id, name, value, value\n" | 530 | " AND name = ?;", |
538 | "FROM state_sync\n" | 531 | &plugin->select_state_one); |
539 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);", | 532 | |
540 | &plugin->insert_state_from_sync); | 533 | PREP ("SELECT name, value_current\n" |
541 | 534 | "FROM state\n" | |
542 | mysql_prepare (plugin->mc, | 535 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" |
543 | "DELETE FROM state_sync\n" | 536 | " AND (name = ? OR substr(name, 1, ?) = ? || '_');", |
544 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?);", | 537 | &plugin->select_state_prefix); |
545 | &plugin->delete_state_sync); | 538 | |
546 | 539 | PREP ("SELECT name, value_signed\n" | |
547 | mysql_prepare (plugin->mc, | 540 | "FROM state\n" |
548 | "SELECT value_current\n" | 541 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)" |
549 | "FROM state\n" | 542 | " AND value_signed IS NOT NULL;", |
550 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | 543 | &plugin->select_state_signed); |
551 | " AND name = ?;", | 544 | #undef PREP |
552 | &plugin->select_state_one); | ||
553 | |||
554 | mysql_prepare (plugin->mc, | ||
555 | "SELECT name, value_current\n" | ||
556 | "FROM state\n" | ||
557 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)\n" | ||
558 | " AND (name = ? OR substr(name, 1, ?) = ? || '_');", | ||
559 | &plugin->select_state_prefix); | ||
560 | |||
561 | mysql_prepare (plugin->mc, | ||
562 | "SELECT name, value_signed\n" | ||
563 | "FROM state\n" | ||
564 | "WHERE channel_id = (SELECT id FROM channels WHERE pub_key = ?)" | ||
565 | " AND value_signed IS NOT NULL;", | ||
566 | &plugin->select_state_signed); | ||
567 | 545 | ||
568 | return GNUNET_OK; | 546 | return GNUNET_OK; |
569 | } | 547 | } |