/*
 * Copyright 2004-2006 the original author or authors.
 * 
 * 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 org.compass.gps.device.jdbc.dialect;

import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;

import org.compass.gps.device.jdbc.mapping.ColumnMapping;
import org.compass.gps.device.jdbc.mapping.VersionColumnMapping;

/**
 * 
 * @author kimchy
 * 
 */
public class DefaultJdbcDialect implements JdbcDialect {

    protected Long getIntegerAsLong(ResultSet rs, ColumnMapping columnMapping) throws SQLException {
        int integer;
        if (columnMapping.isUsingColumnIndex()) {
            integer = rs.getInt(columnMapping.getColumnIndex());
        } else {
            integer = rs.getInt(columnMapping.getColumnName());
        }
        return new Long(integer);
    }

    protected Long getLong(ResultSet rs, ColumnMapping columnMapping) throws SQLException {
        if (columnMapping.isUsingColumnIndex()) {
            return (Long) rs.getObject(columnMapping.getColumnIndex());
        }
        return (Long) rs.getObject(columnMapping.getColumnName());
    }

    protected Long getDateAsLong(ResultSet rs, ColumnMapping columnMapping) throws SQLException {
        Date date = null;
        if (columnMapping.isUsingColumnIndex()) {
            date = rs.getDate(columnMapping.getColumnIndex());
        } else {
            date = rs.getDate(columnMapping.getColumnName());
        }
        return new Long(date.getTime());
    }

    protected Long getTimeAsLong(ResultSet rs, ColumnMapping columnMapping) throws SQLException {
        Date date = null;
        if (columnMapping.isUsingColumnIndex()) {
            date = rs.getTime(columnMapping.getColumnIndex());
        } else {
            date = rs.getTime(columnMapping.getColumnName());
        }
        return new Long(date.getTime());
    }

    protected Long getTimestampAsLong(ResultSet rs, ColumnMapping columnMapping) throws SQLException {
        Timestamp timestamp = null;
        if (columnMapping.isUsingColumnIndex()) {
            timestamp = rs.getTimestamp(columnMapping.getColumnIndex());
        } else {
            timestamp = rs.getTimestamp(columnMapping.getColumnName());
        }
        return new Long(timestamp.getTime());
    }

    public Long getVersion(ResultSet rs, VersionColumnMapping versionMapping) throws SQLException {
        Long result = null;
        int sqlType = versionMapping.getSqlType();
        if (sqlType == Types.INTEGER) {
            result = getIntegerAsLong(rs, versionMapping);
        } else if (sqlType == Types.DATE) {
            result = getDateAsLong(rs, versionMapping);
        } else if (sqlType == Types.TIMESTAMP) {
            result = getTimestampAsLong(rs, versionMapping);
        } else if (sqlType == Types.TIME) {
            result = getTimeAsLong(rs, versionMapping);
        } else {
            result = getLong(rs, versionMapping);
        }
        return result;
    }

    public void setParameter(PreparedStatement ps, int paramIndex, String value) throws SQLException {
        ParameterMetaData metaData = ps.getParameterMetaData();
        int type = metaData.getParameterType(paramIndex);
        if (type == Types.BIGINT) {
            long lValue = Long.parseLong(value);
            ps.setLong(paramIndex, lValue);
        } else if (type == Types.INTEGER) {
            int iValue = Integer.parseInt(value);
            ps.setInt(paramIndex, iValue);
        } else if (type == Types.SMALLINT) {
            short iValue = Short.parseShort(value);
            ps.setShort(paramIndex, iValue);
        } else if (type == Types.VARCHAR || type == Types.LONGVARCHAR || type == Types.CHAR) {
            ps.setString(paramIndex, value);
        } else {
            throw new IllegalArgumentException("Failed to set parameter for type ["
                    + metaData.getParameterTypeName(paramIndex) + "], not supported");
        }
    }

    public String getStringValue(ResultSet rs, ColumnMapping mapping) throws SQLException {
        String value = null;
        if (mapping.isUsingColumnIndex()) {
            value = rs.getString(mapping.getColumnIndex());
        } else {
            value = rs.getString(mapping.getColumnName());
        }
        if (rs.wasNull()) {
            return null;
        }
        return value;
    }

    public String getStringValue(ResultSet rs, int columnIndex) throws SQLException {
        String value = rs.getString(columnIndex);
        if (rs.wasNull()) {
            return null;
        }
        return value;
    }
}

@@ -53,18 +53,17 @@
 hors.%0A *
- 
 %0A
+
  * Licen
@@ -234,17 +234,16 @@
 se at%0A *
- 
 %0A *     
@@ -288,17 +288,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -992,17 +992,16 @@
 %0A%0A/**%0A *
- 
 %0A * @aut
@@ -1013,17 +1013,16 @@
 imchy%0A *
- 
 %0A */%0Apub
@@ -1437,32 +1437,341 @@
 nteger);%0A    %7D%0A%0A
+    protected Long getNumericAsLong(ResultSet rs, ColumnMapping columnMapping) throws SQLException %7B%0A        if (columnMapping.isUsingColumnIndex()) %7B%0A            return new Long(rs.getLong(columnMapping.getColumnIndex()));%0A        %7D%0A        return new Long(rs.getLong(columnMapping.getColumnName()));%0A    %7D%0A%0A
     protected Lo
@@ -3794,32 +3794,138 @@
 ersionMapping);%0A
+        %7D else if (sqlType == Types.NUMERIC) %7B%0A            result = getNumericAsLong(rs, versionMapping);%0A
         %7D else %7B
@@ -5623,10 +5623,11 @@
 ;%0A    %7D%0A
-
 %7D%0A
+%0A

support for Oracle 10g for Type.NUMERIC (thanks to Wayne Robinson)