aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/sqlitestudiocli/commands/clicommandremove.cpp
blob: 6474a78ea587379deb6abb28b4004385de94b65d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "clicommandremove.h"
#include "cli.h"
#include "services/dbmanager.h"
#include "db/db.h"

void CliCommandRemove::execute()
{
    QString dbName = syntax.getArgument(DB_NAME);
    Db* db = DBLIST->getByName(dbName);
    if (!db)
    {
        println(tr("No such database: %1").arg(dbName));
        return;
    }

    bool isCurrent = cli->getCurrentDb() == db;
    QString name = db->getName();

    DBLIST->removeDb(db);
    println(tr("Database removed: %1").arg(name));

    QList<Db*> dblist = DBLIST->getDbList();
    if (isCurrent && dblist.size() > 0)
    {
        cli->setCurrentDb(dblist[0]);
        println(tr("New current database set:"));
        println(cli->getCurrentDb()->getName());
    }
    else
        cli->setCurrentDb(nullptr);
}

QString CliCommandRemove::shortHelp() const
{
    return tr("removes database from the list");
}

QString CliCommandRemove::fullHelp() const
{
    return tr(
                "Removes <name> database from the list of registered databases. "
                "If the database was not on the list (see %1 command), then error message is printed "
                "and nothing more happens."
                ).arg(cmdName("dblist"));
}

void CliCommandRemove::defineSyntax()
{
    syntax.setName("remove");
    syntax.addArgument(DB_NAME, tr("name", "CLI command syntax"));
}