mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-05-19 03:58:28 -05:00
Add rough implementation of some command line options
Support some additional command line options for opening and running SQL scripts directly. Don't just take the first argument as a SQLite database to open as it could be a command line option - Qt and X11 have some default ones.
This commit is contained in:
+6
-1
@@ -123,8 +123,10 @@ void MainWindow::init()
|
||||
ui->editLogUser->setFont(font);
|
||||
}
|
||||
|
||||
void MainWindow::fileOpen(const QString & fileName)
|
||||
bool MainWindow::fileOpen(const QString& fileName)
|
||||
{
|
||||
bool retval = false;
|
||||
|
||||
QString wFile = fileName;
|
||||
if (!QFile::exists(wFile))
|
||||
{
|
||||
@@ -140,6 +142,7 @@ void MainWindow::fileOpen(const QString & fileName)
|
||||
{
|
||||
statusEncodingLabel->setText(db.getPragma("encoding"));
|
||||
setCurrentFile(wFile);
|
||||
retval = true;
|
||||
} else {
|
||||
QString err = tr("An error occurred: %1").arg(db.lastErrorMessage);
|
||||
QMessageBox::warning(this, QApplication::applicationName(), err);
|
||||
@@ -151,6 +154,8 @@ void MainWindow::fileOpen(const QString & fileName)
|
||||
loadPragmas();
|
||||
openSqlTab(true);
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
void MainWindow::fileOpen()
|
||||
|
||||
+4
-2
@@ -27,6 +27,8 @@ public:
|
||||
MainWindow(QWidget* parent = 0);
|
||||
~MainWindow();
|
||||
|
||||
DBBrowserDB* getDb() { return &db; }
|
||||
|
||||
private:
|
||||
struct PragmaValues
|
||||
{
|
||||
@@ -89,9 +91,10 @@ protected:
|
||||
void resizeEvent(QResizeEvent *event);
|
||||
|
||||
public slots:
|
||||
virtual void fileOpen( const QString & fileName );
|
||||
virtual bool fileOpen(const QString& fileName);
|
||||
virtual void logSql(const QString &sql, int msgtype);
|
||||
virtual void dbState(bool dirty);
|
||||
virtual void browseRefresh();
|
||||
|
||||
private slots:
|
||||
virtual void createTreeContextMenu(const QPoint & qPoint);
|
||||
@@ -110,7 +113,6 @@ private slots:
|
||||
virtual void navigateNext();
|
||||
virtual void navigateGoto();
|
||||
virtual void setRecordsetLabel();
|
||||
virtual void browseRefresh();
|
||||
virtual void createTable();
|
||||
virtual void createIndex();
|
||||
virtual void compact();
|
||||
|
||||
+64
-4
@@ -4,6 +4,7 @@
|
||||
#include <QTranslator>
|
||||
#include <QLibraryInfo>
|
||||
#include <QLocale>
|
||||
#include <QDebug>
|
||||
|
||||
#if 0 and defined(Q_WS_MAC)
|
||||
#include <Carbon/Carbon.h>
|
||||
@@ -90,16 +91,75 @@ int main( int argc, char ** argv )
|
||||
apptranslator.load("translations/tr_" + QLocale::system().name());
|
||||
a.installTranslator(&apptranslator);
|
||||
|
||||
// Parse command line
|
||||
QString fileToOpen;
|
||||
QStringList sqlToExecute;
|
||||
bool quitApplication = false;
|
||||
for(int i=1;i<a.arguments().size();i++)
|
||||
{
|
||||
// Check next command line argument
|
||||
if(a.arguments().at(i) == "-h" || a.arguments().at(i) == "--help")
|
||||
{
|
||||
// Help
|
||||
qWarning() << qPrintable(QObject::tr("Usage: %1 [options] [db]\n").arg(argv[0]));
|
||||
qWarning() << qPrintable(QObject::tr("Possible command line arguments:"));
|
||||
qWarning() << qPrintable(QObject::tr(" -h, --help\t\tShow command line options"));
|
||||
qWarning() << qPrintable(QObject::tr(" -s, --sql [file]\tExecute this SQL file after opening the DB"));
|
||||
qWarning() << qPrintable(QObject::tr(" -q, --quit\t\tExit application after running scripts"));
|
||||
qWarning() << qPrintable(QObject::tr(" [file]\t\tOpen this SQLite database"));
|
||||
return 0;
|
||||
} else if(a.arguments().at(i) == "-s" || a.arguments().at(i) == "--sql") {
|
||||
// Run SQL file: If file exists add it to list of scripts to execute
|
||||
if(++i >= a.arguments().size())
|
||||
qWarning() << qPrintable(QObject::tr("The -s/--sql option requires an argument"));
|
||||
else if(!QFile::exists(a.arguments().at(i)))
|
||||
qWarning() << qPrintable(QObject::tr("The file %1 does not exist").arg(a.arguments().at(i)));
|
||||
else
|
||||
sqlToExecute.append(a.arguments().at(i));
|
||||
} else if(a.arguments().at(i) == "-q" || a.arguments().at(i) == "--quit") {
|
||||
quitApplication = true;
|
||||
} else {
|
||||
// Other: Check if it's a valid file name
|
||||
if(QFile::exists(a.arguments().at(i)))
|
||||
fileToOpen = a.arguments().at(i);
|
||||
else
|
||||
qWarning() << qPrintable(QObject::tr("Invalid option/non-existant file: %1").arg(a.arguments().at(i)));
|
||||
}
|
||||
}
|
||||
|
||||
MainWindow w;
|
||||
#if defined(Q_WS_MAC)
|
||||
// AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
|
||||
// NewAEEventHandlerUPP(odocHandler),reinterpret_cast<long>(&w),false);
|
||||
#endif // Q_WS_MAC
|
||||
|
||||
// Show main window
|
||||
w.show();
|
||||
if (argc>1) {
|
||||
//first and only argument we accept is the name of the database to open
|
||||
w.fileOpen(QString(argv[1]));
|
||||
a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
|
||||
|
||||
// Open database if one was specified
|
||||
if(fileToOpen.size())
|
||||
{
|
||||
if(w.fileOpen(fileToOpen))
|
||||
{
|
||||
// If database could be opened run the SQL scripts
|
||||
foreach(const QString& f, sqlToExecute)
|
||||
{
|
||||
QFile file(f);
|
||||
if(file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
w.getDb()->executeMultiSQL(file.readAll(), false, true);
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
w.browseRefresh();
|
||||
}
|
||||
}
|
||||
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
|
||||
|
||||
// Quit application now if user doesn't want to see the UI
|
||||
if(quitApplication)
|
||||
return 0;
|
||||
|
||||
// Run application
|
||||
return a.exec();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user