var DB = require("topsin.database");
var _e = require("topsin.error");
var _ = require("lodash");

var DEMO_CONN = 'DEMO_CONN';
DB.addConnection({
  database_type: 'pg',
  database_host: '10.92.20.25:5432',
  database_name: 'TOPMES_ATC_LASER_V6',
  database_user: 'toplinker',
  database_pwd: 'TopLinker0510'
}, DEMO_CONN);

var query = DB.query(DEMO_CONN);
query.begin();
try {
  var r = query.selectValue('SELECT 1', {});
  if (query.lastError().isValid()) {
    throw query.lastError().text();
  }
  print("r: ", r);

  // for (var i = 0; i < 100; i++) {
  //   print("insert " + i);
  //   var insert_sql = "INSERT INTO mes_prod_process_bom(prod_process_id, bom_uname) VALUES (211208100000010898, 'dirty')";
  //   query.execSql(insert_sql);
  //   if (query.lastError().isValid()) {
  //     throw query.lastError().text();
  //   }
  // }
  for (var i = 0; i < 10; i++) {
    print("insert " + i);
    
    var insert_sql = _.format("INSERT INTO mes_prod_process(id, partnumber) VALUES ({0}, 'dirty')", '21011310000001287' + i);
    print(insert_sql);
    query.execSql(insert_sql);
    if (query.lastError().isValid()) {
      throw query.lastError().text();
    }
  }

  query.commit();
} catch (e) {
  query.rollback();
  print(e);
}

DB.removeConnection(DEMO_CONN);