Skip to content

Commit

Permalink
Add Gradle instructions to custom SQL database guide
Browse files Browse the repository at this point in the history
  • Loading branch information
zaleslaw committed Dec 5, 2024
1 parent af370d7 commit 43415ad
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions docs/StardustDocs/topics/readSqlFromCustomDatabase.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@

# How to Extend DataFrame Library for Custom SQL Database Support: Example with HSQLDB

This guide demonstrates how advanced users can extend the Kotlin DataFrame library to support a custom SQL database, using HSQLDB as an example. By following these steps, you will be able to integrate your custom database into the DataFrame library, allowing for seamless DataFrame creation, manipulation, and querying.
This guide demonstrates how advanced users can extend the Kotlin DataFrame library to support a custom SQL database,
using HSQLDB as an example. By following these steps,
you will be able to integrate your custom database into the DataFrame library,
allowing for seamless DataFrame creation, manipulation, and querying.

This guide is intended for Gradle projects,
but the experience will be similar in Kotlin Notebooks,
as demonstrated in this [Kotlin DataFrame SQL Example](https://github.com/zaleslaw/KotlinDataFrame-SQL-Examples/blob/master/notebooks/customdb.ipynb).

---

## Prerequisites

1. **Create a Gradle Project**:

Add the following dependencies to your `build.gradle.kts`:
Add the following dependencies and dataframe plugin to your `build.gradle.kts`:

```kotlin
plugins {
id("org.jetbrains.kotlinx.dataframe") version "$dataframe_version"
}

dependencies {
implementation("org.jetbrains.kotlinx:dataframe:$dataframe_version")
implementation("org.hsqldb:hsqldb:$version")
Expand All @@ -39,14 +50,6 @@ To enable HSQLDB integration, implement a custom `DbType` by overriding required
**Create the HSQLDB Type**

```kotlin
import org.jetbrains.kotlinx.dataframe.io.TableColumnMetadata
import org.jetbrains.kotlinx.dataframe.io.TableMetadata
import org.jetbrains.kotlinx.dataframe.io.db.DbType
import org.jetbrains.kotlinx.dataframe.schema.ColumnSchema
import java.sql.ResultSet
import java.util.Locale
import kotlin.reflect.KType

/**
* Represents the HSQLDB database type.
*
Expand Down Expand Up @@ -87,10 +90,9 @@ public object HSQLDB : DbType("hsqldb") {
**Defining Helper Functions**

Define utility functions to manage database connections and tables.
For example purposes, we create a small function that can populate the table with a schema and some sample data.

```kotlin
import java.sql.Connection

const val URL = "jdbc:hsqldb:hsql://localhost/testdb"
const val USER_NAME = "SA"
const val PASSWORD = ""
Expand All @@ -102,7 +104,7 @@ fun removeTable(con: Connection): Int {
}

fun createAndPopulateTable(con: Connection) {
var stmt = con.createStatement()
val stmt = con.createStatement()
stmt.executeUpdate(
"""CREATE TABLE IF NOT EXISTS orders (
id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
Expand Down Expand Up @@ -148,9 +150,9 @@ fun main() {
DriverManager.getConnection(URL, USER_NAME, PASSWORD).use { con ->
createAndPopulateTable(con)

val df = con.readDataFrame("SELECT * FROM orders", dbType = HSQLDB).rename {
all()
}.into { it.name.lowercase(Locale.getDefault()).toCamelCaseByDelimiters(DELIMITERS_REGEX) }
val df = con
.readDataFrame("SELECT * FROM orders", dbType = HSQLDB)
.rename { all() }.into { it.name.lowercase(Locale.getDefault()).toCamelCaseByDelimiters(DELIMITERS_REGEX) }
.cast<Orders>(verify = true)

df.filter { it.price > 800 }.print()
Expand All @@ -160,7 +162,7 @@ fun main() {
}
```

Running the above main function will output filtered rows from the `orders` table where `price > 800`.
Running the `main` function above will output filtered rows from the `orders` table where `price > 800`.

It will also demonstrate how to define and use custom SQL database extensions in the DataFrame library.

Expand Down

0 comments on commit 43415ad

Please sign in to comment.