/** {@inheritDoc} */
 public String retrieve(long id, Connection conn)
     throws EntityDoesNotExistException, SQLException {
   if (cache != null && id < cache.length) {
     String value = cache[(int) id];
     if (value != null) {
       return value;
     }
   }
   Statement stmt = conn.createStatement();
   ResultSet rs = null;
   try {
     rs = stmt.executeQuery("SELECT data FROM " + getTable() + " WHERE data_key = " + id);
     if (!rs.next()) {
       throw new EntityDoesNotExistException(
           "Item #" + id + " does not exist in table " + getTable());
     }
     String value = unescape(rs.getString(1));
     if (cache != null && id < cache.length) {
       cache[(int) id] = value;
     }
     return value;
   } finally {
     DatabaseUtils.close(rs);
     DatabaseUtils.close(stmt);
   }
 }
 /////////////// private
 private DataSource getDataSource() throws Exception {
   FileSystem fs = getFileSystem();
   jdbcDataSource ds = new jdbcDataSource();
   ds.setDatabase("jdbc:hsqldb:.");
   ds.setUser("sa");
   ds.setPassword("");
   if (!DatabaseUtils.hasTable(ds, "ledge_id_table")) {
     DatabaseUtils.runScript(ds, fs.getReader("sql/database/IdGeneratorTables.sql", "UTF-8"));
   }
   if (!DatabaseUtils.hasTable(ds, "ledge_naming_context")) {
     DatabaseUtils.runScript(ds, fs.getReader("sql/naming/DBNamingTables.sql", "UTF-8"));
   }
   DatabaseUtils.runScript(ds, fs.getReader("sql/naming/DBNamingTest.sql", "UTF-8"));
   return ds;
 }
Exemple #3
0
 private void cleanupPersistentResourceTables() throws SQLException {
   Connection conn = dataSource.getConnection();
   try {
     Statement stmt = conn.createStatement();
     try {
       List<String> tables = new ArrayList<String>();
       ResultSet rset =
           stmt.executeQuery(
               "SELECT db_table_name FROM coral_resource_class WHERE db_table_name IS NOT NULL");
       try {
         while (rset.next()) {
           if (DatabaseUtils.hasTable(dataSource, rset.getString(1))) {
             tables.add(rset.getString(1));
           }
         }
       } finally {
         rset.close();
       }
       if (tables.size() > 0) {
         logger.info("dropping PersitentResource tables");
         for (String table : tables) {
           stmt.execute("DROP TABLE " + table);
         }
       }
     } finally {
       stmt.close();
     }
   } finally {
     conn.close();
   }
 }
  /** {@inheritDoc} */
  public long create(String value, Connection conn) throws SQLException {
    String str = value;
    if (str.length() > 255) {
      throw new IllegalArgumentException(
          "maximum lenght of string attributes "
              + "is 255 characters. Use text attributes "
              + "wherever greater capacity is desired");
    }

    long id = getNextId();
    Statement stmt = conn.createStatement();
    try {
      stmt.execute(
          "INSERT INTO "
              + getTable()
              + "(data_key, data) VALUES ("
              + id
              + ", '"
              + escape(str)
              + "')");
      return id;
    } finally {
      DatabaseUtils.close(stmt);
    }
  }
Exemple #5
0
 private void runScript(String path) throws Exception {
   logger.info("running " + adapt(path));
   Reader scriptReader = fileSystem.getReader(adapt(path), "UTF-8");
   if (scriptReader == null) {
     throw new IOException("script " + path + " missing from classpath");
   }
   DatabaseUtils.runScript(dataSource, scriptReader);
 }
 /** {@inheritDoc} */
 public void preload(Connection conn) throws SQLException {
   Statement stmt = conn.createStatement();
   ResultSet rs = null;
   try {
     rs = stmt.executeQuery("SELECT max(data_key) from " + getTable());
     rs.next();
     int count = rs.getInt(1);
     cache = new String[count + 1];
     rs.close();
     rs = stmt.executeQuery("SELECT data_key, data from " + getTable());
     while (rs.next()) {
       cache[rs.getInt(1)] = unescape(rs.getString(2));
     }
   } finally {
     DatabaseUtils.close(rs);
     DatabaseUtils.close(stmt);
   }
 }
 /** {@inheritDoc} */
 public void update(long id, String value, Connection conn)
     throws EntityDoesNotExistException, SQLException {
   String str = value;
   if (str.length() > 255) {
     throw new IllegalArgumentException(
         "maximum lenght of string attributes "
             + "is 255 characters. Use text attributes "
             + "wherever greater capacity is desired");
   }
   if (cache != null && id < cache.length) {
     cache[(int) id] = str;
   }
   Statement stmt = conn.createStatement();
   try {
     checkExists(id, stmt);
     stmt.execute(
         "UPDATE " + getTable() + " SET data = '" + escape(str) + "' WHERE data_key = " + id);
   } finally {
     DatabaseUtils.close(stmt);
   }
 }
Exemple #8
0
 private boolean hasTable(String table) throws Exception {
   return DatabaseUtils.hasTable(dataSource, table);
 }