Package com.almworks.sqlite4java

Examples of com.almworks.sqlite4java.SQLiteStatement


  }
  List<TypeGuessSummary> getTypeGuesses(final String queryStr, final long idval) {
    return dbQueue.execute(new SQLiteJob<List<TypeGuessSummary>>() {
        protected List<TypeGuessSummary> job(SQLiteConnection db) throws SQLiteException {
          List<TypeGuessSummary> outputList = new ArrayList<TypeGuessSummary>();
          SQLiteStatement stmt = db.prepare(queryStr);
          try {
            stmt.bind(1, idval);
            while (stmt.step()) {
              outputList.add(new TypeGuessSummary(FSAnalyzer.this, stmt.columnLong(0), stmt.columnLong(1), stmt.columnLong(2)));
            }
          } finally {
            stmt.dispose();
          }
          return outputList;
        }
      }).complete();
  }
View Full Code Here


  static String precachedTypeSummaryQuery = "SELECT TypeGuesses.fid, Types.typelabel, SchemaGuesses.schemaid, Files.crawlid, Files.fname, Files.owner, Files.groupowner, Files.permissions, Files.size, Files.modified, Files.path FROM SchemaGuesses, TypeGuesses, Files, Types WHERE TypeGuesses.fid = SchemaGuesses.fid AND TypeGuesses.fid = Files.fid AND TypeGuesses.typeid = Types.typeid AND TypeGuesses.typeId = ?";
  public TypeSummary getPrecachedTypeSummary(final long typeid) {
    return dbQueue.execute(new SQLiteJob<TypeSummary>() {
        protected TypeSummary job(SQLiteConnection db) throws SQLiteException {
          SQLiteStatement stmt = db.prepare(precachedTypeSummaryQuery);
          stmt.bind(1, typeid);

          TypeSummary ts = null;
          try {
            List<TypeGuessSummary> tgslist = null;           
            while (stmt.step()) {
              int i = 0;
              long fid = stmt.columnLong(i++);
              String typelabel = stmt.columnString(i++);
              long schemaid = stmt.columnLong(i++);
              long crawlid = stmt.columnLong(i++);
              String fname = stmt.columnString(i++);
              String owner = stmt.columnString(i++);
              String groupowner = stmt.columnString(i++);
              String permissions = stmt.columnString(i++);
              long size = stmt.columnLong(i++);
              String modified = stmt.columnString(i++);
              String path = stmt.columnString(i++);

              if (ts == null) {
                ts = new TypeSummary(FSAnalyzer.this, typeid);
                ts.addCachedData(new TypeSummaryData(typeid, typelabel));
                tgslist = new ArrayList<TypeGuessSummary>();
              }
              TypeGuessSummary tg = new TypeGuessSummary(FSAnalyzer.this, fid, typeid, schemaid);
              FileSummary fs = new FileSummary(FSAnalyzer.this, fid);
              FileSummaryData fsd = new FileSummaryData(FSAnalyzer.this, true, fid, crawlid, fname, owner, groupowner, permissions, size, modified, path);
              fs.addCachedData(fsd);
              tg.addCachedData(fs);
              tgslist.add(tg);
            }
            ts.addCachedData(tgslist);
          } catch (SQLiteException sle) {
            sle.printStackTrace();
          } finally {
            stmt.dispose();
          }
          return ts;
        }}).complete();
  }
View Full Code Here

  }

  public User getUser(final String login) {
    return queue.execute(new SQLiteJob<User>() {
      protected User job(SQLiteConnection connectionthrows SQLiteException {
        SQLiteStatement st = connection.prepare(
            "SELECT " +
              FIELD_ID + ", " +
              FIELD_NAME + ", " +
              FIELD_PASSWORD +
            " FROM " + TABLE_USERS + " WHERE " + FIELD_LOGIN + "=?;"
        );
       
        st.bind(1, login);
       
        if (st.step()) {
          return new User(st.columnLong(0), login, st.columnString(2), st.columnString(1));
        } else {
          return null;
        }
      }
    }).complete();
View Full Code Here

  }
 
  public User addUser(final String login, final String password, final String name) {
    return queue.execute(new SQLiteJob<User>() {
      protected User job(SQLiteConnection connectionthrows SQLiteException {
        SQLiteStatement st = connection.prepare(
            "INSERT INTO " + TABLE_USERS + " " +
                "(" +
                  FIELD_LOGIN  + ", " +
                  FIELD_PASSWORD + ", " +
                  FIELD_NAME +
                ") VALUES (?, ?, ?);"
        );
       
        st.bind(1, login);
        st.bind(2, password);
        st.bind(3, name);
       
        st.step();

        long newId = connection.getLastInsertId();
        return new User(newId, login, password, name);
      }
    }).complete();
View Full Code Here

  }

  public Message addMessage(final long userId, final long timeMillis, final String text) {
    return queue.execute(new SQLiteJob<Message>() {
      protected Message job(SQLiteConnection connectionthrows SQLiteException {
        SQLiteStatement st = connection.prepare(
            "INSERT INTO " + TABLE_MESSAGES + " " +
                "(" +
                  FIELD_USER_ID + ", " +
                  FIELD_TIME_MILLIS + ", " +
                  FIELD_TEXT +
                ") VALUES (?, ?, ?);"
        );
       
        st.bind(1, userId);
        st.bind(2, timeMillis);
        st.bind(3, text);
       
        st.step();

        long newId = connection.getLastInsertId();
        return new Message(newId, userId, timeMillis, text);
      }
    }).complete();
View Full Code Here

 
  public Message[] getMessagesOrderedSince(final long timeMillis) {
    return queue.execute(new SQLiteJob<Message[]>() {
      protected Message[] job(SQLiteConnection connection) throws SQLiteException {

        SQLiteStatement st = connection.prepare(
          "SELECT " +
            FIELD_ID + ", " +
            FIELD_USER_ID + ", " +
            FIELD_TIME_MILLIS + ", " +
            FIELD_TEXT +
          " FROM " + TABLE_MESSAGES +
          " WHERE " + FIELD_TIME_MILLIS + " > ?" +
          " ORDER BY " + FIELD_TIME_MILLIS + " ASC;"
        );
       
        st.bind(1, timeMillis);

        LinkedList<Message> resList = new LinkedList<>();

        while (st.step()) {
          resList.add(new Message(
              st.columnLong(0),
              st.columnLong(1),
              st.columnLong(2),
              st.columnString(3)
            )
          );
        }
        Message[] res = resList.toArray(new Message[] {});
        return res;
View Full Code Here

  public User[] getUsers() throws SQLiteException {
    return queue.execute(new SQLiteJob<User[]>() {
      protected User[] job(SQLiteConnection connection)
          throws SQLiteException {

        SQLiteStatement st = connection.prepare(
          "SELECT " +
            FIELD_ID + ", " +
            FIELD_LOGIN + ", " +
            FIELD_PASSWORD + ", " +
            FIELD_NAME +
          " FROM " + TABLE_USERS + ";"
        );

        LinkedList<User> resList = new LinkedList<>();

        while (st.step()) {
          resList.add(new User(
              st.columnLong(0),
              st.columnString(1),
              st.columnString(2),
              st.columnString(3)
            )
          );
        }
        User[] res = resList.toArray(new User[] {});
        return res;
View Full Code Here

          it.remove();
        }
      }
    }

    SQLiteStatement st = null;
    try {
      SQLiteConnection conn = db.get();
      if(timeoutThread != null) {
        timeoutThread.startQuery(conn, query);
      }
      // We don't want to cache the statements here so we use "false"
      // Don't use the method without boolean because it will use cached = true!!!
      st = conn.prepare(query, false);
      if(timeoutThread != null) {
        timeoutThread.endQuery(conn);
      }
      List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
      if(st.step()) {
        do {
          // true if there is data (SQLITE_ROW) was returned, false if statement has been completed (SQLITE_DONE)
          Map<String, Object> objectToRead = new HashMap<String, Object>();
          for(int i = 0; i < st.columnCount(); i++) {
            objectToRead.put(st.getColumnName(i), st.columnValue(i));
          }
          list.add(objectToRead);
        } while(st.step() && list.size() < maxResults);
      }
      if(list.size() == maxResults) {
        throw new SQLException("Hard limit on number of results reached (" + maxResults + "), please use a LIMIT for this query.");
      }
      return JSONSerDe.ser(list);
    } catch(SQLiteException e) {
      throw new SQLException(e);
    } catch(JSONSerDeException e) {
      throw new SQLException(e);
    } catch(SQLException e) {
      throw new SQLException(e);
    } finally {
      if(st != null) {
        st.dispose();
      }
    }
  }
View Full Code Here

      // It suffices to set it to . as it is the tasks' work directory
      // Warning: this pragma is deprecated and may be removed in further versions, however there is no choice
      // other than recompiling SQLite or modifying the environment.
      conn.open(true);
      conn.exec("PRAGMA temp_store_directory = '" + new File(".").getAbsolutePath() + "'");
      SQLiteStatement st = conn.prepare("PRAGMA temp_store_directory");
      st.step();
      LOG.info("Changed temp_store_directory to: " + st.columnString(0));
      // journal_mode=OFF speeds up insertions
      conn.exec("PRAGMA journal_mode=OFF");
      /*
       * page_size is one of of the most important parameters for speed up indexation. SQLite performs a merge sort for
       * sorting data before inserting it in an index. The buffer SQLites uses for sorting has a size equals to
View Full Code Here

       * Key performance trick: Cache PreparedStatements when possible. We will have one PreparedStatement per each
       * different Tuple Schema (table).
       */
      Map<String, SQLiteStatement> stMap = stCache.get(partition);

      SQLiteStatement pS = stMap.get(tuple.getSchema().getName());
      if(pS == null) {
        SQLiteConnection conn = connCache.get(partition);
        // Create a PreparedStatement according to the received Tuple
        String preparedStatement = "INSERT INTO " + tuple.getSchema().getName() + " VALUES (";
        // NOTE: tuple.getSchema().getFields().size() - 1 : quick way of skipping "_partition" fields here
        for(int i = 0; i < tuple.getSchema().getFields().size() - 1; i++) {
          preparedStatement += "?, ";
        }
        preparedStatement = preparedStatement.substring(0, preparedStatement.length() - 2) + ");";
        pS = conn.prepare(preparedStatement);
        stMap.put(tuple.getSchema().getName(), pS);
      }

      int count = 1, tupleCount = 0;
      for(Field field : tuple.getSchema().getFields()) {
        if(field.getName().equals(PARTITION_TUPLE_FIELD)) {
          tupleCount++;
          continue;
        }

        if(tuple.get(tupleCount) == null) {
          pS.bindNull(count);
        } else {
          switch(field.getType()) {

          case INT:
            pS.bind(count, (Integer) tuple.get(tupleCount));
            break;
          case LONG:
            pS.bind(count, (Long) tuple.get(tupleCount));
            break;
          case DOUBLE:
            pS.bind(count, (Double) tuple.get(tupleCount));
            break;
          case FLOAT:
            pS.bind(count, (Float) tuple.get(tupleCount));
            break;
          case STRING:
            pS.bind(count, tuple.get(tupleCount).toString());
            break;
          case BOOLEAN: // Remember: In SQLite there are no booleans
            pS.bind(count, ((Boolean) tuple.get(tupleCount)) == true ? 1 : 0);
          default:
            break;
          }
        }
        count++;
        tupleCount++;
      }
      pS.step();
      pS.reset();

      records++;
      if(records == getBatchSize()) {
        SQLiteConnection conn = connCache.get(partition);
        conn.exec("COMMIT");
View Full Code Here

TOP

Related Classes of com.almworks.sqlite4java.SQLiteStatement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.