-1

This is my current request to insert data:

let record = {
    ip : '127.0.0.1',
    ...
};
conn.query('INSERT INTO events_log SET ?', [record]);

Is it possible to apply INET6_ATON function to the IP without converting it to a plain query? There are tens of parameters in this object and I wouldn't want to disassemble it.

The IP field is varbinary(16) type.

Eugene
  • 905
  • 1
  • 10
  • 22
  • 1
    `INET6_ATON(?)` should work fine. You can put functions around placeholders, just don't quote the placeholder because that makes it a string. – user3783243 Apr 02 '21 at 15:46
  • @user3783243 Do you mean `let ip = 'INET6_ATON(127.0.0.1)';` ? ER_DATA_TOO_LONG, changing INET6_ATON to 123INET6_ATON doesn't trigger an error as well. – Eugene Apr 02 '21 at 15:52
  • No, I meant in the query. `conn.query('INSERT INTO events_log SET INET6_ATON(?)` although maybe I'm mistaken about how node builds the identifiers to values... Is that SQL literal or pseudo? I think `INSERT INTO events_log (ip) values(INET6_ATON(?))` would work – user3783243 Apr 02 '21 at 17:21
  • Of course, it would work because its plain SQL, and I have the JS object which I have to pass to the node-mysql library – Eugene Apr 03 '21 at 10:37

1 Answers1

2

Yes, it is possible:

let INET6_ATON = { toSqlString: function() { return 'INET6_ATON(' + mysql.escape(ip) + ')'; } };

let record = {
    ip : INET6_ATON,
    ...
};
conn.query('INSERT INTO events_log SET ?', [record]);
Eugene
  • 905
  • 1
  • 10
  • 22