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

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

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

This comment has been minimized.

Show comment
Hide comment
@brianc

brianc Dec 11, 2012

Owner

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.

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

This comment has been minimized.

Show comment
Hide comment
@liamks

liamks Jan 15, 2013

Contributor

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.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@brianc

brianc Jan 16, 2013

Owner

totally! ❤️

On Tue, Jan 15, 2013 at 10:16 AM, Liam Kaufman notifications@github.comwrote:

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.


Reply to this email directly or view it on GitHubhttps://github.com/brianc/node-postgres/issues/217#issuecomment-12274892.

Owner

brianc commented Jan 16, 2013

totally! ❤️

On Tue, Jan 15, 2013 at 10:16 AM, Liam Kaufman notifications@github.comwrote:

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.


Reply to this email directly or view it on GitHubhttps://github.com/brianc/node-postgres/issues/217#issuecomment-12274892.

@liamks

This comment has been minimized.

Show comment
Hide comment
@liamks

liamks Jan 18, 2013

Contributor

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

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@brianc

brianc Jan 24, 2013

Owner

just merged this tonight. Thanks again!

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