Permalink
Browse files

Fix defaultAlias generation to avoid using reserved words in SQL

  • Loading branch information...
seratch committed May 25, 2016
1 parent 4013528 commit 4282b2fdcb0cf3fdcd79a2b16d6fac46f89887f4
@@ -76,6 +76,119 @@ object CodeGenerator {
"isInstanceOf" -> "instanceOf",
"synchronized" -> "isSynchronized"
)
val SQLReservedWords = Seq(
"access",
"add",
"all",
"alter",
"and",
"any",
"as",
"asc",
"audit",
"between",
"by",
"char",
"check",
"cluster",
"column",
"column_value",
"comment",
"compress",
"connect",
"create",
"current",
"date",
"decimal",
"default",
"delete",
"desc",
"distinct",
"drop",
"else",
"exclusive",
"exists",
"file",
"float",
"for",
"from",
"grant",
"group",
"having",
"identified",
"immediate",
"in",
"increment",
"index",
"initial",
"insert",
"integer",
"intersect",
"into",
"is",
"level",
"like",
"lock",
"long",
"maxextents",
"minus",
"mlslabel",
"mode",
"modify",
"nested_table_id",
"noaudit",
"nocompress",
"not",
"nowait",
"null",
"number",
"of",
"offline",
"on",
"online",
"option",
"or",
"order",
"pctfree",
"prior",
"public",
"raw",
"rename",
"resource",
"revoke",
"row",
"rowid",
"rownum",
"rows",
"select",
"session",
"set",
"share",
"size",
"smallint",
"start",
"successful",
"synonym",
"sysdate",
"table",
"then",
"to",
"trigger",
"uid",
"union",
"unique",
"update",
"user",
"validate",
"values",
"varchar",
"varchar2",
"view",
"whenever",
"where",
"with"
)
}
/**
@@ -60,7 +60,10 @@ trait ModelGenerator extends CodeGenerator {
def code(namespaces: Seq[String], name: String, tableName: Option[String], nameAndTypeNamePairs: Seq[(String, String)]): String = {
val namespace = toNamespace(modelPackage, namespaces)
val modelClassName = toClassName(name)
val alias = modelClassName.filter(_.isUpper).map(_.toLower).mkString
val alias = {
val a = modelClassName.filter(_.isUpper).map(_.toLower).mkString
if (CodeGenerator.SQLReservedWords.contains(a)) a + "_" else a
}
val timestampPrefix = if (withId) ",\n" else { if (nameAndTypeNamePairs.isEmpty) "" else ",\n" }
val caseClassFieldsPrimaryKeyRow = if (withId) s""" ${primaryKeyName}: ${primaryKeyType}""" else ""
val extractorsPrimaryKeyRow = if (withId) s""" ${primaryKeyName} = rs.get(rn.${primaryKeyName})""" else ""
@@ -58,17 +58,18 @@ trait ReverseScaffoldAllGenerator extends CodeGenerator {
}
private def toNormalizedEntityName(tableName: String, tables: Seq[Table]): String = {
if (isJoinTable(tableName, tables)) {
val _tableName = tableName.toLowerCase(Locale.ENGLISH)
if (isJoinTable(_tableName, tables)) {
// normalize join table entity name
val joinedTableNames = tables.map(_.name.toLowerCase(Locale.ENGLISH))
.filter(t => tableName != t && (tableName.startsWith(t) || tableName.endsWith(t)))
.filter(t => _tableName != t && (_tableName.startsWith(t) || _tableName.endsWith(t)))
val normalizedName = joinedTableNames.foldLeft(tableName.toLowerCase(Locale.ENGLISH)) {
case (tableName, joined) => tableName.replaceFirst(joined, Inflector.singularize(joined))
val normalizedName = joinedTableNames.foldLeft(_tableName.toLowerCase(Locale.ENGLISH)) {
case (table, joined) => tableName.replaceFirst(joined, Inflector.singularize(joined))
}
Inflector.singularize(toClassName(normalizedName))
} else {
Inflector.singularize(toClassName(tableName))
Inflector.singularize(toClassName(_tableName))
}
}

0 comments on commit 4282b2f

Please sign in to comment.