<commit_before>//$HeadURL$
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2011 by:
 - Department of Geography, University of Bonn -
 and
 - lat/lon GmbH -

 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

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. <NAME>
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 e-mail: <EMAIL>
 ----------------------------------------------------------------------------*/
package org.deegree.commons.jdbc;

/**
 * An SQL identifier (e.g. a table, column or sequence name) with optional qualifier and optional escaping.
 * <p>
 * <h4>Qualification</h4>
 * Each identifier consists of a name part and an optional qualifier part and has the syntax
 * <code>(qualifier.)* name</code>. Examples:
 * <ul>
 * <li><code>mytable</code></li>
 * <li><code>myschema.mytable</code></li>
 * <li><code>mycolumn</code></li>
 * <li><code>mytable.mycolumn</code></li>
 * <li><code>myschema.mytable.mycolumn</code></li>
 * </ul>
 * <h4>Escaping</h4>
 * Escaping an identifier has two effects (TODO verify on all DBMS): it allows to use normally reserved characters/words
 * and tells the DBMS to treat the identifier in a case-sensitive manner. However, different DBMSs use different ways to
 * mark an escaped identifier, e.g. for table names:
 * <ul>
 * <li>PostgreSQL uses quotes: "escaped table name"</li>
 * <li>Oracle uses quotes: "escaped table name" (needs verification)</li>
 * <li>SQL Server uses brackets: [escaped table name] (needs verification)</li>
 * <li>MySQL uses ticks: `escaped table name` (needs verification)</li>
 * </ul>
 * </p>
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author last edited by: $Author$
 * 
 * @version $Revision$, $Date$
 */
public class SQLIdentifier implements Comparable<SQLIdentifier> {

    private boolean isEscaped;

    private final String qualifier;

    private final String name;

    private char escapeStartChar;

    private char escapeEndChar;

    private String normalizedString;

    /**
     * Creates a new {@link SQLIdentifier} instance from a literal identifier, which may use a qualifier and/or be
     * surrounded by escaping characters.
     * <p>
     * Checked escaping characters include:
     * <ul>
     * <li>Quotes: <code>"</code> (used by PostgreSQL and Oracle)</li>
     * <li>Brackets: <code>[</code> and <code>]</code> (used by MS SQL Server)</li>
     * <li>Backticks: <code>`</code> (used by MySQL)</li>
     * </ul>
     * </p>
     * 
     * @param identifier
     *            literal identifier, must not be <code>null</code>
     */
    public SQLIdentifier( String identifier ) throws IllegalArgumentException {
        if ( identifier == null || identifier.isEmpty() ) {
            throw new IllegalArgumentException( "An SQL identifier can not be empty." );
        }
        String s = identifier;
        if ( identifier.length() > 1 ) {
            char firstChar = identifier.charAt( 0 );
            char lastChar = identifier.charAt( identifier.length() - 1 );
            switch ( firstChar ) {
            case '"': {
                if ( lastChar != '"' ) {
                    throw new IllegalArgumentException( "SQL identifier (=" + identifier
                                                        + ") starts with a quote, but doesn't end with a quote." );
                }
                isEscaped = true;
                escapeStartChar = '"';
                escapeEndChar = '"';
                s = identifier.substring( 1, identifier.length() - 1 );
                break;
            }
            case '[': {
                if ( lastChar != '"' ) {
                    throw new IllegalArgumentException( "SQL identifier (=" + identifier
                                                        + ") starts with a bracket, but doesn't end with a bracket." );
                }
                isEscaped = true;
                escapeStartChar = '[';
                escapeEndChar = ']';
                s = identifier.substring( 1, identifier.length() - 1 );
                break;
            }
            case '`': {
                if ( lastChar != '`' ) {
                    throw new IllegalArgumentException( "SQL identifier (=" + identifier
                                                        + ") starts with a backtick, but doesn't end with a backtick." );
                }
                isEscaped = true;
                escapeStartChar = '`';
                escapeEndChar = '`';
                s = identifier.substring( 1, identifier.length() - 1 );
                break;
            }
            default: {
                isEscaped = false;
                s = identifier;
            }
            }
        }

        int pos = s.lastIndexOf( '.' );
        if ( pos >= 0 ) {
            qualifier = s.substring( 0, pos );
            name = s.substring( pos + 1, s.length() );
        } else {
            qualifier = null;
            name = s;
        }

        StringBuilder sb = new StringBuilder();
        if ( isEscaped ) {
            sb.append( escapeStartChar );
        }
        if ( qualifier != null ) {
            if ( isEscaped ) {
                sb.append( qualifier.toUpperCase() );
            } else {
                sb.append( qualifier );
            }
            sb.append( "." );
        }
        if ( isEscaped ) {
            sb.append( name.toUpperCase() );
        } else {
            sb.append( name );
        }
        if ( isEscaped ) {
            sb.append( escapeEndChar );
        }
        normalizedString = sb.toString();
    }

    protected SQLIdentifier( String table, String schema ) {
        this.name = table;
        this.qualifier = schema;
        this.isEscaped = false;
    }

    /**
     * Returns the qualifier part of this identifier.
     * 
     * @return qualifier, can be <code>null</code> (unqualified)
     */
    public String getQualifier() {
        return qualifier;
    }
<commit_msg>Fixed test regression.<commit_after>//$HeadURL$
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2011 by:
 - Department of Geography, University of Bonn -
 and
 - lat/lon GmbH -

 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

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. <NAME>
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 e-mail: <EMAIL>
 ----------------------------------------------------------------------------*/
package org.deegree.commons.jdbc;

/**
 * An SQL identifier (e.g. a table, column or sequence name) with optional qualifier and optional escaping.
 * <p>
 * <h4>Qualification</h4>
 * Each identifier consists of a name part and an optional qualifier part and has the syntax
 * <code>(qualifier.)* name</code>. Examples:
 * <ul>
 * <li><code>mytable</code></li>
 * <li><code>myschema.mytable</code></li>
 * <li><code>mycolumn</code></li>
 * <li><code>mytable.mycolumn</code></li>
 * <li><code>myschema.mytable.mycolumn</code></li>
 * </ul>
 * <h4>Escaping</h4>
 * Escaping an identifier has two effects (TODO verify on all DBMS): it allows to use normally reserved characters/words
 * and tells the DBMS to treat the identifier in a case-sensitive manner. However, different DBMSs use different ways to
 * mark an escaped identifier, e.g. for table names:
 * <ul>
 * <li>PostgreSQL uses quotes: "escaped table name"</li>
 * <li>Oracle uses quotes: "escaped table name" (needs verification)</li>
 * <li>SQL Server uses brackets: [escaped table name] (needs verification)</li>
 * <li>MySQL uses ticks: `escaped table name` (needs verification)</li>
 * </ul>
 * </p>
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @author last edited by: $Author$
 * 
 * @version $Revision$, $Date$
 */
public class SQLIdentifier implements Comparable<SQLIdentifier> {

    private boolean isEscaped;

    private final String qualifier;

    private final String name;

    private char escapeStartChar;

    private char escapeEndChar;

    private String normalizedString;

    /**
     * Creates a new {@link SQLIdentifier} instance from a literal identifier, which may use a qualifier and/or be
     * surrounded by escaping characters.
     * <p>
     * Checked escaping characters include:
     * <ul>
     * <li>Quotes: <code>"</code> (used by PostgreSQL and Oracle)</li>
     * <li>Brackets: <code>[</code> and <code>]</code> (used by MS SQL Server)</li>
     * <li>Backticks: <code>`</code> (used by MySQL)</li>
     * </ul>
     * </p>
     * 
     * @param identifier
     *            literal identifier, must not be <code>null</code>
     */
    public SQLIdentifier( String identifier ) throws IllegalArgumentException {
        if ( identifier == null || identifier.isEmpty() ) {
            throw new IllegalArgumentException( "An SQL identifier can not be empty." );
        }
        String s = identifier;
        if ( identifier.length() > 1 ) {
            char firstChar = identifier.charAt( 0 );
            char lastChar = identifier.charAt( identifier.length() - 1 );
            switch ( firstChar ) {
            case '"': {
                if ( lastChar != '"' ) {
                    throw new IllegalArgumentException( "SQL identifier (=" + identifier
                                                        + ") starts with a quote, but doesn't end with a quote." );
                }
                isEscaped = true;
                escapeStartChar = '"';
                escapeEndChar = '"';
                s = identifier.substring( 1, identifier.length() - 1 );
                break;
            }
            case '[': {
                if ( lastChar != '"' ) {
                    throw new IllegalArgumentException( "SQL identifier (=" + identifier
                                                        + ") starts with a bracket, but doesn't end with a bracket." );
                }
                isEscaped = true;
                escapeStartChar = '[';
                escapeEndChar = ']';
                s = identifier.substring( 1, identifier.length() - 1 );
                break;
            }
            case '`': {
                if ( lastChar != '`' ) {
                    throw new IllegalArgumentException( "SQL identifier (=" + identifier
                                                        + ") starts with a backtick, but doesn't end with a backtick." );
                }
                isEscaped = true;
                escapeStartChar = '`';
                escapeEndChar = '`';
                s = identifier.substring( 1, identifier.length() - 1 );
                break;
            }
            default: {
                isEscaped = false;
                s = identifier;
            }
            }
        }

        int pos = s.lastIndexOf( '.' );
        if ( pos >= 0 ) {
            qualifier = s.substring( 0, pos );
            name = s.substring( pos + 1, s.length() );
        } else {
            qualifier = null;
            name = s;
        }

        StringBuilder sb = new StringBuilder();
        if ( isEscaped ) {
            sb.append( escapeStartChar );
        }
        if ( qualifier != null ) {
            if ( isEscaped ) {
                sb.append( qualifier );
            } else {
                sb.append( qualifier.toUpperCase() );
            }
            sb.append( "." );
        }
        if ( isEscaped ) {
            sb.append( name );
        } else {
            sb.append( name.toUpperCase() );
        }
        if ( isEscaped ) {
            sb.append( escapeEndChar );
        }
        normalizedString = sb.toString();
    }

    protected SQLIdentifier( String table, String schema ) {
        this.name = table;
        this.qualifier = schema;
        this.isEscaped = false;
    }

    /**
     * Returns the qualifier part of this identifier.
     * 
     * @return qualifier, can be <code>null</code> (unqualified)
     */
    public String getQualifier() {
        return qualifier;
    }

{'fraction_non_alphanumeric': 0.13099509409094237, 'fraction_numerical': 0.008200922603792926, 'mean_word_length': 1.7862097103223173, 'pattern_counts': {'":': 0, '<': 141, '<?xml version=': 0, '>': 145, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3755807', 'n_tokens_mistral': 3762, 'n_tokens_neox': 3506, 'n_words': 1192}