Search in sources :

Example 1 with OracleCallableStatement

use of oracle.jdbc.OracleCallableStatement in project translationstudio8 by heartsome.

the class TMDatabaseImpl method insertTextData.

/**
	 * 插入数据到TEXTDATA表,更新LANGS表和MATRIX_LANG表
	 * @param params
	 * @return 插入TEXTDATA表记录的ID
	 * @throws SQLException
	 */
public String insertTextData(String type, String groupId, String hash, String pureText, String content, String lang, String preContext, String nextContext) throws SQLException {
    /*
		 * 步骤 1.添加记录到TEXTDATA表 2.查看LANGS表是否有刚才添加的语言,没有则需要增加一条记录,然后需要创建相对应的matrix表 3.添加记录到Matrix表
		 */
    String textDataId = null;
    PreparedStatement stmt = null;
    try {
        String langCode = Utils.langToCode(lang).toUpperCase();
        String sql = dbConfig.getOperateDbSQL("get-lang-bycode");
        if (!langCaches.contains(lang)) {
            Map<Integer, Map<String, String>> langRs = query(sql, new Object[] { lang });
            if (langRs.size() == 0) {
                // 说明对应的Matrix表还没有创建
                sql = dbConfig.getOperateDbSQL("insert-lang");
                stmt = conn.prepareStatement(sql);
                stmt.setString(1, lang);
                stmt.executeUpdate();
                stmt.close();
                // 创建表
                List<String> createMatrixTables = dbConfig.getCreateMatrixTables();
                for (String i : createMatrixTables) {
                    i = i.replaceAll("__LANG__", langCode);
                    stmt = conn.prepareStatement(i);
                    stmt.execute();
                    stmt.close();
                }
                // 创建索引
                List<String> createMatrixIndex = dbConfig.getCreateMatrixIndexes();
                for (String i : createMatrixIndex) {
                    i = i.replaceAll("__LANG__", langCode);
                    stmt = conn.prepareStatement(i);
                    stmt.execute();
                    stmt.close();
                }
                langCaches.add(lang);
            }
        }
        if (pureText != null && lang != null && content != null) {
            sql = dbConfig.getOperateDbSQL("insert-textdata");
            OracleCallableStatement callStmt = (OracleCallableStatement) conn.prepareCall(sql);
            int i = 1;
            callStmt.setString(i++, type);
            callStmt.setInt(i++, Integer.parseInt(groupId));
            callStmt.setInt(i++, Integer.parseInt(hash));
            callStmt.setStringForClob(i++, pureText);
            callStmt.setStringForClob(i++, content);
            callStmt.setString(i++, lang);
            callStmt.setString(i++, preContext);
            callStmt.setString(i++, nextContext);
            callStmt.registerOutParameter(i++, Types.INTEGER);
            callStmt.execute();
            textDataId = callStmt.getString(i - 1);
            callStmt.close();
            int[] ngrams = generateNgrams(lang, pureText);
            if (ngrams.length > 0) {
                String insertMatrix = dbConfig.getMatrixSQL("insert");
                insertMatrix = insertMatrix.replaceAll("__LANG__", langCode);
                stmt = conn.prepareStatement(insertMatrix);
                for (int j = 0; j < ngrams.length; j++) {
                    stmt.setInt(1, Integer.parseInt(textDataId));
                    stmt.setInt(2, ngrams[j]);
                    stmt.setShort(3, (short) ngrams.length);
                    stmt.addBatch();
                }
                stmt.executeBatch();
                stmt.close();
            }
        }
    } finally {
        if (stmt != null) {
            stmt.close();
        }
    }
    return textDataId;
}
Also used : OraclePreparedStatement(oracle.jdbc.OraclePreparedStatement) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map) OracleCallableStatement(oracle.jdbc.OracleCallableStatement)

Aggregations

PreparedStatement (java.sql.PreparedStatement)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OracleCallableStatement (oracle.jdbc.OracleCallableStatement)1 OraclePreparedStatement (oracle.jdbc.OraclePreparedStatement)1