summaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/sqlitestudiocli/commands/clicommandclose.cpp
blob: 44fc72ca9de33cb3987cc20208c774035171e382 (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 "clicommandclose.h"
#include "cli.h"
#include "db/db.h"
#include "services/dbmanager.h"

void CliCommandClose::execute()
{
    if (!syntax.isArgumentSet(DB_NAME) && !cli->getCurrentDb())
    {
        println(tr("Cannot call %1 when no database is set to be current. Specify current database with %2 command or pass database name to %3.")
                .arg(cmdName("close")).arg(cmdName("use")).arg(cmdName("close")));
        return;
    }

    if (syntax.isArgumentSet(DB_NAME))
    {
        Db* db = DBLIST->getByName(syntax.getArgument(DB_NAME));
        if (db)
        {
            db->close();
            println(tr("Connection to database %1 closed.").arg(db->getName()));
        }
        else
            println(tr("No such database: %1. Use %2 to see list of known databases.").arg(syntax.getArgument(DB_NAME)).arg(cmdName("dblist")));
    }
    else if (cli->getCurrentDb())
    {
        cli->getCurrentDb()->close();
        println(tr("Connection to database %1 closed.").arg(cli->getCurrentDb()->getName()));
    }
}

QString CliCommandClose::shortHelp() const
{
    return tr("closes given (or current) database");
}

QString CliCommandClose::fullHelp() const
{
    return tr(
                "Closes database connection. If the database was already closed, nothing happens. "
                "If <name> is provided, it should be name of the database to close (as printed by %1 command). "
                "The the <name> is not provided, then current working database is closed (see help for %2 for details)."
                ).arg(cmdName("dblist")).arg(cmdName("use"));
}

void CliCommandClose::defineSyntax()
{
    syntax.setName("close");
    syntax.addArgument(DB_NAME, tr("name", "CLI command syntax"), false);
}