Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding support for TEXT fields, remove non-standard ascii characters #2

Merged
merged 3 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ Create a new `momyfile.json` file like this:
"createdAt": "DATETIME",
"field1": "number",
"field2": "string",
"field3": "boolean"
"field3": "boolean",
"field4": "TEXT"
}
}
}
Expand Down Expand Up @@ -103,6 +104,7 @@ Currently these native types are supported:
- `DATE`
- `DATETIME`
- `TIME`
- `TEXT`

There're also some aliases:

Expand Down
21 changes: 19 additions & 2 deletions lib/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const
type: 'DATE',
convert: val => {
if (!val) return 'NULL'
if (typeof val == 'number') val = moment(val).format('YYYY-MM-DD')
if (typeof val === 'string' || typeof val === 'number') {
var m = moment(val).isValid()? moment(val) : moment(val, 'x')
val = m.format('YYYY-MM-DD')
}
if (!/\d{4}-\d{2}-\d{2}/.test(val)) return 'NULL'
return `"${ val }"`
}
Expand All @@ -25,7 +28,10 @@ const
type: 'DATETIME',
convert: val => {
if (!val) return 'NULL'
if (typeof val == 'number') val = moment(val).format('YYYY-MM-DD HH:mm:ss')
if (typeof val === 'string' || typeof val === 'number') {
var m = moment(val).isValid()? moment(val) : moment(val, 'x')
val = m.format('YYYY-MM-DD HH:mm:ss')
}
if (!/\d{4}-\d{2}-\d{2} \d{2}:\d{2}(:\d{2})?/.test(val)) return 'NULL'
return `"${ val }"`
}
Expand Down Expand Up @@ -56,6 +62,17 @@ const
.replace(/"/g, '\\"') // escape double quotations
return `"${ val }"`
}
},
'TEXT': {
type: 'TEXT',
convert: val => {
val = (val || '')
.toString()
.replace(/\\/g, '\\\\') // escape backslashs
.replace(/"/g, '\\"') // escape double quotations
.replace(/[^\x00-\x7F]/g, "") // remove non ascii characters
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I'll remove this line, later.
We need to care about Unicode chars equally.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fixing the code like this.

.replace(/[\x00-\x1F\x7F]/g, '')

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Umm..., we should keep \x09 and \x10.

.replace(/[\x00-\x08\x11-\x1F\x7F]/g, '')

@odedelharar could you explain the use case you thought?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I had a problem with a field with a non-standard character. MySQL wouldn't allow inserting this line. If we want to keep this line (we should have some safeguard there), we should include all characters allowed in standard DB encoding (unless you have examples of non-standard encoding). So my code allows all common ASCII characters (0-127).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@odedelharar thanks. I've checked the spec of MySQL: \0 \b \t \n \r \x1a supposed to be allowed. Actually sqlstring module care about those control chars.
https://github.com/mysqljs/sqlstring/blob/master/lib/SqlString.js#L2

At this point, I'd like to remove control chars except above, and escape them by sqlstring.

The meaning of Non-standard depends on what charset we use, I think.
If you need another custom filter, feel free to send a new PR 😉

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll find a way to remove the offending characters without removing anything else. In my case the character was \xEF\xBF\xBD or �.

return `"${ val }"`
}
}
}

Expand Down