Comments (21)
Possible solution:
In mysql2/lib/compile_text_parser.js at line 12 replace to (and pass typeCast or the full connection configuration as parameter to the compile function):
if (typeof typeCast == "function") {
var value = typeCast.call(this, fields[i]);
else {
var value = readCodeFor(fields[i].columnType, fields[i].characterSet));
}
result.push(' this['+ srcEscape(fields[i].name) + '] = ' + value;
from node-mysql2.
Can you post very small self contained example with your use case? I'll try to add typecasting code to generated parser. Note that it has to be invoked at packet parsing time, not when parser is generated
from node-mysql2.
This is my custom typecasting function in felixge/node-mysql: https://gist.github.com/hlwebs/6373184
- I remove the DATETIME case, so now every date is treated as a string
- Line 48 I convert all the binary and varbinary columns to hexadecimal
- Line 52 is a custom application login to decrypt some strings
from node-mysql2.
This should be in application layer imho.
I'll think how to integrate this into parser generation. The whole point of generated parser is to avoid big switch as type is known before you start receiving rows (and if you have many of them they all have same types)
from node-mysql2.
The last two points could be in the application layer without any problem, but the datetime is a big problem for me because of the 2,000 calendars events that I retrieve in one query and I don't need to parse as a Date object.
In felixge/node-mysql the typeCast is executed when the parser is generated right?
from node-mysql2.
felixge/node-mysql does not generate parser as far as I know. There is one generic parser for any packet type.
If you are really focused on performance, we need some way to integrate you typeCast function into parser generator, e.i your function generates inline javascript source to be included inside compile_text_parser.js
from node-mysql2.
Note that I'm trying to do as little type conversions as possible by default. For date/time types string is returned because I receive it as string over network (in text protocol). Ints and floats converted to Number because it's faster to parse directly to number from buffer than convert buffer slice to string (and later parse string to Number)
from node-mysql2.
what if I add API like
connection.registerTypeParser(type, function(field) {
// generate javascript here
});
your (simplified) example:
[Types.STRING, Types.VAR_STRING, Types.TINY_BLOB, Types.TINY_BLOB, Types.BLOB].forEach(function(type) {
connection.registerTypeParser(type, function(field, context) {
context.decrypt = crypt.decrypt;
if (field.characterSet === Charsets.BINARY) {
if (field.length > 16)
return "decrypt(packet.readLengthCodedBuffer().toString('hex'));"
else
return "packet.readLengthCodedBuffer().toString('hex');"
} else {
return "packet.readLengthCodedString();"
}
});
});
from node-mysql2.
I think this is a good idea and now I understand what do you mean by the "generated parser". With this new possible API, how are you planning to declare the default ones so they can be replaced. Example:
case Types.DATETIME:
return "new Date(packet.readLengthCodedString());";
to:
return "packet.readLengthCodedString();";
Also I see another API incompatibility with felixge/node-mysql. The Type object is not public, but in the felixge code it's: https://github.com/felixge/node-mysql/blob/master/index.js (Line 23).
English is not my primary language, sorry if I'm not being clear enough
from node-mysql2.
it is public - https://github.com/sidorares/node-mysql2/blob/master/index.js (line 11)
I think user handlers should just override default. Not sure if we need to support stack of parsers ('next parser' reference). Maybe just an option to generate default parser conditionally from your code.
Also might worth to require generation of the whole column assignment - including ' this['+ srcEscape(fields[i].name) + '] =
part. That way you can have user supplied name mangling etc
from node-mysql2.
answer to your question:
connection.registerTypeParser(Types.DATETIME, function(field, context) {
return "packet.readLengthCodedString();"
});
if there is no 'parsers stack' maybe setTypeParser
is better name (also need to have 2 versions for binary and text protocol. setTextRowTypeParser
+ setBinaryRowTypeParser
?)
from node-mysql2.
setTextRowTypeParser and setBinaryRowTypeParser sounds good to me.
About the generation of the whole column assignment, I don't see any use case for this, if anyone need to change the column name, they can do it in the SQL with "column AS new-name".
from node-mysql2.
https://github.com/felixge/node-mysql#joins-with-overlapping-column-names currently not supported. Might be a use case for generation of the whole column assignment
from node-mysql2.
I don't follow you, you're going to take multiples JOINS (or FROMS) and treat them as column types? I thought that the parser generator was only for column types.
from node-mysql2.
with joins field.columnName
might be not unique across columns. node-mysql allows you to prefix it with table name
from node-mysql2.
I understand this, but If someone need to replicate this feature with the type parser API you propose they are going to need to override every type.
I think this should be done in another place and not in the parser generator. Right now felixge/node-mysql is doing it after the type casting: https://github.com/felixge/node-mysql/blob/master/lib/protocol/packets/RowDataPacket.js#L30
from node-mysql2.
yes, you are right
from node-mysql2.
What do you think about create a new parent object for execute and query where we can move the getFieldsKey function (right now is the same code for both) and add the nestTables logic here too (I think is the same logic for binary and text). Also we can refactor some code from both objects to make them leaner.
from node-mysql2.
execute and query definitely need refactoring - they share a lot of common code. Yes, I want to move it to parent class.
from node-mysql2.
also, In execute command I actually have prepare + execute sequence. This was probably a bad idea, might be better to make 'composable' sequence and two separate commands. execute response itself is very similar to query response, the difference is in rows serialisation.
from node-mysql2.
Implemented in #351
from node-mysql2.
Related Issues (20)
- True negative: ssl-mode query params in connectionString considered as invalid HOT 1
- TypeError: LRU is not a constructor HOT 4
- PoolConnection.execute() retrun object v3.9.7 HOT 6
- Deadlock found when trying to get lock HOT 3
- [createConnection] Can't add new command when connection is in closed state HOT 8
- Sporadic read ETIMEDOUT error HOT 9
- Incorrect typings lead to pool.destroy is not a function HOT 2
- Invalid ssl option but still DB connection working fine HOT 2
- Access Denied after database restart (but granted after login via CLI) HOT 23
- Importing mysql2 causes source maps not working in project bundled with webpack HOT 7
- DELIMITER causes syntax error
- Building query for ORDER BY ? DESC/ASC doesn't seem to use the values HOT 1
- How do you close connections???? HOT 1
- [bug][regression] mysql2.setMaxParserCache fails with TypeError HOT 3
- bigNumberStrings: true is not return type number
- namedPlaceholders: "ER_WRONG_ARGUMENTS" in data object with property number type for LIMIT clause.
- Read ECONNRESET after a few minutes idle HOT 1
- Typescript conversion HOT 9
- bigint will lose precision when it is larger than Number.MAX_SAFE_INTEGER HOT 2
- Performance Monitoring
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-mysql2.