/*
* Copyright 1999-2015 dangdang.com.
*
* 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 io.shardingjdbc.dbtest.common;
import static org.junit.Assert.assertTrue;
import java.sql.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.shardingjdbc.dbtest.config.bean.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import io.shardingjdbc.dbtest.exception.DbTestException;
import org.junit.Assert;
public class DatabaseUtil {
/**
* Generating sql.
*
* @param table table
* @param config Map column,data
* @return sql
*/
public static String analyzeSql(final String table, final Map config) {
List colsConfigs = new ArrayList<>();
List valueConfigs = new ArrayList<>();
for (Map.Entry stringStringEntry : config.entrySet()) {
colsConfigs.add(stringStringEntry.getKey());
valueConfigs.add("?");
}
StringBuilder sbsql = new StringBuilder("insert into ");
sbsql.append(table);
sbsql.append(" ( ");
sbsql.append(StringUtils.join(colsConfigs, ","));
sbsql.append(" )");
sbsql.append(" values ");
sbsql.append(" ( ");
sbsql.append(StringUtils.join(valueConfigs, ","));
sbsql.append(" )");
return sbsql.toString();
}
/**
* Insert initialization data.
*
* @param conn Jdbc connection
* @param sql sql
* @param datas init data
* @param config Table field type
* @return Success or failure
* @throws SQLException SQL executes exceptions
* @throws ParseException Precompiled anomaly
*/
public static boolean insertUsePreparedStatement(final Connection conn, final String sql,
final List