Skip to content

Commit

Permalink
Implement equals in ColumnTypes. (#860)
Browse files Browse the repository at this point in the history
* Implement Equals in ColumnTypes.

* Add nullable field to equals method of ColumnType

* Added super.equals in StringColumnType
  • Loading branch information
AliLozano authored Apr 14, 2020
1 parent b00135c commit 319178b
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/ColumnType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

/**
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -137,6 +157,18 @@ class EntityIDColumnType<T : Comparable<T>>(val idColumn: Column<T>) : ColumnTyp
},
idColumn.table as IdTable<T>
)

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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 "\'\'",
Expand All @@ -274,6 +331,9 @@ abstract class StringColumnType(
'\n' to "\\n"
)
}



}

/**
Expand All @@ -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
}
}

/**
Expand Down Expand Up @@ -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
}

}

/**
Expand Down Expand Up @@ -446,6 +535,18 @@ class EnumerationColumnType<T : Enum<T>>(
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
}
}

/**
Expand All @@ -467,6 +568,18 @@ class EnumerationNameColumnType<T : Enum<T>>(
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
Expand Down

0 comments on commit 319178b

Please sign in to comment.