Fixed reconnect
[oweals/gnunet.git] / src / datastore / plugin_datastore_sqlite.c
index 46486d9337b06e60726f0b85560aacbbdaa351b2..931202707121a5f1f1caecc1f092ceafc3b1ec22 100644 (file)
@@ -1,17 +1,17 @@
  /*
   * This file is part of GNUnet
   * (C) 2009, 2011 Christian Grothoff (and other contributing authors)
-  * 
+  *
   * GNUnet 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 3, or (at your
   * option) any later version.
-  * 
+  *
   * GNUnet 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 GNUnet; see the file COPYING.  If not, write to the
   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
@@ -157,7 +157,7 @@ sq_prepare (sqlite3 * dbh, const char *zSql, sqlite3_stmt ** ppStmt)
 
 /**
  * Create our database indices.
- * 
+ *
  * @param dbh handle to the database
  */
 static void
@@ -326,32 +326,47 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
       (sq_prepare
        (plugin->dbh,
         "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ "
-        "FROM gn090 INDEXED BY idx_repl_rvalue " "WHERE repl=?2 AND"
-        "      (rvalue>=?1 OR"
-        "        NOT EXISTS (SELECT 1 FROM gn090 INDEXED BY idx_repl_rvalue WHERE repl=?2 AND rvalue>=?1 LIMIT 1))"
-        "     ORDER BY rvalue ASC" "     LIMIT 1",
-        &plugin->selRepl) != SQLITE_OK) ||
+        "FROM gn090 "
+#if SQLITE_VERSION_NUMBER >= 3007000
+       "INDEXED BY idx_repl_rvalue "
+#endif
+       "WHERE repl=?2 AND "
+        " (rvalue>=?1 OR "
+        "  NOT EXISTS (SELECT 1 FROM gn090 "
+#if SQLITE_VERSION_NUMBER >= 3007000
+       "INDEXED BY idx_repl_rvalue "
+#endif
+       "WHERE repl=?2 AND rvalue>=?1 LIMIT 1) ) "
+        "ORDER BY rvalue ASC LIMIT 1", &plugin->selRepl) != SQLITE_OK) ||
       (sq_prepare
-       (plugin->dbh,
-        "SELECT MAX(repl) " "FROM gn090 INDEXED BY idx_repl_rvalue",
+       (plugin->dbh, "SELECT MAX(repl) FROM gn090"
+#if SQLITE_VERSION_NUMBER >= 3007000
+       " INDEXED BY idx_repl_rvalue"
+#endif
+       "",
         &plugin->maxRepl) != SQLITE_OK) ||
       (sq_prepare
        (plugin->dbh,
         "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ "
-        "FROM gn090 INDEXED BY idx_expire"
-        " WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) "
-        " ORDER BY expire ASC LIMIT 1", &plugin->selExpi) != SQLITE_OK) ||
+        "FROM gn090 "
+#if SQLITE_VERSION_NUMBER >= 3007000
+       "INDEXED BY idx_expire "
+#endif
+        "WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) "
+        "ORDER BY expire ASC LIMIT 1", &plugin->selExpi) != SQLITE_OK) ||
       (sq_prepare
        (plugin->dbh,
         "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ "
-        "FROM gn090 INDEXED BY idx_anon_type_hash "
+        "FROM gn090 " 
+#if SQLITE_VERSION_NUMBER >= 3007000
+       "INDEXED BY idx_anon_type_hash " 
+#endif
         "WHERE (anonLevel = 0 AND type=?1) "
         "ORDER BY hash DESC LIMIT 1 OFFSET ?2",
         &plugin->selZeroAnon) != SQLITE_OK) ||
       (sq_prepare
        (plugin->dbh,
-        "INSERT INTO gn090 (repl, type, prio, "
-        "anonLevel, expire, rvalue, hash, vhash, value) "
+        "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
         "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
         &plugin->insertContent) != SQLITE_OK) ||
       (sq_prepare
@@ -724,7 +739,7 @@ execute_get (struct Plugin *plugin, sqlite3_stmt * stmt,
  * the given processor for the item.
  *
  * @param cls our plugin context
- * @param offset offset of the result (modulo num-results); 
+ * @param offset offset of the result (modulo num-results);
  *               specific ordering does not matter for the offset
  * @param type entries of which type should be considered?
  *        Use 0 for any type.
@@ -947,7 +962,7 @@ repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
 
 /**
  * Get a random item for replication.  Returns a single random item
- * from those with the highest replication counters.  The item's 
+ * from those with the highest replication counters.  The item's
  * replication counter is decremented by one IF it was positive before.
  * Call 'proc' with all values ZERO or NULL if the datastore is empty.
  *