@avidian/duckdb-bun 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +286 -2
  3. package/package.json +96 -4
  4. package/src/bindings/api/appender.ts +288 -0
  5. package/src/bindings/api/data-chunk.ts +58 -0
  6. package/src/bindings/api/database.ts +232 -0
  7. package/src/bindings/api/datetime.ts +158 -0
  8. package/src/bindings/api/index.ts +13 -0
  9. package/src/bindings/api/logical-types.ts +227 -0
  10. package/src/bindings/api/query.ts +616 -0
  11. package/src/bindings/api/scalar-function.ts +274 -0
  12. package/src/bindings/api/table-function.ts +339 -0
  13. package/src/bindings/api/values.ts +546 -0
  14. package/src/bindings/api/vector.ts +266 -0
  15. package/src/bindings/generated/capi.ts +695 -0
  16. package/src/bindings/generated/contract.ts +162 -0
  17. package/src/bindings/index.ts +10 -0
  18. package/src/bindings/runtime/convert.ts +307 -0
  19. package/src/bindings/runtime/diagnostics.ts +45 -0
  20. package/src/bindings/runtime/dispatch.ts +213 -0
  21. package/src/bindings/runtime/exception.ts +103 -0
  22. package/src/bindings/runtime/flags.ts +19 -0
  23. package/src/bindings/runtime/handle.ts +217 -0
  24. package/src/bindings/runtime/kinds.ts +53 -0
  25. package/src/bindings/runtime/marshal.ts +190 -0
  26. package/src/bindings/runtime/native.ts +153 -0
  27. package/src/bindings/runtime/ring.ts +54 -0
  28. package/src/bindings/runtime/scope.ts +133 -0
  29. package/src/bindings/runtime/trace.ts +65 -0
  30. package/src/connection.ts +35 -0
  31. package/src/database.ts +27 -0
  32. package/src/errors.ts +30 -0
  33. package/src/file-assets.d.ts +10 -0
  34. package/src/index.ts +17 -0
  35. package/src/library.ts +83 -0
  36. package/src/neo.ts +16 -0
  37. package/src/reader.ts +47 -0
  38. package/src/statement.ts +61 -0
  39. package/src/types.ts +52 -0
  40. package/src/version.ts +6 -0
  41. package/vendor/node-api/LICENSE +7 -0
  42. package/vendor/node-api/PROVENANCE.md +54 -0
  43. package/vendor/node-api/node-bindings.d.ts +1669 -0
  44. package/vendor/node-api/src/DuckDBAppender.ts +237 -0
  45. package/vendor/node-api/src/DuckDBClientContext.ts +13 -0
  46. package/vendor/node-api/src/DuckDBConnection.ts +315 -0
  47. package/vendor/node-api/src/DuckDBDataChunk.ts +412 -0
  48. package/vendor/node-api/src/DuckDBDataChunkWriter.ts +144 -0
  49. package/vendor/node-api/src/DuckDBExtractedStatements.ts +37 -0
  50. package/vendor/node-api/src/DuckDBInstance.ts +35 -0
  51. package/vendor/node-api/src/DuckDBInstanceCache.ts +29 -0
  52. package/vendor/node-api/src/DuckDBLogicalType.ts +446 -0
  53. package/vendor/node-api/src/DuckDBMaterializedResult.ts +39 -0
  54. package/vendor/node-api/src/DuckDBPendingResult.ts +83 -0
  55. package/vendor/node-api/src/DuckDBPreparedStatement.ts +376 -0
  56. package/vendor/node-api/src/DuckDBPreparedStatementCollection.ts +5 -0
  57. package/vendor/node-api/src/DuckDBPreparedStatementWeakRefCollection.ts +31 -0
  58. package/vendor/node-api/src/DuckDBResult.ts +403 -0
  59. package/vendor/node-api/src/DuckDBResultReader.ts +283 -0
  60. package/vendor/node-api/src/DuckDBScalarFunction.ts +149 -0
  61. package/vendor/node-api/src/DuckDBScalarFunctionBindInfo.ts +34 -0
  62. package/vendor/node-api/src/DuckDBScalarFunctionInfo.ts +34 -0
  63. package/vendor/node-api/src/DuckDBScalarFunctionInitInfo.ts +35 -0
  64. package/vendor/node-api/src/DuckDBTableFunction.ts +149 -0
  65. package/vendor/node-api/src/DuckDBTableFunctionBindInfo.ts +56 -0
  66. package/vendor/node-api/src/DuckDBTableFunctionInfo.ts +36 -0
  67. package/vendor/node-api/src/DuckDBTableFunctionInitInfo.ts +52 -0
  68. package/vendor/node-api/src/DuckDBType.ts +1 -0
  69. package/vendor/node-api/src/DuckDBTypeId.ts +46 -0
  70. package/vendor/node-api/src/DuckDBValueConverter.ts +23 -0
  71. package/vendor/node-api/src/DuckDBValueConverters.ts +461 -0
  72. package/vendor/node-api/src/DuckDBVector.ts +1 -0
  73. package/vendor/node-api/src/JS.ts +11 -0
  74. package/vendor/node-api/src/JSDuckDBValueConverter.ts +85 -0
  75. package/vendor/node-api/src/JSInputTypeForTypeId.ts +101 -0
  76. package/vendor/node-api/src/JSToDuckDBValueConverter.ts +136 -0
  77. package/vendor/node-api/src/JSTypeForTypeId.ts +78 -0
  78. package/vendor/node-api/src/Json.ts +7 -0
  79. package/vendor/node-api/src/JsonDuckDBValueConverter.ts +72 -0
  80. package/vendor/node-api/src/JsonTypeForTypeId.ts +86 -0
  81. package/vendor/node-api/src/ToDuckDBValueConverter.ts +68 -0
  82. package/vendor/node-api/src/ToDuckDBValueConverters.ts +303 -0
  83. package/vendor/node-api/src/configurationOptionDescriptions.ts +13 -0
  84. package/vendor/node-api/src/conversion/bytesFromString.ts +5 -0
  85. package/vendor/node-api/src/conversion/dateTimeStringConversion.ts +343 -0
  86. package/vendor/node-api/src/conversion/displayStringForDuckDBValue.ts +12 -0
  87. package/vendor/node-api/src/conversion/stringFromBlob.ts +48 -0
  88. package/vendor/node-api/src/conversion/stringFromDecimal.ts +129 -0
  89. package/vendor/node-api/src/conversion/varintDecode.ts +42 -0
  90. package/vendor/node-api/src/convertColumnsFromChunks.ts +28 -0
  91. package/vendor/node-api/src/convertColumnsObjectFromChunks.ts +29 -0
  92. package/vendor/node-api/src/convertRowObjectsFromChunks.ts +21 -0
  93. package/vendor/node-api/src/convertRowsFromChunks.ts +16 -0
  94. package/vendor/node-api/src/createConfig.ts +14 -0
  95. package/vendor/node-api/src/createDuckDBValueConverter.ts +17 -0
  96. package/vendor/node-api/src/createResult.ts +15 -0
  97. package/vendor/node-api/src/createValue.ts +355 -0
  98. package/vendor/node-api/src/duckdb.ts +51 -0
  99. package/vendor/node-api/src/enums.ts +7 -0
  100. package/vendor/node-api/src/getColumnsFromChunks.ts +13 -0
  101. package/vendor/node-api/src/getColumnsObjectFromChunks.ts +16 -0
  102. package/vendor/node-api/src/getRowObjectsFromChunks.ts +13 -0
  103. package/vendor/node-api/src/getRowsFromChunks.ts +12 -0
  104. package/vendor/node-api/src/index.ts +3 -0
  105. package/vendor/node-api/src/readValue.ts +31 -0
  106. package/vendor/node-api/src/sleep.ts +5 -0
  107. package/vendor/node-api/src/sql.ts +7 -0
  108. package/vendor/node-api/src/tsconfig.json +7 -0
  109. package/vendor/node-api/src/typeForValue.ts +144 -0
  110. package/vendor/node-api/src/types/BaseDuckDBType.ts +31 -0
  111. package/vendor/node-api/src/types/DuckDBAnyType.ts +13 -0
  112. package/vendor/node-api/src/types/DuckDBArrayType.ts +43 -0
  113. package/vendor/node-api/src/types/DuckDBBigIntType.ts +21 -0
  114. package/vendor/node-api/src/types/DuckDBBigNumType.ts +21 -0
  115. package/vendor/node-api/src/types/DuckDBBitType.ts +13 -0
  116. package/vendor/node-api/src/types/DuckDBBlobType.ts +13 -0
  117. package/vendor/node-api/src/types/DuckDBBooleanType.ts +13 -0
  118. package/vendor/node-api/src/types/DuckDBDateType.ts +29 -0
  119. package/vendor/node-api/src/types/DuckDBDecimalType.ts +46 -0
  120. package/vendor/node-api/src/types/DuckDBDoubleType.ts +21 -0
  121. package/vendor/node-api/src/types/DuckDBEnumType.ts +111 -0
  122. package/vendor/node-api/src/types/DuckDBFloatType.ts +21 -0
  123. package/vendor/node-api/src/types/DuckDBGeometryType.ts +36 -0
  124. package/vendor/node-api/src/types/DuckDBHugeIntType.ts +21 -0
  125. package/vendor/node-api/src/types/DuckDBIntegerLiteralType.ts +13 -0
  126. package/vendor/node-api/src/types/DuckDBIntegerType.ts +21 -0
  127. package/vendor/node-api/src/types/DuckDBIntervalType.ts +13 -0
  128. package/vendor/node-api/src/types/DuckDBListType.ts +35 -0
  129. package/vendor/node-api/src/types/DuckDBMapType.ts +47 -0
  130. package/vendor/node-api/src/types/DuckDBSQLNullType.ts +17 -0
  131. package/vendor/node-api/src/types/DuckDBSmallIntType.ts +21 -0
  132. package/vendor/node-api/src/types/DuckDBStringLiteralType.ts +13 -0
  133. package/vendor/node-api/src/types/DuckDBStructType.ts +104 -0
  134. package/vendor/node-api/src/types/DuckDBTimeNSType.ts +20 -0
  135. package/vendor/node-api/src/types/DuckDBTimeTZType.ts +23 -0
  136. package/vendor/node-api/src/types/DuckDBTimeType.ts +20 -0
  137. package/vendor/node-api/src/types/DuckDBTimestampMillisecondsType.ts +29 -0
  138. package/vendor/node-api/src/types/DuckDBTimestampNanosecondsType.ts +29 -0
  139. package/vendor/node-api/src/types/DuckDBTimestampSecondsType.ts +29 -0
  140. package/vendor/node-api/src/types/DuckDBTimestampTZNanosecondsType.ts +32 -0
  141. package/vendor/node-api/src/types/DuckDBTimestampTZType.ts +32 -0
  142. package/vendor/node-api/src/types/DuckDBTimestampType.ts +32 -0
  143. package/vendor/node-api/src/types/DuckDBTinyIntType.ts +21 -0
  144. package/vendor/node-api/src/types/DuckDBType.ts +86 -0
  145. package/vendor/node-api/src/types/DuckDBUBigIntType.ts +21 -0
  146. package/vendor/node-api/src/types/DuckDBUHugeIntType.ts +21 -0
  147. package/vendor/node-api/src/types/DuckDBUIntegerType.ts +21 -0
  148. package/vendor/node-api/src/types/DuckDBUSmallIntType.ts +21 -0
  149. package/vendor/node-api/src/types/DuckDBUTinyIntType.ts +21 -0
  150. package/vendor/node-api/src/types/DuckDBUUIDType.ts +20 -0
  151. package/vendor/node-api/src/types/DuckDBUnionType.ts +74 -0
  152. package/vendor/node-api/src/types/DuckDBVarCharType.ts +13 -0
  153. package/vendor/node-api/src/types/DuckDBVariantType.ts +13 -0
  154. package/vendor/node-api/src/types/index.ts +44 -0
  155. package/vendor/node-api/src/values/DuckDBArrayValue.ts +18 -0
  156. package/vendor/node-api/src/values/DuckDBBitValue.ts +117 -0
  157. package/vendor/node-api/src/values/DuckDBBlobValue.ts +26 -0
  158. package/vendor/node-api/src/values/DuckDBDateValue.ts +43 -0
  159. package/vendor/node-api/src/values/DuckDBDecimalValue.ts +44 -0
  160. package/vendor/node-api/src/values/DuckDBGeometryValue.ts +30 -0
  161. package/vendor/node-api/src/values/DuckDBIntervalValue.ts +22 -0
  162. package/vendor/node-api/src/values/DuckDBListValue.ts +18 -0
  163. package/vendor/node-api/src/values/DuckDBMapValue.ts +25 -0
  164. package/vendor/node-api/src/values/DuckDBStructValue.ts +22 -0
  165. package/vendor/node-api/src/values/DuckDBTimeNSValue.ts +23 -0
  166. package/vendor/node-api/src/values/DuckDBTimeTZValue.ts +96 -0
  167. package/vendor/node-api/src/values/DuckDBTimeValue.ts +34 -0
  168. package/vendor/node-api/src/values/DuckDBTimestampMillisecondsValue.ts +29 -0
  169. package/vendor/node-api/src/values/DuckDBTimestampNanosecondsValue.ts +28 -0
  170. package/vendor/node-api/src/values/DuckDBTimestampSecondsValue.ts +28 -0
  171. package/vendor/node-api/src/values/DuckDBTimestampTZNanosecondsValue.ts +44 -0
  172. package/vendor/node-api/src/values/DuckDBTimestampTZValue.ts +54 -0
  173. package/vendor/node-api/src/values/DuckDBTimestampValue.ts +45 -0
  174. package/vendor/node-api/src/values/DuckDBTupleValue.ts +22 -0
  175. package/vendor/node-api/src/values/DuckDBUUIDValue.ts +48 -0
  176. package/vendor/node-api/src/values/DuckDBUnionValue.ts +22 -0
  177. package/vendor/node-api/src/values/DuckDBValue.ts +53 -0
  178. package/vendor/node-api/src/values/DuckDBVariantValue.ts +34 -0
  179. package/vendor/node-api/src/values/index.ts +24 -0
  180. package/vendor/node-api/src/vectors/DuckDBArrayVector.ts +107 -0
  181. package/vendor/node-api/src/vectors/DuckDBBigIntVector.ts +75 -0
  182. package/vendor/node-api/src/vectors/DuckDBBigNumVector.ts +98 -0
  183. package/vendor/node-api/src/vectors/DuckDBBitVector.ts +102 -0
  184. package/vendor/node-api/src/vectors/DuckDBBlobVector.ts +112 -0
  185. package/vendor/node-api/src/vectors/DuckDBBooleanVector.ts +84 -0
  186. package/vendor/node-api/src/vectors/DuckDBDateVector.ts +80 -0
  187. package/vendor/node-api/src/vectors/DuckDBDecimal128Vector.ts +107 -0
  188. package/vendor/node-api/src/vectors/DuckDBDecimal16Vector.ts +104 -0
  189. package/vendor/node-api/src/vectors/DuckDBDecimal32Vector.ts +104 -0
  190. package/vendor/node-api/src/vectors/DuckDBDecimal64Vector.ts +104 -0
  191. package/vendor/node-api/src/vectors/DuckDBDoubleVector.ts +68 -0
  192. package/vendor/node-api/src/vectors/DuckDBEnum16Vector.ts +75 -0
  193. package/vendor/node-api/src/vectors/DuckDBEnum32Vector.ts +75 -0
  194. package/vendor/node-api/src/vectors/DuckDBEnum8Vector.ts +75 -0
  195. package/vendor/node-api/src/vectors/DuckDBFloatVector.ts +68 -0
  196. package/vendor/node-api/src/vectors/DuckDBGeometryVector.ts +97 -0
  197. package/vendor/node-api/src/vectors/DuckDBHugeIntVector.ts +89 -0
  198. package/vendor/node-api/src/vectors/DuckDBIntegerVector.ts +75 -0
  199. package/vendor/node-api/src/vectors/DuckDBIntervalVector.ts +100 -0
  200. package/vendor/node-api/src/vectors/DuckDBListVector.ts +221 -0
  201. package/vendor/node-api/src/vectors/DuckDBMapVector.ts +88 -0
  202. package/vendor/node-api/src/vectors/DuckDBSQLNullVector.ts +53 -0
  203. package/vendor/node-api/src/vectors/DuckDBSmallIntVector.ts +75 -0
  204. package/vendor/node-api/src/vectors/DuckDBStructVector.ts +138 -0
  205. package/vendor/node-api/src/vectors/DuckDBTimeNSVector.ts +80 -0
  206. package/vendor/node-api/src/vectors/DuckDBTimeTZVector.ts +80 -0
  207. package/vendor/node-api/src/vectors/DuckDBTimeVector.ts +80 -0
  208. package/vendor/node-api/src/vectors/DuckDBTimestampMillisecondsVector.ts +88 -0
  209. package/vendor/node-api/src/vectors/DuckDBTimestampNanosecondsVector.ts +88 -0
  210. package/vendor/node-api/src/vectors/DuckDBTimestampSecondsVector.ts +88 -0
  211. package/vendor/node-api/src/vectors/DuckDBTimestampTZNanosecondsVector.ts +88 -0
  212. package/vendor/node-api/src/vectors/DuckDBTimestampTZVector.ts +86 -0
  213. package/vendor/node-api/src/vectors/DuckDBTimestampVector.ts +83 -0
  214. package/vendor/node-api/src/vectors/DuckDBTinyIntVector.ts +75 -0
  215. package/vendor/node-api/src/vectors/DuckDBTupleVector.ts +125 -0
  216. package/vendor/node-api/src/vectors/DuckDBUBigIntVector.ts +75 -0
  217. package/vendor/node-api/src/vectors/DuckDBUHugeIntVector.ts +89 -0
  218. package/vendor/node-api/src/vectors/DuckDBUIntegerVector.ts +75 -0
  219. package/vendor/node-api/src/vectors/DuckDBUSmallIntVector.ts +75 -0
  220. package/vendor/node-api/src/vectors/DuckDBUTinyIntVector.ts +75 -0
  221. package/vendor/node-api/src/vectors/DuckDBUUIDVector.ts +89 -0
  222. package/vendor/node-api/src/vectors/DuckDBUnionVector.ts +84 -0
  223. package/vendor/node-api/src/vectors/DuckDBValidity.ts +104 -0
  224. package/vendor/node-api/src/vectors/DuckDBVarCharVector.ts +100 -0
  225. package/vendor/node-api/src/vectors/DuckDBVariantVector.ts +609 -0
  226. package/vendor/node-api/src/vectors/DuckDBVector.ts +33 -0
  227. package/vendor/node-api/src/vectors/createVector.ts +269 -0
  228. package/vendor/node-api/src/vectors/dataAccessors.ts +387 -0
  229. package/vendor/node-api/src/vectors/index.ts +50 -0
  230. package/vendor/node-api/src/vectors/vectorRegistry.ts +24 -0
  231. package/vendor/node-api/src/version.ts +5 -0
@@ -0,0 +1,237 @@
1
+ import duckdb from "../../../src/bindings/index.ts";
2
+ import { createValue } from './createValue';
3
+ import { DuckDBConnection } from './DuckDBConnection';
4
+ import { DuckDBDataChunk } from './DuckDBDataChunk';
5
+ import { DuckDBLogicalType } from './DuckDBLogicalType';
6
+ import {
7
+ BIGNUM,
8
+ BIT,
9
+ DECIMAL,
10
+ DuckDBArrayType,
11
+ DuckDBEnumType,
12
+ DuckDBListType,
13
+ DuckDBMapType,
14
+ DuckDBStructType,
15
+ DuckDBType,
16
+ DuckDBUnionType,
17
+ TIMESTAMP_MS,
18
+ TIMESTAMP_NS,
19
+ TIMESTAMP_S,
20
+ TIMESTAMPTZ,
21
+ TIMESTAMPTZ_NS,
22
+ TIMETZ,
23
+ UUID,
24
+ VARIANT,
25
+ } from './DuckDBType';
26
+ import { typeForValue } from './typeForValue';
27
+ import {
28
+ arrayValue,
29
+ DuckDBArrayValue,
30
+ DuckDBBitValue,
31
+ DuckDBDateValue,
32
+ DuckDBDecimalValue,
33
+ DuckDBIntervalValue,
34
+ DuckDBListValue,
35
+ DuckDBMapValue,
36
+ DuckDBStructValue,
37
+ DuckDBTimestampMillisecondsValue,
38
+ DuckDBTimestampNanosecondsValue,
39
+ DuckDBTimestampSecondsValue,
40
+ DuckDBTimestampTZValue,
41
+ DuckDBTimestampTZNanosecondsValue,
42
+ DuckDBTimestampValue,
43
+ DuckDBTimeTZValue,
44
+ DuckDBTimeValue,
45
+ DuckDBTupleValue,
46
+ DuckDBUnionValue,
47
+ DuckDBUUIDValue,
48
+ DuckDBValue,
49
+ DuckDBVariantValue,
50
+ listValue,
51
+ structValue,
52
+ tupleValue,
53
+ } from './values';
54
+
55
+ export class DuckDBAppender {
56
+ public readonly connection: DuckDBConnection;
57
+ private readonly appender: duckdb.Appender;
58
+ constructor(connection: DuckDBConnection, appender: duckdb.Appender) {
59
+ this.connection = connection;
60
+ this.appender = appender;
61
+ }
62
+ public closeSync() {
63
+ duckdb.appender_close_sync(this.appender);
64
+ }
65
+ public flushSync() {
66
+ duckdb.appender_flush_sync(this.appender);
67
+ }
68
+ public clear() {
69
+ duckdb.appender_clear(this.appender);
70
+ }
71
+ public get columnCount(): number {
72
+ return duckdb.appender_column_count(this.appender);
73
+ }
74
+ public columnType(columnIndex: number): DuckDBType {
75
+ return DuckDBLogicalType.create(
76
+ duckdb.appender_column_type(this.appender, columnIndex)
77
+ ).asType();
78
+ }
79
+ public endRow() {
80
+ duckdb.appender_end_row(this.appender);
81
+ }
82
+ public appendDefault() {
83
+ duckdb.append_default(this.appender);
84
+ }
85
+ public appendBoolean(value: boolean) {
86
+ duckdb.append_bool(this.appender, value);
87
+ }
88
+ public appendTinyInt(value: number) {
89
+ duckdb.append_int8(this.appender, value);
90
+ }
91
+ public appendSmallInt(value: number) {
92
+ duckdb.append_int16(this.appender, value);
93
+ }
94
+ public appendInteger(value: number) {
95
+ duckdb.append_int32(this.appender, value);
96
+ }
97
+ public appendBigInt(value: bigint) {
98
+ duckdb.append_int64(this.appender, value);
99
+ }
100
+ public appendHugeInt(value: bigint) {
101
+ duckdb.append_hugeint(this.appender, value);
102
+ }
103
+ public appendUTinyInt(value: number) {
104
+ duckdb.append_uint8(this.appender, value);
105
+ }
106
+ public appendUSmallInt(value: number) {
107
+ duckdb.append_uint16(this.appender, value);
108
+ }
109
+ public appendUInteger(value: number) {
110
+ duckdb.append_uint32(this.appender, value);
111
+ }
112
+ public appendUBigInt(value: bigint) {
113
+ duckdb.append_uint64(this.appender, value);
114
+ }
115
+ public appendUHugeInt(value: bigint) {
116
+ duckdb.append_uhugeint(this.appender, value);
117
+ }
118
+ public appendDecimal(value: DuckDBDecimalValue) {
119
+ // The width and scale of the DECIMAL type here aren't actually used.
120
+ this.appendValue(value, DECIMAL(value.width, value.scale));
121
+ }
122
+ public appendFloat(value: number) {
123
+ duckdb.append_float(this.appender, value);
124
+ }
125
+ public appendDouble(value: number) {
126
+ duckdb.append_double(this.appender, value);
127
+ }
128
+ public appendDate(value: DuckDBDateValue) {
129
+ duckdb.append_date(this.appender, value);
130
+ }
131
+ public appendTime(value: DuckDBTimeValue) {
132
+ duckdb.append_time(this.appender, value);
133
+ }
134
+ public appendTimeTZ(value: DuckDBTimeTZValue) {
135
+ this.appendValue(value, TIMETZ);
136
+ }
137
+ public appendTimestamp(value: DuckDBTimestampValue) {
138
+ duckdb.append_timestamp(this.appender, value);
139
+ }
140
+ public appendTimestampTZ(value: DuckDBTimestampTZValue) {
141
+ this.appendValue(value, TIMESTAMPTZ);
142
+ }
143
+ public appendTimestampTZNanoseconds(value: DuckDBTimestampTZNanosecondsValue) {
144
+ this.appendValue(value, TIMESTAMPTZ_NS);
145
+ }
146
+ public appendTimestampSeconds(value: DuckDBTimestampSecondsValue) {
147
+ this.appendValue(value, TIMESTAMP_S);
148
+ }
149
+ public appendTimestampMilliseconds(value: DuckDBTimestampMillisecondsValue) {
150
+ this.appendValue(value, TIMESTAMP_MS);
151
+ }
152
+ public appendTimestampNanoseconds(value: DuckDBTimestampNanosecondsValue) {
153
+ this.appendValue(value, TIMESTAMP_NS);
154
+ }
155
+ public appendInterval(value: DuckDBIntervalValue) {
156
+ duckdb.append_interval(this.appender, value);
157
+ }
158
+ public appendVarchar(value: string) {
159
+ duckdb.append_varchar(this.appender, value);
160
+ }
161
+ public appendBlob(value: Uint8Array) {
162
+ duckdb.append_blob(this.appender, value);
163
+ }
164
+ public appendEnum(value: string, type: DuckDBEnumType) {
165
+ this.appendValue(value, type);
166
+ }
167
+ public appendList(
168
+ value: DuckDBListValue | readonly DuckDBValue[],
169
+ type?: DuckDBListType
170
+ ) {
171
+ this.appendValue(
172
+ value instanceof DuckDBListValue ? value : listValue(value),
173
+ type
174
+ );
175
+ }
176
+ public appendStruct(
177
+ value: DuckDBStructValue | Readonly<Record<string, DuckDBValue>>,
178
+ type?: DuckDBStructType
179
+ ) {
180
+ this.appendValue(
181
+ value instanceof DuckDBStructValue ? value : structValue(value),
182
+ type
183
+ );
184
+ }
185
+ public appendTuple(
186
+ value: DuckDBTupleValue | readonly DuckDBValue[],
187
+ type?: DuckDBStructType
188
+ ) {
189
+ this.appendValue(
190
+ value instanceof DuckDBTupleValue ? value : tupleValue(value),
191
+ type
192
+ );
193
+ }
194
+ public appendMap(value: DuckDBMapValue, type?: DuckDBMapType) {
195
+ this.appendValue(value, type);
196
+ }
197
+ public appendArray(
198
+ value: DuckDBArrayValue | readonly DuckDBValue[],
199
+ type?: DuckDBArrayType
200
+ ) {
201
+ this.appendValue(
202
+ value instanceof DuckDBArrayValue ? value : arrayValue(value),
203
+ type
204
+ );
205
+ }
206
+ public appendUnion(value: DuckDBUnionValue, type?: DuckDBUnionType) {
207
+ this.appendValue(value, type);
208
+ }
209
+ public appendUUID(value: DuckDBUUIDValue) {
210
+ this.appendValue(value, UUID);
211
+ }
212
+ public appendBit(value: DuckDBBitValue) {
213
+ this.appendValue(value, BIT);
214
+ }
215
+ public appendBigNum(value: bigint) {
216
+ this.appendValue(value, BIGNUM);
217
+ }
218
+ public appendVariant(value: DuckDBVariantValue | null) {
219
+ if (value === null) {
220
+ this.appendNull();
221
+ return;
222
+ }
223
+ this.appendValue(value, VARIANT);
224
+ }
225
+ public appendNull() {
226
+ duckdb.append_null(this.appender);
227
+ }
228
+ public appendValue(value: DuckDBValue, type?: DuckDBType) {
229
+ duckdb.append_value(
230
+ this.appender,
231
+ createValue(type ? type : typeForValue(value), value)
232
+ );
233
+ }
234
+ public appendDataChunk(dataChunk: DuckDBDataChunk) {
235
+ duckdb.append_data_chunk(this.appender, dataChunk.chunk);
236
+ }
237
+ }
@@ -0,0 +1,13 @@
1
+ import duckdb from "../../../src/bindings/index.ts";
2
+
3
+ export class DuckDBClientContext {
4
+ private readonly client_context: duckdb.ClientContext;
5
+
6
+ constructor(client_context: duckdb.ClientContext) {
7
+ this.client_context = client_context;
8
+ }
9
+
10
+ public get connectionId(): number {
11
+ return duckdb.client_context_get_connection_id(this.client_context);
12
+ }
13
+ }
@@ -0,0 +1,315 @@
1
+ import duckdb from "../../../src/bindings/index.ts";
2
+ import { DuckDBAppender } from './DuckDBAppender';
3
+ import { DuckDBClientContext } from './DuckDBClientContext';
4
+ import { DuckDBExtractedStatements } from './DuckDBExtractedStatements';
5
+ import { DuckDBInstance } from './DuckDBInstance';
6
+ import { DuckDBMaterializedResult } from './DuckDBMaterializedResult';
7
+ import { DuckDBPendingResult } from './DuckDBPendingResult';
8
+ import { DuckDBPreparedStatement } from './DuckDBPreparedStatement';
9
+ import { DuckDBPreparedStatementWeakRefCollection } from './DuckDBPreparedStatementWeakRefCollection';
10
+ import { DuckDBResult } from './DuckDBResult';
11
+ import { DuckDBResultReader } from './DuckDBResultReader';
12
+ import { DuckDBScalarFunction } from './DuckDBScalarFunction';
13
+ import { DuckDBTableFunction } from './DuckDBTableFunction';
14
+ import { DuckDBType } from './DuckDBType';
15
+ import { DuckDBValue } from './values';
16
+
17
+ export class DuckDBConnection {
18
+ private readonly connection: duckdb.Connection;
19
+ private readonly preparedStatements: DuckDBPreparedStatementWeakRefCollection;
20
+ constructor(connection: duckdb.Connection) {
21
+ this.connection = connection;
22
+ this.preparedStatements = new DuckDBPreparedStatementWeakRefCollection();
23
+ }
24
+ public static async create(
25
+ instance?: DuckDBInstance
26
+ ): Promise<DuckDBConnection> {
27
+ if (instance) {
28
+ return instance.connect();
29
+ }
30
+ return (await DuckDBInstance.fromCache()).connect();
31
+ }
32
+ /** Same as disconnectSync. */
33
+ public closeSync() {
34
+ this.disconnectSync();
35
+ }
36
+ public disconnectSync() {
37
+ this.preparedStatements.destroySync();
38
+ duckdb.disconnect_sync(this.connection);
39
+ }
40
+ public get clientContext(): DuckDBClientContext {
41
+ return this.getClientContext();
42
+ }
43
+ public getClientContext(): DuckDBClientContext {
44
+ return new DuckDBClientContext(
45
+ duckdb.connection_get_client_context(this.connection)
46
+ );
47
+ }
48
+ public interrupt() {
49
+ duckdb.interrupt(this.connection);
50
+ }
51
+ public get progress(): duckdb.QueryProgress {
52
+ return duckdb.query_progress(this.connection);
53
+ }
54
+ public async run(
55
+ sql: string,
56
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
57
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
58
+ ): Promise<DuckDBMaterializedResult> {
59
+ if (values) {
60
+ const prepared = await this.runUntilLast(sql);
61
+ try {
62
+ prepared.bind(values, types);
63
+ const result = await prepared.run();
64
+ return result;
65
+ } finally {
66
+ prepared.destroySync();
67
+ }
68
+ } else {
69
+ return new DuckDBMaterializedResult(
70
+ await duckdb.query(this.connection, sql)
71
+ );
72
+ }
73
+ }
74
+ public async runAndRead(
75
+ sql: string,
76
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
77
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
78
+ ): Promise<DuckDBResultReader> {
79
+ return new DuckDBResultReader(await this.run(sql, values, types));
80
+ }
81
+ public async runAndReadAll(
82
+ sql: string,
83
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
84
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
85
+ ): Promise<DuckDBResultReader> {
86
+ const reader = await this.runAndRead(sql, values, types);
87
+ await reader.readAll();
88
+ return reader;
89
+ }
90
+ public async runAndReadUntil(
91
+ sql: string,
92
+ targetRowCount: number,
93
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
94
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
95
+ ): Promise<DuckDBResultReader> {
96
+ const reader = await this.runAndRead(sql, values, types);
97
+ await reader.readUntil(targetRowCount);
98
+ return reader;
99
+ }
100
+ public async stream(
101
+ sql: string,
102
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
103
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
104
+ ): Promise<DuckDBResult> {
105
+ const prepared = await this.runUntilLast(sql);
106
+ try {
107
+ if (values) {
108
+ prepared.bind(values, types);
109
+ }
110
+ const result = await prepared.stream();
111
+ return result;
112
+ } finally {
113
+ prepared.destroySync();
114
+ }
115
+ }
116
+ public async streamAndRead(
117
+ sql: string,
118
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
119
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
120
+ ): Promise<DuckDBResultReader> {
121
+ return new DuckDBResultReader(await this.stream(sql, values, types));
122
+ }
123
+ public async streamAndReadAll(
124
+ sql: string,
125
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
126
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
127
+ ): Promise<DuckDBResultReader> {
128
+ const reader = await this.streamAndRead(sql, values, types);
129
+ await reader.readAll();
130
+ return reader;
131
+ }
132
+ public async streamAndReadUntil(
133
+ sql: string,
134
+ targetRowCount: number,
135
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
136
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
137
+ ): Promise<DuckDBResultReader> {
138
+ const reader = await this.streamAndRead(sql, values, types);
139
+ await reader.readUntil(targetRowCount);
140
+ return reader;
141
+ }
142
+ public async start(
143
+ sql: string,
144
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
145
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
146
+ ): Promise<DuckDBPendingResult> {
147
+ const prepared = await this.runUntilLast(sql);
148
+ try {
149
+ if (values) {
150
+ prepared.bind(values, types);
151
+ }
152
+ return prepared.start();
153
+ } finally {
154
+ prepared.destroySync();
155
+ }
156
+ }
157
+ public async startThenRead(
158
+ sql: string,
159
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
160
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
161
+ ): Promise<DuckDBResultReader> {
162
+ const pending = await this.start(sql, values, types);
163
+ await pending.runAllTasks();
164
+ return new DuckDBResultReader(await pending.getResult());
165
+ }
166
+ public async startThenReadAll(
167
+ sql: string,
168
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
169
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
170
+ ): Promise<DuckDBResultReader> {
171
+ const reader = await this.startThenRead(sql, values, types);
172
+ await reader.readAll();
173
+ return reader;
174
+ }
175
+ public async startThenReadUntil(
176
+ sql: string,
177
+ targetRowCount: number,
178
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
179
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
180
+ ): Promise<DuckDBResultReader> {
181
+ const reader = await this.startThenRead(sql, values, types);
182
+ await reader.readUntil(targetRowCount);
183
+ return reader;
184
+ }
185
+ public async startStream(
186
+ sql: string,
187
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
188
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
189
+ ): Promise<DuckDBPendingResult> {
190
+ const prepared = await this.runUntilLast(sql);
191
+ try {
192
+ if (values) {
193
+ prepared.bind(values, types);
194
+ }
195
+ return prepared.startStream();
196
+ } finally {
197
+ prepared.destroySync();
198
+ }
199
+ }
200
+ public async startStreamThenRead(
201
+ sql: string,
202
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
203
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
204
+ ): Promise<DuckDBResultReader> {
205
+ const pending = await this.startStream(sql, values, types);
206
+ await pending.runAllTasks();
207
+ return new DuckDBResultReader(await pending.getResult());
208
+ }
209
+ public async startStreamThenReadAll(
210
+ sql: string,
211
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
212
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
213
+ ): Promise<DuckDBResultReader> {
214
+ const reader = await this.startStreamThenRead(sql, values, types);
215
+ await reader.readAll();
216
+ return reader;
217
+ }
218
+ public async startStreamThenReadUntil(
219
+ sql: string,
220
+ targetRowCount: number,
221
+ values?: DuckDBValue[] | Record<string, DuckDBValue>,
222
+ types?: DuckDBType[] | Record<string, DuckDBType | undefined>
223
+ ): Promise<DuckDBResultReader> {
224
+ const reader = await this.startStreamThenRead(sql, values, types);
225
+ await reader.readUntil(targetRowCount);
226
+ return reader;
227
+ }
228
+ public async prepare(sql: string): Promise<DuckDBPreparedStatement> {
229
+ const prepared = await this.createPrepared(sql);
230
+ this.preparedStatements.add(prepared);
231
+ return prepared;
232
+ }
233
+ private async createPrepared(sql: string): Promise<DuckDBPreparedStatement> {
234
+ return new DuckDBPreparedStatement(
235
+ await duckdb.prepare(this.connection, sql)
236
+ );
237
+ }
238
+ public async extractStatements(
239
+ sql: string
240
+ ): Promise<DuckDBExtractedStatements> {
241
+ const { extracted_statements, statement_count } =
242
+ await duckdb.extract_statements(this.connection, sql);
243
+ if (statement_count === 0) {
244
+ throw new Error(
245
+ `Failed to extract statements: ${duckdb.extract_statements_error(
246
+ extracted_statements
247
+ )}`
248
+ );
249
+ }
250
+ return new DuckDBExtractedStatements(
251
+ this.connection,
252
+ extracted_statements,
253
+ statement_count,
254
+ this.preparedStatements
255
+ );
256
+ }
257
+ private async runUntilLast(sql: string): Promise<DuckDBPreparedStatement> {
258
+ const extractedStatements = await this.extractStatements(sql);
259
+ const statementCount = extractedStatements.count;
260
+ if (statementCount > 1) {
261
+ for (let i = 0; i < statementCount - 1; i++) {
262
+ const prepared = await extractedStatements.prepare(i);
263
+ try {
264
+ await prepared.run();
265
+ } finally {
266
+ prepared.destroySync();
267
+ }
268
+ }
269
+ }
270
+ return extractedStatements.prepare(statementCount - 1);
271
+ }
272
+ public getTableNames(query: string, qualified: boolean): readonly string[] {
273
+ const names: string[] = [];
274
+ const list_value = duckdb.get_table_names(
275
+ this.connection,
276
+ query,
277
+ qualified
278
+ );
279
+ const count = duckdb.get_list_size(list_value);
280
+ for (let i = 0; i < count; i++) {
281
+ const varchar_value = duckdb.get_list_child(list_value, i);
282
+ const name = duckdb.get_varchar(varchar_value);
283
+ names.push(name);
284
+ }
285
+ names.sort();
286
+ return names;
287
+ }
288
+ public async createAppender(
289
+ table: string,
290
+ schema?: string | null,
291
+ catalog?: string | null
292
+ ): Promise<DuckDBAppender> {
293
+ return new DuckDBAppender(
294
+ this,
295
+ duckdb.appender_create_ext(
296
+ this.connection,
297
+ catalog ?? null,
298
+ schema ?? null,
299
+ table
300
+ )
301
+ );
302
+ }
303
+ public registerTableFunction(tableFunction: DuckDBTableFunction) {
304
+ duckdb.register_table_function(
305
+ this.connection,
306
+ tableFunction.table_function
307
+ );
308
+ }
309
+ public registerScalarFunction(scalarFunction: DuckDBScalarFunction) {
310
+ duckdb.register_scalar_function(
311
+ this.connection,
312
+ scalarFunction.scalar_function
313
+ );
314
+ }
315
+ }