All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jumpmind.db.platform.DefaultValueHelper Maven / Gradle / Ivy

Go to download

This library is a fork of Apache DdlUtils. DdlUtils is a small, easy-to-use component for working with Database Definition (DDL) files.

There is a newer version: 3.5.19
Show newest version
package org.jumpmind.db.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.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;

import org.jumpmind.db.model.TypeMap;
import org.jumpmind.util.FormatUtils;

/**
 * Helper class for dealing with default values, e.g. converting them to other
 * types.
 */
public class DefaultValueHelper {

    /**
     * Converts the given default value from the specified original to the
     * target jdbc type.
     * 
     * @param defaultValue
     *            The default value
     * @param originalTypeCode
     *            The original type code
     * @param targetTypeCode
     *            The target type code
     * @return The converted default value
     */
    public String convert(String defaultValue, int originalTypeCode, int targetTypeCode) {
        String result = defaultValue;

        if (defaultValue != null) {
            switch (originalTypeCode) {
                case Types.BIT:
                    result = convertBoolean(defaultValue, targetTypeCode).toString();
                    break;
                case Types.DATE:
                    if (targetTypeCode == Types.TIMESTAMP) {
                        try {
                            Date date = Date.valueOf(result);

                            return new Timestamp(date.getTime()).toString();
                        } catch (IllegalArgumentException ex) {
                        }
                    }
                    break;
                case Types.TIME:
                    if (targetTypeCode == Types.TIMESTAMP) {
                        try {
                            Time time = Time.valueOf(result);

                            return new Timestamp(time.getTime()).toString();
                        } catch (IllegalArgumentException ex) {
                        }
                    }
                    break;
                default:
                    if (PlatformUtils.supportsJava14JdbcTypes()
                            && (originalTypeCode == PlatformUtils.determineBooleanTypeCode())) {
                        result = convertBoolean(defaultValue, targetTypeCode).toString();
                    }
                    break;
            }
        }
        return result;
    }

    /**
     * Converts a boolean default value to the given target type.
     * 
     * @param defaultValue
     *            The default value
     * @param targetTypeCode
     *            The target type code
     * @return The converted value
     */
    private Object convertBoolean(String defaultValue, int targetTypeCode) {
        boolean value = FormatUtils.toBoolean(defaultValue);
        Object result = null;

        if ((targetTypeCode == Types.BIT)
                || (PlatformUtils.supportsJava14JdbcTypes() && (targetTypeCode == PlatformUtils
                        .determineBooleanTypeCode()))) {
            result = value;
        } else if (TypeMap.isNumericType(targetTypeCode)) {
            result = (value ? new Integer(1) : new Integer(0));
        } else {
            result = Boolean.toString(value);
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy