package ru.dbotthepony.kstarbound.io

import java.sql.Connection
import java.sql.PreparedStatement

data class SavepointStatements(val begin: PreparedStatement, val commit: PreparedStatement, val rollback: PreparedStatement)

fun Connection.createSavepoint(name: String): SavepointStatements {
	check('"' !in name) { "Invalid identifier: $name" }

	val begin = prepareStatement("""
		SAVEPOINT "$name"
	""".trimIndent())

	val commit = prepareStatement("""
		RELEASE SAVEPOINT "$name"
	""".trimIndent())

	val rollback = prepareStatement("""
		ROLLBACK TO SAVEPOINT "$name"
	""".trimIndent())

	return SavepointStatements(begin, commit, rollback)
}
