Java Code Examples for com.almworks.sqlite4java.SQLiteConnection#open()

The following examples show how to use com.almworks.sqlite4java.SQLiteConnection#open() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AttestationServer.java    From AttestationServer with MIT License 5 votes vote down vote up
static void open(final SQLiteConnection conn, final boolean readOnly) throws SQLiteException {
    if (readOnly) {
        conn.openReadonly();
    } else {
        conn.open();
    }
    conn.setBusyTimeout(BUSY_TIMEOUT);
    conn.exec("PRAGMA foreign_keys=ON");
    conn.exec("PRAGMA journal_mode=WAL");
}
 
Example 2
Source File: SqliteStreamOperator.java    From attic-apex-malhar with Apache License 2.0 4 votes vote down vote up
@Override
public void setup(OperatorContext context)
{
  db = new SQLiteConnection(new File("/tmp/sqlite.db"));
  java.util.logging.Logger.getLogger("com.almworks.sqlite4java").setLevel(java.util.logging.Level.SEVERE);
  SQLiteStatement st;

  try {
    db.open(true);
    // create the temporary tables here
    for (int i = 0; i < inputSchemas.size(); i++) {
      InputSchema inputSchema = inputSchemas.get(i);
      ArrayList<String> indexes = new ArrayList<String>();
      if (inputSchema == null || inputSchema.columnInfoMap.isEmpty()) {
        continue;
      }
      String columnSpec = "";
      String columnNames = "";
      String insertQuestionMarks = "";
      int j = 0;
      for (Map.Entry<String, ColumnInfo> entry : inputSchema.columnInfoMap.entrySet()) {
        if (!columnSpec.isEmpty()) {
          columnSpec += ",";
          columnNames += ",";
          insertQuestionMarks += ",";
        }
        columnSpec += entry.getKey();
        columnSpec += " ";
        columnSpec += entry.getValue().type;
        if (entry.getValue().isColumnIndex) {
          indexes.add(entry.getKey());
        }
        columnNames += entry.getKey();
        insertQuestionMarks += "?";
        entry.getValue().bindIndex = ++j;
      }
      String createTempTableStmt = "CREATE TEMP TABLE " + inputSchema.name + "(" + columnSpec + ")";
      st = db.prepare(createTempTableStmt);
      st.step();
      st.dispose();
      for (String index : indexes) {
        String createIndexStmt = "CREATE INDEX " + inputSchema.name + "_" + index + "_idx ON " + inputSchema.name + " (" + index + ")";
        st = db.prepare(createIndexStmt);
        st.step();
        st.dispose();
      }
      String insertStmt = "INSERT INTO " + inputSchema.name + " (" + columnNames + ") VALUES (" + insertQuestionMarks + ")";

      insertStatements.add(i, db.prepare(insertStmt));
      // We are calling "DELETE FROM" on the tables and because of the "truncate optimization" in sqlite, it should be fast.
      // See http://sqlite.org/lang_delete.html
      deleteStatements.add(i, db.prepare("DELETE FROM " + inputSchema.name));
    }
    beginStatement = db.prepare("BEGIN");
    commitStatement = db.prepare("COMMIT");
    execStatement = db.prepare(statement);
  } catch (SQLiteException ex) {
    throw new RuntimeException(ex);
  }
}