Skip to content

Commit

Permalink
Fix detekt issues and reference code by line numbers instead of whole…
Browse files Browse the repository at this point in the history
… functions
  • Loading branch information
vnikolova committed Dec 4, 2024
1 parent e69b7e9 commit b40da34
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ fun createTables() {
tables.forEach { table ->
SchemaUtils.create(table)
}

}

fun runCreateExamples() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ class AliasExamples {
fun useAlias() {
val filmTable1 = StarWarsFilmsTable.alias("ft1")
val allFilms = filmTable1.selectAll() // can be used in joins etc'
println(allFilms)

//use the same table in a join multiple times
// use the same table in a join multiple times
val sequelTable = StarWarsFilmsTable.alias("sql")
val originalAndSequelNames = StarWarsFilmsTable
.join(sequelTable, JoinType.INNER, StarWarsFilmsTable.sequelId, sequelTable[StarWarsFilmsTable.id])
.select(StarWarsFilmsTable.name, sequelTable[StarWarsFilmsTable.name])
.map { it[StarWarsFilmsTable.name] to it[sequelTable[StarWarsFilmsTable.name]] }
println(originalAndSequelNames)


//selecting from subqueries
// selecting from subqueries
val starWarsFilms = StarWarsFilmsTable
.select(StarWarsFilmsTable.id, StarWarsFilmsTable.name)
.alias("swf")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import org.example.tables.StarWarsFilmsTable
import org.example.tables.CitiesTable
import org.example.tables.StarWarsFilmsIntIdTable
import org.example.tables.StarWarsFilmsTable
import org.jetbrains.exposed.sql.*

/*
Expand All @@ -9,22 +9,22 @@ import org.jetbrains.exposed.sql.*
line numbers in the `code-block` element of the referenced file.
*/

const val MOVIE_SEQUEL_ID = 7
const val MOVIE_SEQUEL_2_ID = 8
private const val MOVIE_ORIGINAL_ID = 4
private const val MOVIE_SEQUEL_ID = 5
private const val MOVIE_SEQUEL_2_ID = 6

class CreateExamples {
fun createFilmRecords() {
/*
INSERT INTO STARWARSFILMS (SEQUEL_ID, "name", DIRECTOR)
VALUES (7, 'The Force Awakens', 'J.J. Abrams')
*/
/*
INSERT INTO STARWARSFILMS (SEQUEL_ID, "name", DIRECTOR)
VALUES (7, 'The Force Awakens', 'J.J. Abrams')
*/

val movie = StarWarsFilmsTable.insert {
StarWarsFilmsTable.insert {
it[sequelId] = MOVIE_SEQUEL_ID
it[name] = "The Force Awakens"
it[director] = "J.J. Abrams"
}
println(movie)
}

fun createIntIdFilmRecords() {
Expand All @@ -42,23 +42,22 @@ class CreateExamples {
}

fun insertIgnoreRecords() {
val insertRow = StarWarsFilmsTable.insert {
StarWarsFilmsTable.insert {
it[sequelId] = MOVIE_SEQUEL_2_ID // column pre-defined with a unique index
it[name] = "The Last Jedi"
it[director] = "Rian Johnson"
}

val insertIgnoreRows = StarWarsFilmsTable.insertIgnore {
StarWarsFilmsTable.insertIgnore {
it[sequelId] = MOVIE_SEQUEL_2_ID
it[name] = "The Last Jedi"
it[director] = "Rian Johnson"
}
println(insertIgnoreRows)

/*
INSERT IGNORE INTO STAR_WARS_FILMS_TABLE (SEQUEL_ID, "name", DIRECTOR)
VALUES (8, 'The Last Jedi', 'Rian Johnson')
*/
/*
INSERT IGNORE INTO STAR_WARS_FILMS_TABLE (SEQUEL_ID, "name", DIRECTOR)
VALUES (8, 'The Last Jedi', 'Rian Johnson')
*/

val rowId = StarWarsFilmsIntIdTable.insertIgnoreAndGetId {
it[sequelId] = MOVIE_SEQUEL_ID
Expand All @@ -70,7 +69,8 @@ class CreateExamples {

fun simpleBatchInsert() {
val cityNames = listOf("Paris", "Moscow", "Helsinki")
val allCitiesID = CitiesTable.batchInsert(cityNames) { name ->

CitiesTable.batchInsert(cityNames) { name ->
this[CitiesTable.name] = name
}
}
Expand All @@ -79,10 +79,10 @@ class CreateExamples {
data class SWFilmData(val sequelId: Int, val name: String, val director: String)

val films = listOf(
SWFilmData(5, "The Empire Strikes Back", "Irvin Kershner"),
SWFilmData(4, "A New Hope", "George Lucas"),
SWFilmData(6, "Return of the Jedi", "Richard Marquand")
)
SWFilmData(MOVIE_SEQUEL_ID, "The Empire Strikes Back", "Irvin Kershner"),
SWFilmData(MOVIE_ORIGINAL_ID, "A New Hope", "George Lucas"),
SWFilmData(MOVIE_SEQUEL_2_ID, "Return of the Jedi", "Richard Marquand")
)

StarWarsFilmsTable.batchInsert(films) { (id, name, director) ->
this[StarWarsFilmsTable.sequelId] = id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import org.jetbrains.exposed.sql.transactions.transaction
line numbers in the `code-block` element of the referenced file.
*/

private const val MOVIE_SEQUEL_ID = 8

fun Query.indexHint(hint: String) = IndexHintQuery(this, hint)

class IndexHintQuery(
Expand Down Expand Up @@ -42,14 +44,11 @@ class CustomSelectExamples {
val originalQuery = StarWarsFilmsTable
.selectAll()
.withDistinct()
.where { StarWarsFilmsTable.sequelId less 8 }
.where { StarWarsFilmsTable.sequelId less MOVIE_SEQUEL_ID }
.groupBy(StarWarsFilmsTable.id)

val queryWithHint = originalQuery
.indexHint("FORCE INDEX (PRIMARY)")
originalQuery.indexHint("FORCE INDEX (PRIMARY)")
.orderBy(StarWarsFilmsTable.sequelId)
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@ import org.jetbrains.exposed.sql.deleteAll
import org.jetbrains.exposed.sql.deleteIgnoreWhere
import org.jetbrains.exposed.sql.deleteWhere

private const val MOVIE_SEQUEL_ID = 6
private const val MOVIE_SEQUEL_2_ID = 7
private const val ACTORS_SEQUEL_ID = 2

class DeleteExamples {
fun delete() {
/*
DELETE FROM STARWARSFILMS WHERE STARWARSFILMS.SEQUEL_ID = 6
*/

val deletedRowsCount = StarWarsFilmsTable.deleteWhere { StarWarsFilmsTable.sequelId eq 6 }
val deletedRowsCount = StarWarsFilmsTable.deleteWhere { StarWarsFilmsTable.sequelId eq MOVIE_SEQUEL_ID }
println(deletedRowsCount)
}

fun deleteIgnore() {
val deleteIgnoreRowsCount = StarWarsFilmsTable.deleteIgnoreWhere { StarWarsFilmsTable.sequelId eq 7 }
val deleteIgnoreRowsCount = StarWarsFilmsTable.deleteIgnoreWhere { StarWarsFilmsTable.sequelId eq MOVIE_SEQUEL_2_ID }
println(deleteIgnoreRowsCount)
}

Expand All @@ -37,10 +41,10 @@ class DeleteExamples {
THEN DELETE
*/

val simpleJoin = StarWarsFilmsTable innerJoin ActorsTable
// val simpleJoin = StarWarsFilmsTable innerJoin ActorsTable
val join = StarWarsFilmsTable.join(ActorsTable, JoinType.INNER, StarWarsFilmsTable.id, ActorsTable.sequelId)

val deletedActorsCount = join.delete(ActorsTable) { ActorsTable.sequelId greater 2 }
val deletedActorsCount = join.delete(ActorsTable) { ActorsTable.sequelId greater ACTORS_SEQUEL_ID }
println(deletedActorsCount)
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
package org.example.examples

import org.example.tables.UsersTable
import org.example.tables.CitiesTable
import org.example.tables.UsersTable
import org.jetbrains.exposed.sql.*

private const val MAX_VARCHAR_LENGTH = 10

class InsertSelectExamples {
fun insertSelect() {
val substring = UsersTable.name.substring(1, 2)
val insertedRows = CitiesTable.insert(UsersTable.select(substring).orderBy(UsersTable.id).limit(2))
println(insertedRows)
}

fun insertSelectWithCol() {
val userCount = UsersTable.selectAll().count()
println(userCount)

val insertedUsers = UsersTable.insert(
UsersTable.select(
stringParam("Foo"),
Random().castTo<String>(VarCharColumnType()).substring(1, 10)
Random().castTo<String>(VarCharColumnType()).substring(1, MAX_VARCHAR_LENGTH)
),
columns = listOf(UsersTable.name, UsersTable.id)
)
println(insertedUsers)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import org.jetbrains.exposed.sql.JoinType
import org.jetbrains.exposed.sql.count
import org.jetbrains.exposed.sql.unionAll

/*
Important: The contents of this file are referenced by line number in `DSL-Joining-Tables.topic`.
If you add, remove, or modify any lines, ensure you update the corresponding
line numbers in the `code-block` element of the referenced file.
*/

class JoinExamples {
fun joinAndCount() {
ActorsIntIdTable.join(
StarWarsFilmsIntIdTable,
JoinType.INNER,
onColumn = ActorsIntIdTable.sequelId,
otherColumn = StarWarsFilmsIntIdTable.sequelId)
otherColumn = StarWarsFilmsIntIdTable.sequelId
)
.select(ActorsIntIdTable.name.count(), StarWarsFilmsIntIdTable.name)
.groupBy(StarWarsFilmsIntIdTable.name)
}
Expand All @@ -22,7 +29,8 @@ class JoinExamples {
ActorsIntIdTable.join(
StarWarsFilmsIntIdTable,
JoinType.INNER,
additionalConstraint = { StarWarsFilmsIntIdTable.sequelId eq ActorsIntIdTable.sequelId })
additionalConstraint = { StarWarsFilmsIntIdTable.sequelId eq ActorsIntIdTable.sequelId }
)
.select(ActorsIntIdTable.name.count(), StarWarsFilmsIntIdTable.name)
.groupBy(StarWarsFilmsIntIdTable.name)
}
Expand All @@ -47,9 +55,11 @@ class JoinExamples {
val abramsDirectedQuery =
StarWarsFilmsIntIdTable.select(StarWarsFilmsIntIdTable.name).where { StarWarsFilmsIntIdTable.director eq "J.J. Abrams" }
val filmNames = lucasDirectedQuery.union(abramsDirectedQuery).map { it[StarWarsFilmsIntIdTable.name] }
println(filmNames)

val originalTrilogyQuery =
StarWarsFilmsIntIdTable.select(StarWarsFilmsIntIdTable.name).where { StarWarsFilmsIntIdTable.sequelId inList (3..5) }
val allFilmNames = lucasDirectedQuery.unionAll(originalTrilogyQuery).map { it[StarWarsFilmsIntIdTable.name] }
println(allFilmNames)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import org.jetbrains.exposed.sql.SqlExpressionBuilder.times
import org.jetbrains.exposed.sql.kotlin.datetime.CurrentDateTime
import org.jetbrains.exposed.sql.kotlin.datetime.datetime

private const val TITLE_MAX_LENGTH = 64
private const val MOVIE_SEQUEL_3_ID = 8
private const val PROJECT_BUDGET = 100
private const val INCREASE_BUDGET_BY = 100

object Projects : Table("projects") {
val title = varchar("title", 64)
val title = varchar("title", TITLE_MAX_LENGTH)
val budget = integer("budget")
val created = datetime("created").defaultExpression(CurrentDateTime)
}
Expand All @@ -17,23 +22,26 @@ class ModifiedRowsExamples {
fun getInsertedCount() {
val insertStatement = StarWarsFilmsTable.insertIgnore {
it[name] = "The Last Jedi"
it[sequelId] = 8
it[sequelId] = MOVIE_SEQUEL_3_ID
it[director] = "Rian Johnson"
}
val rowCount: Int = insertStatement.insertedCount
println(rowCount)
}

fun returnData() {
// returns all table columns by default
val createdProjects: LocalDateTime = Projects.insertReturning {
it[title] = "Project A"
it[budget] = 100
it[budget] = PROJECT_BUDGET
}.single()[Projects.created]
println(createdProjects)

val updatedBudgets: List<Int> = Projects.updateReturning(listOf(Projects.budget)) {
it[budget] = Projects.budget.times(5)
it[budget] = Projects.budget.times(INCREASE_BUDGET_BY)
}.map {
it[Projects.budget]
}
println(updatedBudgets)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import org.jetbrains.exposed.sql.transactions.transaction
If you add, remove, or modify any lines, ensure you update the corresponding
line numbers and/or names in the `code-block` element of the referenced file.
*/

private const val MOVIE_ORIGINAL_ID = 4
private const val MOVIE_SEQUEL_ID = 5
private const val MOVIE_SEQUEL_2_ID = 6
private const val MOVIE_SEQUEL_3_ID = 8

class QueryingExamples {

fun useWhereConditions() {
Expand All @@ -31,14 +37,14 @@ class QueryingExamples {
println(allMatchingRegex.toList())

val allBetween = StarWarsFilmsTable.selectAll()
.where { StarWarsFilmsTable.sequelId.between(4, 6) }
.where { StarWarsFilmsTable.sequelId.between(MOVIE_ORIGINAL_ID, MOVIE_SEQUEL_2_ID) }
println(allBetween.toList())

val allInList = StarWarsFilmsTable.selectAll()
.where { StarWarsFilmsTable.sequelId inList listOf(6, 4) }
.where { StarWarsFilmsTable.sequelId inList listOf(MOVIE_SEQUEL_2_ID, MOVIE_ORIGINAL_ID) }
println(allInList.toList())

val topRated = listOf(5 to "Empire Strikes Back", 4 to "A New Hope")
val topRated = listOf(MOVIE_SEQUEL_ID to "Empire Strikes Back", MOVIE_ORIGINAL_ID to "A New Hope")
val multipleInList = StarWarsFilmsTable.selectAll()
.where {
StarWarsFilmsTable.sequelId to StarWarsFilmsTable.name inList topRated
Expand All @@ -47,15 +53,15 @@ class QueryingExamples {

val anyFromArray = StarWarsFilmsTable.selectAll()
.where {
StarWarsFilmsTable.sequelId eq anyFrom(arrayOf(6, 4))
StarWarsFilmsTable.sequelId eq anyFrom(arrayOf(MOVIE_SEQUEL_2_ID, MOVIE_ORIGINAL_ID))
}
println(anyFromArray.toList())
}

fun aggregateAndSort() {
val count = StarWarsFilmsTable.selectAll()
.where {
StarWarsFilmsTable.sequelId eq 8
StarWarsFilmsTable.sequelId eq MOVIE_SEQUEL_3_ID
}
.count()
println(count)
Expand Down Expand Up @@ -94,19 +100,19 @@ class QueryingExamples {
}

fun findWithConditionalJoin(actorName: String?) {
transaction {
val query = StarWarsFilmsTable.selectAll() // Base query

// Conditionally adjust the query
actorName?.let { name ->
query.adjustColumnSet {
innerJoin(ActorsTable, { StarWarsFilmsTable.sequelId }, { ActorsTable.sequelId })
}
.adjustSelect {
select(StarWarsFilmsTable.columns + ActorsTable.columns)
}
.andWhere { ActorsTable.name eq name }
}
}
transaction {
val query = StarWarsFilmsTable.selectAll() // Base query

// Conditionally adjust the query
actorName?.let { name ->
query.adjustColumnSet {
innerJoin(ActorsTable, { StarWarsFilmsTable.sequelId }, { ActorsTable.sequelId })
}
.adjustSelect {
select(StarWarsFilmsTable.columns + ActorsTable.columns)
}
.andWhere { ActorsTable.name eq name }
}
}
}
}
Loading

0 comments on commit b40da34

Please sign in to comment.