Github user sansanichfb commented on a diff in the pull request: https://github.com/apache/incubator-hawq/pull/1353#discussion_r182614235 --- Diff: pxf/pxf-jdbc/src/main/java/org/apache/hawq/pxf/plugins/jdbc/JdbcAccessor.java --- @@ -0,0 +1,469 @@ +package org.apache.hawq.pxf.plugins.jdbc; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.hawq.pxf.api.OneRow; +import org.apache.hawq.pxf.api.OneField; +import org.apache.hawq.pxf.api.ReadAccessor; +import org.apache.hawq.pxf.api.WriteAccessor; +import org.apache.hawq.pxf.api.io.DataType; +import org.apache.hawq.pxf.api.UserDataException; +import org.apache.hawq.pxf.api.utilities.ColumnDescriptor; +import org.apache.hawq.pxf.api.utilities.InputData; + +import java.util.List; +import java.io.IOException; +import java.text.ParseException; +import java.math.BigDecimal; +import java.sql.Types; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.SQLTimeoutException; +import java.sql.Statement; +import java.sql.Timestamp; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * JDBC tables accessor + * + * The SELECT queries are processed by {@link java.sql.Statement} + * + * The INSERT queries are processed by {@link java.sql.PreparedStatement} and + * built-in JDBC batches of arbitrary size + */ +public class JdbcAccessor extends JdbcPlugin implements ReadAccessor, WriteAccessor { + /** + * Class constructor + */ + public JdbcAccessor(InputData inputData) throws UserDataException { + super(inputData); + } + + /** + * openForRead() implementation + * Create query, open JDBC connection, execute query and store the result into resultSet + * + * @throws SQLException if a database access error occurs + * @throws SQLTimeoutException if a problem with the connection occurs + * @throws ParseException if th SQL statement provided in PXF InputData is incorrect + * @throws ClassNotFoundException if the superclass implementation disappeared + */ + @Override + public boolean openForRead() throws SQLException, SQLTimeoutException, ParseException, ClassNotFoundException { + if (statementRead != null && !statementRead.isClosed()) { + return true; + } + + super.openConnection(); + + queryRead = buildSelectQuery(); + statementRead = dbConn.createStatement(); + resultSetRead = statementRead.executeQuery(queryRead); + + return true; + } + + /** + * readNextObject() implementation + * Retreive the next tuple from resultSet and return it + * + * @throws SQLException if a problem in resultSet occurs + */ + @Override + public OneRow readNextObject() throws SQLException { + if (resultSetRead.next()) { + return new OneRow(resultSetRead); + } + return null; + } + + /** + * closeForRead() implementation + * + * @throws SQLException if a database access error occurs + */ + @Override + public void closeForRead() throws SQLException { + if (statementRead != null && !statementRead.isClosed()) { + statementRead.close(); + statementRead = null; + } + super.closeConnection(); + } + + /** + * openForWrite() implementation + * Create query template and open JDBC connection + * + * @throws SQLException if a database access error occurs + * @throws SQLTimeoutException if a problem with the connection occurs + * @throws ParseException if the SQL statement provided in PXF InputData is incorrect + * @throws ClassNotFoundException if the superclass implementation has disappeared + */ + @Override + public boolean openForWrite() throws SQLException, SQLTimeoutException, ParseException, ClassNotFoundException { + if (statementWrite != null && !statementWrite.isClosed()) { + return true; + } + + super.openConnection(); + if (dbMeta.supportsTransactions()) { + dbConn.setAutoCommit(false); + } + + queryWrite = buildInsertQuery(); + statementWrite = dbConn.prepareStatement(queryWrite); + + if ((batchSize != 0) && (!dbMeta.supportsBatchUpdates())) { + LOG.info( --- End diff -- IMO warning is more appropriate here since the user had an expectation and explicitly set batch set to non-zero.
---