Skip to content

Commit

Permalink
fix detekt issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vnikolova committed Oct 2, 2024
1 parent f63fc63 commit 15b663c
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@ import org.example.tables.Genre
import org.jetbrains.exposed.dao.id.CompositeID
import java.util.*

const val MOVIE_SEQUELID = 8
const val MOVIE2_SEQUELID = 9

class CreateExamples {
fun createFilms() {
val movie = StarWarsFilmEntity.new {
name = "The Last Jedi"
sequelId = 8
sequelId = MOVIE_SEQUELID
director = "Rian Johnson"
}
println(movie)

// Create a new record with id
val movie2 = StarWarsFilmEntity.new(id = 2) {
name = "The Rise of Skywalker"
sequelId = 9
sequelId = MOVIE2_SEQUELID
director = "J.J. Abrams"
}
println(movie2)
}

fun createNewWithCompositeId() {
Expand All @@ -33,5 +38,4 @@ class CreateExamples {
genre = Genre.SCI_FI
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package org.example.examples

import org.example.entities.StarWarsFilmEntity

const val SEQUEL_ID = 8

class DeleteExamples {
fun deleteFilm() {
val movie = StarWarsFilmEntity.new {
name = "The Last Jedi"
sequelId = 8
sequelId = SEQUEL_ID
director = "Rian Johnson"
}
val deletedMovie = movie.delete()
println(deletedMovie)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.wrapAsExpression
import java.util.*

const val MOVIE_SEQUELID = 8
const val MIN_MOVIE_RATING = 5
const val MOVIE_RATING = 4.2

class ReadExamples {
fun readAll() {
// Read all movies
Expand All @@ -37,7 +41,7 @@ class ReadExamples {
// Get an entity by its id value
val movie = StarWarsFilmEntity.findById(2)

if(movie != null) {
if (movie != null) {
// Read a property value
val movieName = movie.name
println("Created a new film named $movieName")
Expand All @@ -48,7 +52,7 @@ class ReadExamples {
}

// Read all with a condition
val specificMovie = StarWarsFilmEntity.find { StarWarsFilmsTable.sequelId eq 8 }
val specificMovie = StarWarsFilmEntity.find { StarWarsFilmsTable.sequelId eq MOVIE_SEQUELID }
specificMovie.forEach({ println("Found a movie with sequelId 8 and name " + it.name) })
}

Expand All @@ -57,7 +61,7 @@ class ReadExamples {
val query = UsersTable.innerJoin(UserRatingsTable).innerJoin(StarWarsFilmsTable)
.select(UsersTable.columns)
.where {
StarWarsFilmsTable.sequelId eq 2 and (UserRatingsTable.value greater 5)
StarWarsFilmsTable.sequelId eq MOVIE_SEQUELID and (UserRatingsTable.value greater MIN_MOVIE_RATING)
}.withDistinct()

val users = UserEntity.wrapRows(query).toList()
Expand Down Expand Up @@ -94,12 +98,11 @@ class ReadExamples {

fun readComputedField() {
StarWarsWFilmWithRankEntity.new {
sequelId = 8
sequelId = MOVIE_SEQUELID
name = "The Last Jedi"
rating = 4.2
rating = MOVIE_RATING
}

StarWarsWFilmWithRankEntity.find { StarWarsFilmsTable.name like "The%" }.map { it.name to it.rank }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import org.example.tables.StarWarsFilmsTable
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq

class UpdateExamples {
fun updateFilmProperty() {
val movie = StarWarsFilmEntity.findById(2)
if (movie != null) {
movie.name = "Episode VIII – The Last Jedi"
}
}
fun updateFilms() {
// Find by id and update
val updatedMovie = StarWarsFilmEntity.findByIdAndUpdate(5) {
val updatedMovie = StarWarsFilmEntity.findByIdAndUpdate(MOVIE_ID) {
it.name = "Episode VIII – The Last Jedi"
}
println(updatedMovie?.name)
Expand All @@ -18,5 +24,4 @@ class UpdateExamples {
}
println(updatedMovie2?.name)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package org.example.tables

import org.jetbrains.exposed.dao.id.IntIdTable

const val MAX_VARCHAR_LENGTH = 50

object CitiesTable : IntIdTable() {
val name = varchar("name", 50)
val name = varchar("name", MAX_VARCHAR_LENGTH)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.sql.Column

const val MAX_ID_LENGTH = 32
const val MAX_VARCHAR_LENGTH = 50

object DirectorsCustomTable : IdTable<String>() {
override val id: Column<EntityID<String>> = varchar("id", 32).entityId()
val name = varchar("name", 50)
override val id: Column<EntityID<String>> = varchar("id", MAX_ID_LENGTH).entityId()
val name = varchar("name", MAX_VARCHAR_LENGTH)

override val primaryKey = PrimaryKey(id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package org.example.tables

import org.jetbrains.exposed.dao.id.CompositeIdTable

const val MAX_VARCHAR_LENGTH = 32
enum class Genre { HORROR, DRAMA, THRILLER, SCI_FI }

object DirectorsTable : CompositeIdTable("directors") {
val name = varchar("name", 50).entityId()
val name = varchar("name", MAX_VARCHAR_LENGTH).entityId()
val guildId = uuid("guild_id").autoGenerate().entityId()
val genre = enumeration<Genre>("genre")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package org.example.tables

import org.jetbrains.exposed.dao.id.CompositeIdTable

const val MAX_VARCHAR_LENGTH = 50

object DirectorsWithGuildRefTable : CompositeIdTable() {
val name = varchar("name", 50).entityId()
val name = varchar("name", MAX_VARCHAR_LENGTH).entityId()
val guildId = reference("guild_id", GuildsTable)
val genre = enumeration<Genre>("genre")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Rank
import org.jetbrains.exposed.sql.SortOrder

const val MAX_VARCHAR_LENGTH = 32

object StarWarsWFilmsWithRankTable : IntIdTable() {
val sequelId = integer("sequel_id").uniqueIndex()
val name = varchar("name", 50)
val name = varchar("name", MAX_VARCHAR_LENGTH)
val rating = double("rating")

val rank = Rank().over().orderBy(rating, SortOrder.DESC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package org.example.tables

import org.jetbrains.exposed.dao.id.IntIdTable

const val MAX_VARCHAR_LENGTH = 50

object UsersTable : IntIdTable() {
val name = varchar("name", 50)
val name = varchar("name", MAX_VARCHAR_LENGTH)
val cityId = reference("cityId", CitiesTable.id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@
<p>
You can update the value of a property just as you would with any property in a Kotlin class:
</p>
<code-block lang="kotlin">
movie.name = "Episode VIII – The Last Jedi"
</code-block>
<code-block lang="kotlin" src="exposed-dao/src/main/kotlin/org/example/examples/UpdateExamples.kt" include-lines="11"/>
<note>
Exposed doesn't make an immediate update when you set a new value for <code>Entity</code>, it just stores it on the inner map.
"Flushing" values to the database occurs at the end of the transaction, or before the next <code>SELECT *</code> from the database.
Expand Down Expand Up @@ -150,10 +148,10 @@
<code>searchQuery()</code>. The results of the function can then be accessed through a property of the entity class:</p>
<tabs>
<tab id="entity-class" title="StarWarsWFilmWithRankEntity">
<code-block lang="kotlin" src="exposed-dao/src/main/kotlin/org/example/entities/ComputedFieldEntity.kt"/>
<code-block lang="kotlin" src="exposed-dao/src/main/kotlin/org/example/entities/StarWarsWFilmWithRankEntity.kt"/>
</tab>
<tab id="table-definition" title="StarWarsWFilmWithRankTable">
<code-block lang="kotlin" src="exposed-dao/src/main/kotlin/org/example/tables/ComputedFieldTable.kt"/>
<tab id="table-definition" title="StarWarsWFilmsWithRankTable">
<code-block lang="kotlin" src="exposed-dao/src/main/kotlin/org/example/tables/StarWarsWFilmsWithRankTable.kt"/>
</tab>
</tabs>
<p>
Expand Down
4 changes: 2 additions & 2 deletions documentation-website/Writerside/topics/DAO-Table-Types.topic
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@
</p>
<tabs>
<tab id="DirectorsCustomTable-definition" title="DirectorsCustomTable">
<code-block lang="kotlin" src="exposed-dao/src/main/kotlin/org/example/tables/CustomColumnTypeTable.kt"/>
<code-block lang="kotlin" src="exposed-dao/src/main/kotlin/org/example/tables/DirectorsCustomTable.kt"/>
</tab>
<tab id="DirectorCustomEntity-definition" title="DirectorCustomEntity">
<code-block lang="kotlin" src="exposed-dao/src/main/kotlin/org/example/entities/CustomColumnTypeEntity.kt"/>
<code-block lang="kotlin" src="exposed-dao/src/main/kotlin/org/example/entities/DirectorCustomEntity.kt"/>
</tab>
</tabs>
<p>
Expand Down

0 comments on commit 15b663c

Please sign in to comment.