Skip to content

Commit cda5331

Browse files
committed
Release 0.0.1
0 parents  commit cda5331

File tree

13 files changed

+386
-0
lines changed

13 files changed

+386
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
coverage.html
2+
*.seed
3+
*.log
4+
*.csv
5+
*.dat
6+
*.out
7+
*.pid
8+
*.gz
9+
10+
pids
11+
logs
12+
results
13+
14+
node_modules
15+
npm-debug.log

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test/
2+
coverage.html
3+
Makefile
4+
.travis.yml
5+
logo.png

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: node_js
2+
node_js:
3+
- '0.11'
4+
- '0.10'
5+
- '0.8'
6+
script: make test-coveralls
7+
before_install:
8+
- 'make install'

AUTHORS

Whitespace-only changes.

History.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0.0.1 / 2014-02-25
2+
==================
3+
4+
* Initial release

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
This software is licensed under the MIT License.
2+
3+
Copyright (C) 2014 fengmk2 <fengmk2@gmail.com> and other contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
TESTS = test/*.test.js
2+
REPORTER = spec
3+
TIMEOUT = 1000
4+
MOCHA_OPTS =
5+
6+
install:
7+
@npm install --registry=http://registry.cnpmjs.org --cache=${HOME}/.npm/.cache/cnpm
8+
9+
test: install
10+
@NODE_ENV=test ./node_modules/.bin/mocha \
11+
--reporter $(REPORTER) \
12+
--timeout $(TIMEOUT) \
13+
$(MOCHA_OPTS) \
14+
$(TESTS)
15+
16+
test-cov:
17+
@$(MAKE) test -s MOCHA_OPTS='--require blanket' REPORTER=html-cov | ./node_modules/.bin/cov
18+
19+
test-coveralls: test
20+
@echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
21+
@-$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js
22+
23+
test-all: test test-cov
24+
25+
autod: install
26+
@./node_modules/.bin/autod -w
27+
@$(MAKE) install
28+
29+
contributors: install
30+
@./node_modules/.bin/contributors -f plain -o AUTHORS
31+
32+
.PHONY: test
33+

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
sqlstring
2+
=======
3+
4+
[![Build Status](https://secure.travis-ci.org/fengmk2/sqlstring.png)](http://travis-ci.org/fengmk2/sqlstring) [![Coverage Status](https://coveralls.io/repos/fengmk2/sqlstring/badge.png)](https://coveralls.io/r/fengmk2/sqlstring) [![Dependency Status](https://gemnasium.com/fengmk2/sqlstring.png)](https://gemnasium.com/fengmk2/sqlstring)
5+
6+
[![NPM](https://nodei.co/npm/sqlstring.png?downloads=true&stars=true)](https://nodei.co/npm/sqlstring/)
7+
8+
![logo](https://raw.github.com/fengmk2/sqlstring/master/logo.png)
9+
10+
SQL escape and format from [mysql/lib/protocol/SqlString.js](https://github.com/felixge/node-mysql/blob/master/lib/protocol/SqlString.js)
11+
12+
## Install
13+
14+
```bash
15+
$ npm install sqlstring
16+
```
17+
18+
## Usage
19+
20+
```js
21+
var sqlstring = require('sqlstring');
22+
23+
sqlstring.format('SELECT * FROM user WHERE age = ? and gender = ?', [18, 'female']);
24+
// "SELECT * FROM user WHERE age = 18 and gender = 'female'"
25+
```
26+
27+
## License
28+
29+
(The MIT License)
30+
31+
Copyright (c) 2014 fengmk2 &lt;fengmk2@gmail.com&gt; and other contributors
32+
33+
Permission is hereby granted, free of charge, to any person obtaining
34+
a copy of this software and associated documentation files (the
35+
'Software'), to deal in the Software without restriction, including
36+
without limitation the rights to use, copy, modify, merge, publish,
37+
distribute, sublicense, and/or sell copies of the Software, and to
38+
permit persons to whom the Software is furnished to do so, subject to
39+
the following conditions:
40+
41+
The above copyright notice and this permission notice shall be
42+
included in all copies or substantial portions of the Software.
43+
44+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
45+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
48+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
49+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
50+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/sqlstring');

lib/sqlstring.js

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/**!
2+
* sqlstring - lib/sqlstring.js
3+
*
4+
* Copyright(c) 2014
5+
* MIT Licensed
6+
*
7+
* Authors:
8+
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
9+
*/
10+
11+
"use strict";
12+
13+
/**
14+
* Module dependencies.
15+
*/
16+
17+
var SqlString = exports;
18+
19+
SqlString.escapeId = function (val, forbidQualified) {
20+
if (Array.isArray(val)) {
21+
return val.map(function(v) {
22+
return SqlString.escapeId(v, forbidQualified);
23+
}).join(', ');
24+
}
25+
26+
if (forbidQualified) {
27+
return '`' + val.replace(/`/g, '``') + '`';
28+
}
29+
return '`' + val.replace(/`/g, '``').replace(/\./g, '`.`') + '`';
30+
};
31+
32+
SqlString.escape = function(val, stringifyObjects, timeZone) {
33+
if (val === undefined || val === null) {
34+
return 'NULL';
35+
}
36+
37+
switch (typeof val) {
38+
case 'boolean': return (val) ? 'true' : 'false';
39+
case 'number': return val+'';
40+
}
41+
42+
if (val instanceof Date) {
43+
val = SqlString.dateToString(val, timeZone || 'local');
44+
}
45+
46+
if (Buffer.isBuffer(val)) {
47+
return SqlString.bufferToString(val);
48+
}
49+
50+
if (Array.isArray(val)) {
51+
return SqlString.arrayToList(val, timeZone);
52+
}
53+
54+
if (typeof val === 'object') {
55+
if (stringifyObjects) {
56+
val = val.toString();
57+
} else {
58+
return SqlString.objectToValues(val, timeZone);
59+
}
60+
}
61+
62+
val = val.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) {
63+
switch(s) {
64+
case "\0": return "\\0";
65+
case "\n": return "\\n";
66+
case "\r": return "\\r";
67+
case "\b": return "\\b";
68+
case "\t": return "\\t";
69+
case "\x1a": return "\\Z";
70+
default: return "\\"+s;
71+
}
72+
});
73+
return "'"+val+"'";
74+
};
75+
76+
SqlString.arrayToList = function(array, timeZone) {
77+
return array.map(function(v) {
78+
if (Array.isArray(v)) return '(' + SqlString.arrayToList(v, timeZone) + ')';
79+
return SqlString.escape(v, true, timeZone);
80+
}).join(', ');
81+
};
82+
83+
SqlString.format = function(sql, values, stringifyObjects, timeZone) {
84+
values = [].concat(values);
85+
86+
return sql.replace(/\?\??/g, function(match) {
87+
if (!values.length) {
88+
return match;
89+
}
90+
91+
if (match == "??") {
92+
return SqlString.escapeId(values.shift());
93+
}
94+
return SqlString.escape(values.shift(), stringifyObjects, timeZone);
95+
});
96+
};
97+
98+
SqlString.dateToString = function(date, timeZone) {
99+
var dt = new Date(date);
100+
101+
if (timeZone != 'local') {
102+
var tz = convertTimezone(timeZone);
103+
104+
dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000));
105+
if (tz !== false) {
106+
dt.setTime(dt.getTime() + (tz * 60000));
107+
}
108+
}
109+
110+
var year = dt.getFullYear();
111+
var month = zeroPad(dt.getMonth() + 1, 2);
112+
var day = zeroPad(dt.getDate(), 2);
113+
var hour = zeroPad(dt.getHours(), 2);
114+
var minute = zeroPad(dt.getMinutes(), 2);
115+
var second = zeroPad(dt.getSeconds(), 2);
116+
var millisecond = zeroPad(dt.getMilliseconds(), 3);
117+
118+
return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second + '.' + millisecond;
119+
};
120+
121+
SqlString.bufferToString = function(buffer) {
122+
var hex = '';
123+
try {
124+
hex = buffer.toString('hex');
125+
} catch (err) {
126+
// node v0.4.x does not support hex / throws unknown encoding error
127+
for (var i = 0; i < buffer.length; i++) {
128+
var byte = buffer[i];
129+
hex += zeroPad(byte.toString(16));
130+
}
131+
}
132+
133+
return "X'" + hex+ "'";
134+
};
135+
136+
SqlString.objectToValues = function(object, timeZone) {
137+
var values = [];
138+
for (var key in object) {
139+
var value = object[key];
140+
if(typeof value === 'function') {
141+
continue;
142+
}
143+
144+
values.push(this.escapeId(key) + ' = ' + SqlString.escape(value, true, timeZone));
145+
}
146+
147+
return values.join(', ');
148+
};
149+
150+
function zeroPad(number, length) {
151+
number = number.toString();
152+
while (number.length < length) {
153+
number = '0' + number;
154+
}
155+
156+
return number;
157+
}
158+
159+
function convertTimezone(tz) {
160+
if (tz == "Z") return 0;
161+
162+
var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
163+
if (m) {
164+
return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60;
165+
}
166+
return false;
167+
}

logo.png

24.9 KB
Loading

package.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "sqlstring",
3+
"version": "0.0.1",
4+
"description": "SQL escape and format from mysql/lib/protocol/SqlString.js",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "make test-all"
8+
},
9+
"config": {
10+
"blanket": {
11+
"pattern": "sqlstring/lib",
12+
"data-cover-flags": {
13+
"debug": false
14+
}
15+
},
16+
"travis-cov": {
17+
"threshold": 20
18+
}
19+
},
20+
"dependencies": {
21+
22+
},
23+
"devDependencies": {
24+
"autod": "*",
25+
"blanket": "*",
26+
"contributors": "*",
27+
"cov": "*",
28+
"coveralls": "*",
29+
"mocha": "*",
30+
"mocha-lcov-reporter": "*",
31+
"should": "3.1.2",
32+
"travis-cov": "*"
33+
},
34+
"homepage": "https://github.com/fengmk2/sqlstring",
35+
"repository": {
36+
"type": "git",
37+
"url": "git://github.com/fengmk2/sqlstring.git",
38+
"web": "https://github.com/fengmk2/sqlstring"
39+
},
40+
"bugs": {
41+
"url": "https://github.com/fengmk2/sqlstring/issues",
42+
"email": "fengmk2@gmail.com"
43+
},
44+
"keywords": [
45+
"sqlstring",
46+
"sql",
47+
"escape",
48+
"sql escape"
49+
],
50+
"engines": {
51+
"node": ">= 0.10.0"
52+
},
53+
"author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)",
54+
"license": "MIT"
55+
}

test/sqlstring.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**!
2+
* sqlstring - test/sqlstring.test.js
3+
*
4+
* Copyright(c) 2014
5+
* MIT Licensed
6+
*
7+
* Authors:
8+
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
9+
*/
10+
11+
"use strict";
12+
13+
/**
14+
* Module dependencies.
15+
*/
16+
17+
var should = require('should');
18+
var sqlstring = require('../');
19+
20+
describe('sqlstring.test.js', function () {
21+
describe('format()', function () {
22+
it('should format sql with params work', function () {
23+
sqlstring.format('SELECT * FROM user WHERE age = ? and gender = ?', [18, 'female'])
24+
.should.equal('SELECT * FROM user WHERE age = 18 and gender = \'female\'');
25+
});
26+
});
27+
});

0 commit comments

Comments
 (0)