Parameters for sql execute

This commit is contained in:
Krad
2021-11-12 14:32:22 +08:00
parent 131fbb2e24
commit 1ffe4224ac
9 changed files with 191 additions and 28 deletions

View File

@@ -3,6 +3,7 @@
//
#include "AccountFormDialog.h"
#include "ChangePasswordFormDialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QToolButton>
@@ -13,11 +14,14 @@
AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFormBaseDialog(parent, f) {
QVBoxLayout* layout = new QVBoxLayout(formWidget);
layout->setSpacing(10);
// add title
QLabel* lbl_title = new QLabel(this);
lbl_title->setAlignment(Qt::AlignCenter);
lbl_title->setText(tr("Account"));
lbl_title->setObjectName("title");
layout->addWidget(lbl_title);
//add usercode
QLabel* lbl_UserCode = new QLabel(this);
lbl_UserCode->setText(tr("User ID"));
QLineEdit* le_UserCode = new QLineEdit(this);
@@ -28,6 +32,7 @@ AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFo
lbl_endline1->setObjectName("endline");
layout->addWidget(lbl_endline1);
//add username
QLabel* lbl_UserName = new QLabel(this);
lbl_UserName->setText(tr("Name"));
QLineEdit* le_UserName = new QLineEdit(this);
@@ -37,6 +42,7 @@ AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFo
lbl_endline2->setObjectName("endline");
layout->addWidget(lbl_endline2);
//add password
QLabel* lbl_Pwd = new QLabel(this);
lbl_Pwd->setText(tr("Password"));
QPushButton* btn_Pwd = new QPushButton(this);
@@ -47,16 +53,18 @@ AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFo
lbl_endline3->setObjectName("endline");
layout->addWidget(lbl_endline3);
//add logout
QLabel* lbl_Logout = new QLabel(this);
lbl_Logout->setText(tr("Logout"));
QPushButton* btnLogout= new QPushButton(this);
btnLogout->setText(tr("Logout"));
QPushButton* btn_Logout= new QPushButton(this);
btn_Logout->setText(tr("Logout"));
layout->addWidget(lbl_Logout);
layout->addWidget(btnLogout);
layout->addWidget(btn_Logout);
QLabel* lbl_endline0 = new QLabel(this);
lbl_endline0->setObjectName("endline");
layout->addWidget(lbl_endline0);
// load current user data
if (User::Current())
{
le_UserCode->setText(User::Current()->getUserCode());
@@ -68,6 +76,11 @@ AccountFormDialog::AccountFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFo
t->m_NewUserName = text;
t->userNameChanged = true;
});
connect(btn_Pwd, &QPushButton::clicked,[t=this](){
ChangePasswordFormDialog dia(t->parentWidget());
dia.setWindowModality(Qt::WindowModal);
dia.exec();
});
}
AccountFormDialog::~AccountFormDialog() {

View File

@@ -0,0 +1,91 @@
//
// Created by Krad on 2021/11/11.
//
#include <QVBoxLayout>
#include <QLabel>
#include <QtWidgets/QLineEdit>
#include <src/models/User.h>
#include "ChangePasswordFormDialog.h"
ChangePasswordFormDialog::ChangePasswordFormDialog(QWidget *parent, Qt::WindowFlags f) : GUIFormBaseDialog(parent, f) {
QVBoxLayout* layout = new QVBoxLayout(formWidget);
layout->setSpacing(10);
// add title
QLabel* lbl_title = new QLabel(this);
lbl_title->setAlignment(Qt::AlignCenter);
lbl_title->setText(tr("Change Password"));
lbl_title->setObjectName("title");
layout->addWidget(lbl_title);
//add old password
QLabel* lbl_Old = new QLabel(this);
lbl_Old->setText(tr("Current Password"));
pwd = new QLineEdit(this);
pwd->setEchoMode(QLineEdit::Password);
layout->addWidget(lbl_Old);
layout->addWidget(pwd);
QLabel* lbl_endline1 = new QLabel(this);
lbl_endline1->setObjectName("endline");
layout->addWidget(lbl_endline1);
//add new password
QLabel* lbl_New = new QLabel(this);
lbl_New->setText(tr("New Password"));
new_pwd = new QLineEdit(this);
new_pwd->setEchoMode(QLineEdit::Password);
layout->addWidget(lbl_New);
layout->addWidget(new_pwd);
QLabel* lbl_endline2 = new QLabel(this);
lbl_endline2->setObjectName("endline");
layout->addWidget(lbl_endline2);
//add confirm password
QLabel* lbl_Confirm = new QLabel(this);
lbl_Confirm->setText(tr("Confirm Password"));
confirm_pwd = new QLineEdit(this);
confirm_pwd->setEchoMode(QLineEdit::Password);
layout->addWidget(lbl_Confirm);
layout->addWidget(confirm_pwd);
QLabel* lbl_endline3 = new QLabel(this);
lbl_endline3->setObjectName("endline");
layout->addWidget(lbl_endline3);
lbl_error = new QLabel(this);
lbl_error->setObjectName("warn");
layout->addWidget(lbl_error);
}
ChangePasswordFormDialog::~ChangePasswordFormDialog() {
}
bool ChangePasswordFormDialog::updateReferenceData() {
if (pwd->text().isEmpty())
{
lbl_error->setText(tr("Please enter your old password!"));
return false;
}
if (new_pwd->text().length()<6) {
lbl_error->setText(tr("New password should at least 6 characters!"));
return false;
}
QString encryptPwd = User::getEncryptedPassword(pwd->text());
if (encryptPwd!=User::Current()->getPassword())
{
lbl_error->setText(tr("Wrong password!"));
return false;
}
if (new_pwd->text() != confirm_pwd->text())
{
lbl_error->setText(tr("Your new password does not match!"));
return false;
}
User::Current()->setPassword(User::getEncryptedPassword(new_pwd->text()));
if (!User::Current()->submitChange()) {
lbl_error->setText(tr("Database update error!"));
User::Current()->restorePassword(encryptPwd);
return false;
}
return true;
}

View File

@@ -0,0 +1,28 @@
//
// Created by Krad on 2021/11/11.
//
#ifndef GUI_CHANGEPASSWORDFORMDIALOG_H
#define GUI_CHANGEPASSWORDFORMDIALOG_H
#include "GUIFormBaseDialog.h"
class QLineEdit;
class QLabel;
class ChangePasswordFormDialog:public GUIFormBaseDialog{
Q_OBJECT
public:
explicit ChangePasswordFormDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
~ChangePasswordFormDialog();
protected:
bool updateReferenceData() override;
private:
QLineEdit* pwd = nullptr;
QLineEdit* new_pwd = nullptr;
QLineEdit* confirm_pwd = nullptr;
QLabel* lbl_error = nullptr;
};
#endif //GUI_CHANGEPASSWORDFORMDIALOG_H

View File

@@ -38,6 +38,14 @@ void SQLHelper::Close() {
}
}
void prepareSQL(QSqlQuery& query,QMap<QString,QVariant>* params)
{
if (!params) return;
for (auto key : params->keys())
{
query.bindValue(key,params->value(key));
}
}
QSqlTableModel* SQLHelper::getTable(const QString &tableName) {
@@ -48,8 +56,9 @@ QSqlTableModel* SQLHelper::getTable(const QString &tableName) {
return (*cache)[tableName];
}
int SQLHelper::QueryCount(QString sql) {
int SQLHelper::QueryCount(QString sql, QMap<QString,QVariant>* params) {
QSqlQuery query(*defaultDatabase);
prepareSQL(query, params);
if (query.exec(sql))
{
query.last();
@@ -58,19 +67,22 @@ int SQLHelper::QueryCount(QString sql) {
return 0;
}
int SQLHelper::ExecuteNoQuery(QString sql) {
int SQLHelper::ExecuteNoQuery(QString sql, QMap<QString,QVariant>* params) {
QSqlQuery query(*defaultDatabase);
qDebug()<<sql;
if (query.exec(sql))
query.prepare(sql);
prepareSQL(query, params);
if (query.exec())
{
return query.numRowsAffected();
}
return 0;
}
void SQLHelper::Query(QString sql,QMap<QString,QVariant>& result) {
void SQLHelper::QueryFirst(QString sql, QMap<QString,QVariant>& result, QMap<QString,QVariant>* params) {
QSqlQuery query(*defaultDatabase);
if (query.exec(sql))
query.prepare(sql);
prepareSQL(query, params);
if (query.exec())
{
if(query.next())
{

View File

@@ -16,9 +16,9 @@ public:
static bool Open();
static bool Open(QSqlDatabase* base);
static void Close();
static void Query(QString sql,QMap<QString,QVariant>& result);
static int QueryCount(QString sql);
static int ExecuteNoQuery(QString sql);
static void QueryFirst(QString sql, QMap<QString,QVariant>& result, QMap<QString,QVariant>* params = nullptr);
static int QueryCount(QString sql, QMap<QString,QVariant>* params = nullptr);
static int ExecuteNoQuery(QString sql, QMap<QString,QVariant>* params = nullptr);
static QSqlTableModel* getTable(const QString & tableName);
private:
static QSqlDatabase* defaultDatabase;

View File

@@ -128,14 +128,6 @@ void LoginWindow::clearInputData()
//m_pUserCodeEdit->setFocus();
}
QString getEncryptedPassword(const QString& password)
{
QByteArray bytePwd = password.toLatin1();
QByteArray bytePwdMd5 = QCryptographicHash::hash(bytePwd, QCryptographicHash::Md5);
return bytePwdMd5.toHex();
}
void LoginWindow::doLogin()
{
@@ -148,7 +140,7 @@ void LoginWindow::doLogin()
QString encryptPwd = getEncryptedPassword(strPassWord);
QString encryptPwd = User::getEncryptedPassword(strPassWord);
QString sql = QString("select UserCode from Account where UserCode='%1' and Password='%2'")
.arg(strUserCode).arg(encryptPwd);
if(User::QueryUser(strUserCode,encryptPwd))

View File

@@ -49,7 +49,8 @@ MainWindow::MainWindow(QWidget* parent) :
"QWidget#commandWidget{min-height:123px;max-height:123px;border-top:1px solid #515151; border-bottom:1px solid #323232;}\n"
"QLabel#logo{min-width:30px;max-width:30px}\n"
"QLabel#company{min-width:150px;max-width:150px; }\n"
"QLabel#systemMsgBar{min-width:500px;}\n"
"QLabel#systemMsgBar{min-width:500px;}"
"QLabel#warn{color:#CCCC00;background:transparent;font-size:20px;}"
"QWidget QWidget#statusBarWidget{min-width:300px;}\n"
"QWidget QToolButton{border:none;border-radius:10%;font-size:26px; font-weight:Bold;padding:5px;}\n"
"QToolButton#btn_main{border:2px solid #0078d8;border-radius:10%;font-size:26px; font-weight:Bold;padding:5px;}\n"

View File

@@ -19,22 +19,37 @@ User::~User() {
bool User::submitChange() {
static QString updateSQL = "update Account %1 %2";
QString setString = "";
QMap<QString,QVariant> params;
#define USER_READONLY_PROPERTY(name)
#define USER_PROPERTY(name)\
USER_READONLY_PROPERTY(name)\
setString += QString((!setString.isEmpty()&&mf_##name)?", ":"") + QString(mf_##name?QString("set %1='%2'").arg(#name, m_##name):"");
if (mf_##name){\
setString += QString((!setString.isEmpty())?", ":"") + QString(QString("set %1=%2").arg(#name, ":" #name));\
params[":" #name] = m_##name;\
}
USER_PROPERTIES_MACRO()
#undef USER_PROPERTY
#undef USER_READONLY_PROPERTY
QString whereString = " where "+getIndexName()+" = '"+getIndexValue()+"'";
return 1 == SQLHelper::ExecuteNoQuery(updateSQL.arg(setString,whereString));
bool result = 1 == SQLHelper::ExecuteNoQuery(updateSQL.arg(setString,whereString),&params);
if (result)
{
#define USER_READONLY_PROPERTY(name)
#define USER_PROPERTY(name) USER_READONLY_PROPERTY(name) mf_##name = false;
USER_PROPERTIES_MACRO()
#undef USER_PROPERTY
#undef USER_READONLY_PROPERTY
}
return result;
}
bool User::QueryUser(QString userID, QString Pwd) {
QString sql = QString("select * from Account where UserCode='%1' and Password='%2'")
.arg(userID).arg(Pwd);
QString sql = QString("select * from Account where UserCode=:userID and Password=:pwd");
QMap<QString,QVariant> map;
SQLHelper::Query(sql, map);
QMap<QString,QVariant> params;
params[":userID"] = userID;
params[":pwd"] = Pwd;
SQLHelper::QueryFirst(sql, map, &params);
if(!map.isEmpty())
{
if (!currentUser) currentUser = new User;

View File

@@ -15,10 +15,18 @@ USER_PROPERTY(Comment)
#include <QObject>
#include <QtCore/QCryptographicHash>
class User:public QObject {
Q_OBJECT
public:
static bool QueryUser(QString userID, QString Pwd);
static QString getEncryptedPassword(const QString& password)
{
QByteArray bytePwd = password.toLatin1();
QByteArray bytePwdMd5 = QCryptographicHash::hash(bytePwd, QCryptographicHash::Md5);
return bytePwdMd5.toHex();
}
static User* Current(){
return currentUser;
}
@@ -46,7 +54,10 @@ public:
USER_PROPERTIES_MACRO()
#undef USER_PROPERTY
#undef USER_READONLY_PROPERTY
void restorePassword(const QString& original_pwd){
m_Password = original_pwd;
mf_Password = false;
}
bool submitChange();
private:
static User* currentUser;