<commit_before>/**
 * Copyright 2017 Yahoo Inc.
 *
 * Licensed 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.
 */
package com.yahoo.athenz.zts.cert.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLTimeoutException;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yahoo.athenz.zts.ResourceException;
import com.yahoo.athenz.zts.cert.CertRecordStoreConnection;
import com.yahoo.athenz.zts.cert.X509CertRecord;

public class JDBCCertRecordStoreConnection implements CertRecordStoreConnection {

    private static final Logger LOG = LoggerFactory.getLogger(JDBCCertRecordStoreConnection.class);

    private static final String PREFIX = "ZTS-JDBCConnection: ";
    private static final int MYSQL_ER_OPTION_DUPLICATE_ENTRY = 1062;

    private static final String SQL_GET_X509_RECORD = "SELECT * FROM certificates WHERE provider=? AND instanceId=?;";
    private static final String SQL_INSERT_X509_RECORD = "INSERT INTO certificates " +
            "(provider, instanceId, service, currentSerial, currentTime, currentIP, prevSerial, prevTime, prevIP) " +
            "VALUES (?, ?,?,?,?,?,?,?,?);";
    private static final String SQL_UPDATE_X509_RECORD = "UPDATE certificates SET " +
            "currentSerial=?, currentTime=?, currentIP=?, prevSerial=?, prevTime=?, prevIP=? " +
            "WHERE provider=? AND instanceId=?;";
    private static final String SQL_DELETE_X509_RECORD = "DELETE from certificates " +
            "WHERE provider=? AND instanceId=?;";
    
    public static final String DB_COLUMN_SERVICE        = "service";
    public static final String DB_COLUMN_CURRENT_IP     = "currentIP";
    public static final String DB_COLUMN_CURRENT_SERIAL = "currentSerial";
    public static final String DB_COLUMN_CURRENT_TIME   = "currentTime";
    public static final String DB_COLUMN_PREV_IP        = "prevIP";
    public static final String DB_COLUMN_PREV_SERIAL    = "prevSerial";
    public static final String DB_COLUMN_PREV_TIME      = "prevTime";

    Connection con = null;
    int queryTimeout = 10;

    public JDBCCertRecordStoreConnection(Connection con) throws SQLException {
        this.con = con;
        con.setAutoCommit(true);
    }

    @Override
    public void setOperationTimeout(int queryTimeout) {
        this.queryTimeout = queryTimeout;
    }
    
    @Override
    public void close() {
        
        if (con == null) {
            return;
        }
        
        try {
            con.close();
            con = null;
        } catch (SQLException ex) {
            LOG.error(PREFIX + "close: state - " + ex.getSQLState() +
                    ", code - " + ex.getErrorCode() + ", message - " + ex.getMessage());
        }
    }
    
    int executeUpdate(PreparedStatement ps, String caller) throws SQLException {
        if (LOG.isDebugEnabled()) {
            LOG.debug(caller + ": " + ps.toString());
        }
        ps.setQueryTimeout(queryTimeout);
        return ps.executeUpdate();
    }

    ResultSet executeQuery(PreparedStatement ps, String caller) throws SQLException {
        if (LOG.isDebugEnabled()) {
            LOG.debug(caller + ": " + ps.toString());
        }
        ps.setQueryTimeout(queryTimeout);
        return ps.executeQuery();
    }
    
    @Override
    public X509CertRecord getX509CertRecord(String provider, String instanceId) {
        
        final String caller = "getX509CertRecord";

        X509CertRecord certRecord = null;
        try (PreparedStatement ps = con.prepareStatement(SQL_GET_X509_RECORD)) {
            ps.setString(1, instanceId);
            
            try (ResultSet rs = executeQuery(ps, caller)) {
                if (rs.next()) {
                    certRecord = new X509CertRecord();
                    certRecord.setProvider(provider);
                    certRecord.setInstanceId(instanceId);
                    certRecord.setService(rs.getString(DB_COLUMN_SERVICE));
                    certRecord.setCurrentIP(rs.getString(DB_COLUMN_CURRENT_IP));
                    certRecord.setCurrentSerial(rs.getString(DB_COLUMN_CURRENT_SERIAL));
                    certRecord.setCurrentTime(new Date(rs.getTimestamp(DB_COLUMN_CURRENT_TIME).getTime()));
                    certRecord.setPrevIP(rs.getString(DB_COLUMN_PREV_IP));
                    certRecord.setPrevSerial(rs.getString(DB_COLUMN_PREV_SERIAL));
                    certRecord.setPrevTime(new Date(rs.getTimestamp(DB_COLUMN_PREV_TIME).getTime()));
                }
            }
        } catch (SQLException ex) {
            throw sqlError(ex, caller);
        }
        return certRecord;
    }
    <commit_msg>Correct mysql get x509 cert record arguments<commit_after>/**
 * Copyright 2017 Yahoo Inc.
 *
 * Licensed 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.
 */
package com.yahoo.athenz.zts.cert.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLTimeoutException;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yahoo.athenz.zts.ResourceException;
import com.yahoo.athenz.zts.cert.CertRecordStoreConnection;
import com.yahoo.athenz.zts.cert.X509CertRecord;

public class JDBCCertRecordStoreConnection implements CertRecordStoreConnection {

    private static final Logger LOG = LoggerFactory.getLogger(JDBCCertRecordStoreConnection.class);

    private static final String PREFIX = "ZTS-JDBCConnection: ";
    private static final int MYSQL_ER_OPTION_DUPLICATE_ENTRY = 1062;

    private static final String SQL_GET_X509_RECORD = "SELECT * FROM certificates WHERE provider=? AND instanceId=?;";
    private static final String SQL_INSERT_X509_RECORD = "INSERT INTO certificates " +
            "(provider, instanceId, service, currentSerial, currentTime, currentIP, prevSerial, prevTime, prevIP) " +
            "VALUES (?, ?,?,?,?,?,?,?,?);";
    private static final String SQL_UPDATE_X509_RECORD = "UPDATE certificates SET " +
            "currentSerial=?, currentTime=?, currentIP=?, prevSerial=?, prevTime=?, prevIP=? " +
            "WHERE provider=? AND instanceId=?;";
    private static final String SQL_DELETE_X509_RECORD = "DELETE from certificates " +
            "WHERE provider=? AND instanceId=?;";
    
    public static final String DB_COLUMN_SERVICE        = "service";
    public static final String DB_COLUMN_CURRENT_IP     = "currentIP";
    public static final String DB_COLUMN_CURRENT_SERIAL = "currentSerial";
    public static final String DB_COLUMN_CURRENT_TIME   = "currentTime";
    public static final String DB_COLUMN_PREV_IP        = "prevIP";
    public static final String DB_COLUMN_PREV_SERIAL    = "prevSerial";
    public static final String DB_COLUMN_PREV_TIME      = "prevTime";

    Connection con = null;
    int queryTimeout = 10;

    public JDBCCertRecordStoreConnection(Connection con) throws SQLException {
        this.con = con;
        con.setAutoCommit(true);
    }

    @Override
    public void setOperationTimeout(int queryTimeout) {
        this.queryTimeout = queryTimeout;
    }
    
    @Override
    public void close() {
        
        if (con == null) {
            return;
        }
        
        try {
            con.close();
            con = null;
        } catch (SQLException ex) {
            LOG.error(PREFIX + "close: state - " + ex.getSQLState() +
                    ", code - " + ex.getErrorCode() + ", message - " + ex.getMessage());
        }
    }
    
    int executeUpdate(PreparedStatement ps, String caller) throws SQLException {
        if (LOG.isDebugEnabled()) {
            LOG.debug(caller + ": " + ps.toString());
        }
        ps.setQueryTimeout(queryTimeout);
        return ps.executeUpdate();
    }

    ResultSet executeQuery(PreparedStatement ps, String caller) throws SQLException {
        if (LOG.isDebugEnabled()) {
            LOG.debug(caller + ": " + ps.toString());
        }
        ps.setQueryTimeout(queryTimeout);
        return ps.executeQuery();
    }
    
    @Override
    public X509CertRecord getX509CertRecord(String provider, String instanceId) {
        
        final String caller = "getX509CertRecord";

        X509CertRecord certRecord = null;
        try (PreparedStatement ps = con.prepareStatement(SQL_GET_X509_RECORD)) {
            ps.setString(1, provider);
            ps.setString(2, instanceId);
            
            try (ResultSet rs = executeQuery(ps, caller)) {
                if (rs.next()) {
                    certRecord = new X509CertRecord();
                    certRecord.setProvider(provider);
                    certRecord.setInstanceId(instanceId);
                    certRecord.setService(rs.getString(DB_COLUMN_SERVICE));
                    certRecord.setCurrentIP(rs.getString(DB_COLUMN_CURRENT_IP));
                    certRecord.setCurrentSerial(rs.getString(DB_COLUMN_CURRENT_SERIAL));
                    certRecord.setCurrentTime(new Date(rs.getTimestamp(DB_COLUMN_CURRENT_TIME).getTime()));
                    certRecord.setPrevIP(rs.getString(DB_COLUMN_PREV_IP));
                    certRecord.setPrevSerial(rs.getString(DB_COLUMN_PREV_SERIAL));
                    certRecord.setPrevTime(new Date(rs.getTimestamp(DB_COLUMN_PREV_TIME).getTime()));
                }
            }
        } catch (SQLException ex) {
            throw sqlError(ex, caller);
        }
        return certRecord;
    }
    
{'fraction_non_alphanumeric': 0.09381521532915955, 'fraction_numerical': 0.009865300701954089, 'mean_word_length': 2.714940098661029, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4632085', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2912, 'n_words': 714}