Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement equals in ColumnTypes. #860

Merged
merged 3 commits into from
Apr 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
AliLozano marked this conversation as resolved.
Show resolved Hide resolved

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