You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by to...@apache.org on 2004/07/27 00:15:39 UTC
cvs commit: jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model Table.betwixt TypeMap.java
tomdz 2004/07/26 15:15:39
Modified: sql/src/java/org/apache/commons/sql/builder SqlBuilder.java
MySqlBuilder.java SybaseBuilder.java
PostgreSqlBuilder.java
sql/src/java/org/apache/commons/sql/model Table.betwixt
TypeMap.java
Log:
Fixed typo in createTable
Tuned type mappings for MySql, PostgreSql, and Sybase
Fixed error issued by betwixt about a missing name attribute in the .betwixt file for table
Revision Changes Path
1.20 +31 -5 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SqlBuilder.java
Index: SqlBuilder.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SqlBuilder.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- SqlBuilder.java 25 Jul 2004 15:08:28 -0000 1.19
+++ SqlBuilder.java 26 Jul 2004 22:15:39 -0000 1.20
@@ -66,7 +66,10 @@
/** The indentation used to indent commands */
private String _indent = " ";
-
+
+ /** Whether the database requires the explicit stating of NULL as the default value */
+ private boolean _requiringNullAsDefaultValue = false;
+
/** Whether primary key constraints are embedded inside the create table statement */
private boolean _primaryKeyEmbedded = true;
@@ -147,6 +150,28 @@
}
/**
+ * Determines whether a NULL needs to be explicitly stated when the column
+ * has no specified default value. Default is false.
+ *
+ * @return <code>true</code> if NULL must be written for empty default values
+ */
+ public boolean isRequiringNullAsDefaultValue()
+ {
+ return _requiringNullAsDefaultValue;
+ }
+ /**
+ * Specifies whether a NULL needs to be explicitly stated when the column
+ * has no specified default value. Default is false.
+ *
+ * @param requiresNullAsDefaultValue Whether NULL must be written for empty
+ * default values
+ */
+ public void setRequiringNullAsDefaultValue(boolean requiresNullAsDefaultValue)
+ {
+ _requiringNullAsDefaultValue = requiresNullAsDefaultValue;
+ }
+
+ /**
* Determines whether primary key constraints are embedded in the create
* table clause or as seperate alter table statements. The default is
* embedded pks.
@@ -524,7 +549,7 @@
*/
public void createTable(Table table) throws IOException
{
- print("CRETE TABLE ");
+ print("CREATE TABLE ");
println(table.getName());
println("(");
@@ -699,7 +724,8 @@
print(" ");
writeColumnNotNullableStmt();
}
- else
+ else if (isRequiringNullAsDefaultValue() &&
+ (TypeMap.isTextType(column.getTypeCode()) || TypeMap.isBinaryType(column.getTypeCode())))
{
print(" ");
writeColumnNullableStmt();
@@ -755,7 +781,7 @@
{
sqlType.append(" (");
sqlType.append(column.getSize());
- if (TypeMap.isDecimalType(column.getType()))
+ if (TypeMap.typeHasScaleAndPrecision(column.getType()))
{
sqlType.append(",");
sqlType.append(column.getScale());
@@ -1050,7 +1076,7 @@
print(key.getForeignTable());
print(" (");
writeForeignReferences(key);
- println(")");
+ print(")");
}
}
}
1.11 +5 -4 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/MySqlBuilder.java
Index: MySqlBuilder.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/MySqlBuilder.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- MySqlBuilder.java 25 Jul 2004 15:08:28 -0000 1.10
+++ MySqlBuilder.java 26 Jul 2004 22:15:39 -0000 1.11
@@ -36,16 +36,17 @@
public MySqlBuilder()
{
setForeignKeysEmbedded(true);
+ // TODO: Not yet supported:
+ //setIndicesEmbedded(true);
setCommentPrefix("#");
addNativeTypeMapping(Types.BINARY, "BLOB");
addNativeTypeMapping(Types.BLOB, "LONGBLOB");
addNativeTypeMapping(Types.BOOLEAN, "BIT");
addNativeTypeMapping(Types.CLOB, "LONGTEXT");
- addNativeTypeMapping(Types.FLOAT, "DOUBLE");
- addNativeTypeMapping(Types.LONGVARBINARY, "MEDIUMBLOB");
- addNativeTypeMapping(Types.LONGVARCHAR, "MEDIUMTEXT");
+ addNativeTypeMapping(Types.LONGVARBINARY, "LONGBLOB");
+ addNativeTypeMapping(Types.LONGVARCHAR, "LONGTEXT");
addNativeTypeMapping(Types.REAL, "FLOAT");
- addNativeTypeMapping(Types.VARBINARY, "BLOB");
+ addNativeTypeMapping(Types.VARBINARY, "MEDIUMBLOB");
}
/* (non-Javadoc)
1.9 +2 -3 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SybaseBuilder.java
Index: SybaseBuilder.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/SybaseBuilder.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- SybaseBuilder.java 25 Jul 2004 15:08:28 -0000 1.8
+++ SybaseBuilder.java 26 Jul 2004 22:15:39 -0000 1.9
@@ -34,20 +34,19 @@
{
public SybaseBuilder()
{
+ // For Sybase a present NULL allows it to save initial storage, so we better create it
+ setRequiringNullAsDefaultValue(true);
setEmbeddedForeignKeysNamed(true);
setForeignKeysEmbedded(false);
setCommentPrefix("/*");
setCommentSuffix("*/");
addNativeTypeMapping(Types.BLOB, "IMAGE");
addNativeTypeMapping(Types.BOOLEAN, "BIT");
- addNativeTypeMapping(Types.CHAR, "UNICHAR");
addNativeTypeMapping(Types.CLOB, "TEXT");
addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION");
- addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION");
addNativeTypeMapping(Types.LONGVARBINARY, "IMAGE");
addNativeTypeMapping(Types.LONGVARCHAR, "TEXT");
addNativeTypeMapping(Types.TIMESTAMP, "DATETIME");
- addNativeTypeMapping(Types.VARCHAR, "UNIVARCHAR");
}
/* (non-Javadoc)
1.10 +2 -2 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/PostgreSqlBuilder.java
Index: PostgreSqlBuilder.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/builder/PostgreSqlBuilder.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- PostgreSqlBuilder.java 25 Jul 2004 15:08:28 -0000 1.9
+++ PostgreSqlBuilder.java 26 Jul 2004 22:15:39 -0000 1.10
@@ -37,7 +37,7 @@
addNativeTypeMapping(Types.BLOB, "BYTEA");
addNativeTypeMapping(Types.CLOB, "TEXT");
addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION");
- addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION");
+ addNativeTypeMapping(Types.FLOAT, "REAL");
addNativeTypeMapping(Types.LONGVARBINARY, "BYTEA");
addNativeTypeMapping(Types.LONGVARCHAR, "TEXT");
addNativeTypeMapping(Types.TINYINT, "SMALLINT");
1.4 +1 -1 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Table.betwixt
Index: Table.betwixt
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/Table.betwixt,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Table.betwixt 16 Dec 2003 15:09:50 -0000 1.3
+++ Table.betwixt 26 Jul 2004 22:15:39 -0000 1.4
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<info>
- <element>
+ <element name="table">
<hide property="primaryKeyColumns"/>
<hide property="autoIncrementColumn"/>
<addDefaults/>
1.7 +43 -10 jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/TypeMap.java
Index: TypeMap.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/sql/src/java/org/apache/commons/sql/model/TypeMap.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TypeMap.java 28 Feb 2004 03:35:48 -0000 1.6
+++ TypeMap.java 26 Jul 2004 22:15:39 -0000 1.7
@@ -73,6 +73,7 @@
public static final String CLOB = "CLOB";
public static final String NUMERIC = "NUMERIC";
public static final String DECIMAL = "DECIMAL";
+ public static final String BOOLEAN = "BOOLEAN";
public static final String BIT = "BIT";
public static final String TINYINT = "TINYINT";
public static final String SMALLINT = "SMALLINT";
@@ -88,12 +89,14 @@
public static final String DATE = "DATE";
public static final String TIME = "TIME";
public static final String TIMESTAMP = "TIMESTAMP";
- public static final String BOOLEANCHAR = "BOOLEANCHAR";
- public static final String BOOLEANINT = "BOOLEANINT";
private static final String[] TEXT_TYPES =
{
- CHAR, VARCHAR, LONGVARCHAR, CLOB, DATE, TIME, TIMESTAMP, BOOLEANCHAR
+ CHAR, VARCHAR, LONGVARCHAR, CLOB
+ };
+ private static final String[] BINARY_TYPES =
+ {
+ BINARY, VARBINARY, LONGVARBINARY, BLOB
};
private static final String[] DECIMAL_TYPES =
{
@@ -116,6 +119,7 @@
registerSqlTypeID(new Integer(Types.CLOB), CLOB);
registerSqlTypeID(new Integer(Types.NUMERIC), NUMERIC);
registerSqlTypeID(new Integer(Types.DECIMAL), DECIMAL);
+ registerSqlTypeID(new Integer(Types.BOOLEAN), BOOLEAN);
registerSqlTypeID(new Integer(Types.BIT), BIT);
registerSqlTypeID(new Integer(Types.TINYINT), TINYINT);
registerSqlTypeID(new Integer(Types.SMALLINT), SMALLINT);
@@ -163,9 +167,9 @@
}
/**
- * Returns true if values for the type need to be quoted.
+ * Determines whether the indicated type is a textual type.
*
- * @param type The type to check.
+ * @param type The code of type to check (as defined by {@link java.sql.Types}
*/
public static final boolean isTextType(int type)
{
@@ -173,9 +177,9 @@
}
/**
- * Returns true if values for the type need to be quoted.
+ * Determines whether the indicated type is a textual type.
*
- * @param type The type to check.
+ * @param type The type to check
*/
public static final boolean isTextType(String type)
{
@@ -192,13 +196,42 @@
}
/**
+ * Determines whether the indicated type is a binary type.
+ *
+ * @param type The code of type to check (as defined by {@link java.sql.Types}
+ */
+ public static final boolean isBinaryType(int type)
+ {
+ return isBinaryType(getJdbcTypeName(type));
+ }
+
+ /**
+ * Determines whether the indicated type is a binary type.
+ *
+ * @param type The type to check
+ */
+ public static final boolean isBinaryType(String type)
+ {
+ for (int i = 0; i < BINARY_TYPES.length; i++)
+ {
+ if (type.equalsIgnoreCase(BINARY_TYPES[i]))
+ {
+ return true;
+ }
+ }
+
+ // If we get this far, there were no matches.
+ return false;
+ }
+
+ /**
* Returns true if values for the type need have size and scale measurements
*
* @param type The type to check.
*/
- public static final boolean isDecimalType(int type)
+ public static final boolean typeHasScaleAndPrecision(int type)
{
- return isDecimalType(getJdbcTypeName(type));
+ return typeHasScaleAndPrecision(getJdbcTypeName(type));
}
/**
@@ -206,7 +239,7 @@
*
* @param type The type to check.
*/
- public static final boolean isDecimalType(String type)
+ public static final boolean typeHasScaleAndPrecision(String type)
{
for (int i = 0; i < DECIMAL_TYPES.length; i++)
{
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org