// ***********************************************************************************
// Fun??o para criar dataset que possibilita a busca de dados diretamente no 
// SQL Server
// ***********************************************************************************
// Par?metros:
// -----------------------------------------------------------------------------------
// fields : Array contendo a query (consulta) a ser passada ao dataset
// constraints : n?o utilizado
// sortFields : n?o utilizado
// ***********************************************************************************
function createDataset(fields, constraints, sortFields) {
	var minhaQuery = fields[0];
	var dataSource = fields[1];

	log.warn("!!!!!!!!!!!! - > " + fields[0]);
	log.warn("!!!!!!!!!!!! - > " + fields[1]);

	var newDataset = DatasetBuilder.newDataset();
	var ic = new javax.naming.InitialContext();
	var ds = ic.lookup(dataSource);
	var created = false;
	try {
		var conn = ds.getConnection();
		var stmt = conn.createStatement();

		try {
			
			if(/INSERT INTO/i.test(minhaQuery) || /UPDATE/i.test(minhaQuery) || /DELETE/i.test(minhaQuery) ){				
			var rs = stmt.execute(minhaQuery);
			
			log.info("depura --- executar comando DML");
			
				return dsExecute();
				
			
			}else{
			
				var rs = stmt.executeQuery(minhaQuery);
				log.info("depura --- executar comando DDL")
			}
			log.warn("depura ----  query seeeem erro ");
		} catch (e) {

			log.warn("depura ----  query " + e.message);
			e.nome = "erroQuery";
			
			
			var erro = e.message;
			erro = erro.replace(/:/g, "..");
			
			var dsErro = DatasetBuilder.newDataset();
			dsErro.addColumn("RESULTADO");
			dsErro.addColumn("MENSAGEM");
			dsErro.addRow(new Array("ERRO", erro));
			
			return dsErro;

		}

		log.info("sem erro");

		var columnCount = rs.getMetaData().getColumnCount();
		log.warn("depura ----  tamanho" + columnCount);

		while (rs.next()) {
			if (!created) {
				for (var i = 1; i <= columnCount; i++) {
					newDataset.addColumn(rs.getMetaData().getColumnName(i));
				}
				created = true;
			}
			var Arr = new Array();
			for (var i = 1; i <= columnCount; i++) {
				var obj = rs.getObject(rs.getMetaData().getColumnName(i));
				if (null != obj) {
					Arr[i - 1] = rs.getObject(rs.getMetaData().getColumnName(i)).toString();
				} else {
					Arr[i - 1] = "NULL";
				}
			}
			newDataset.addRow(Arr);
		}
	} catch (e) {
	
		var erro = e.message;
		erro = erro.replace(/:/g, "..");
		
		var dsErro = DatasetBuilder.newDataset();
		dsErro.addColumn("RESULTADO");
		dsErro.addColumn("MENSAGEM");
		dsErro.addRow(new Array("ERRO", erro));


		return dsErro;
		log.info("ERRO==============> " + e.message);
	} finally {
		if (stmt != null)
			stmt.close();
		if (conn != null)
			conn.close();
//		conn.commit();
	}
	return newDataset;
}


function dsExecute(){
	
	var dsInsert = DatasetBuilder.newDataset();
	dsInsert.addColumn("RESULTADO");
	dsInsert.addColumn("MENSAGEM");
	dsInsert.addRow(new Array("OK", "registro inserido/altera/excluido com sucesso."));
	log.warn("registro inserido/altera/excluido com sucesso.");
	
	return dsInsert;

	
	
}