diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4104fbc --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "tslint.enable": false +} \ No newline at end of file diff --git a/README.md b/README.md index 21f6d20..63893da 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Sqleton -> A minimal MySQL client built with [electron](https://electronjs.org/) and [Vue.js](https://vuejs.org/) +> A minimal MySQL & SQLite client built with [electron](https://electronjs.org/) and [Vue.js](https://vuejs.org/) ## Project is under construction - Current and planned basic features: @@ -9,6 +9,8 @@ + [ ] Write and execute queries + [ ] Add a new record to a table + [ ] Add a new table to a database +- Changes by @ColinMcNeil + * Supports SQLite .db files ## Development Environment Requirements - Node >= 8.9 diff --git a/package.json b/package.json index c652eb4..d3fa866 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,10 @@ "not ie <= 8" ], "dependencies": { + "electron-rebuild": "^1.8.2", "mysql": "^2.15.0", "source-map-support": "^0.5.6", + "sqlite3": "^4.0.2", "vue": "^2.5.11", "vuex": "^3.0.1" }, diff --git a/src/main/database/connection.js b/src/main/database/connection.js index 74d6cc0..085937d 100644 --- a/src/main/database/connection.js +++ b/src/main/database/connection.js @@ -1,8 +1,12 @@ import mysql from 'mysql' +const sqlite3 = require('sqlite3').verbose() const extractCount = (response) => response['results'][0]['count(1)'] export default { + isSQLite() { + return this.credentials.type === 'sqlite' + }, /** * Create a connection to a database for the given credentials. * If a connection already exists, disconnect and create a new connection. @@ -12,7 +16,7 @@ export default { * @param {Object} credentials * @param {Function} callback */ - createConnection (credentials, callback) { + createConnection(credentials, callback) { this.database = credentials.database this.credentials = credentials @@ -23,6 +27,11 @@ export default { return callback(error) } } + if (this.isSQLite()) { + this.connection = new sqlite3.Database(this.credentials.host) + this.database='default' + return this.connection.serialize(callback) + } this.connection = mysql.createConnection(credentials) @@ -47,14 +56,18 @@ export default { }) }, - disconnect () { - this.connection.end(function (error) { - if (error) { - console.error('disconnect', error) - throw error - } - }) - + disconnect() { + if (this.isSQLite() && this.connection.close) { + this.connection.close() + } + else if(this.connection.end){ + this.connection.end(function (error) { + if (error) { + console.error('disconnect', error) + throw error + } + }) + } this.connection = undefined }, @@ -64,7 +77,20 @@ export default { * @param {String} query Prepared query * @returns {Promise} */ - executeQuery (query) { + executeQuery(query) { + if (this.isSQLite()) { + return new Promise((resolve, reject) => { + this.connection.all(query, function (error, results) { + if (error) { + reject({ success: false, message: error }) + } else { + const fieldsArr = Object.keys(results[0]) + const fields = fieldsArr.map(fieldStr=>({name:fieldStr})) + resolve({ success: true, results, fields }) + } + }) + }) + } return new Promise((resolve, reject) => { this.connection.query(query, function (error, results, fields) { if (error) { @@ -83,8 +109,9 @@ export default { * * @returns {Promise} */ - count (table) { + count(table) { const query = mysql.format('SELECT count(1) FROM ??', [table]) + return this.executeQuery(query) .then(response => extractCount(response)) @@ -95,7 +122,10 @@ export default { * * @returns {Promise} */ - databases () { + databases() { + if (this.isSQLite()) return new Promise((resolve) => { + resolve({ success: true, results: [{ Database: this.credentials.host }], fields:[{name:'Database'}] }) + }) return this.executeQuery('SHOW DATABASES') }, @@ -104,7 +134,8 @@ export default { * * @returns {Promise} */ - tables () { + tables() { + if (this.isSQLite()) return this.executeQuery("select name from sqlite_master where type='table'") return this.executeQuery('SHOW TABLES') }, @@ -114,7 +145,10 @@ export default { * @param {String} database Database name * @returns {Promise} */ - changeDatabase (database) { + changeDatabase(database) { + if (this.isSQLite()) return new Promise((resolve) => { + resolve({ success: true }) + }) return new Promise((resolve, reject) => { this.connection.changeUser({ database }, function (error) { if (error) { @@ -132,7 +166,8 @@ export default { * @param {String} database Database name * @returns {Promise} */ - tablesForDatabase (database) { + tablesForDatabase(database) { + if(this.isSQLite()) return this.tables() return this.changeDatabase(database) .then(() => this.tables()) }, @@ -160,8 +195,11 @@ export default { * * @returns {Promise} */ - describeTable (table) { - const query = mysql.format('DESCRIBE ??', [table]) + describeTable(table) { + let query = mysql.format('DESCRIBE ??', [table]) + if (this.isSQLite()) { + query = '.schema '+ table + } return this.executeQuery(query) }, diff --git a/src/main/ipc/replies/databases-reply.js b/src/main/ipc/replies/databases-reply.js index f761b19..fc55b9e 100644 --- a/src/main/ipc/replies/databases-reply.js +++ b/src/main/ipc/replies/databases-reply.js @@ -4,6 +4,7 @@ export default { handle (event) { return connection.databases() .then(response => { + console.log(response) event.sender.send('databases-response', response) }) .catch(error => { diff --git a/src/main/ipc/replies/table-data-reply.js b/src/main/ipc/replies/table-data-reply.js index da452e6..c1a0c8d 100644 --- a/src/main/ipc/replies/table-data-reply.js +++ b/src/main/ipc/replies/table-data-reply.js @@ -5,7 +5,6 @@ export default { return connection.getTableData(table, limit, offset) .then(response => { const data = { ...response, table, limit, offset } - event.sender.send('table-data-response', data) }) .catch(error => { diff --git a/src/renderer/components/views/ConnectionManager/CreateConnection.vue b/src/renderer/components/views/ConnectionManager/CreateConnection.vue index 65ae19e..00cc832 100644 --- a/src/renderer/components/views/ConnectionManager/CreateConnection.vue +++ b/src/renderer/components/views/ConnectionManager/CreateConnection.vue @@ -12,31 +12,44 @@ -
- - -
-
- - -
-
- - +
+
+ + +
-
- - + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
@@ -55,6 +68,7 @@