Skip to content

Commit e60a99f

Browse files
committed
Code
1 parent 8437fcf commit e60a99f

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

src/main/kotlin/griffio/Main.kt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package griffio
2+
3+
import app.cash.sqldelight.db.QueryResult
4+
import app.cash.sqldelight.db.SqlDriver
5+
import app.cash.sqldelight.db.SqlPreparedStatement
6+
import app.cash.sqldelight.driver.jdbc.JdbcDriver
7+
import app.cash.sqldelight.driver.jdbc.asJdbcDriver
8+
import griffio.queries.Sample
9+
import org.postgresql.copy.CopyManager
10+
import org.postgresql.core.BaseConnection
11+
import org.postgresql.ds.PGSimpleDataSource
12+
import java.io.Reader
13+
import kotlin.io.path.Path
14+
import kotlin.io.path.reader
15+
16+
private fun getSqlDriver() = PGSimpleDataSource().apply {
17+
setURL("jdbc:postgresql://localhost:5432/laterals")
18+
applicationName = "App Main"
19+
}.asJdbcDriver()
20+
21+
// SqlDelight doesn't expose the generated query directly - only the driver has access to it for execution
22+
// Create a driver implementation for PostgreSql CopyManager to use the compiled query for execution
23+
class CopyInManagerDriver(private val driver: JdbcDriver, private val file: Reader) : SqlDriver by driver {
24+
override fun execute(
25+
identifier: Int?,
26+
sql: String,
27+
parameters: Int,
28+
binders: (SqlPreparedStatement.() -> Unit)?
29+
): QueryResult<Long> {
30+
// CopyManager utility class is the JDBC way to use the COPY command with STDIN
31+
// https://github.com/pgjdbc/pgjdbc/blob/master/pgjdbc/src/main/java/org/postgresql/copy/CopyManager.java
32+
val copyManager = CopyManager(getSqlDriver().getConnection() as BaseConnection)
33+
val copyIn = copyManager.copyIn(sql, file)
34+
return QueryResult.Value(copyIn)
35+
}
36+
}
37+
38+
fun main() {
39+
val driver = getSqlDriver()
40+
val sample = Sample(driver)
41+
val copySample = Sample(CopyInManagerDriver(driver, Path("csv/Kickstarter.csv").reader() ))
42+
copySample.kickStarterQueries.copy()
43+
println(sample.kickStarterQueries.select().executeAsList().joinToString("\n"))
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
CREATE TABLE Kickstarter_Data (
2+
pledged NUMERIC,
3+
fx_rate NUMERIC,
4+
backers_count INTEGER,
5+
launched_at NUMERIC,
6+
deadline NUMERIC,
7+
goal INTEGER
8+
);
9+
10+
CREATE TABLE Regions (
11+
id INTEGER,
12+
name VARCHAR(255)
13+
);
14+
15+
CREATE TABLE SalesPeople (
16+
id INTEGER,
17+
full_name VARCHAR(255),
18+
home_region_id INTEGER
19+
);
20+
21+
CREATE TABLE Sales (
22+
id INTEGER,
23+
amount NUMERIC,
24+
product_id INTEGER,
25+
salesperson_id INTEGER,
26+
region_id INTEGER
27+
);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
copy:
2+
COPY Kickstarter_Data (
3+
pledged,
4+
fx_rate,
5+
backers_count,
6+
launched_at,
7+
deadline,
8+
goal
9+
)
10+
FROM STDIN
11+
WITH (FORMAT csv, HEADER TRUE, DELIMITER ',');
12+
13+
select:
14+
SELECT
15+
pledged_usd,
16+
avg_pledge_usd,
17+
duration,
18+
(usd_from_goal / duration) AS usd_needed_daily
19+
FROM Kickstarter_Data,
20+
LATERAL (SELECT pledged / NULLIF(fx_rate, 0) AS pledged_usd) pu,
21+
LATERAL (SELECT pledged_usd / NULLIF(backers_count, 0) AS avg_pledge_usd) apu,
22+
LATERAL (SELECT goal / NULLIF(fx_rate, 0) AS goal_usd) gu,
23+
LATERAL (SELECT goal_usd - pledged_usd AS usd_from_goal) ufg,
24+
LATERAL (SELECT (deadline - launched_at) / 86400.00 AS duration) dr;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
select:
2+
SELECT
3+
sp.id salesperson_id,
4+
sp.full_name,
5+
sp.home_region_id,
6+
rg.name AS home_region_name,
7+
home_region_sales.total_sales
8+
FROM SalesPeople sp
9+
JOIN Regions rg ON sp.home_region_id = rg.id
10+
JOIN LATERAL (
11+
SELECT SUM(amount) AS total_sales
12+
FROM Sales s
13+
WHERE s.salesperson_id = sp.id
14+
AND s.region_id = sp.home_region_id
15+
) home_region_sales ON TRUE;

0 commit comments

Comments
 (0)