// Minimal SQLite3 C API wrapper.
// This module is for internal use and should not be used directly.
module sqlite/capi

pub import std/num/int32

extern import
  c file "sqlite-define.c"
extern import
  c file "sqlite3.c"
extern import
  c file "sqlite-inline.c"

extern make-sqlite-ok(): int32
  c inline "SQLITE_OK"
pub val int32/sqlite-ok: int32 = make-sqlite-ok()

pub extern initialize(): int32
  c inline "sqlite3_initialize()"

pub extern shutdown(): int32
  c inline "sqlite3_shutdown()"

pub extern errstr(x: int32): string
  c "kk_sqlite3_errstr"

pub extern extended-errcode(^db: any): int32
  c "kk_sqlite3_extended_errcode"

pub extern error-offset(^db: any): int
  c "kk_sqlite3_error_offset"

pub extern open-v2(^filename: string, flags: int32, ^vfs: string): (any, int32)
  c "kk_sqlite3_open_v2"

pub extern close(^db: any): int32
  c "kk_sqlite3_close"

pub extern prepare-v3(^db: any, ^sql: sslice, flags: int32): (any, sslice, int32)
  c "kk_sqlite3_prepare_v3"

pub extern finalize(^stmt: any): int32
  c "kk_sqlite3_finalize"

pub extern step(^stmt: any): int32
  c "kk_sqlite3_step"

pub extern reset(^stmt: any): int32
  c "kk_sqlite3_reset"

pub extern column-count(^stmt: any): int
  c "kk_sqlite3_column_count"

pub extern column-type(^stmt: any, col: int32): int32
  c "kk_sqlite3_column_type"
pub extern int32/integer(): int32 { c inline "SQLITE_INTEGER" }
pub extern int32/float(): int32 { c inline "SQLITE_FLOAT" }
pub extern int32/blob(): int32 { c inline "SQLITE_BLOB" }
pub extern int32/null(): int32 { c inline "SQLITE_NULL" }
pub extern int32/text(): int32 { c inline "SQLITE_TEXT" }

//TODO(zephyr): waiting on a proper bytes type in koka
// pub extern column-blob(^stmt: any, col: int32): maybe<bytes>
//   c "kk_sqlite3_column_blob"

pub extern column-double(^stmt: any, col: int32): float64
  c "kk_sqlite3_column_double"

pub extern column-integer(^stmt: any, col: int32): int
  c "kk_sqlite3_column_integer"

pub extern column-text(^stmt: any, col: int32): string
  c "kk_sqlite3_column_text"

// pub extern bind-blob(^stmt: any, col: int32, ^value: bytes): int32
//   c "kk_sqlite3_bind_blob"

pub extern bind-double(^stmt: any, col: int32, ^value: float64): int32
  c "kk_sqlite3_bind_double"

pub extern bind-int64(^stmt: any, col: int32, value: int64): int32
  c "kk_sqlite3_bind_int64"

pub extern bind-null(^stmt: any, col: int32): int32
  c "kk_sqlite3_bind_null"

pub extern bind-text(^stmt: any, col: int32, ^value: string): int32
  c "kk_sqlite3_bind_text"

pub extern bind-zeroblob(^stmt: any, col: int32, length: int64): int32
  c "kk_sqlite3_bind_zeroblob"
