<commit_before>/*
 * Copyright (c) 2009-2013 <NAME>
 */
package fi.tnie.db;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import fi.tnie.db.ent.Attribute;
import fi.tnie.db.ent.Entity;
import fi.tnie.db.ent.EntityMetaData;
import fi.tnie.db.expr.QueryExpression;
import fi.tnie.db.expr.TableReference;
import fi.tnie.db.meta.Column;
import fi.tnie.db.meta.ColumnMap;
import fi.tnie.db.types.ReferenceType;

public class ExtractorMap<
	A extends Attribute,
	R,
	T extends ReferenceType<T>,
	E extends Entity<A, R, T, E>
> {

	private List<AttributeExtractor<A, ?, ?, E, ?, ?, ?, ?>> attributeExctractorList;

	private static Logger logger = Logger.getLogger(ExtractorMap.class);


	/**
	 * TODO: when initialized like this, this does not read references properly
	 * @param rsmd
	 * @param em
	 * @throws SQLException
	 */
	public ExtractorMap(ResultSetMetaData rsmd,
			EntityMetaData<A, R, T, E> em,
			ValueExtractorFactory vef) throws SQLException {
		int cc = rsmd.getColumnCount();
		this.attributeExctractorList =
			new ArrayList<AttributeExtractor<A, ?, ?, E, ?, ?, ?, ?>>(cc);

		ColumnMap cm = em.getBaseTable().columnMap();

		for (int c = 1; c <= cc; c++) {
			String cl = rsmd.getColumnLabel(c);
			Column col = cm.get(cl);
			int t = col.getDataType().getDataType();

			AttributeExtractor<A, ?, ?, E, ?, ?, ?, ?> ae = createAttributeExtractor(t, c, col, em, vef);

			if (ae != null) {
				this.attributeExctractorList.add(ae);
			}

//			ValueExtractor<?, ?, ?> ve = vef.createExtractor(rsmd, c);
//
////			A a = em.getAttribute(col);
//
//			if (a != null) {
//				logger().info("attribute for column label: " + cl + " => " + a);
//				AttributeExtractor<?, ?, ?, A, E> ae = createAttributeExtractor(a, ve);
//				this.attributeExctractorList.add(ae);
//			}
//			else {
//				logger().warn("no attribute for column label: " + cl);
//				logger().warn("column: " + col);
//			}


//			Set<R> refs = em.getReferences(col);
//
//			for (R r : refs) {
//				ForeignKey fk = em.getForeignKey(r);
//			}
		}
	}

	public AttributeExtractor<A, ?, ?, E, ?, ?, ?, ?> createAttributeExtractor(int sqltype, int c, Column col, EntityMetaData<A, R, T, E> em, ValueExtractorFactory vef)
				throws SQLException {

		final A attribute = em.getAttribute(col);
		AttributeExtractor<A, ?, ?, E, ?, ?, ?, ?> ae = null;

		switch (sqltype) {
			case Types.INTEGER:
			case Types.SMALLINT:
			case Types.TINYINT:
				ae = new IntegerAttributeExtractor<A, R, T, E>(attribute, em, vef, c);
				break;
			case Types.VARCHAR:
//				e = new VarcharExtractor(col);
			case Types.CHAR:
//				e = new CharExtractor(col);
				break;
			case Types.DATE:
//				e = new DateExtractor(col);
				break;
			case Types.TIMESTAMP:
//				e = new TimestampExtractor(col);
				break;
			default:
				//
	//			e = new ObjectExtractor(colno);<commit_msg>Support more attribute extractors in ExtractorMap<commit_after>/*
 * Copyright (c) 2009-2013 <NAME>
 */
package fi.tnie.db;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import fi.tnie.db.ent.Attribute;
import fi.tnie.db.ent.Entity;
import fi.tnie.db.ent.EntityMetaData;
import fi.tnie.db.expr.QueryExpression;
import fi.tnie.db.expr.TableReference;
import fi.tnie.db.meta.Column;
import fi.tnie.db.meta.ColumnMap;
import fi.tnie.db.types.ReferenceType;

public class ExtractorMap<
	A extends Attribute,
	R,
	T extends ReferenceType<T>,
	E extends Entity<A, R, T, E>
> {

	private List<AttributeExtractor<A, ?, ?, E, ?, ?, ?, ?>> attributeExctractorList;

	private static Logger logger = Logger.getLogger(ExtractorMap.class);


	/**
	 * TODO: when initialized like this, this does not read references properly
	 * @param rsmd
	 * @param em
	 * @throws SQLException
	 */
	public ExtractorMap(ResultSetMetaData rsmd,
			EntityMetaData<A, R, T, E> em,
			ValueExtractorFactory vef) throws SQLException {
		int cc = rsmd.getColumnCount();
		this.attributeExctractorList =
			new ArrayList<AttributeExtractor<A, ?, ?, E, ?, ?, ?, ?>>(cc);

		ColumnMap cm = em.getBaseTable().columnMap();

		for (int c = 1; c <= cc; c++) {
			String cl = rsmd.getColumnLabel(c);
			Column col = cm.get(cl);
			int t = col.getDataType().getDataType();

			AttributeExtractor<A, ?, ?, E, ?, ?, ?, ?> ae = createAttributeExtractor(t, c, col, em, vef);

			if (ae != null) {
				this.attributeExctractorList.add(ae);
			}

//			ValueExtractor<?, ?, ?> ve = vef.createExtractor(rsmd, c);
//
////			A a = em.getAttribute(col);
//
//			if (a != null) {
//				logger().info("attribute for column label: " + cl + " => " + a);
//				AttributeExtractor<?, ?, ?, A, E> ae = createAttributeExtractor(a, ve);
//				this.attributeExctractorList.add(ae);
//			}
//			else {
//				logger().warn("no attribute for column label: " + cl);
//				logger().warn("column: " + col);
//			}


//			Set<R> refs = em.getReferences(col);
//
//			for (R r : refs) {
//				ForeignKey fk = em.getForeignKey(r);
//			}
		}
	}

	public AttributeExtractor<A, ?, ?, E, ?, ?, ?, ?> createAttributeExtractor(int sqltype, int c, Column col, EntityMetaData<A, R, T, E> em, ValueExtractorFactory vef)
				throws SQLException {

		final A attribute = em.getAttribute(col);
		AttributeExtractor<A, ?, ?, E, ?, ?, ?, ?> ae = null;

		switch (sqltype) {
			case Types.INTEGER:
			case Types.SMALLINT:
			case Types.TINYINT:
				ae = new IntegerAttributeExtractor<A, R, T, E>(a, em, vef, c);
				break;
			case Types.VARCHAR:
				ae = new VarcharAttributeExtractor<A, R, T, E>(a, em, vef, c);
				break;
			case Types.CHAR:
				ae = new CharAttributeExtractor<A, R, T, E>(a, em, vef, c);
				break;
			case Types.DATE:
				ae = new DateAttributeExtractor<A, R, T, E>(a, em, vef, c);
				break;
			case Types.TIMESTAMP:
				ae = new TimestampAttributeExtractor<A, R, T, E>(a, em, vef, c);				
				break;
			default:
				//
	//			e = new ObjectExtractor(colno);
{'fraction_non_alphanumeric': 0.16364822972752488, 'fraction_numerical': 0.0032631750693424702, 'mean_word_length': 3.3879742304939153, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7227220', 'n_tokens_mistral': 2503, 'n_tokens_neox': 2263, 'n_words': 515}