📄 sqlbuilder.java
字号:
package org.apache.ddlutils.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.io.IOException;
import java.io.Writer;
import java.rmi.server.UID;
import java.sql.Types;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.Closure;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.map.ListOrderedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ddlutils.DdlUtilsException;
import org.apache.ddlutils.Platform;
import org.apache.ddlutils.PlatformInfo;
import org.apache.ddlutils.alteration.AddColumnChange;
import org.apache.ddlutils.alteration.AddForeignKeyChange;
import org.apache.ddlutils.alteration.AddIndexChange;
import org.apache.ddlutils.alteration.AddPrimaryKeyChange;
import org.apache.ddlutils.alteration.AddTableChange;
import org.apache.ddlutils.alteration.ColumnAutoIncrementChange;
import org.apache.ddlutils.alteration.ColumnDataTypeChange;
import org.apache.ddlutils.alteration.ColumnDefaultValueChange;
import org.apache.ddlutils.alteration.ColumnOrderChange;
import org.apache.ddlutils.alteration.ColumnRequiredChange;
import org.apache.ddlutils.alteration.ColumnSizeChange;
import org.apache.ddlutils.alteration.ModelChange;
import org.apache.ddlutils.alteration.ModelComparator;
import org.apache.ddlutils.alteration.PrimaryKeyChange;
import org.apache.ddlutils.alteration.RemoveColumnChange;
import org.apache.ddlutils.alteration.RemoveForeignKeyChange;
import org.apache.ddlutils.alteration.RemoveIndexChange;
import org.apache.ddlutils.alteration.RemovePrimaryKeyChange;
import org.apache.ddlutils.alteration.RemoveTableChange;
import org.apache.ddlutils.alteration.TableChange;
import org.apache.ddlutils.model.Column;
import org.apache.ddlutils.model.Database;
import org.apache.ddlutils.model.ForeignKey;
import org.apache.ddlutils.model.Index;
import org.apache.ddlutils.model.IndexColumn;
import org.apache.ddlutils.model.ModelException;
import org.apache.ddlutils.model.Reference;
import org.apache.ddlutils.model.Table;
import org.apache.ddlutils.model.TypeMap;
import org.apache.ddlutils.util.CallbackClosure;
import org.apache.ddlutils.util.MultiInstanceofPredicate;
/**
* This class is a collection of Strategy methods for creating the DDL required to create and drop
* databases and tables.
*
* It is hoped that just a single implementation of this class, for each database should make creating DDL
* for each physical database fairly straightforward.
*
* An implementation of this class can always delegate down to some templating technology such as Velocity if
* it requires. Though often that can be quite complex when attempting to reuse code across many databases.
* Hopefully only a small amount code needs to be changed on a per database basis.
*
* @version $Revision: 504014 $
*/
public abstract class SqlBuilder
{
/** The line separator for in between sql commands. */
private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
/** The placeholder for the size value in the native type spec. */
protected static final String SIZE_PLACEHOLDER = "{0}";
/** The Log to which logging calls will be made. */
protected final Log _log = LogFactory.getLog(SqlBuilder.class);
/** The platform that this builder belongs to. */
private Platform _platform;
/** The current Writer used to output the SQL to. */
private Writer _writer;
/** The indentation used to indent commands. */
private String _indent = " ";
/** An optional locale specification for number and date formatting. */
private String _valueLocale;
/** The date formatter. */
private DateFormat _valueDateFormat;
/** The date time formatter. */
private DateFormat _valueTimeFormat;
/** The number formatter. */
private NumberFormat _valueNumberFormat;
/** Helper object for dealing with default values. */
private DefaultValueHelper _defaultValueHelper = new DefaultValueHelper();
/** The character sequences that need escaping. */
private Map _charSequencesToEscape = new ListOrderedMap();
//
// Configuration
//
/**
* Creates a new sql builder.
*
* @param platform The plaftform this builder belongs to
*/
public SqlBuilder(Platform platform)
{
_platform = platform;
}
/**
* Returns the platform object.
*
* @return The platform
*/
public Platform getPlatform()
{
return _platform;
}
/**
* Returns the platform info object.
*
* @return The info object
*/
public PlatformInfo getPlatformInfo()
{
return _platform.getPlatformInfo();
}
/**
* Returns the writer that the DDL is printed to.
*
* @return The writer
*/
public Writer getWriter()
{
return _writer;
}
/**
* Sets the writer for printing the DDL to.
*
* @param writer The writer
*/
public void setWriter(Writer writer)
{
_writer = writer;
}
/**
* Returns the default value helper.
*
* @return The default value helper
*/
public DefaultValueHelper getDefaultValueHelper()
{
return _defaultValueHelper;
}
/**
* Returns the string used to indent the SQL.
*
* @return The indentation string
*/
public String getIndent()
{
return _indent;
}
/**
* Sets the string used to indent the SQL.
*
* @param indent The indentation string
*/
public void setIndent(String indent)
{
_indent = indent;
}
/**
* Returns the locale that is used for number and date formatting
* (when printing default values and in generates insert/update/delete
* statements).
*
* @return The locale or <code>null</code> if default formatting is used
*/
public String getValueLocale()
{
return _valueLocale;
}
/**
* Sets the locale that is used for number and date formatting
* (when printing default values and in generates insert/update/delete
* statements).
*
* @param localeStr The new locale or <code>null</code> if default formatting
* should be used; Format is "language[_country[_variant]]"
*/
public void setValueLocale(String localeStr)
{
if (localeStr != null)
{
int sepPos = localeStr.indexOf('_');
String language = null;
String country = null;
String variant = null;
if (sepPos > 0)
{
language = localeStr.substring(0, sepPos);
country = localeStr.substring(sepPos + 1);
sepPos = country.indexOf('_');
if (sepPos > 0)
{
variant = country.substring(sepPos + 1);
country = country.substring(0, sepPos);
}
}
else
{
language = localeStr;
}
if (language != null)
{
Locale locale = null;
if (variant != null)
{
locale = new Locale(language, country, variant);
}
else if (country != null)
{
locale = new Locale(language, country);
}
else
{
locale = new Locale(language);
}
_valueLocale = localeStr;
setValueDateFormat(DateFormat.getDateInstance(DateFormat.SHORT, locale));
setValueTimeFormat(DateFormat.getTimeInstance(DateFormat.SHORT, locale));
setValueNumberFormat(NumberFormat.getNumberInstance(locale));
return;
}
}
_valueLocale = null;
setValueDateFormat(null);
setValueTimeFormat(null);
setValueNumberFormat(null);
}
/**
* Returns the format object for formatting dates in the specified locale.
*
* @return The date format object or null if no locale is set
*/
protected DateFormat getValueDateFormat()
{
return _valueDateFormat;
}
/**
* Sets the format object for formatting dates in the specified locale.
*
* @param format The date format object
*/
protected void setValueDateFormat(DateFormat format)
{
_valueDateFormat = format;
}
/**
* Returns the format object for formatting times in the specified locale.
*
* @return The time format object or null if no locale is set
*/
protected DateFormat getValueTimeFormat()
{
return _valueTimeFormat;
}
/**
* Sets the date format object for formatting times in the specified locale.
*
* @param format The time format object
*/
protected void setValueTimeFormat(DateFormat format)
{
_valueTimeFormat = format;
}
/**
* Returns the format object for formatting numbers in the specified locale.
*
* @return The number format object or null if no locale is set
*/
protected NumberFormat getValueNumberFormat()
{
return _valueNumberFormat;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -