All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jp.co.tis.gsp.tools.dba.CsvInsertHandler Maven / Gradle / Ivy

Go to download

To automate the routine work of the DBA, it is a tool to be able to concentrate on the data modeling work.

There is a newer version: 5.0.0
Show newest version
/*
 * Copyright (C) 2015 coastland
 *
 * 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 jp.co.tis.gsp.tools.dba;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.co.tis.gsp.tools.dba.dialect.Dialect;

import org.apache.commons.lang.StringUtils;
import org.seasar.framework.util.tiger.CollectionsUtil;


public class CsvInsertHandler {
	private static final Map TYPE_NAMES = new HashMap();
	private final Connection conn;
	private final List columns;
	private final List types;
	private PreparedStatement stmt;
	private final String schema;
	private final String tableName;
	private final Dialect dialect;

	static {
		TYPE_NAMES.put("VARCHAR", Types.VARCHAR);
		TYPE_NAMES.put("DATE", Types.DATE);
		TYPE_NAMES.put("TIMESTAMP", Types.TIMESTAMP);
		TYPE_NAMES.put("ARRAY", Types.ARRAY);
	}
	
	public CsvInsertHandler(Connection conn, Dialect dialect, String schema, String tableName, String[] headers) throws SQLException {
		this.conn = conn;
		this.schema = schema;
		this.tableName = tableName;
		this.columns = CollectionsUtil.newArrayList(headers.length);
		this.types   = CollectionsUtil.newArrayList(headers.length);
		this.dialect = dialect;
		initialize(headers);
	}

	public void prepare() throws SQLException {
		StringBuilder sb = new StringBuilder("INSERT INTO ");
		sb.append(schema).append(".").append(tableName).append("(")
			.append(StringUtils.join(columns, ','))
			.append(") VALUES (");
		for(int i=0; i= 0) {
				String[] columnTokens = header.split(":",2);
				columns.add(columnTokens[0]);
				types.add(getTypeByName(columnTokens[1]));
			} else {
				columns.add(header);
				types.add(dialect.guessType(conn, schema, tableName, header));
			}
		}
		return null;
	}

	private int getTypeByName(String typeName) {
		Integer type = TYPE_NAMES.get(typeName);
		if (type == null) {
			return Types.VARCHAR;
		}
		return type;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy