]> Shamusworld >> Repos - schematic/blobdiff - src/mainwin.cpp
Minor tweaks to ODBC connection string to work better on win32.
[schematic] / src / mainwin.cpp
index fe7931f6e6b7964cda186a3e8d17a187fa23c243..55ba367f8d31e36ef642fb9e6fcd8b4bbdc76f9d 100644 (file)
@@ -106,40 +106,42 @@ MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
 
        // Finally, set up database connection
 
-       db = QSqlDatabase::addDatabase("QMYSQL");
+       db = QSqlDatabase::addDatabase("QODBC");
        bool ok = false;
 
        // Prime the SQL Settings dialog (in case we need it)
 
        SQLSettingsDialog sqlSettings;
-       sqlSettings.edit1->setText(dbHostName);
-       sqlSettings.edit2->setText(dbName);
-       sqlSettings.edit3->setText(dbUserName);
-       sqlSettings.edit4->setText(dbPassword);
+       sqlSettings.edit1->setText(dbDriver);
+       sqlSettings.edit2->setText(dbHostName);
+       sqlSettings.edit3->setText(dbName);
+       sqlSettings.edit4->setText(dbUserName);
+       sqlSettings.edit5->setText(dbPassword);
 
        do
        {
-               // Set up the DB connection with saved settings
-               db.setHostName(dbHostName);
-               db.setDatabaseName(dbName);
-               db.setUserName(dbUserName);
-               db.setPassword(dbPassword);
+               // Set up the ODBC DB connection with saved settings
+               // NB: For this to work properly on 64-bit Windows, you need to use the 32-bit driver!
+               QString odbc = QString("DRIVER={%1};DATABASE=%2;SERVER=%3;UID=%4;PASSWORD=%5")
+                       .arg(dbDriver).arg(dbName).arg(dbHostName).arg(dbUserName).arg(dbPassword);
+               db.setDatabaseName(odbc);
                ok = db.open();
 
-//printf("Error: %s\n", db.lastError().databaseText().toAscii().data());
-//printf("Error: %s\n", db.lastError().driverText().toAscii().data());
-
                // If unsuccessful, run the SQL settings/test dialog
                if (!ok)
                {
+                       sqlSettings.error1->setText(db.lastError().databaseText());
+                       sqlSettings.error2->setText(db.lastError().driverText());
+
                        if (sqlSettings.exec())
                        {
                                // User thinks this will work (hit OK button), so prime the variables
                                // for the next attempt
-                               dbHostName = sqlSettings.edit1->text();
-                               dbName = sqlSettings.edit2->text();
-                               dbUserName = sqlSettings.edit3->text();
-                               dbPassword = sqlSettings.edit4->text();
+                               dbDriver = sqlSettings.edit1->text();
+                               dbHostName = sqlSettings.edit2->text();
+                               dbName = sqlSettings.edit3->text();
+                               dbUserName = sqlSettings.edit4->text();
+                               dbPassword = sqlSettings.edit5->text();
                        }
                        else
                                return;                                         // User cancelled the dialog, so quit
@@ -147,36 +149,52 @@ MainWindow::MainWindow(): aboutWin(new AboutWindow(this)),
        }
        while (!ok);
 
-       // Do Login dialog
-       LoginDialog loginDlg;
-       bool done = false;
+       // Check to see how many users are in the system; if less than 2, we don't do a login
+       QSqlQuery query;
+       query.prepare("SELECT COUNT(*) FROM User");
+       query.exec();
+       query.next();
 
-       do
+       if (query.value(0).toInt() > 1)
        {
-               bool accept = loginDlg.exec();
+               // Do Login dialog
+               LoginDialog loginDlg;
+               bool done = false;
 
-               // Check to see if user cancelled out
-               if (!accept)
-                       done = true;
-               else
+               do
                {
-                       // Search DB for this username/login pair
-                       QSqlQuery query("SELECT UID, name, login FROM User WHERE Login=? AND Password=?");
-                       query.addBindValue(loginDlg.edit1->text());
-                       query.addBindValue(loginDlg.edit2->text());
-                       query.exec();
+                       bool accept = loginDlg.exec();
 
-                       while (query.next())
-                       {
-                               // We have a winner!
-                               loggedInUID = query.value(0).toInt();
-                               fullName = query.value(1).toString();
-                               login = query.value(2).toString();
+                       // Check to see if user cancelled out
+                       if (!accept)
                                done = true;
+                       else
+                       {
+                               // Search DB for this username/login pair
+                               QSqlQuery query;
+                               query.prepare("SELECT UID, name, login FROM User WHERE Login=? AND Password=?");
+                               query.addBindValue(loginDlg.edit1->text());
+                               query.addBindValue(loginDlg.edit2->text());
+                               query.exec();
+
+                               while (query.next())
+                               {
+                                       // We have a winner!
+                                       loggedInUID = query.value(0).toInt();
+                                       fullName = query.value(1).toString();
+                                       login = query.value(2).toString();
+                                       done = true;
+                               }
                        }
                }
+               while (!done);
+       }
+       else
+       {
+               fullName = "Administrator";
+               login = "admin";
+               loggedInUID = 1;
        }
-       while (!done);
 
        QString s = QString("User: %1 (%2)").arg(fullName).arg(login);
        scmWidget->username->setText(s);
@@ -230,7 +248,8 @@ void MainWindow::HandleNewVendorDialog(void)
 
        // Presumably, the user has given us good data, so we try to populate the
        // database with this new vendor data.
-       QSqlQuery query1("INSERT INTO  VALUES (?, ?, ?)");
+       QSqlQuery query;
+       query.prepare("INSERT INTO  VALUES (?, ?, ?)");
 
        
 }
@@ -238,7 +257,8 @@ void MainWindow::HandleNewVendorDialog(void)
 
 void MainWindow::FillVendorLevelCombo(QComboBox * combo)
 {
-       QSqlQuery query("SELECT VLID, Description FROM VendorLevel");
+       QSqlQuery query;
+       query.prepare("SELECT VLID, Description FROM VendorLevel");
        query.exec();
 
        while (query.next())
@@ -253,7 +273,8 @@ void MainWindow::FillVendorLevelCombo(QComboBox * combo)
 
 void MainWindow::FillContactTypeCombo(QComboBox * combo)
 {
-       QSqlQuery query("SELECT CTID, Description FROM ContactType");
+       QSqlQuery query;
+       query.prepare("SELECT CTID, Description FROM ContactType");
        query.exec();
 
        while (query.next())
@@ -271,7 +292,8 @@ void MainWindow::FillVendorClassList(QListWidget * list)
        std::vector<VendorType> groupList;
 
        // Pull in definitions from DB for Vendor Classes/Groups
-       QSqlQuery query1("SELECT vgid, description FROM VendorGroup ORDER BY seqNo");
+       QSqlQuery query1;
+       query1.prepare("SELECT vgid, description FROM VendorGroup ORDER BY seqNo");
        query1.exec();
 
        while (query1.next())
@@ -282,18 +304,19 @@ void MainWindow::FillVendorClassList(QListWidget * list)
                groupList.push_back(v);
        }
 
-       QSqlQuery query2("SELECT vtid, vgid, description FROM VendorType ORDER BY seqNo");
-       query2.exec();
+//     QSqlQuery query2;
+       query1.prepare("SELECT vtid, vgid, description FROM VendorType ORDER BY seqNo");
+       query1.exec();
 
        int previousID = -1, groupListIndex = 0;
        QListWidgetItem * item;
 
-       while (query2.next())
+       while (query1.next())
        {
 //             VendorType v;
-               int vtid            = query2.value(0).toInt();
-               int vgid            = query2.value(1).toInt();
-               QString description = query2.value(2).toString();
+               int vtid            = query1.value(0).toInt();
+               int vgid            = query1.value(1).toInt();
+               QString description = query1.value(2).toString();
 
                // Check to see if we need to insert new header yet.
                // If we're not still in same group, push the next group header into the list
@@ -327,6 +350,7 @@ void MainWindow::ReadSettings(void)
 //     ((TTEdit *)qApp)->charWnd->resize(size);
 //     ((TTEdit *)qApp)->charWnd->move(pos);
 
+       dbDriver = settings.value("dbDriver", "myodbc-5.1").toString();
        dbHostName = settings.value("dbHostName", "localhost").toString();
        dbName = settings.value("dbName", "schematic").toString();
        dbUserName = settings.value("dbUserName", "scm_user").toString();
@@ -342,6 +366,7 @@ void MainWindow::WriteSettings(void)
 //     settings.setValue("charWndPos", ((TTEdit *)qApp)->charWnd->pos());
 //     settings.setValue("charWndSize", ((TTEdit *)qApp)->charWnd->size());
 
+       settings.setValue("dbDriver", dbDriver);
        settings.setValue("dbHostName", dbHostName);
        settings.setValue("dbName", dbName);
        settings.setValue("dbUserName", dbUserName);