Skip to content
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
20 changes: 14 additions & 6 deletions src/components/QueryHistoryList.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<script setup lang="ts">
import { XIcon } from 'lucide-vue-next'
import { computed } from 'vue'
import { QUERY_HISTORY, injectRequired } from '../services/keys'
import type { QueryHistoryEntry } from '../services/QueryHistory'

/** `databaseKey`: only the history of this database is shown (see `QueryHistory.key`). */
const props = defineProps<{ databaseKey: string }>()
const emit = defineEmits<{ pick: [sql: string] }>()
const history = injectRequired(QUERY_HISTORY)
const entries = computed(databaseEntries)

function databaseEntries(): QueryHistoryEntry[] {
return history.list(props.databaseKey)
}

/** The time of the run, in the date format of the user. */
function ranAt(entry: QueryHistoryEntry): string {
Expand All @@ -16,20 +24,20 @@ function ranAt(entry: QueryHistoryEntry): string {
<div class="history">
<div class="section">
<span>
History <span class="count">{{ history.entries.length }}</span>
History <span class="count">{{ entries.length }}</span>
</span>
<button
class="clear"
title="Remove the whole history"
:disabled="history.entries.length === 0"
@click="history.clear()"
title="Remove the history of this database"
:disabled="entries.length === 0"
@click="history.clear(databaseKey)"
>
Clear
</button>
</div>
<div v-if="history.entries.length === 0" class="empty">No history yet</div>
<div v-if="entries.length === 0" class="empty">No history yet</div>
<ul v-else class="list">
<li v-for="entry in history.entries" :key="entry.id" @click="emit('pick', entry.sql)">
<li v-for="entry in entries" :key="entry.id" @click="emit('pick', entry.sql)">
<span class="dim">{{ ranAt(entry) }}</span>
<span class="dim connection" :title="entry.connection">{{ entry.connection }}</span>
<span class="name sql" :title="entry.sql">{{ entry.sql }}</span>
Expand Down
10 changes: 8 additions & 2 deletions src/components/QueryView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const saved = injectRequired(SAVED_QUERIES)
const savedDialog = useTemplateRef<InstanceType<typeof SavedQueryDialog>>('savedDialog')
// The saved queries of this tab are the ones of its server.
const savedKey = computed(savedQueriesKey)
// The history of this tab is the one of its open database. It follows a switch of database.
const historyKey = computed(historyDatabaseKey)

const host = useTemplateRef<HTMLDivElement>('host')
const columns = computed(resultColumns)
Expand All @@ -57,6 +59,10 @@ function savedQueriesKey(): string {
return saved.key(props.session.config)
}

function historyDatabaseKey(): string {
return history.key(props.session.config)
}

function resultColumns(): GridColumn[] {
const columns: GridColumn[] = []
for (const name of props.session.result?.columns ?? []) {
Expand Down Expand Up @@ -155,7 +161,7 @@ async function runChecked(sql: string): Promise<void> {
return
}
// Saved before the run. So SQL that fails is kept too.
history.add(sql, props.session.title)
history.add(sql, historyKey.value, props.session.title)
await props.session.runQuery(sql)
}

Expand Down Expand Up @@ -342,7 +348,7 @@ watch(sessionPlan, showPlanTab)
@pick="useHistory"
@rename="savedDialog?.rename($event)"
/>
<QueryHistoryList @pick="useHistory" />
<QueryHistoryList :database-key="historyKey" @pick="useHistory" />
</aside>
</div>
<ExportRowsMenu
Expand Down
5 changes: 4 additions & 1 deletion src/constants/history.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/** The history of run SQL is saved under this key. */
export const QUERY_HISTORY_STORAGE_KEY = 'sql-pro.query-history'

/** The history keeps this many entries. Older ones are dropped. */
/** The history keeps this many entries per database. Older ones are dropped. */
export const QUERY_HISTORY_MAX_LENGTH = 200

/** The history keeps this many entries over all databases, so the storage cannot grow without end. */
export const QUERY_HISTORY_TOTAL_MAX_LENGTH = 2000

/** The saved queries (named SQL) are saved under this key. */
export const SAVED_QUERIES_STORAGE_KEY = 'sql-pro.saved-queries'

Expand Down
67 changes: 59 additions & 8 deletions src/services/QueryHistory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { QUERY_HISTORY_MAX_LENGTH, QUERY_HISTORY_STORAGE_KEY } from '../constants/history'
import {
QUERY_HISTORY_MAX_LENGTH,
QUERY_HISTORY_STORAGE_KEY,
QUERY_HISTORY_TOTAL_MAX_LENGTH,
} from '../constants/history'
import type { ConnectionConfig } from '../types'
import type { ProfileStorage } from './ProfileStorage'

export interface QueryHistoryEntry {
Expand All @@ -8,13 +13,17 @@ export interface QueryHistoryEntry {
ranAt: string
/** The title of the tab it ran in. */
connection: string
/** The database it ran on (see `QueryHistory.key`). */
database: string
}

/**
* The SQL that was run in the Query tab, with the time of each run.
* The newest entry is first. The list is kept in the storage of the profile.
* Each entry belongs to one database. The newest entry is first.
* The list is kept in the storage of the profile.
*/
export class QueryHistory {
/** The entries of all databases, newest first. */
entries: QueryHistoryEntry[]
private storage: ProfileStorage

Expand All @@ -23,8 +32,34 @@ export class QueryHistory {
this.entries = this.read()
}

/** Adds the SQL as the newest entry. Empty SQL is ignored. */
add(sql: string, connection: string): void {
/**
* Names the database of a connection, like `mysql:127.0.0.1:3306/shop`.
* The user is left out, so two accounts on one database share the history.
* SQLite has no server, so its key is the file path.
*/
key(config: ConnectionConfig | null): string {
if (!config) {
return ''
}
if (config.driver === 'sqlite') {
return `sqlite:${config.database}`
}
return `${config.driver}:${config.host}:${config.port}/${config.database}`
}

/** The entries of one database, newest first. */
list(database: string): QueryHistoryEntry[] {
const entries: QueryHistoryEntry[] = []
for (const entry of this.entries) {
if (entry.database === database) {
entries.push(entry)
}
}
return entries
}

/** Adds the SQL as the newest entry of the database. Empty SQL is ignored. */
add(sql: string, database: string, connection: string): void {
const clean = sql.trim()
if (!clean) {
return
Expand All @@ -34,8 +69,14 @@ export class QueryHistory {
sql: clean,
ranAt: new Date().toISOString(),
connection,
database,
})
this.entries.length = Math.min(this.entries.length, QUERY_HISTORY_MAX_LENGTH)
const ofDatabase = this.list(database)
if (ofDatabase.length > QUERY_HISTORY_MAX_LENGTH) {
// Only one entry is added at a time, so at most the oldest one is too many.
this.entries.splice(this.entries.lastIndexOf(ofDatabase[ofDatabase.length - 1]), 1)
}
this.entries.length = Math.min(this.entries.length, QUERY_HISTORY_TOTAL_MAX_LENGTH)
this.write()
}

Expand All @@ -49,11 +90,19 @@ export class QueryHistory {
}
}

clear(): void {
this.entries = []
/** Removes the entries of one database. The others stay. */
clear(database: string): void {
const kept: QueryHistoryEntry[] = []
for (const entry of this.entries) {
if (entry.database !== database) {
kept.push(entry)
}
}
this.entries = kept
this.write()
}

/** Old entries saved without a database are dropped, because their database is not known. */
private read(): QueryHistoryEntry[] {
const entries: QueryHistoryEntry[] = []
try {
Expand All @@ -64,13 +113,15 @@ export class QueryHistory {
typeof item.id === 'string' &&
typeof item.sql === 'string' &&
typeof item.ranAt === 'string' &&
typeof item.connection === 'string'
typeof item.connection === 'string' &&
typeof item.database === 'string'
) {
entries.push({
id: item.id,
sql: item.sql,
ranAt: item.ranAt,
connection: item.connection,
database: item.database,
})
}
}
Expand Down
Loading
Loading