Skip to content

Commit

Permalink
exec/execFile/end
Browse files Browse the repository at this point in the history
  • Loading branch information
FvckSh1t committed Dec 28, 2013
1 parent aceb672 commit dfbddac
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 49 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ Execute you *.sql file which contains multiple sql statements. Usate: init datab
execsql -c "localhost" "root" "root"
```

3. Execute a `.sql` file
- Execute a bunch of sql statements
```shell
execsql ./db.sql
execsql "use db_cam; delete from admin;"
```

- Execute a `.sql` file
```shell
execsql -f ./db.sql
```

### As a Node dependency
Expand All @@ -33,14 +38,16 @@ Execute you *.sql file which contains multiple sql statements. Usate: init datab
2. Require and use
```js
var execsql = require('execsql'),
connConfig = {
dbConfig = {
host: 'localhost',
user: 'root',
password: 'root'
},
sql = 'use db_cam;',
sqlFile = __dirname + '/db.sql';
execsql.config(connConfig)
.exec(sqlFile, function(err, results){
execsql.config(dbConfig)
.exec(sql)
.execFile(sqlFile, function(err, results){
console.log(results);
});
}).end();
```
21 changes: 15 additions & 6 deletions bin/execsql
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ var fs = require('fs'),
configFile = __dirname + '/../config.dat',
argv = require('optimist')
.boolean('config').alias('c', 'config')
.boolean('file').alias('f', 'file')
.argv,
isConfig = argv['config'];
isConfig = argv['config'],
isFile = argv['file'],
config;

if (isConfig) {
var config = {
config = {
host: argv['_'][0],
user: argv['_'][1],
password: argv['_'][2]
Expand All @@ -23,17 +26,23 @@ if (isConfig) {
fs.writeFileSync(configFile, JSON.stringify(config));
console.log('db config has been set');
} else {
var config;
try {
config = JSON.parse(fs.readFileSync(configFile));
} catch (err) {
throw new Error('invalid db config');
}
var sqlFile = argv['_'][0];
execsql.config(config).exec(sqlFile, function (err, results) {
execsql.config(config);
if (isFile) {
execsql.execFile(argv['_'][0], done);
} else {
execsql.exec(argv['_'][0], done);
}
function done(err, results) {
if (err) throw err;
results.forEach(function (result, i) {
console.log(i + ' -- \r\n' + JSON.stringify(result));
});
});
execsql.end();
process.exit(0);
}
}
34 changes: 22 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,36 @@ var fs = require('fs'),
mysql = require('mysql'),
config = {
multipleStatements: true
};
},
conn;

function exec(filename, callback) {
function exec(sql, callback) {
conn.query(sql, function (err, results) {
if (!_.isArray(results)) {
results = [results];
}
callback(err, results);
});
return this;
}

function execFile(filename, callback) {
fs.readFile(filename, 'utf8', function (err, data) {
if (err) throw err;
var conn = mysql.createConnection(config);
conn.connect();
conn.query(data, function (err, results) {
if (!_.isArray(results)) {
results = [results];
}
callback(err, results);
});
conn.end();
exec(data, callback);
});
return this;
}

exports.exec = exec;
exports.execFile = execFile;
exports.end = function () {
conn.end();
return this;
};
exports.config = function (options) {
_.extend(config, _.pick(options, ['host', 'user', 'password']));
conn = mysql.createConnection(config);
conn.connect();
return this;
}
};
66 changes: 41 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
{
"name": "execsql",
"version": "0.0.1",
"description": "Execute you *.sql file which contains multiple sql statements. Usate: init database.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"mysql",
"execsql",
"initsql"
],
"author": "Camwide",
"contributors": ["Fritz-Lium"],
"homepage": "https://github.com/Fritz-Lium/execsql",
"repository": "[email protected]:Fritz-Lium/execsql.git",
"license": "BSD-2-Clause",
"dependencies": {
"underscore": "1.5.2",
"optimist": "0.6.0",
"mysql": "2.0.0-rc2"
},
"bin": {
"execsql": "./bin/execsql"
}
"name": "execsql",
"version": "0.0.2",
"description": "Execute you *.sql file which contains multiple sql statements. Usate: init database.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"mysql",
"execsql",
"initsql"
],
"author": {
"name": "Camwide"
},
"contributors": [
{
"name": "Fritz-Lium"
}
],
"homepage": "https://github.com/Fritz-Lium/execsql",
"repository": {
"type": "git",
"url": "[email protected]:Fritz-Lium/execsql.git"
},
"license": "BSD-2-Clause",
"dependencies": {
"underscore": "1.5.2",
"optimist": "0.6.0",
"mysql": "2.0.0-rc2"
},
"bin": {
"execsql": "./bin/execsql"
},
"readme": "execsql\n=======\n\nAn npm project. Node.js\nExecute you *.sql file which contains multiple sql statements. Usate: init database.\n\n## Usage\n\n### As a CLI tool\n\n1. Make sure that you have `execsql` installed globally\n\t```shell\n\tnpm install -g execsql\n\t```\n\n2. Configure your db access for the first time\n\t```shell\n\texecsql -c \"localhost\" \"root\" \"root\"\n\t```\n\n3. Execute a `.sql` file\n\t```shell\n\texecsql ./db.sql\n\t```\n\n### As a Node dependency\n\n1. Make sure that you have `execsql` installed locally\n\t```shell\n\tnpm install execsql\n\t```\n\n2. Require and use\n\t```js\n\tvar execsql = require('execsql'),\n\t\tdbConfig = {\n\t\t\thost: 'localhost',\n\t\t\tuser: 'root',\n\t\t\tpassword: 'root'\n\t\t},\n\t\tsqlFile = __dirname + '/db.sql';\n\texecsql.config(dbConfig)\n\t\t.exec(sqlFile, function(err, results){\n\t\t\tconsole.log(results);\n\t\t});\n\t```\n",
"readmeFilename": "README.md",
"bugs": {
"url": "https://github.com/Fritz-Lium/execsql/issues"
},
"_id": "[email protected]",
"_from": "[email protected]"
}

0 comments on commit dfbddac

Please sign in to comment.