|
| 1 | +# CrossDB - A ultra high-performance, lightweight |
| 2 | +# embedded and server OLTP RDBMS |
| 3 | +# https://github.com/crossdb-org/crossdb |
| 4 | +# |
| 5 | +# This package implements CrossDB Driver for Nim language |
| 6 | +# |
| 7 | +# (c) 2024 George Lemon | MIT License |
| 8 | +# Made by Humans from OpenPeeps |
| 9 | +# https://github.com/openpeeps/crossdb-nim |
| 10 | + |
| 11 | +type |
| 12 | + va_list* {.importc: "va_list", header: "<stdarg.h>".} = object |
| 13 | + |
| 14 | +{.push importc, header: "crossdb.h".} |
| 15 | +type |
| 16 | + xdb_errno_e* = enum |
| 17 | + XDB_OK = 0 |
| 18 | + XDB_ERROR = 1 |
| 19 | + XDB_E_PARAM = 2 |
| 20 | + XDB_E_STMT = 3 |
| 21 | + XDB_E_NODB = 4 |
| 22 | + XDB_E_NOTFOUND = 5 |
| 23 | + XDB_E_EXISTS = 6 |
| 24 | + XDB_E_FULL = 7 |
| 25 | + XDB_E_CONSTRAINT = 8 |
| 26 | + XDB_E_AUTH = 9 |
| 27 | + XDB_E_MEMORY = 10 |
| 28 | + XDB_E_FILE = 11 |
| 29 | + XDB_E_SOCK = 12 |
| 30 | + |
| 31 | + xdb_ret = int |
| 32 | + |
| 33 | + xdb_type_t* = enum |
| 34 | + XDB_TYPE_NULL = 0 # 1 bit |
| 35 | + XDB_TYPE_TINYINT = 1 # 1 byte |
| 36 | + XDB_TYPE_SMALLINT = 2 # 2 bytes |
| 37 | + XDB_TYPE_INT = 3 # 4 bytes |
| 38 | + XDB_TYPE_BIGINT = 4 # 8 bytes |
| 39 | + XDB_TYPE_UTINYINT = 5 # 1 byte |
| 40 | + XDB_TYPE_USMALLINT = 6 # 2 bytes |
| 41 | + XDB_TYPE_UINT = 7 # 4 bytes |
| 42 | + XDB_TYPE_UBIGINT = 8 # 8 bytes |
| 43 | + XDB_TYPE_FLOAT = 9 # 4 bytes |
| 44 | + XDB_TYPE_DOUBLE = 10 # 8 bytes |
| 45 | + XDB_TYPE_TIMESTAMP = 11 # 8 bytes |
| 46 | + XDB_TYPE_CHAR = 12 # fixed-length string(at most 65535 byte) |
| 47 | + XDB_TYPE_BINARY = 13 # fixed-length binary(at most 65535 byte) |
| 48 | + XDB_TYPE_VCHAR = 14 # varied-length string(at most 65535 byte) |
| 49 | + XDB_TYPE_VBINARY = 15 # varied-length binary(at most 65535 byte) |
| 50 | + # MAC,IPv4,IPv6,CIDR |
| 51 | + # XDB_TYPE_DECIMAL = 16 # TBD decimal |
| 52 | + # XDB_TYPE_GEOMETRY = 17 # TBD geometry |
| 53 | + # XDB_TYPE_JSON = 18 # TBD json string |
| 54 | + # XDB_TYPE_DYNAMIC = 20, |
| 55 | + XDB_TYPE_MAX = 21 |
| 56 | + |
| 57 | + # |
| 58 | + # CrossDB Result |
| 59 | + # |
| 60 | + xdb_restype_t* = enum |
| 61 | + XDB_RET_ROW = 0, XDB_RET_REPLY, XDB_RET_META, XDB_RET_MSG, XDB_RET_COMPRESS, XDB_RET_INSERT, ## (meta + row) |
| 62 | + XDB_RET_DELETE, ## (meta + row) |
| 63 | + XDB_RET_UPDATE, ## (old meta, old row, set meta, set row) |
| 64 | + XDB_RET_EOF = 0xF |
| 65 | + |
| 66 | + xdb_status_t* = enum |
| 67 | + XDB_STATUS_MORE_RESULTS = (1 shl 3) |
| 68 | + |
| 69 | + xdb_row_t* = uint64 |
| 70 | + |
| 71 | + xdb_rowlist_t* {.bycopy.} = object |
| 72 | + rl_count*: uint32 |
| 73 | + rl_curid*: uint32 |
| 74 | + rl_pRows*: array[4096, xdb_row_t] |
| 75 | + |
| 76 | + xdb_res_t* {.bycopy.} = object |
| 77 | + len_type*: uint32 |
| 78 | + errcode*, status*: uint16 |
| 79 | + meta_len*: uint32 |
| 80 | + col_count*: uint16 |
| 81 | + stmt_type*, rsvd*: uint8 |
| 82 | + row_count*, affected_rows*, insert_id*, |
| 83 | + col_meta*, row_data*, data_len*: uint64 |
| 84 | + |
| 85 | + xdb_msg_t* {.bycopy.} = object |
| 86 | + ## MSB 4bit are type |
| 87 | + len_type*: uint32 |
| 88 | + `len`*: uint16 |
| 89 | + msg*: array[2048, char] |
| 90 | + |
| 91 | + xdb_rowdat_t* {.bycopy.} = object |
| 92 | + ## MSB 4bit are type |
| 93 | + len_type*: uint32 |
| 94 | + rowdat*: UncheckedArray[uint8] |
| 95 | + |
| 96 | + xdb_col_t* {.bycopy.} = object |
| 97 | + col_len*: uint16 |
| 98 | + ## colum total len |
| 99 | + col_type*: uint8 |
| 100 | + ## 2 xdb_type_t |
| 101 | + col_dtid*: uint8 |
| 102 | + ## 3 |
| 103 | + col_off*: uint32 |
| 104 | + ## 4 |
| 105 | + col_flags*: uint16 |
| 106 | + ## 10 |
| 107 | + col_vid*: uint16 |
| 108 | + ## 8 |
| 109 | + col_decimal*: uint8 |
| 110 | + ## 12 |
| 111 | + col_charset*: uint8 |
| 112 | + ## 12 |
| 113 | + col_nmlen*: uint8 |
| 114 | + ## 13 |
| 115 | + col_name*: UncheckedArray[char] |
| 116 | + ## 14 |
| 117 | + |
| 118 | + xdb_meta_t* {.bycopy.} = object |
| 119 | + len_type*: uint32 |
| 120 | + ## MSB 4bit are type |
| 121 | + col_count*, col_vcount*, |
| 122 | + cols_off*, rsvd*: uint16 |
| 123 | + row_size*, null_off*, rsvd2*: uint32 |
| 124 | + col_list*, tbl_nmlen*: uint16 |
| 125 | + tbl_name*: UncheckedArray[char] |
| 126 | + ## xdb_col_t cols[]; |
| 127 | + |
| 128 | + xdb_conn_t* = pointer |
| 129 | + xdb_stmt_t* = pointer |
| 130 | + |
| 131 | +# |
| 132 | +# Connection |
| 133 | +# |
| 134 | +proc xdb_open*(path: cstring): xdb_conn_t |
| 135 | +proc xdb_connect*(host: cstring; user: cstring; pass: cstring; db: cstring; |
| 136 | + port: uint16): xdb_conn_t |
| 137 | +proc xdb_close*(pConn: xdb_conn_t) |
| 138 | +proc xdb_curdb*(pConn: xdb_conn_t): cstring |
| 139 | + |
| 140 | +# |
| 141 | +# SQL |
| 142 | +# |
| 143 | +proc xdb_exec*(pConn: xdb_conn_t; sql: cstring): ptr xdb_res_t |
| 144 | +proc xdb_exec2*(pConn: xdb_conn_t; sql: cstring; len: cint): ptr xdb_res_t |
| 145 | +proc xdb_bexec*(pConn: xdb_conn_t; sql: cstring): ptr xdb_res_t {.varargs.} |
| 146 | +proc xdb_vbexec*(pConn: xdb_conn_t; sql: cstring; ap: va_list): ptr xdb_res_t |
| 147 | +proc xdb_pexec*(pConn: xdb_conn_t; sql: cstring): ptr xdb_res_t {.varargs.} |
| 148 | +proc xdb_next_result*(pConn: xdb_conn_t): ptr xdb_res_t |
| 149 | +proc xdb_more_result*(pConn: xdb_conn_t): bool |
| 150 | +proc xdb_free_result*(pRes: ptr xdb_res_t) |
| 151 | + |
| 152 | +# |
| 153 | +# Result |
| 154 | +# |
| 155 | +proc xdb_column_meta*(meta: uint64; iCol: uint16): ptr xdb_col_t |
| 156 | +proc xdb_column_type*(meta: uint64; iCol: uint16): xdb_type_t |
| 157 | +proc xdb_column_name*(meta: uint64; iCol: uint16): cstring |
| 158 | +proc xdb_fetch_row*(pRes: ptr xdb_res_t): ptr xdb_row_t |
| 159 | +proc xdb_column_int*(meta: uint64; pRow: ptr xdb_row_t; iCol: uint16): cint |
| 160 | +proc xdb_column_int64*(meta: uint64; pRow: ptr xdb_row_t; iCol: uint16): int64 |
| 161 | +proc xdb_column_float*(meta: uint64; pRow: ptr xdb_row_t; iCol: uint16): cfloat |
| 162 | +proc xdb_column_double*(meta: uint64; pRow: ptr xdb_row_t; iCol: uint16): cdouble |
| 163 | +proc xdb_column_str*(meta: uint64; pRow: ptr xdb_row_t; iCol: uint16): cstring |
| 164 | +proc xdb_column_str2*(meta: uint64; pRow: ptr xdb_row_t; iCol: uint16; |
| 165 | + pLen: ptr cint): cstring |
| 166 | +proc xdb_column_blob*(meta: uint64; pRow: ptr xdb_row_t; iCol: uint16; |
| 167 | + pLen: ptr cint): pointer |
| 168 | +type |
| 169 | + xdb_row_callback* = proc (meta: uint64; pRow: ptr xdb_row_t; pArg: pointer): cint |
| 170 | + |
| 171 | +# |
| 172 | +# Prepared Statement |
| 173 | +# |
| 174 | +proc xdb_stmt_prepare*(pConn: xdb_conn_t; sql: cstring): xdb_stmt_t |
| 175 | +proc xdb_bind_int*(pStmt: xdb_stmt_t; para_id: uint16; val: cint): xdb_ret |
| 176 | +proc xdb_bind_int64*(pStmt: xdb_stmt_t; para_id: uint16; val: int64): xdb_ret |
| 177 | +proc xdb_bind_float*(pStmt: xdb_stmt_t; para_id: uint16; val: cfloat): xdb_ret |
| 178 | +proc xdb_bind_double*(pStmt: xdb_stmt_t; para_id: uint16; val: cdouble): xdb_ret |
| 179 | +proc xdb_bind_str*(pStmt: xdb_stmt_t; para_id: uint16; str: cstring): xdb_ret |
| 180 | +proc xdb_bind_str2*(pStmt: xdb_stmt_t; para_id: uint16; str: cstring; len: cint): xdb_ret |
| 181 | +proc xdb_clear_bindings*(pStmt: xdb_stmt_t): xdb_ret |
| 182 | +proc xdb_stmt_exec*(pStmt: xdb_stmt_t): ptr xdb_res_t |
| 183 | +proc xdb_stmt_bexec*(pStmt: xdb_stmt_t): ptr xdb_res_t {.varargs.} |
| 184 | +proc xdb_stmt_vbexec*(pStmt: xdb_stmt_t; ap: va_list): ptr xdb_res_t |
| 185 | +proc xdb_stmt_close*(pStmt: xdb_stmt_t) |
| 186 | + |
| 187 | +# proc xdb_stmt_exec_cb*(pStmt: xdb_stmt_t; callback: xdb_row_callback; |
| 188 | +# pArg: pointer): ptr xdb_res_t |
| 189 | +# proc xdb_stmt_bexec_cb*(pStmt: xdb_stmt_t; callback: xdb_row_callback; |
| 190 | +# pArg: pointer): ptr xdb_res_t {.varargs.} |
| 191 | +# proc xdb_stmt_vbexec_cb*(pStmt: xdb_stmt_t; callback: xdb_row_callback; |
| 192 | +# pArg: pointer; ap: va_list): ptr xdb_res_t |
| 193 | + |
| 194 | +# |
| 195 | +# Transaction |
| 196 | +# |
| 197 | +proc xdb_begin*(pConn: xdb_conn_t): xdb_ret |
| 198 | +proc xdb_commit*(pConn: xdb_conn_t): xdb_ret |
| 199 | +proc xdb_rollback*(pConn: xdb_conn_t): xdb_ret |
| 200 | + |
| 201 | +# |
| 202 | +# Misc |
| 203 | +# |
| 204 | +# todo |
| 205 | +{.pop.} |
0 commit comments