From 319178bb70a128831f591f125f6cc064d138a193 Mon Sep 17 00:00:00 2001 From: Ali Lozano Date: Tue, 14 Apr 2020 03:17:02 -0500 Subject: [PATCH] Implement equals in ColumnTypes. (#860) * Implement Equals in ColumnTypes. * Add nullable field to equals method of ColumnType * Added super.equals in StringColumnType --- .../org/jetbrains/exposed/sql/ColumnType.kt | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt index f1a38b371f..00a56b0ed7 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt @@ -70,6 +70,15 @@ interface IColumnType { */ abstract class ColumnType(override var nullable: Boolean = false) : IColumnType { override fun toString(): String = sqlType() + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as ColumnType + + if (nullable != other.nullable) return false + return true + } } /** @@ -99,6 +108,17 @@ class AutoIncColumnType( } override fun sqlType(): String = resolveAutoIncType(delegate) + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as AutoIncColumnType + + if (delegate != other.delegate) return false + + return true + } } /** Returns `true` if this is an auto-increment column, `false` otherwise. */ @@ -137,6 +157,18 @@ class EntityIDColumnType>(val idColumn: Column) : ColumnTyp }, idColumn.table as IdTable ) + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as EntityIDColumnType<*> + + if (idColumn != other.idColumn) return false + + return true + } + } // Numeric columns @@ -224,6 +256,19 @@ class DecimalColumnType( is Int -> value.toBigDecimal() else -> error("Unexpected value of type Double: $value of ${value::class.qualifiedName}") }.setScale(scale, RoundingMode.HALF_EVEN) + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + if (!super.equals(other)) return false + + other as DecimalColumnType + + if (precision != other.precision) return false + if (scale != other.scale) return false + + return true + } } // Character columns @@ -266,6 +311,18 @@ abstract class StringColumnType( append('\'') } + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + if (!super.equals(other)) return false + + other as StringColumnType + + if (collate != other.collate) return false + + return true + } + companion object { private val charactersToEscape = mapOf( '\'' to "\'\'", @@ -274,6 +331,9 @@ abstract class StringColumnType( '\n' to "\\n" ) } + + + } /** @@ -290,6 +350,18 @@ open class VarCharColumnType( append(" COLLATE ${escape(collate)}") } } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + if (!super.equals(other)) return false + + other as VarCharColumnType + + if (colLength != other.colLength) return false + + return true + } } /** @@ -332,6 +404,23 @@ class BinaryColumnType( val length: Int ) : BasicBinaryColumnType() { override fun sqlType(): String = currentDialect.dataTypeProvider.binaryType(length) + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + if (!super.equals(other)) return false + + other as BinaryColumnType + + if (length != other.length) return false + + return true + } + + override fun hashCode(): Int { + return length + } + } /** @@ -446,6 +535,18 @@ class EnumerationColumnType>( is Enum<*> -> value.ordinal else -> error("$value of ${value::class.qualifiedName} is not valid for enum ${klass.simpleName}") } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + if (!super.equals(other)) return false + + other as EnumerationColumnType<*> + + if (klass != other.klass) return false + + return true + } } /** @@ -467,6 +568,18 @@ class EnumerationNameColumnType>( is Enum<*> -> value.name else -> error("$value of ${value::class.qualifiedName} is not valid for enum ${klass.qualifiedName}") } + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + if (!super.equals(other)) return false + + other as EnumerationNameColumnType<*> + + if (klass != other.klass) return false + + return true + } } // Date/Time columns