Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.
Open
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tslint.enable": false
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
72 changes: 55 additions & 17 deletions src/main/database/connection.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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

Expand All @@ -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)

Expand All @@ -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
},

Expand All @@ -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) {
Expand All @@ -83,8 +109,9 @@ export default {
*
* @returns {Promise<Number>}
*/
count (table) {
count(table) {
const query = mysql.format('SELECT count(1) FROM ??', [table])


return this.executeQuery(query)
.then(response => extractCount(response))
Expand All @@ -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')
},

Expand All @@ -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')
},

Expand All @@ -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) {
Expand All @@ -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())
},
Expand Down Expand Up @@ -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)
},
Expand Down
1 change: 1 addition & 0 deletions src/main/ipc/replies/databases-reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default {
handle (event) {
return connection.databases()
.then(response => {
console.log(response)
event.sender.send('databases-response', response)
})
.catch(error => {
Expand Down
1 change: 0 additions & 1 deletion src/main/ipc/replies/table-data-reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,44 @@
<label>SQL Server:</label>
<select class="form-control" v-model="credentials.type">
<option value="mysql">MySQL</option>
<option value="sqlite">SQLite</option>
</select>
</div>
<div class="form-group">
<label>Host:</label>
<input type="text" class="form-control"
v-model="credentials.host"
>
</div>
<div class="form-group">
<label>Port:</label>
<input type="text" class="form-control"
v-model="credentials.port"
>
</div>
<div class="form-group">
<label>Username:</label>
<input type="text" class="form-control"
v-model="credentials.user"
>
<div v-show="credentials.type==='sqlite'">
<div class="form-group">
<label>DB File:</label>
<input type="text" class="form-control"
@click="chooseFile"
v-model="credentials.host"
>
</div>
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" class="form-control"
v-model="credentials.password"
>

<div v-show="credentials.type==='mysql'">
<div class="form-group">
<label>Host:</label>
<input type="text" class="form-control"
v-model="credentials.host"
>
</div>
<div class="form-group">
<label>Port:</label>
<input type="text" class="form-control"
v-model="credentials.port"
>
</div>
<div class="form-group">
<label>Username:</label>
<input type="text" class="form-control"
v-model="credentials.user"
>
</div>
<div class="form-group">
<label>Password:</label>
<input type="password" class="form-control"
v-model="credentials.password"
>
</div>
</div>

<div class="form-group">
Expand All @@ -55,6 +68,7 @@
</template>

<script>
const { dialog } = require('electron').remote
export default {
name: 'CreateConnection',

Expand All @@ -79,6 +93,13 @@ export default {
methods: {
connect () {
this.$emit('connect', this.credentials)
},
chooseFile() {
this.credentials.host=dialog.showOpenDialog({
properties: ['openFile'],
filters: [{name:'DB Files',extensions:['db']}, {name:'All Files', extensions:['*']}]
})[0]

}
}
}
Expand All @@ -95,4 +116,3 @@ form {
margin-top: 40px;
}
</style>

1 change: 0 additions & 1 deletion src/renderer/components/views/ResultsListing/Results.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ export default {
border-bottom: 1px solid #ddd;
}
</style>

6 changes: 4 additions & 2 deletions src/renderer/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ export default {
handleTables({ commit }, { results, fields }) {
commit('SET_SELECTED_TABLE', '')
commit('SET_QUERY_RESULTS', [])

const tables = results.map(res => res[fields[0].name])
let tables = results
if(!fields) tables=tables.map(res=> res.name)
else
tables = tables.map(res => res[fields[0].name])
commit('SET_TABLES', tables)

commit('SET_LOADING', false)
Expand Down
Loading