Skip to content

Commit

Permalink
Updated to 2.0, improved Java interop, deprecated old methods. Major …
Browse files Browse the repository at this point in the history
…syntax changes.
  • Loading branch information
Sxtanna committed Jul 30, 2017
1 parent c7ed812 commit c83514d
Show file tree
Hide file tree
Showing 34 changed files with 1,379 additions and 584 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
*.iml
out
gen

dependency-reduced-pom.xml
57 changes: 45 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ MySQL Kotlin wrapper based on HikariCP
#### From Kotlin
``` kotlin
val kuery = Kuery[file: File]
val kuery = Kuery[config: KueryConfig]
```

#### From Java
``` java
final Kuery kuery = Kuery.get(file: File);
final Kuery kuery = Kuery.get(config: KueryConfig);
```

### 1-1. To initialize and shutdown a database use these two methods
``` kotlin
``` java
kuery.enable()
```
and
``` kotlin
``` java
kuery.disable()
```

Expand All @@ -45,23 +47,54 @@ val resource = kuery.resource()
``` java
final Connection resource = kuery.resource();
```
*[Database#resource](https://github.com/KDatabases/Core/blob/master/src/main/kotlin/com.sxtanna/database/base/Database.kt#L96) will throw an IllegalStateException if it's unable to create a resource a/o the database isn't enabled*
*[Database#resource](https://github.com/KDatabases/Core/blob/master/src/main/kotlin/com.sxtanna/database/base/Database.kt#L96) will throw an IllegalStateException if it's unable to into a resource a/o the database isn't enabled*


### 2-1. Or you could utilize the Database's ability to automatically manage the connection with the *invoke* methods
### 2-1. Or you could utilize the Database's ability to automatically manage the connection with various sql functions

#### From Kotlin
``` kotlin
kuery {
createTable("Table", "UUID" co Char(36, true), "Name" co VarChar(36))
#### Creating a Table
##### From Kotlin
``` java
kuery {
create("User", "name" co VarChar.of(255, true)) // deprecated in favour of the cacheable builders
ex.
Create.table("User").co("name", VarChar(255, true))()
}
```

#### From Java
##### From Java
``` java
kuery.execute(task -> task.createTable("Users", Column.tableColumns(Column.col("UUID", new SqlType.Char(36, true)), Column.col("Name", new SqlType.VarChar(16)))));
kuery.execute(task -> {
task.execute(Create.table("User").co("name", new VarChar(255, true)));
});
```
*Yes, I know the Java version is ugly AF, but this can be slightly fixed with static imports*

##### Storing a Create Statement (syntax is nearly the same for Kotlin and Java)
``` java
val createUser = Create.table("User").co("name", VarChar.of(255, true))

vs

final CreateBuilder createUser = Create.table("User").co("name", VarChar.of(255, true));
```
#### Using them however is quite different
##### From Kotlin
``` java
kuery {
createUser()
}
```
##### From Java
``` java
kuery.execute(task -> task.createTable("Users", tableColumns(col("UUID", new Char(36, true)), col("Name", new VarChar(16)))));
kuery.execute(task -> {
task.execute(createUser);
});
```

## More examples soon.
#### For Java examples.
- [JKueryTest](src/test/java/com/sxtanna/database/tests/JKueryTest.java)
- [JKueryTestOrm](src/test/java/com/sxtanna/database/tests/JKueryTestOrm.java)
#### For Kotlin examples.
- [KKueryTest](src/test/kotlin/com/sxtanna/database/tests/KKueryTest.kt)
- [KKueryTestOrm](src/test/kotlin/com/sxtanna/database/tests/KKueryTestOrm.kt)
18 changes: 12 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
<parent>
<artifactId>Super</artifactId>
<groupId>com.sxtanna.database</groupId>
<version>1.3.1</version>
<version>2.0</version>
<relativePath>../Super/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>Kuery</artifactId>
<version>1.3.4</version>
<version>2.0</version>
<packaging>jar</packaging>

<name>${project.artifactId}</name>
<description>MySQL Kotlin wrapper based on HikariCP</description>
<url>https://github.com/KDatabases/Keury</url>
<url>https://github.com/KDatabases/Kuery</url>

<scm>
<url>git://github.com/KDatabases/Keury.git</url>
<connection>scm:git:git://github.com/KDatabases/Keury.git</connection>
<url>git://github.com/KDatabases/Kuery.git</url>
<connection>scm:git:git://github.com/KDatabases/Kuery.git</connection>
</scm>

<dependencies>
<dependency>
<groupId>com.sxtanna.database</groupId>
<artifactId>Core</artifactId>
<version>1.2</version>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
Expand All @@ -44,6 +44,12 @@
<artifactId>mysql-connector-java</artifactId>
<version>${versionMySQLConnector}</version>
</dependency>

<dependency>
<groupId>com.sxtanna.database</groupId>
<artifactId>Tests</artifactId>
<version>1.0</version>
</dependency>
</dependencies>


Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/com/sxtanna/database/Kuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ import com.sxtanna.database.base.Database
import com.sxtanna.database.config.DatabaseConfigManager
import com.sxtanna.database.config.KueryConfig
import com.sxtanna.database.ext.loadOrSave
import com.sxtanna.database.struct.base.Creator
import com.sxtanna.database.task.KueryTask
import com.sxtanna.database.type.SqlObject
import com.sxtanna.database.type.base.SqlObject
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import java.io.File
import java.sql.Connection
import java.sql.ResultSet
import kotlin.reflect.KClass

class Kuery(override val config : KueryConfig) : Database<Connection, KueryConfig, KueryTask>() {
class Kuery private constructor(override val config : KueryConfig) : Database<Connection, KueryConfig, KueryTask>() {

override val name : String = "Kuery"
lateinit var pool : HikariDataSource
private set

val debug get() = config.debug ?: false
val debug by lazy { config.debug ?: false }
val creators = mutableMapOf<KClass<out SqlObject>, ResultSet.() -> SqlObject>()

override fun load() {
Expand Down Expand Up @@ -60,11 +61,18 @@ class Kuery(override val config : KueryConfig) : Database<Connection, KueryConfi
creators[T::class] = creator
}

fun <T : SqlObject> addCreator(creator : Creator<T>) {
creators[creator.clazz] = creator
}


companion object : DatabaseConfigManager<KueryConfig, Kuery> {

@JvmStatic
override fun get(file : File) : Kuery = Kuery(getConfig(file))
override fun get(file : File) = super.get(file)

@JvmStatic
override fun get(config : KueryConfig) = Kuery(config)

@JvmStatic
override fun getConfig(file : File) = file.loadOrSave(KueryConfig.DEFAULT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sxtanna.database.struct.obj
package com.sxtanna.database.ext

import com.sxtanna.database.struct.obj.SqlType.*
import com.sxtanna.database.struct.obj.SqlType.Char
Expand Down
81 changes: 0 additions & 81 deletions src/main/kotlin/com/sxtanna/database/ext/extensions.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@file:JvmName("KData")

package com.sxtanna.database.ext

import com.sxtanna.database.struct.obj.Sort
import com.sxtanna.database.struct.obj.SqlType
import com.sxtanna.database.struct.obj.Target
import kotlin.reflect.KProperty1

/**
Expand Down Expand Up @@ -39,6 +43,11 @@ const val LONG_TEXT_SIZE = NORM_MAX_UNSIGN
/**
* Represents all rows in a select statement
*/
@get:JvmName("allRows")
val ALL_ROWS = arrayOf("*")
@get:JvmName("noSort")
val NO_SORTS = arrayOf<Sort>()
@get:JvmName("noWhere")
val NO_WHERE = arrayOf<Target>()

typealias Adapter = KProperty1<*, *>.() -> SqlType
71 changes: 71 additions & 0 deletions src/main/kotlin/com/sxtanna/database/ext/kext.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@file:JvmName("Kext")

package com.sxtanna.database.ext

import com.sxtanna.database.struct.base.Creator
import com.sxtanna.database.struct.base.Duo
import com.sxtanna.database.struct.obj.Sort
import com.sxtanna.database.struct.obj.Target
import com.sxtanna.database.task.builder.CreateBuilder
import com.sxtanna.database.task.builder.InsertBuilder
import com.sxtanna.database.task.builder.SelectBuilder
import com.sxtanna.database.task.builder.UpdateBuilder
import com.sxtanna.database.type.base.SqlObject
import java.sql.ResultSet
import java.util.function.Function
import java.util.function.Supplier


@JvmSynthetic
fun <O> attempt(catch : Boolean = false, block : () -> O) : O? {
try {
return block()
}
catch (e : Exception) {}
return null
}

@JvmOverloads
fun <O> attempt(catch : Boolean = false, block : Supplier<O>) : O? = attempt(catch) { block.get() }


fun Boolean.value(input : String?) = if (this) input ?: "" else ""

/**
* Special infix function for [Duo]
*
* @param value The value to into with this Duo
* @sample createColumns
*/
infix fun <T> String.co(value : T) = Duo(this, value)


fun <O : SqlObject> create(clazz : Class<O>, creator : Function<ResultSet, O>) = object : Creator<O>(clazz.kotlin) {
override fun apply(t : ResultSet) : O = creator.apply(t)
}


fun data(vararg any : Any) = any

fun sorts(vararg sorts : Sort) = sorts

fun targets(vararg targets : Target) = targets


//region Build Function Creators
@JvmSynthetic
inline fun <reified T : SqlObject> createTable(name : String = T::class.simpleName!!) = CreateBuilder.from<T>(name)

@JvmSynthetic
inline fun <reified T : SqlObject> selectFrom(table : String = T::class.simpleName!!, noinline block : SelectBuilder<T>.() -> Unit = {}) = SelectBuilder(T::class, table).apply(block)

@JvmSynthetic
inline fun <reified T : SqlObject> insertInto(table : String = T::class.simpleName!!, noinline block : InsertBuilder<T>.() -> Unit = {}) = InsertBuilder(T::class, table).apply(block)

@JvmSynthetic
inline fun <reified T : SqlObject> updateIn(table : String = T::class.simpleName!!, noinline block : UpdateBuilder<T>.() -> Unit = {}) = UpdateBuilder(T::class, table).apply(block)
//endregion


private fun createColumns() = Duo.valueColumns("One" co 1, "Two" co 2, "True" co true)

Loading

0 comments on commit c83514d

Please sign in to comment.