コード例 #1
0
 /** INTERNAL */
 public void init(
     Connection conn,
     String schemaName,
     String triggerName,
     String tableName,
     boolean before,
     int type)
     throws SQLException {
   this.schema = schemaName;
   this.table = tableName;
   this.indexPath = getIndexPath(conn);
   this.indexAccess = getIndexAccess(conn);
   ArrayList<String> keyList = New.arrayList();
   DatabaseMetaData meta = conn.getMetaData();
   ResultSet rs =
       meta.getColumns(
           null, escapeMetaDataPattern(schemaName), escapeMetaDataPattern(tableName), null);
   ArrayList<String> columnList = New.arrayList();
   while (rs.next()) {
     columnList.add(rs.getString("COLUMN_NAME"));
   }
   columnTypes = new int[columnList.size()];
   columns = new String[columnList.size()];
   columnList.toArray(columns);
   rs =
       meta.getColumns(
           null, escapeMetaDataPattern(schemaName), escapeMetaDataPattern(tableName), null);
   for (int i = 0; rs.next(); i++) {
     columnTypes[i] = rs.getInt("DATA_TYPE");
   }
   if (keyList.size() == 0) {
     rs = meta.getPrimaryKeys(null, escapeMetaDataPattern(schemaName), tableName);
     while (rs.next()) {
       keyList.add(rs.getString("COLUMN_NAME"));
     }
   }
   if (keyList.size() == 0) {
     throw throwException("No primary key for table " + tableName);
   }
   ArrayList<String> indexList = New.arrayList();
   PreparedStatement prep =
       conn.prepareStatement(
           "SELECT COLUMNS FROM " + SCHEMA + ".INDEXES WHERE SCHEMA=? AND TABLE=?");
   prep.setString(1, schemaName);
   prep.setString(2, tableName);
   rs = prep.executeQuery();
   if (rs.next()) {
     String cols = rs.getString(1);
     if (cols != null) {
       for (String s : StringUtils.arraySplit(cols, ',', true)) {
         indexList.add(s);
       }
     }
   }
   if (indexList.size() == 0) {
     indexList.addAll(columnList);
   }
   keys = new int[keyList.size()];
   setColumns(keys, keyList, columnList);
   indexColumns = new int[indexList.size()];
   setColumns(indexColumns, indexList, columnList);
 }
コード例 #2
0
 /**
  * Do the search.
  *
  * @param conn the database connection
  * @param text the query
  * @param limit the limit
  * @param offset the offset
  * @param data whether the raw data should be returned
  * @return the result set
  */
 protected static ResultSet search(
     Connection conn, String text, int limit, int offset, boolean data) throws SQLException {
   SimpleResultSet result = createResultSet(data);
   if (conn.getMetaData().getURL().startsWith("jdbc:columnlist:")) {
     // this is just to query the result set columns
     return result;
   }
   if (text == null || text.trim().length() == 0) {
     return result;
   }
   try {
     IndexAccess access = getIndexAccess(conn);
     /*## LUCENE2 ##
     access.modifier.flush();
     String path = getIndexPath(conn);
     IndexReader reader = IndexReader.open(path);
     Analyzer analyzer = new StandardAnalyzer();
     Searcher searcher = new IndexSearcher(reader);
     QueryParser parser = new QueryParser(LUCENE_FIELD_DATA, analyzer);
     Query query = parser.parse(text);
     Hits hits = searcher.search(query);
     int max = hits.length();
     if (limit == 0) {
         limit = max;
     }
     for (int i = 0; i < limit && i + offset < max; i++) {
         Document doc = hits.doc(i + offset);
         float score = hits.score(i + offset);
     //*/
     // ## LUCENE3 ##
     // take a reference as the searcher may change
     Searcher searcher = access.searcher;
     // reuse the same analyzer; it's thread-safe;
     // also allows subclasses to control the analyzer used.
     Analyzer analyzer = access.writer.getAnalyzer();
     QueryParser parser = new QueryParser(Version.LUCENE_30, LUCENE_FIELD_DATA, analyzer);
     Query query = parser.parse(text);
     // Lucene 3 insists on a hard limit and will not provide
     // a total hits value. Take at least 100 which is
     // an optimal limit for Lucene as any more
     // will trigger writing results to disk.
     int maxResults = (limit == 0 ? 100 : limit) + offset;
     TopDocs docs = searcher.search(query, maxResults);
     if (limit == 0) {
       limit = docs.totalHits;
     }
     for (int i = 0, len = docs.scoreDocs.length;
         i < limit && i + offset < docs.totalHits && i + offset < len;
         i++) {
       ScoreDoc sd = docs.scoreDocs[i + offset];
       Document doc = searcher.doc(sd.doc);
       float score = sd.score;
       // */
       String q = doc.get(LUCENE_FIELD_QUERY);
       if (data) {
         int idx = q.indexOf(" WHERE ");
         JdbcConnection c = (JdbcConnection) conn;
         Session session = (Session) c.getSession();
         Parser p = new Parser(session);
         String tab = q.substring(0, idx);
         ExpressionColumn expr = (ExpressionColumn) p.parseExpression(tab);
         String schemaName = expr.getOriginalTableAliasName();
         String tableName = expr.getColumnName();
         q = q.substring(idx + " WHERE ".length());
         Object[][] columnData = parseKey(conn, q);
         result.addRow(schemaName, tableName, columnData[0], columnData[1], score);
       } else {
         result.addRow(q, score);
       }
     }
     /*## LUCENE2 ##
     // TODO keep it open if possible
     reader.close();
     //*/
   } catch (Exception e) {
     throw convertException(e);
   }
   return result;
 }