package org.apache.torque.templates.platform;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;
import org.apache.torque.templates.typemapping.SchemaType;
import org.apache.torque.templates.typemapping.SqlType;


/**
 * Default implementation for the Platform interface.
 *
 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
 * @version $Id: PlatformDefaultImpl.java 1405649 2012-11-04 22:07:22Z tfischer $
 */
public class PlatformDefaultImpl implements Platform
{
    /** The date format for formatting database timestamps. */
    private static final String TIMESTAMP_FORMAT = "''yyyy-MM-dd HH:mm:ss''";

    /**
     * Maps the Torque schema types to sql types.
     */
    private Map<SchemaType, SqlType> schemaTypeToSqlTypeMap;

    /**
     * Default constructor.
     */
    public PlatformDefaultImpl()
    {
        initialize();
    }

    private void initialize()
    {
        schemaTypeToSqlTypeMap
                = Collections.synchronizedMap(
                    new HashMap<SchemaType, SqlType>());
        for (SchemaType schemaType : SchemaType.values())
        {
            setSchemaTypeToSqlTypeMapping(
                    schemaType,
                    new SqlType(schemaType.name()));
        }
        setSchemaTypeToSqlTypeMapping(
                SchemaType.BOOLEANCHAR,
                new SqlType("CHAR"));
        setSchemaTypeToSqlTypeMapping(
                SchemaType.BOOLEANINT,
                new SqlType("INTEGER"));
    }

    /**
     * Adds a mapping to the torque schema type -> sql type map.
     *
     * @param schemaType the torque schema type which should be mapped,
     *        not null.
     * @param sqlType the sql type for the torque schema type, not null.
     */
    protected void setSchemaTypeToSqlTypeMapping(
            SchemaType schemaType,
            SqlType sqlType)
    {
        if (schemaType == null)
        {
            throw new NullPointerException("schemaType must not be null");
        }
        if (sqlType == null)
        {
            throw new NullPointerException("sqlType must not be null");
        }
        schemaTypeToSqlTypeMap.put(schemaType, sqlType);
    }

    /**
     * @see Platform#getSqlTypeForSchemaType(SchemaType)
     */
    public SqlType getSqlTypeForSchemaType(SchemaType schemaType)
    {
        return schemaTypeToSqlTypeMap.get(schemaType);
    }

    /**
     * @return Only produces a SQL fragment if null values are
     * disallowed.
     * @see Platform#getNullString(boolean)
     */
    public String getNullString(boolean notNull)
    {
        return (notNull ? "NOT NULL" : "");
    }

    /**
     * @see Platform#getAutoIncrement()
     */
    public String getAutoIncrement()
    {
        return "IDENTITY";
    }

    /**
     * @see Platform#hasScale(String)
     * TODO collect info for all platforms
     */
    public boolean hasScale(String sqlType)
    {
        return true;
    }

    /**
     * @see Platform#hasSize(String)
     * TODO collect info for all platforms
     */
    public boolean hasSize(String sqlType)
    {
        return true;
    }

    /**
     * Returns a possible SQL suffix for column definitions of certain
     * SQL Types, e.g. for Oracle VARCHAR2 columns, it typically
     * makes sense to use 'x CHAR' instead of 'x' as size.
     *
     * @param sqlType the SQL type to determine the suffix for.
     *
     * @return The size suffix, not null.
     *         This implementation always returns the empty string.
     */
    public String getSizeSuffix(String sqlType)
    {
        return StringUtils.EMPTY;
    }

   /**
     * @see Platform#createNotNullBeforeAutoincrement()
     */
    public boolean createNotNullBeforeAutoincrement()
    {
        return true;
    }

    /**
     * @see Platform#quoteAndEscape(String)
     */
    public String quoteAndEscape(String text)
    {
        String result = text.replace("'", "''");
        if (escapeBackslashes())
        {
            result = result.replace("\\", "\\\\");
        }
        return "'" + result + "'";
    }

    /**
     * Returns whether backslashes must be escaped in string literals.
     *
     * @return true if backslashes bust be escaped, false otherwise.
     */
    protected boolean escapeBackslashes()
    {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public String getDateString(Date date)
    {
        return getTimestampString(date);
    }

    /**
     * {@inheritDoc}
     */
    public String getTimeString(Date date)
    {
        return getTimestampString(date);
    }

    /**
     * {@inheritDoc}
     */
    public String getTimestampString(Date date)
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat(TIMESTAMP_FORMAT);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        return dateFormat.format(date);
    }

    /**
     * Returns whether the database has schema support where a schema
     * is not tied to a user (oracle) or database (mysql), but can be created
     * separately.
     *
     * @return this implementation returns false.
     */
    public boolean usesStandaloneSchema()
    {
        return false;
    }
}
