ThatJeffSmith

Custom SQLFORMATs with SQLcl

Tell Others About This Story:

When I make jokes about copying and pasting code from StackOverflow or Github, I’m not really joking.

Funny, because it's true.

Funny, because it’s true.

But in this case, it’s OK. Because, I’m copying and pasting code from OUR GitHub project, AND my boss wrote the code.

A question came in, and it’s come in a few times, but here’s the most recent variant:

So basically, you want to use one of our formatters, but you don’t like it 100%, so you want to change it a bit. And I said, just write your own then.

To the GitHubs!

So I’m going to change up the code to write the records delimited by a ‘;’ and no quotes on the strings.

var CopyFormatter  = Java.type("oracle.dbtools.raptor.format.CopyFormatter")
var FormatRegistry = Java.type("oracle.dbtools.raptor.format.FormatRegistry")
var NLSUtils       = Java.type("oracle.dbtools.raptor.utils.NLSUtils");
 
var cmd = {};
	cmd.rownum = 0;
	cmd.start       = function() { 
	}
	cmd.startRow    = function() { 
		cmd.rownum++;
		ctx.write(cmd.rownum+ "\n");
	}
 
	cmd.printColumn = function(val,view,model) {
		try{
		 var v  = NLSUtils.getValue(conn,val);
		 ctx.write(v + ";");
		} catch(e){
			ctx.write(e);
		}
	} 
	cmd.endRow = function () {
 
	} 
	cmd.end    = function () {
 
	}
    cmd.type = function() {
    	return "weird";
  	}
 
    cmd.ext = function() {
    	return ".weird";
  	}
// Actual Extend of the Java CommandListener
var weirdFormat = Java.extend(CopyFormatter, {
		start: 		 cmd.start,
		startRow: 	 cmd.startRow,
		printColumn: cmd.printColumn,
		endRow: 	 cmd.endRow ,
        end: 		 cmd.end,
        getType: 	 cmd.type,
        getExt: 	 cmd.ext, 
        setTableName: function(){}
 
});
 
// Registering the new Command
FormatRegistry.registerFormater(new weirdFormat());

Yes, I hard-coded the delimiter – ‘;’

Note that I named this format ‘weird’ – totally not judging folks in Europe for calling it CSV but not using a comma 🙂

So let’s see it work.

Any bugs here are mine - I'm not a great copy-paster to begin with and my JS skills are hilariously inept.

Any bugs here are mine – I’m not a great copy-paster to begin with and my JS skills are hilariously inept.

Related Posts Plugin for WordPress, Blogger...
Tell Others About This Story:

Similar Posts by Content Area: , , ,