commit 60d2a3221aa776b960ece927ed80a1e51ca48a25 Author: Julien Lengrand-Lambert Date: Wed Mar 4 13:25:21 2020 +0100 Create repository diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ee85f37 --- /dev/null +++ b/.gitignore @@ -0,0 +1,62 @@ +.DS_Store +.idea/shelf +/confluence/target +/dependencies/repo +/android.tests.dependencies +/dependencies/android.tests.dependencies +/dist +/local +/gh-pages +/ideaSDK +/clionSDK +/android-studio/sdk +out/ +/tmp +workspace.xml +*.versionsBackup +/idea/testData/debugger/tinyApp/classes* +/jps-plugin/testData/kannotator +/js/js.translator/testData/out/ +/js/js.translator/testData/out-min/ +/js/js.translator/testData/out-pir/ +.gradle/ +build/ +!**/src/**/build +!**/test/**/build +*.iml +!**/testData/**/*.iml +.idea/libraries/Gradle*.xml +.idea/libraries/Maven*.xml +.idea/artifacts/PILL_*.xml +.idea/artifacts/KotlinPlugin.xml +.idea/modules +.idea/runConfigurations/JPS_*.xml +.idea/runConfigurations/PILL_*.xml +.idea/libraries +.idea/modules.xml +.idea/gradle.xml +.idea/compiler.xml +.idea/inspectionProfiles/profiles_settings.xml +.idea/.name +.idea/artifacts/dist_auto_* +.idea/artifacts/dist.xml +.idea/artifacts/ideaPlugin.xml +.idea/artifacts/kotlinc.xml +.idea/artifacts/kotlin_compiler_jar.xml +.idea/artifacts/kotlin_plugin_jar.xml +.idea/artifacts/kotlin_jps_plugin_jar.xml +.idea/artifacts/kotlin_daemon_client_jar.xml +.idea/artifacts/kotlin_imports_dumper_compiler_plugin_jar.xml +.idea/artifacts/kotlin_main_kts_jar.xml +.idea/artifacts/kotlin_compiler_client_embeddable_jar.xml +.idea/artifacts/kotlin_reflect_jar.xml +.idea/jarRepositories.xml +kotlin-ultimate/ +node_modules/ +.rpt2_cache/ +libraries/tools/kotlin-test-js-runner/lib/ +libraries/tools/kotlin-source-map-loader/lib/ +local.properties + +datasets/* +!datasets/.gitkeep \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..1bec35e --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..2c657d0 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b5b3635 --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..4adfd6a --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +# Exposed Imdb + +A simple project to learn more about [exposed](https://github.com/JetBrains/Exposed), using the [imdb datasets](https://datasets.imdbws.com/). + +Please note that if you use this project, you have to comply with the [Imbd license](https://www.imdb.com/interfaces/). + +## LICENSE + +In short, [CC BY-NC-SA 4.0](https://tldrlegal.com/license/creative-commons-attribution-noncommercial-sharealike-4.0-international-(cc-by-nc-sa-4.0)). + +See [LICENSE](/LICENSE) + +## Learnings + + +* Use `;DB_CLOSE_DELAY=-1` if you want to persist the in-memory database information over more than a single transaction. + * `Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver", user = "root", password = "")` + + +## Author + +* [Julien Lengrand-Lambert](https://github.com/jlengrand/) \ No newline at end of file diff --git a/README_imdb b/README_imdb new file mode 100644 index 0000000..1097f1b --- /dev/null +++ b/README_imdb @@ -0,0 +1,71 @@ + IMDb Datasets + +Subsets of IMDb data are available for access to customers for personal and non-commercial use. You can hold local copies of this data, and it is subject to our terms and conditions. Please refer to the Non-Commercial Licensing and copyright/license and verify compliance. + +Data Location + +The dataset files can be accessed and downloaded from https://datasets.imdbws.com/. The data is refreshed daily. + +IMDb Dataset Details + +Each dataset is contained in a gzipped, tab-separated-values (TSV) formatted file in the UTF-8 character set. The first line in each file contains headers that describe what is in each column. A ‘\N’ is used to denote that a particular field is missing or null for that title/name. The available datasets are as follows: + +title.akas.tsv.gz - Contains the following information for titles: + + titleId (string) - a tconst, an alphanumeric unique identifier of the title + ordering (integer) – a number to uniquely identify rows for a given titleId + title (string) – the localized title + region (string) - the region for this version of the title + language (string) - the language of the title + types (array) - Enumerated set of attributes for this alternative title. One or more of the following: "alternative", "dvd", "festival", "tv", "video", "working", "original", "imdbDisplay". New values may be added in the future without warning + attributes (array) - Additional terms to describe this alternative title, not enumerated + isOriginalTitle (boolean) – 0: not original title; 1: original title + +title.basics.tsv.gz - Contains the following information for titles: + + tconst (string) - alphanumeric unique identifier of the title + titleType (string) – the type/format of the title (e.g. movie, short, tvseries, tvepisode, video, etc) + primaryTitle (string) – the more popular title / the title used by the filmmakers on promotional materials at the point of release + originalTitle (string) - original title, in the original language + isAdult (boolean) - 0: non-adult title; 1: adult title + startYear (YYYY) – represents the release year of a title. In the case of TV Series, it is the series start year + endYear (YYYY) – TV Series end year. ‘\N’ for all other title types + runtimeMinutes – primary runtime of the title, in minutes + genres (string array) – includes up to three genres associated with the title + +title.crew.tsv.gz – Contains the director and writer information for all the titles in IMDb. Fields include: + + tconst (string) - alphanumeric unique identifier of the title + directors (array of nconsts) - director(s) of the given title + writers (array of nconsts) – writer(s) of the given title + +title.episode.tsv.gz – Contains the tv episode information. Fields include: + + tconst (string) - alphanumeric identifier of episode + parentTconst (string) - alphanumeric identifier of the parent TV Series + seasonNumber (integer) – season number the episode belongs to + episodeNumber (integer) – episode number of the tconst in the TV series + +title.principals.tsv.gz – Contains the principal cast/crew for titles + + tconst (string) - alphanumeric unique identifier of the title + ordering (integer) – a number to uniquely identify rows for a given titleId + nconst (string) - alphanumeric unique identifier of the name/person + category (string) - the category of job that person was in + job (string) - the specific job title if applicable, else '\N' + characters (string) - the name of the character played if applicable, else '\N' + +title.ratings.tsv.gz – Contains the IMDb rating and votes information for titles + + tconst (string) - alphanumeric unique identifier of the title + averageRating – weighted average of all the individual user ratings + numVotes - number of votes the title has received + +name.basics.tsv.gz – Contains the following information for names: + + nconst (string) - alphanumeric unique identifier of the name/person + primaryName (string)– name by which the person is most often credited + birthYear – in YYYY format + deathYear – in YYYY format if applicable, else '\N' + primaryProfession (array of strings)– the top-3 professions of the person + knownForTitles (array of tconsts) – titles the person is known for \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..3b31952 --- /dev/null +++ b/build.gradle @@ -0,0 +1,28 @@ +plugins { + id 'org.jetbrains.kotlin.jvm' version '1.3.70-eap-42' +} + +group 'org.example' +version '1.0-SNAPSHOT' + +repositories { + maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' } +// maven { url 'https://dl.bintray.com/kotlin/exposed' } + mavenCentral() + jcenter() +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + compile "org.jetbrains.exposed:exposed-core:0.21.1" + compile "org.jetbrains.exposed:exposed-dao:0.21.1" + compile "org.jetbrains.exposed:exposed-jdbc:0.21.1" + compile "com.h2database:h2:1.4.200" +} + +compileKotlin { + kotlinOptions.jvmTarget = "1.8" +} +compileTestKotlin { + kotlinOptions.jvmTarget = "1.8" +} \ No newline at end of file diff --git a/datasets/.gitkeep b/datasets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..26643a7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' + +services: + + mysql-development: + image: mysql:8.0.19 + environment: + MYSQL_ROOT_PASSWORD: aRootPassword + MYSQL_DATABASE: imdb + ports: + - "3308:3306" diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..29e08e8 --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..f3d88b1 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..982785a --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Feb 26 17:28:32 CET 2020 +distributionUrl=https\://services.gradle.org/distributions/gradle-6.1-all.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..2fe81a7 --- /dev/null +++ b/gradlew @@ -0,0 +1,183 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..24467a1 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,100 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..ff8986c --- /dev/null +++ b/settings.gradle @@ -0,0 +1,11 @@ +pluginManagement { + repositories { + maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' } + + mavenCentral() + + maven { url 'https://plugins.gradle.org/m2/' } + } +} +rootProject.name = 'exposed-imdb' + diff --git a/src/main/kotlin/dsl/Titles.kt b/src/main/kotlin/dsl/Titles.kt new file mode 100644 index 0000000..4f238c5 --- /dev/null +++ b/src/main/kotlin/dsl/Titles.kt @@ -0,0 +1,33 @@ +package dsl + +import org.jetbrains.exposed.dao.IntEntity +import org.jetbrains.exposed.dao.IntEntityClass +import org.jetbrains.exposed.dao.id.EntityID +import org.jetbrains.exposed.dao.id.IntIdTable +import org.jetbrains.exposed.sql.Column + +object Titles : IntIdTable(){ + val tconst : Column = varchar("tconst", 10).uniqueIndex() + val titleType : Column = varchar("titleType", 50) // Own Table? + val primaryTitle : Column = varchar("primaryTitle", 50) + val originalTitle : Column = varchar("originalTitle", 50) + val isAdult : Column = bool("isAdult") + val startYear : Column = integer("startYear") + val endYear : Column = integer("endYear").nullable() + val runtimeMinutes : Column = long("runtimeMinutes") + val genres : Column = varchar("genres", 50) // Own Table? +} + +class Title(id: EntityID) : IntEntity(id) { + companion object : IntEntityClass(Titles) + + var tconst by Titles.tconst + var titleType by Titles.titleType + var primaryTitle by Titles.primaryTitle + var originalTitle by Titles.originalTitle + var isAdult by Titles.isAdult + var startYear by Titles.startYear + var endYear by Titles.endYear + var runtimeMinutes by Titles.runtimeMinutes + var genres by Titles.genres +} \ No newline at end of file diff --git a/src/main/kotlin/loader/LoadImdb.kt b/src/main/kotlin/loader/LoadImdb.kt new file mode 100644 index 0000000..2337f08 --- /dev/null +++ b/src/main/kotlin/loader/LoadImdb.kt @@ -0,0 +1,9 @@ +package loader + +import org.jetbrains.exposed.sql.Database + +fun main(){ + + var db = Database.connect("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", driver = "org.h2.Driver", user = "root", password = "") + println("Running loader") +} \ No newline at end of file diff --git a/src/main/kotlin/loader/Titlebasics.kt b/src/main/kotlin/loader/Titlebasics.kt new file mode 100644 index 0000000..3f1f977 --- /dev/null +++ b/src/main/kotlin/loader/Titlebasics.kt @@ -0,0 +1,66 @@ +package loader + +import dsl.Titles +import org.jetbrains.exposed.dao.IntEntity +import org.jetbrains.exposed.dao.IntEntityClass +import org.jetbrains.exposed.dao.id.EntityID +import org.jetbrains.exposed.dao.id.IntIdTable +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.transactions.transaction +import java.io.File + +val NO_DATA = "\\N"; + +class Titlebasics(private val db: Database) { + + fun createTable(){ + transaction(db) { SchemaUtils.create (Titles) } + } + + fun loadData(){ + val nameBasicsReader = File("./datasets/title.basics.tsv").bufferedReader() + nameBasicsReader.readLine() + + println("Title.Basics loaded") + + for (ptr in 1..10){ + val line = nameBasicsReader.readLine() + val items = line.split("\t") + println(items) + + transaction { + Titles.insert { + it[tconst] = items[0] + it[titleType] = items[1] + it[primaryTitle] = items[2] + it[originalTitle] = items[3] + it[isAdult] = items[4].toBoolean() + it[startYear] = items[5].toInt() + it[endYear] = if (items[6] != NO_DATA) items[6].toInt() else null + it[runtimeMinutes] = items[7].toLong() + it[genres] = items[8] + } + } + + // transaction { + // loader.Title.new { + // tconst = items[0] + // titleType = items[1] + // primaryTitle = items[2] + // originalTitle = items[3] + // isAdult = items[4].toBoolean() + // startYear = items[5].toInt() + // endYear = if (items[6] != dsl.getNO_DATA) items[6].toInt() else null + // runtimeMinutes = items[7].toLong() + // genres = items[8] + // } + // } + + println("--------") + } + } + + fun showAll() { + transaction(db) { Titles.selectAll().map { println(it) } } + } +} \ No newline at end of file diff --git a/src/main/kotlin/test.kt b/src/main/kotlin/test.kt new file mode 100644 index 0000000..12f5c4f --- /dev/null +++ b/src/main/kotlin/test.kt @@ -0,0 +1,120 @@ +import org.jetbrains.exposed.sql.Table + +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.transactions.transaction + +object Users : Table() { + val id = varchar("id", 10) // Column<String> + val name = varchar("name", length = 50) // Column<String> + val cityId = (integer("city_id") references Cities.id).nullable() // Column<Int?> + + override val primaryKey = PrimaryKey(id, name = "PK_User_ID") // name is optional here +} + +object Cities : Table() { + val id = integer("id").autoIncrement() // Column<Int> + val name = varchar("name", 50) // Column<String> + + override val primaryKey = PrimaryKey(id, name = "PK_Cities_ID") +} + +fun main() { + Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver", user = "root", password = "") + + transaction { + addLogger(StdOutSqlLogger) + + SchemaUtils.create (Cities, Users) + + val saintPetersburgId = Cities.insert { + it[name] = "St. Petersburg" + } get Cities.id + + val munichId = Cities.insert { + it[name] = "Munich" + } get Cities.id + + val pragueId = Cities.insert { + it.update(name, stringLiteral(" Prague ").trim().substring(1, 2)) + }[Cities.id] + + val pragueName = Cities.select { Cities.id eq pragueId }.single()[Cities.name] + + Users.insert { + it[id] = "andrey" + it[name] = "Andrey" + it[Users.cityId] = saintPetersburgId + } + + Users.insert { + it[id] = "sergey" + it[name] = "Sergey" + it[Users.cityId] = munichId + } + + Users.insert { + it[id] = "eugene" + it[name] = "Eugene" + it[Users.cityId] = munichId + } + + Users.insert { + it[id] = "alex" + it[name] = "Alex" + it[Users.cityId] = null + } + + Users.insert { + it[id] = "smth" + it[name] = "Something" + it[Users.cityId] = null + } + + Users.update({ Users.id eq "alex"}) { + it[name] = "Alexey" + } + + Users.deleteWhere{ Users.name like "%thing"} + + println("All cities:") + + for (city in Cities.selectAll()) { + println("${city[Cities.id]}: ${city[Cities.name]}") + } + + println("Manual join:") + (Users innerJoin Cities).slice(Users.name, Cities.name). + select {(Users.id.eq("andrey") or Users.name.eq("Sergey")) and + Users.id.eq("sergey") and Users.cityId.eq(Cities.id)}.forEach { + println("${it[Users.name]} lives in ${it[Cities.name]}") + } + + println("Join with foreign key:") + + + (Users innerJoin Cities).slice(Users.name, Users.cityId, Cities.name). + select { Cities.name.eq("St. Petersburg") or Users.cityId.isNull()}.forEach { + if (it[Users.cityId] != null) { + println("${it[Users.name]} lives in ${it[Cities.name]}") + } + else { + println("${it[Users.name]} lives nowhere") + } + } + + println("Functions and group by:") + + ((Cities innerJoin Users).slice(Cities.name, Users.id.count()).selectAll().groupBy(Cities.name)).forEach { + val cityName = it[Cities.name] + val userCount = it[Users.id.count()] + + if (userCount > 0) { + println("$userCount user(s) live(s) in $cityName") + } else { + println("Nobody lives in $cityName") + } + } + + SchemaUtils.drop (Users, Cities) + } +} \ No newline at end of file