<commit_before>/*
 * Copyright (C) 2004-2005  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package com.exedio.cope;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

final class StringColumn extends Column
{
	static final int JDBC_TYPE = Types.VARCHAR;
	
	final int minimumLength;
	final int maximumLength;
	final String[] allowedValues;

	StringColumn(
			final Table table, final String id, final boolean notNull,
			final int minimumLength, final int maximumLength)
	{
		super(table, id, false, notNull, JDBC_TYPE);
		this.minimumLength = minimumLength;
		this.maximumLength = maximumLength;
		this.allowedValues = null;
	}
	
	StringColumn(
			final Table table, final String id, final boolean notNull,
			final String[] allowedValues)
	{
		super(table, id, false, notNull, JDBC_TYPE);
		this.minimumLength = 0;
		this.maximumLength = Integer.MAX_VALUE;
		this.allowedValues = allowedValues;

		if(allowedValues.length<2)
			throw new RuntimeException(id);
		for(int i = 0; i<allowedValues.length; i++)
			allowedValues[i] = allowedValues[i].intern();
	}
	
	final String getDatabaseType()
	{
		return table.database.getStringType(maximumLength);
	}

	final String getCheckConstraintIfNotNull()
	{
		final StringBuffer bf = new StringBuffer();

		if(allowedValues!=null)
		{
			bf.append(protectedID + " IN (");

			for(int j = 0; j<allowedValues.length; j++)
			{
				if(j>0)
					bf.append(',');

				bf.append('\'').
					append(allowedValues[j]).
					append('\'');
			}
			bf.append(')');
			return bf.toString();
		}
		
		if(minimumLength>0)
		{
			if(maximumLength!=Integer.MAX_VALUE)
				bf.append("(LENGTH(" + protectedID + ")>=" + minimumLength + ") AND (LENGTH(" + protectedID + ")<=" + maximumLength + ')');
			else
				bf.append("LENGTH(" + protectedID + ")>=" + minimumLength);
		}
		else
		{
			if(maximumLength!=Integer.MAX_VALUE)
				bf.append("LENGTH(" + protectedID + ")<=" + maximumLength);
		}<commit_msg>TODO: short syntax, if minimumLength==maximumLength<commit_after>/*
 * Copyright (C) 2004-2005  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package com.exedio.cope;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

final class StringColumn extends Column
{
	static final int JDBC_TYPE = Types.VARCHAR;
	
	final int minimumLength;
	final int maximumLength;
	final String[] allowedValues;

	StringColumn(
			final Table table, final String id, final boolean notNull,
			final int minimumLength, final int maximumLength)
	{
		super(table, id, false, notNull, JDBC_TYPE);
		this.minimumLength = minimumLength;
		this.maximumLength = maximumLength;
		this.allowedValues = null;
	}
	
	StringColumn(
			final Table table, final String id, final boolean notNull,
			final String[] allowedValues)
	{
		super(table, id, false, notNull, JDBC_TYPE);
		this.minimumLength = 0;
		this.maximumLength = Integer.MAX_VALUE;
		this.allowedValues = allowedValues;

		if(allowedValues.length<2)
			throw new RuntimeException(id);
		for(int i = 0; i<allowedValues.length; i++)
			allowedValues[i] = allowedValues[i].intern();
	}
	
	final String getDatabaseType()
	{
		return table.database.getStringType(maximumLength);
	}

	final String getCheckConstraintIfNotNull()
	{
		final StringBuffer bf = new StringBuffer();

		if(allowedValues!=null)
		{
			bf.append(protectedID + " IN (");

			for(int j = 0; j<allowedValues.length; j++)
			{
				if(j>0)
					bf.append(',');

				bf.append('\'').
					append(allowedValues[j]).
					append('\'');
			}
			bf.append(')');
			return bf.toString();
		}
		
		if(minimumLength>0)
		{
			if(maximumLength!=Integer.MAX_VALUE)
			{
				// TODO: short syntax, if minimumLength==maximumLength
				bf.append("(LENGTH(" + protectedID + ")>=" + minimumLength + ") AND (LENGTH(" + protectedID + ")<=" + maximumLength + ')');
			}
			else
				bf.append("LENGTH(" + protectedID + ")>=" + minimumLength);
		}
		else
		{
			if(maximumLength!=Integer.MAX_VALUE)
				bf.append("LENGTH(" + protectedID + ")<=" + maximumLength);
		}
{'fraction_non_alphanumeric': 0.11494252873563218, 'fraction_numerical': 0.010946907498631636, 'mean_word_length': 3.9077887197851386, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '120001', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1632, 'n_words': 527}