|
51 | 51 | * [closeSync([closeOption])](#-19-odbcstatement-closesynccloseoption) |
52 | 52 | * [.primaryKeys(catalog, schema, table [, callback])](#-20-odbcstatement-primarykeyscatalog-schema-table--callback) |
53 | 53 | * [.primaryKeysSync(catalog, schema, table)](#-21-odbcstatement-primarykeyssyncatalog-schema-table) |
| 54 | +* [.foreignKeys(pkCatalog, pkSchema, pkTable, fkCatalog, fkSchema, fkTable [, callback])](#-22-odbcstatement-foreignkeyspkcatalog-pkschema-pktable-fkcatalog-fkschema-fktable--callback) |
| 55 | +* [.foreignKeysSync(pkCatalog, pkSchema, pkTable, fkCatalog, fkSchema, fkTable)](#-23-odbcstatement-foreignkeysyncpkcatalog-pkschema-pktable-fkcatalog-fkschema-fktable) |
54 | 56 |
|
55 | 57 | **ODBCResult APIs** |
56 | 58 | * [.fetch([option] [, callback])](#-20-odbcresult-fetchoption--callback) |
@@ -855,6 +857,107 @@ ibmdb.open(cn, function(err, db) { |
855 | 857 | }); |
856 | 858 | ``` |
857 | 859 |
|
| 860 | +### <a name="foreignKeysApi"></a> 22) (ODBCStatement) .foreignKeys(pkCatalog, pkSchema, pkTable, fkCatalog, fkSchema, fkTable [, callback]) |
| 861 | + |
| 862 | +Returns foreign key relationships between two tables by calling `SQLForeignKeys()` via the ODBC CLI. |
| 863 | +The result set describes which columns in one table reference primary key columns in another. |
| 864 | + |
| 865 | +* **pkCatalog** - String or `null`. Catalog qualifier of the primary-key table. |
| 866 | +* **pkSchema** - String or `null`. Schema of the primary-key table. |
| 867 | +* **pkTable** - String or `null`. Name of the primary-key table (exact match). |
| 868 | +* **fkCatalog** - String or `null`. Catalog qualifier of the foreign-key table. |
| 869 | +* **fkSchema** - String or `null`. Schema of the foreign-key table. |
| 870 | +* **fkTable** - String or `null`. Name of the foreign-key table (exact match). |
| 871 | +* **callback** - _OPTIONAL_ - `callback (err, rows)`. If omitted a Promise is returned. |
| 872 | + |
| 873 | +At least one of `pkTable` or `fkTable` must be non-null (ODBC requirement). |
| 874 | + |
| 875 | +Behavior when only one table is specified: |
| 876 | +- Only `pkTable` given: returns all foreign keys in other tables that reference it. |
| 877 | +- Only `fkTable` given: returns all foreign keys in that table and the primary keys they reference. |
| 878 | +- Both given: returns the specific foreign key relationship between the two tables. |
| 879 | + |
| 880 | +Can also be called directly on a `Database` object as a convenience: |
| 881 | +`db.foreignKeys(pkCatalog, pkSchema, pkTable, fkCatalog, fkSchema, fkTable [, callback])` |
| 882 | + |
| 883 | +Each row in the result set contains the columns returned by `SQLForeignKeys`, including: |
| 884 | +`PKTABLE_SCHEM`, `PKTABLE_NAME`, `PKCOLUMN_NAME`, `FKTABLE_SCHEM`, `FKTABLE_NAME`, |
| 885 | +`FKCOLUMN_NAME`, `KEY_SEQ`, `UPDATE_RULE`, `DELETE_RULE`, `FK_NAME`, `PK_NAME`. |
| 886 | + |
| 887 | +> **Note:** After all rows are fetched, `foreignKeys()` calls `SQLFreeStmt(SQL_CLOSE)` internally to |
| 888 | +> close the result cursor. The statement handle itself remains valid — call `stmt.closeSync()` when |
| 889 | +> you are done with the statement to release it. |
| 890 | +
|
| 891 | +```javascript |
| 892 | +const ibmdb = require("ibm_db"); |
| 893 | +const cn = "DATABASE=dbname;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=dbuser;PWD=xxx"; |
| 894 | + |
| 895 | +// Async callback form via Database convenience method |
| 896 | +ibmdb.open(cn, function(err, db) { |
| 897 | + // Get the FK in ORDERS that references CUSTOMERS |
| 898 | + db.foreignKeys(null, "MYSCHEMA", "CUSTOMERS", null, "MYSCHEMA", "ORDERS", function(err, keys) { |
| 899 | + if (err) console.log(err); |
| 900 | + else console.log(keys); |
| 901 | + // e.g. [ { PKTABLE_SCHEM: 'MYSCHEMA', PKTABLE_NAME: 'CUSTOMERS', |
| 902 | + // PKCOLUMN_NAME: 'CUST_ID', FKTABLE_NAME: 'ORDERS', |
| 903 | + // FKCOLUMN_NAME: 'CUST_REF', KEY_SEQ: 1, FK_NAME: 'FK_ORDERS_CUST' } ] |
| 904 | + db.closeSync(); |
| 905 | + }); |
| 906 | +}); |
| 907 | + |
| 908 | +// Promise form via Database convenience method |
| 909 | +async function run() { |
| 910 | + const db = await ibmdb.open(cn); |
| 911 | + const keys = await db.foreignKeys(null, "MYSCHEMA", "CUSTOMERS", null, "MYSCHEMA", "ORDERS"); |
| 912 | + console.log(keys); |
| 913 | + db.closeSync(); |
| 914 | +} |
| 915 | + |
| 916 | +// Direct use on a bare statement handle obtained via db.conn.createStatement() |
| 917 | +ibmdb.open(cn, function(err, db) { |
| 918 | + db.conn.createStatement(function(err, stmt) { |
| 919 | + stmt.foreignKeys(null, "MYSCHEMA", "CUSTOMERS", null, "MYSCHEMA", "ORDERS", function(err, keys) { |
| 920 | + console.log(keys); |
| 921 | + stmt.closeSync(); // free the statement handle when done |
| 922 | + db.closeSync(); |
| 923 | + }); |
| 924 | + }); |
| 925 | +}); |
| 926 | +``` |
| 927 | + |
| 928 | +### <a name="foreignKeysSyncApi"></a> 23) (ODBCStatement) .foreignKeysSync(pkCatalog, pkSchema, pkTable, fkCatalog, fkSchema, fkTable) |
| 929 | + |
| 930 | +Synchronously returns foreign key relationships between two tables. |
| 931 | +Parameters and return value are the same as `foreignKeys()`. |
| 932 | + |
| 933 | +Can also be called directly on a `Database` object as a convenience: |
| 934 | +`db.foreignKeysSync(pkCatalog, pkSchema, pkTable, fkCatalog, fkSchema, fkTable)` |
| 935 | + |
| 936 | +> **Note:** After all rows are fetched, `foreignKeysSync()` calls `SQLFreeStmt(SQL_CLOSE)` internally |
| 937 | +> to close the result cursor. The statement handle itself remains valid — call `stmt.closeSync()` when |
| 938 | +> you are done with the statement to release it. |
| 939 | +
|
| 940 | +```javascript |
| 941 | +const ibmdb = require("ibm_db"); |
| 942 | +const cn = "DATABASE=dbname;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=dbuser;PWD=xxx"; |
| 943 | + |
| 944 | +// Sync form via Database convenience method |
| 945 | +ibmdb.open(cn, function(err, db) { |
| 946 | + const keys = db.foreignKeysSync(null, "MYSCHEMA", "CUSTOMERS", null, "MYSCHEMA", "ORDERS"); |
| 947 | + console.log(keys); |
| 948 | + db.closeSync(); |
| 949 | +}); |
| 950 | + |
| 951 | +// Direct use on a bare statement handle obtained via db.conn.createStatementSync() |
| 952 | +ibmdb.open(cn, function(err, db) { |
| 953 | + const stmt = db.conn.createStatementSync(); |
| 954 | + const keys = stmt.foreignKeysSync(null, "MYSCHEMA", "CUSTOMERS", null, "MYSCHEMA", "ORDERS"); |
| 955 | + console.log(keys); |
| 956 | + stmt.closeSync(); // free the statement handle when done |
| 957 | + db.closeSync(); |
| 958 | +}); |
| 959 | +``` |
| 960 | + |
858 | 961 | ### <a name="fetchApi"></a> 20) (ODBCResult) .fetch([option] [, callback]) |
859 | 962 |
|
860 | 963 | Fetch a row of data from an ODBCResult object asynchronously. |
|
0 commit comments