Repository: hive
Updated Branches:
  refs/heads/branch-3 6a3d42b3b -> ca5e241c2


HIVE-20444: Parameter is not properly quoted in 
DbNotificationListener.addWriteNotificationLog (Daniel Dai, reviewed by mahesh 
kumar behera, Sankar Hariappan)


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/ca5e241c
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/ca5e241c
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/ca5e241c

Branch: refs/heads/branch-3
Commit: ca5e241c22aa77755d00624248e1f0bd9f12ef1e
Parents: 6a3d42b
Author: Daniel Dai <dai...@gmail.com>
Authored: Fri Sep 21 14:44:19 2018 -0700
Committer: Daniel Dai <dai...@gmail.com>
Committed: Fri Sep 21 14:44:19 2018 -0700

----------------------------------------------------------------------
 .../listener/DbNotificationListener.java        | 57 +++++++++++++-------
 1 file changed, 39 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/ca5e241c/hcatalog/server-extensions/src/main/java/org/apache/hive/hcatalog/listener/DbNotificationListener.java
----------------------------------------------------------------------
diff --git 
a/hcatalog/server-extensions/src/main/java/org/apache/hive/hcatalog/listener/DbNotificationListener.java
 
b/hcatalog/server-extensions/src/main/java/org/apache/hive/hcatalog/listener/DbNotificationListener.java
index 2ab59d7..909ed56 100644
--- 
a/hcatalog/server-extensions/src/main/java/org/apache/hive/hcatalog/listener/DbNotificationListener.java
+++ 
b/hcatalog/server-extensions/src/main/java/org/apache/hive/hcatalog/listener/DbNotificationListener.java
@@ -19,6 +19,7 @@ package org.apache.hive.hcatalog.listener;
 
 import java.io.IOException;
 import java.sql.Connection;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
@@ -728,7 +729,8 @@ public class DbNotificationListener extends 
TransactionalMetaStoreEventListener
     LOG.debug("DbNotificationListener: adding write notification log for : 
{}", event.getMessage());
     assert ((dbConn != null) && (sqlGenerator != null));
 
-    Statement stmt =null;
+    Statement stmt = null;
+    PreparedStatement pst = null;
     ResultSet rs = null;
     String dbName = acidWriteEvent.getDatabase();
     String tblName = acidWriteEvent.getTable();
@@ -754,16 +756,26 @@ public class DbNotificationListener extends 
TransactionalMetaStoreEventListener
         // if rs is empty then no lock is taken and thus it can not cause 
deadlock.
         long nextNLId = getNextNLId(stmt, sqlGenerator,
                 
"org.apache.hadoop.hive.metastore.model.MTxnWriteNotificationLog");
-        s = "insert into \"TXN_WRITE_NOTIFICATION_LOG\" (\"WNL_ID\", 
\"WNL_TXNID\", \"WNL_WRITEID\"," +
-                " \"WNL_DATABASE\", \"WNL_TABLE\"," +
-                " \"WNL_PARTITION\", \"WNL_TABLE_OBJ\", \"WNL_PARTITION_OBJ\", 
\"WNL_FILES\", \"WNL_EVENT_TIME\")" +
-                " values (" + nextNLId
-                + "," + acidWriteEvent.getTxnId() +  "," + 
acidWriteEvent.getWriteId()+  "," +
-                quoteString(dbName)+  "," +  quoteString(tblName)+  "," + 
quoteString(partition)+  "," +
-                quoteString(tableObj)+  "," + quoteString(partitionObj) +  "," 
+  quoteString(files)+
-                "," +  now() + ")";
-        LOG.info("Going to execute insert <" + s + ">");
-        stmt.execute(sqlGenerator.addEscapeCharacters(s));
+        s = "insert into \"TXN_WRITE_NOTIFICATION_LOG\" " +
+                "(\"WNL_ID\", \"WNL_TXNID\", \"WNL_WRITEID\", 
\"WNL_DATABASE\", \"WNL_TABLE\", " +
+                "\"WNL_PARTITION\", \"WNL_TABLE_OBJ\", \"WNL_PARTITION_OBJ\", 
" +
+                "\"WNL_FILES\", \"WNL_EVENT_TIME\") VALUES 
(?,?,?,?,?,?,?,?,?,?)";
+        int currentTime = now();
+        pst = dbConn.prepareStatement(sqlGenerator.addEscapeCharacters(s));
+        pst.setLong(1, nextNLId);
+        pst.setLong(2, acidWriteEvent.getTxnId());
+        pst.setLong(3, acidWriteEvent.getWriteId());
+        pst.setString(4, dbName);
+        pst.setString(5, tblName);
+        pst.setString(6, partition);
+        pst.setString(7, tableObj);
+        pst.setString(8, partitionObj);
+        pst.setString(9, files);
+        pst.setInt(10, currentTime);
+        LOG.info("Going to execute insert <" + s.replaceAll("\\?", "{}") + 
">", nextNLId
+                , acidWriteEvent.getTxnId(), acidWriteEvent.getWriteId(), 
quoteString(dbName), quoteString(tblName),
+                quoteString(partition), quoteString(tableObj), 
quoteString(partitionObj), quoteString(files), currentTime);
+        pst.execute();
       } else {
         String existingFiles = rs.getString(1);
         if (existingFiles.contains(sqlGenerator.addEscapeCharacters(files))) {
@@ -773,20 +785,29 @@ public class DbNotificationListener extends 
TransactionalMetaStoreEventListener
           return;
         }
         long nlId = rs.getLong(2);
+        int currentTime = now();
         files = ReplChangeManager.joinWithSeparator(Lists.newArrayList(files, 
existingFiles));
-        s = "update \"TXN_WRITE_NOTIFICATION_LOG\" set \"WNL_TABLE_OBJ\" = " + 
 quoteString(tableObj) + "," +
-                " \"WNL_PARTITION_OBJ\" = " + quoteString(partitionObj) + "," +
-                " \"WNL_FILES\" = " + quoteString(files) + "," +
-                " \"WNL_EVENT_TIME\" = " + now() +
-                " where \"WNL_ID\" = " + nlId;
-        LOG.info("Going to execute update <" + s + ">");
-        stmt.executeUpdate(sqlGenerator.addEscapeCharacters(s));
+        s = "update \"TXN_WRITE_NOTIFICATION_LOG\" set \"WNL_TABLE_OBJ\" = ? 
," +
+                " \"WNL_PARTITION_OBJ\" = ? ," +
+                " \"WNL_FILES\" = ? ," +
+                " \"WNL_EVENT_TIME\" = ?" +
+                " where \"WNL_ID\" = ?";
+        pst = dbConn.prepareStatement(sqlGenerator.addEscapeCharacters(s));
+        pst.setString(1, tableObj);
+        pst.setString(2, partitionObj);
+        pst.setString(3, files);
+        pst.setInt(4, currentTime);
+        pst.setLong(5, nlId);
+        LOG.info("Going to execute update <" + s.replaceAll("\\?", "{}") + 
">", quoteString(tableObj),
+                quoteString(partitionObj), quoteString(files), currentTime, 
nlId);
+        pst.executeUpdate();
       }
     } catch (SQLException e) {
       LOG.warn("failed to add write notification log" + e.getMessage());
       throw e;
     } finally {
       closeStmt(stmt);
+      closeStmt(pst);
       close(rs);
     }
   }

Reply via email to