Skip to content

Commit 8c98a40

Browse files
authored
Add primaryKeys() and foreignKeys() functions on Connection (#282)
* Add primaryKeys() and foreignKeys() functions on Connection Signed-off-by: Mark Irish <[email protected]>
1 parent 549794e commit 8c98a40

File tree

10 files changed

+874
-3
lines changed

10 files changed

+874
-3
lines changed

lib/Connection.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,60 @@ class Connection {
295295
});
296296
}
297297

298+
// TODO: Documentation
299+
primaryKeys(catalog, schema, table, callback = undefined) {
300+
// promise...
301+
if (callback === undefined) {
302+
if (!this.odbcConnection)
303+
{
304+
throw new Error(Connection.CONNECTION_CLOSED_ERROR);
305+
}
306+
return new Promise((resolve, reject) => {
307+
this.odbcConnection.primaryKeys(catalog, schema, table, (error, result) => {
308+
if (error) {
309+
reject(error);
310+
} else {
311+
resolve(result);
312+
}
313+
});
314+
});
315+
}
316+
317+
// ...or callback
318+
if (!this.odbcConnection) {
319+
callback(new Error(Connection.CONNECTION_CLOSED_ERROR));
320+
} else {
321+
this.odbcConnection.primaryKeys(catalog, schema, table, callback);
322+
}
323+
}
324+
325+
// TODO: Documentation
326+
foreignKeys(catalog, schema, table, fkCatalog, fkSchema, fkTable, callback = undefined) {
327+
// promise...
328+
if (callback === undefined) {
329+
if (!this.odbcConnection)
330+
{
331+
throw new Error(Connection.CONNECTION_CLOSED_ERROR);
332+
}
333+
return new Promise((resolve, reject) => {
334+
this.odbcConnection.foreignKeys(catalog, schema, table, fkCatalog, fkSchema, fkTable, (error, result) => {
335+
if (error) {
336+
reject(error);
337+
} else {
338+
resolve(result);
339+
}
340+
});
341+
});
342+
}
343+
344+
// ...or callback
345+
if (!this.odbcConnection) {
346+
callback(new Error(Connection.CONNECTION_CLOSED_ERROR));
347+
} else {
348+
this.odbcConnection.foreignKeys(catalog, schema, table, fkCatalog, fkSchema, fkTable, callback);
349+
}
350+
}
351+
298352
// TODO: Documentation
299353
columns(catalog, schema, table, type, callback = undefined) {
300354
// promise...

lib/odbc.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ declare namespace odbc {
9494

9595
createStatement(callback: (error: NodeOdbcError, statement: Statement) => undefined): undefined;
9696

97-
tables(catalog: string, schema: string, table: string, callback: (error: NodeOdbcError, result: Result<unknown>) => undefined): undefined;
97+
primaryKeys(catalog: string, schema: string, table: string, callback: (error: NodeOdbcError, result: Result<unknown>) => undefined): undefined;
98+
99+
foreignKeys(pkCatalog: string, pkSchema: string, pkTable: string, fkCatalog: string, fkSchema: string, fkTable: string, callback: (error: NodeOdbcError, result: Result<unknown>) => undefined): undefined;
100+
101+
tables(catalog: string, schema: string, table: string, type: string, callback: (error: NodeOdbcError, result: Result<unknown>) => undefined): undefined;
98102

99103
columns(catalog: string, schema: string, table: string, callback: (error: NodeOdbcError, result: Result<unknown>) => undefined): undefined;
100104

@@ -120,6 +124,10 @@ declare namespace odbc {
120124

121125
createStatement(): Promise<Statement>;
122126

127+
primaryKeys(catalog: string, schema: string, table: string, callback: (error: NodeOdbcError, result: Result<unknown>) => undefined): Promise<Result<unknown>>;
128+
129+
foreignKeys(pkCatalog: string, pkSchema: string, pkTable: string, fkCatalog: string, fkSchema: string, fkTable: string, callback: (error: NodeOdbcError, result: Result<unknown>) => undefined): Promise<Result<unknown>>;
130+
123131
tables(catalog: string, schema: string, table: string, type: string): Promise<Result<unknown>>;
124132

125133
columns(catalog: string, schema: string, table: string, column: string): Promise<Result<unknown>>;

src/odbc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <uv.h>
2727
#include <napi.h>
2828
#include <wchar.h>
29+
#include <new>
2930

3031
#include <algorithm>
3132

@@ -212,6 +213,9 @@ typedef struct StatementData {
212213
SQLTCHAR *catalog = NULL;
213214
SQLTCHAR *schema = NULL;
214215
SQLTCHAR *table = NULL;
216+
SQLTCHAR *fkCatalog = NULL;
217+
SQLTCHAR *fkSchema = NULL;
218+
SQLTCHAR *fkTable = NULL;
215219
SQLTCHAR *type = NULL;
216220
SQLTCHAR *column = NULL;
217221
SQLTCHAR *procedure = NULL;
@@ -249,6 +253,9 @@ typedef struct StatementData {
249253
delete[] this->catalog; this->catalog = NULL;
250254
delete[] this->schema; this->schema = NULL;
251255
delete[] this->table; this->table = NULL;
256+
delete[] this->fkCatalog; this->fkCatalog = NULL;
257+
delete[] this->fkSchema; this->fkSchema = NULL;
258+
delete[] this->fkTable; this->fkTable = NULL;
252259
delete[] this->type; this->type = NULL;
253260
delete[] this->column; this->column = NULL;
254261
delete[] this->procedure; this->procedure = NULL;

0 commit comments

Comments
 (0)