Skip to content

Add char[] and varchar[] to the automatic array parser #217

Closed
ryangrahamnc opened this Issue Nov 6, 2012 · 5 comments

3 participants

@ryangrahamnc

varchar[] and char[] do not automatically get converted to arrays of strings in the database result.

Example input, trying with both text[] and varchar[]:

pg.connect(pgConnObj, function(err, client){
client.query('select \'{sadf,abc}\'::text[]', [], function(err, result){
console.log(111, result.rows);
});
client.query('select \'{sadf,abc}\'::varchar[]', [], function(err, result){
console.log(222, result.rows);
});
});

Which outputs:
111 [ { text: [ 'sadf', 'abc' ] } ]
222 [ { varchar: '{sadf,abc}' } ]

(text[] works, but varchar[] does not)

This can be fixed by adding these to the lib/arrayParser.js list of registers, but I'm unsure if there are other changes needed (like in lib/binaryParsers.js, for example):
register(1014, parseStringArray); //char[]
register(1015, parseStringArray); //varchar[]

@brianc
Owner
brianc commented Dec 11, 2012

any way you could write a test for this? or a pull request? Send me an email if you have any problems getting the test suite running locally. I'm trying to encourage more contribution. Pull requests === goodness.

@liamks
liamks commented Jan 15, 2013

I took a crack at it and the following seems to work for text and varchar:

var arry1 = '{"hello","hello"}';
var arry2 = '{asdf,asdf}';
var arry3 = '{{"asdf","asdf"},{"asdf","eeee"}}';
var arry4 = '{4,4}';
var arry5 = '{{5},5}';

function parse(str){
  var leftCurly = str.match(/{/g),
      rightCurly = str.match(/}/g),
      str2, strs, arry = [];

  if(leftCurly.length !== rightCurly.length){
    throw "array dimension not balanced";
  }

  if(leftCurly.length === 0){
    return str;
  }

  str2 = str.substring(1,str.length-1);
  if(leftCurly.length === 1){
    return str2.replace(/"/g,'').split(',');
  }else{
    strs = str2.match(/{(.*?)}/g);
    if(strs.join(',') !== str2) throw('array dimension not balanced');

    for (var i = 0; i < strs.length; i++) {
      arry.push(parse(strs[i]))
    };
    return arry;
  }
}

console.log(parse(arry1));
console.log(parse(arry2));
console.log(parse(arry3));
console.log(parse(arry4));
console.log(parse(arry5));

If you like the approach I'll submit a pull request.

@brianc
Owner
@liamks
liamks commented Jan 18, 2013

Perfect I just submitted a pull request - it turns out I just needed to register the datatypes like @ryangrahamnc suspected.

@brianc
Owner
brianc commented Jan 24, 2013

just merged this tonight. Thanks again!

@brianc brianc closed this Jan 24, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.