duckdb 0.4.1-dev129.0 → 0.4.1-dev1291.0

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.
@@ -12,6 +12,7 @@ Napi::Object RegisterModule(Napi::Env env, Napi::Object exports) {
12
12
  node_duckdb::Database::Init(env, exports);
13
13
  node_duckdb::Connection::Init(env, exports);
14
14
  node_duckdb::Statement::Init(env, exports);
15
+ node_duckdb::QueryResult::Init(env, exports);
15
16
 
16
17
  exports.DefineProperties({
17
18
  DEFINE_CONSTANT_INTEGER(exports, node_duckdb::Database::DUCKDB_NODEJS_ERROR, ERROR) DEFINE_CONSTANT_INTEGER(
@@ -1,22 +1,40 @@
1
1
  #pragma once
2
2
  #define NODE_ADDON_API_DISABLE_DEPRECATED
3
+ #include "duckdb.hpp"
4
+
3
5
  #include <napi.h>
4
6
  #include <queue>
5
7
  #include <unordered_map>
6
8
 
7
- #include "duckdb.hpp"
8
-
9
9
  namespace node_duckdb {
10
10
 
11
11
  struct Task {
12
- Task(Napi::Reference<Napi::Object> &object_, Napi::Function cb_) : object(object_) {
13
- if (!cb_.IsUndefined() && cb_.IsFunction()) {
14
- callback = Persistent(cb_); // TODO not sure what this does
12
+ Task(Napi::Reference<Napi::Object> &object, Napi::Function cb) : object(object) {
13
+ if (!cb.IsUndefined() && cb.IsFunction()) {
14
+ callback = Persistent(cb); // TODO not sure what this does
15
15
  }
16
16
  object.Ref();
17
17
  }
18
+ explicit Task(Napi::Reference<Napi::Object> &object) : object(object) {
19
+ object.Ref();
20
+ }
21
+
22
+ // Called on a worker thread (i.e., not the main event loop thread)
18
23
  virtual void DoWork() = 0;
19
24
 
25
+ // Called on the event loop thread after the work has been completed. By
26
+ // default, call the associated callback, if defined. If you're writing
27
+ // a Task that uses promises, override this method instead of Callback.
28
+ virtual void DoCallback() {
29
+ auto env = object.Env();
30
+ Napi::HandleScope scope(env);
31
+
32
+ if (!callback.Value().IsUndefined()) {
33
+ Callback();
34
+ }
35
+ }
36
+
37
+ // Called on the event loop thread by DoCallback (see above)
20
38
  virtual void Callback() {
21
39
  auto env = object.Env();
22
40
  Napi::HandleScope scope(env);
@@ -40,7 +58,8 @@ class Connection;
40
58
 
41
59
  class Database : public Napi::ObjectWrap<Database> {
42
60
  public:
43
- Database(const Napi::CallbackInfo &info);
61
+ explicit Database(const Napi::CallbackInfo &info);
62
+ ~Database() override;
44
63
  static Napi::Object Init(Napi::Env env, Napi::Object exports);
45
64
  void Process(Napi::Env env);
46
65
  void TaskComplete(Napi::Env env);
@@ -50,8 +69,9 @@ public:
50
69
  static bool HasInstance(Napi::Value val) {
51
70
  Napi::Env env = val.Env();
52
71
  Napi::HandleScope scope(env);
53
- if (!val.IsObject())
72
+ if (!val.IsObject()) {
54
73
  return false;
74
+ }
55
75
  Napi::Object obj = val.As<Napi::Object>();
56
76
  return obj.InstanceOf(constructor.Value());
57
77
  }
@@ -75,17 +95,19 @@ private:
75
95
  std::mutex task_mutex;
76
96
  bool task_inflight;
77
97
  static Napi::FunctionReference constructor;
98
+ Napi::Env env;
99
+ int64_t bytes_allocated = 0;
78
100
  };
79
101
 
80
102
  struct JSArgs;
81
- void DuckDBNodeUDFLauncher(Napi::Env env, Napi::Function jsudf, nullptr_t *, JSArgs *data);
103
+ void DuckDBNodeUDFLauncher(Napi::Env env, Napi::Function jsudf, std::nullptr_t *, JSArgs *data);
82
104
 
83
- typedef Napi::TypedThreadSafeFunction<nullptr_t, JSArgs, DuckDBNodeUDFLauncher> DuckDBNodeUDFFUnction;
105
+ typedef Napi::TypedThreadSafeFunction<std::nullptr_t, JSArgs, DuckDBNodeUDFLauncher> duckdb_node_udf_function_t;
84
106
 
85
107
  class Connection : public Napi::ObjectWrap<Connection> {
86
108
  public:
87
- Connection(const Napi::CallbackInfo &info);
88
- ~Connection();
109
+ explicit Connection(const Napi::CallbackInfo &info);
110
+ ~Connection() override;
89
111
  static Napi::Object Init(Napi::Env env, Napi::Object exports);
90
112
 
91
113
  public:
@@ -97,8 +119,9 @@ public:
97
119
  static bool HasInstance(Napi::Value val) {
98
120
  Napi::Env env = val.Env();
99
121
  Napi::HandleScope scope(env);
100
- if (!val.IsObject())
122
+ if (!val.IsObject()) {
101
123
  return false;
124
+ }
102
125
  Napi::Object obj = val.As<Napi::Object>();
103
126
  return obj.InstanceOf(constructor.Value());
104
127
  }
@@ -107,15 +130,15 @@ public:
107
130
  static Napi::FunctionReference constructor;
108
131
  std::unique_ptr<duckdb::Connection> connection;
109
132
  Database *database_ref;
110
- std::unordered_map<std::string, DuckDBNodeUDFFUnction> udfs;
133
+ std::unordered_map<std::string, duckdb_node_udf_function_t> udfs;
111
134
  };
112
135
 
113
136
  struct StatementParam;
114
137
 
115
138
  class Statement : public Napi::ObjectWrap<Statement> {
116
139
  public:
117
- Statement(const Napi::CallbackInfo &info);
118
- ~Statement();
140
+ explicit Statement(const Napi::CallbackInfo &info);
141
+ ~Statement() override;
119
142
  static Napi::Object Init(Napi::Env env, Napi::Object exports);
120
143
  void SetProcessFirstParam() {
121
144
  ignore_first_param = false;
@@ -125,9 +148,8 @@ public:
125
148
  Napi::Value All(const Napi::CallbackInfo &info);
126
149
  Napi::Value Each(const Napi::CallbackInfo &info);
127
150
  Napi::Value Run(const Napi::CallbackInfo &info);
128
- Napi::Value Bind(const Napi::CallbackInfo &info);
129
-
130
- Napi::Value Finalize_(const Napi::CallbackInfo &info);
151
+ Napi::Value Finish(const Napi::CallbackInfo &info);
152
+ Napi::Value Stream(const Napi::CallbackInfo &info);
131
153
 
132
154
  public:
133
155
  static Napi::FunctionReference constructor;
@@ -140,6 +162,21 @@ private:
140
162
  std::unique_ptr<StatementParam> HandleArgs(const Napi::CallbackInfo &info);
141
163
  };
142
164
 
165
+ class QueryResult : public Napi::ObjectWrap<QueryResult> {
166
+ public:
167
+ explicit QueryResult(const Napi::CallbackInfo &info);
168
+ ~QueryResult() override;
169
+ static Napi::Object Init(Napi::Env env, Napi::Object exports);
170
+ std::unique_ptr<duckdb::QueryResult> result;
171
+
172
+ public:
173
+ static Napi::FunctionReference constructor;
174
+ Napi::Value NextChunk(const Napi::CallbackInfo &info);
175
+
176
+ private:
177
+ Database *database_ref;
178
+ };
179
+
143
180
  struct TaskHolder {
144
181
  std::unique_ptr<Task> task;
145
182
  napi_async_work request;
@@ -158,4 +195,6 @@ public:
158
195
  }
159
196
  };
160
197
 
198
+ Napi::Array EncodeDataChunk(Napi::Env env, duckdb::DataChunk &chunk, bool with_types, bool with_data);
199
+
161
200
  } // namespace node_duckdb