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.

Copy
<script>
    db.lockTable("master", "exclusive");
</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.

Copy
<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("oracle", "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>
test_data -> schema_o : customer   28 deleted,   10 inserted, 0 encoding-error,  500 rows/s
+----------------+---------------+---------------+--------------------+--------------------+--------------------+---------------+-----+-------+------------------+
|CUSTOMER_NUM    |FNAME          |LNAME          |COMPANY             |ADDRESS1            |ADDRESS2            |CITY           |STATE|ZIPCODE|PHONE             |
+----------------+---------------+---------------+--------------------+--------------------+--------------------+---------------+-----+-------+------------------+
|             101|Ludwig         |Pauli          |All Sports Supplies |213 Erstwild Court  |                    |Sunnyvale      |CA   |94086  |408-789-8075      |
|             102|Carole         |Sadler         |Sports Spot         |785 Geary St        |                    |San Francisco  |CA   |94117  |415-822-1289      |
|             103|Philip         |Currie         |Phil's Sports       |654 Poplar          |P. O. Box 3498      |Palo Alto      |CA   |94303  |415-328-4543      |
|             104|Anthony        |Higgins        |Play Ball!          |East Shopping Cntr. |422 Bay Road        |Redwood City   |CA   |94026  |415-368-1100      |
|             105|Raymond        |Vector         |Los Altos Sports    |1899 La Loma Drive  |                    |Los Altos      |CA   |94022  |415-776-3249      |
|             106|George         |Watson         |Watson & Son        |1143 Carver Place   |                    |Mountain View  |CA   |94063  |415-389-8789      |
|             107|Charles        |Ream           |Athletic Supplies   |41 Jordan Avenue    |                    |Palo Alto      |CA   |94304  |415-356-9876      |
|             108|Donald         |Quinn          |Quinn's Sports      |587 Alvarado        |                    |Redwood City   |CA   |94063  |415-544-8729      |
|             109|Jane           |Miller         |Sport Stuff         |Mayfair Mart        |7345 Ross Blvd.     |Sunnyvale      |CA   |94086  |408-723-8789      |
|             110|Roy            |Jaeger         |AA Athletics        |520 Topaz Way       |                    |Redwood City   |CA   |94062  |415-743-3611      |
+----------------+---------------+---------------+--------------------+--------------------+--------------------+---------------+-----+-------+------------------+

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.

Copy
<script>
	var dborig = Ax.db.of("demo_sports");
    var dbdest = Ax.db.of("creaicon");

    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');`);
    console.log(dborig.executeQuery(`SELECT * FROM tblcopy_test`));

    dbdest.execute(`DROP TABLE IF EXISTS tblcopy_test`);
    dbdest.execute(`CREATE TABLE tblcopy_test (p1 char(10), c1 CHAR(20), PRIMARY KEY(p1));`);
    console.log(dbdest.executeQuery(`SELECT * FROM tblcopy_test`));


    // 1st round
    var event1 = dborig.copy(dbdest, 'tblcopy_test', config => {
        config.setCopyType("MERGE");
    });
    console.log(dbdest.executeQuery(`SELECT * FROM tblcopy_test`));

    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);
    console.log(dbdest.executeQuery(`SELECT * FROM tblcopy_test`));
</script>

Similar behaviour can be obtained by using ResultSet Writer Class:

Copy
<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`);
  
    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");
        }
    );

    
    console.log(dbdest.executeQuery(`SELECT * FROM tblcopy_test`));
</script>