Table copy operations in cross database contexts may be complex. For example, moving data from DB2 to Informix to Oracle to SQLServer may lead to many data type conflicts. You also need some kind of application to keep connections on both databases and logic to do data transfer.
Table copy function allows to perform cross database copy operations with little effort.
1 Table lock
You can lock a table either in EXCLUSIVE or SHARE mode.
<script> Ax.db.execute('CREATE TABLE test (col char(10))'); Ax.db.lockTable("test", "exclusive"); Ax.db.execute('DROP TABLE test'); </script>
2 Table copy
In the following example we copy a table from our current database to an oracle instance defined in our servers configuration. Destination table will be create if not exits.
<script> // This is our current connection var db_src = Ax.db; // Connect to a server named "oracle" and to it's database 'schema_o' var db_dst = db_src.of("schema_o"); // Transfer table 'customer' from src to dst var event = db_src.copy(db_dst, "customer", config => { // DELETE: Delete all rows on destination // DROP: Drop table and recreate // MERGE: Add new rows and update already existing ones (by PK) // TRUNCATE: Execute truncate command config.setCopyType("DELETE"); // Copy ordering (may be useful to preserve sequence ordering as sequences will be regenerated on destination) config.setOrderBy("customer_num"); // Activate Batch insert buffering 1000 rows config.setBatchSize(1000); // Set Filter condition (Where) config.setCondition("1=1"); // Order by condition config.setOrderBy("CUSTOMER_NUM"); // Skip first 100 rows config.setSkip(100); // Copy only 100 rows (order is important if we use this option) config.setMaxRows(100); // For example, when source is ORACLE AL32UTF8 reduce to UTF-8 config.setEncoding("UTF-8"); // Set fetch size for buffering data readed config.setFetchSize(2000); }); console.log(event); console.log(db_dst.executeQuery("SELECT * FROM customer ORDER BY customer_num")); </script>
2.1 Table copy examples
MERGE copy type, allows to insert new rows from source into destination table and update row content of already existing rows by matching primary key columns.
If no primarhy key is present in destination table, the source rows are just appended into destination table.
<script> var dborig = Ax.db.of("demo_sports"); var dbdest = Ax.db; dborig.execute(`DROP TABLE IF EXISTS tblcopy_test`); dborig.execute(`CREATE TABLE tblcopy_test (p1 char(10), c1 CHAR(20), PRIMARY KEY(p1));`); dborig.execute(`INSERT INTO tblcopy_test VALUES('A', 'ROW A');`); dborig.execute(`INSERT INTO tblcopy_test VALUES('B', 'ROW B');`); dborig.execute(`INSERT INTO tblcopy_test VALUES('C', 'ROW C');`); let rs = dborig.executeQuery(`SELECT * FROM tblcopy_test`); console.log(rs); rs.close(); dbdest.execute(`DROP TABLE IF EXISTS tblcopy_test`); dbdest.execute(`CREATE TABLE tblcopy_test (p1 char(10), c1 CHAR(20), PRIMARY KEY(p1));`); rs = dbdest.executeQuery(`SELECT * FROM tblcopy_test`); console.log(rs); rs.close(); // 1st round var event1 = dborig.copy(dbdest, 'tblcopy_test', config => { config.setCopyType("MERGE"); }); rs = dbdest.executeQuery(`SELECT * FROM tblcopy_test`); console.log(rs); rs.close(); dborig.execute(`UPDATE tblcopy_test SET c1 = 'ROW A UPDATED' WHERE p1 = 'A'`); dbdest.execute(`DELETE FROM tblcopy_test WHERE p1 = 'B'`); // 2nd round var event2 = dborig.copy(dbdest, 'tblcopy_test', config => { config.setCopyType("MERGE"); }); // DESTINATION console.log(event2); rs = dbdest.executeQuery(`SELECT * FROM tblcopy_test`); console.log(rs); rs.close(); dborig.execute('DROP TABLE tblcopy_test'); dbdest.execute('DROP TABLE tblcopy_test'); </script>
Similar behaviour can be obtained by using ResultSet Writer Class:
<script> var dborig = Ax.db.of("demo_sports"); dborig.setLogger(console.getLogger()); var dbdest = Ax.db; dbdest.setLogger(console.getLogger()); dborig.execute(`DROP TABLE IF EXISTS tblcopy_test`); dborig.execute(`CREATE TABLE tblcopy_test (p1 char(10), c1 CHAR(20), PRIMARY KEY(p1));`); dborig.execute(`INSERT INTO tblcopy_test VALUES('A', 'ROW A');`); dborig.execute(`INSERT INTO tblcopy_test VALUES('B', 'ROW B');`); dborig.execute(`INSERT INTO tblcopy_test VALUES('C', 'ROW C');`); dbdest.execute(`DROP TABLE IF EXISTS tblcopy_test`); dbdest.execute(`CREATE TABLE tblcopy_test (p1 char(10), c1 CHAR(20), PRIMARY KEY(p1))`); var rso = dborig.executeQuery(`SELECT * FROM tblcopy_test`).toMemory(); new Ax.rs.Writer(rso).db(options => { options.setLogger(console.getLogger()); // apply operations on current database options.setConnection(dbdest.getObject()); // apply operations on table "antibiotics" options.setTableName("tblcopy_test"); options.setTablePrimaryKeyColumns("p1"); } ); let rs = dbdest.executeQuery(`SELECT * FROM tblcopy_test`); console.log(rs); rs.close(); dborig.execute('DROP TABLE tblcopy_test'); dbdest.execute('DROP TABLE tblcopy_test'); </script>