Skip to content

Commit

Permalink
GRAILS-8296 and GRAILS-5520 added support for column default value an…
Browse files Browse the repository at this point in the history
…d comment for columns and tables
  • Loading branch information
Burt Beckwith committed Sep 30, 2012
1 parent 1eb2cb1 commit 7ac9b65
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
Expand Up @@ -30,6 +30,8 @@ class ColumnConfig {
int length = -1 int length = -1
int precision = -1 int precision = -1
int scale = -1 int scale = -1
String defaultValue
String comment


String toString() { String toString() {
"column[name:$name, index:$index, unique:$unique, length:$length, precision:$precision, scale:$scale]" "column[name:$name, index:$index, unique:$unique, length:$length, precision:$precision, scale:$scale]"
Expand Down
Expand Up @@ -1813,6 +1813,10 @@ protected static void createClassProperties(GrailsDomainClass domainClass, Persi


Mapping gormMapping = getMapping(domainClass.getClazz()); Mapping gormMapping = getMapping(domainClass.getClazz());


if (gormMapping != null) {
table.setComment(gormMapping.getComment());
}

for (GrailsDomainClassProperty currentGrailsProp : persistentProperties) { for (GrailsDomainClassProperty currentGrailsProp : persistentProperties) {


// if its inherited skip // if its inherited skip
Expand Down Expand Up @@ -2683,6 +2687,11 @@ private static void bindSimpleValue(String type, SimpleValue simpleValue, boolea
private static void bindColumn(GrailsDomainClassProperty property, GrailsDomainClassProperty parentProperty, private static void bindColumn(GrailsDomainClassProperty property, GrailsDomainClassProperty parentProperty,
Column column, ColumnConfig cc, String path, Table table, String sessionFactoryBeanName) { Column column, ColumnConfig cc, String path, Table table, String sessionFactoryBeanName) {


if (cc != null) {
column.setComment(cc.getComment());
column.setDefaultValue(cc.getDefaultValue());
}

Class<?> userType = getUserType(property); Class<?> userType = getUserType(property);
String columnName = getColumnNameForPropertyAndPath(property, path, cc, sessionFactoryBeanName); String columnName = getColumnNameForPropertyAndPath(property, path, cc, sessionFactoryBeanName);
if ((property.isAssociation() || property.isBasicCollectionType()) && userType == null) { if ((property.isAssociation() || property.isBasicCollectionType()) && userType == null) {
Expand Down
Expand Up @@ -422,6 +422,8 @@ class HibernateMappingBuilder {
if (namedArgs["enumType"]) cc.enumType = namedArgs["enumType"] if (namedArgs["enumType"]) cc.enumType = namedArgs["enumType"]
if (namedArgs["index"]) cc.index = namedArgs["index"] if (namedArgs["index"]) cc.index = namedArgs["index"]
if (namedArgs["unique"]) cc.unique = namedArgs["unique"] if (namedArgs["unique"]) cc.unique = namedArgs["unique"]
if (namedArgs.defaultValue) cc.defaultValue = namedArgs.defaultValue
if (namedArgs.comment) cc.comment = namedArgs.comment
cc.length = namedArgs["length"] ?: -1 cc.length = namedArgs["length"] ?: -1
cc.precision = namedArgs["precision"] ?: -1 cc.precision = namedArgs["precision"] ?: -1
cc.scale = namedArgs["scale"] ?: -1 cc.scale = namedArgs["scale"] ?: -1
Expand Down Expand Up @@ -537,6 +539,10 @@ class HibernateMappingBuilder {
mapping.datasources = names mapping.datasources = names
} }


void comment(String comment) {
mapping.comment = comment
}

void methodMissing(String name, args) { void methodMissing(String name, args) {
if ('user-type' == name && args && (args[0] instanceof Map)) { if ('user-type' == name && args && (args[0] instanceof Map)) {
hibernateCustomUserType(args[0]) hibernateCustomUserType(args[0])
Expand Down
Expand Up @@ -114,7 +114,7 @@ class Mapping {
ColumnConfig discriminatorColumn ColumnConfig discriminatorColumn


/** /**
* Obtains a ColumnConfig object for the given name * Obtains a PropertyConfig object for the given name
*/ */
PropertyConfig getPropertyConfig(String name) { columns[name] } PropertyConfig getPropertyConfig(String name) { columns[name] }


Expand Down Expand Up @@ -143,4 +143,9 @@ class Mapping {
* @return the datasource names * @return the datasource names
*/ */
List<String> datasources = [GrailsDomainClassProperty.DEFAULT_DATA_SOURCE] List<String> datasources = [GrailsDomainClassProperty.DEFAULT_DATA_SOURCE]

/**
* DDL comment.
*/
String comment
} }
Expand Up @@ -737,4 +737,36 @@ class HibernateMappingBuilderTests extends GroovyTestCase {
assertTrue mapping.getPropertyConfig('firstName').updateable assertTrue mapping.getPropertyConfig('firstName').updateable
assertFalse mapping.getPropertyConfig('lastName').updateable assertFalse mapping.getPropertyConfig('lastName').updateable
} }

void testDefaultValue() {
def builder = new HibernateMappingBuilder("Foo")
def mapping = builder.evaluate {
comment 'wahoo'
name comment: 'bar'
foo defaultValue: '5'
}
assertEquals '5', mapping.getPropertyConfig('foo').columns[0].defaultValue
assertNull mapping.getPropertyConfig('name').columns[0].defaultValue
}

void testColumnComment() {
def builder = new HibernateMappingBuilder("Foo")
def mapping = builder.evaluate {
comment 'wahoo'
name comment: 'bar'
foo defaultValue: '5'
}
assertEquals 'bar', mapping.getPropertyConfig('name').columns[0].comment
assertNull mapping.getPropertyConfig('foo').columns[0].comment
}

void testTableComment() {
def builder = new HibernateMappingBuilder("Foo")
def mapping = builder.evaluate {
comment 'wahoo'
name comment: 'bar'
foo defaultValue: '5'
}
assertEquals 'wahoo', mapping.comment
}
} }

0 comments on commit 7ac9b65

Please sign in to comment.