Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a new filed #125

Open
qht1003077897 opened this issue Apr 30, 2024 · 7 comments
Open

add a new filed #125

qht1003077897 opened this issue Apr 30, 2024 · 7 comments

Comments

@qht1003077897
Copy link

I want to add new fields to my new version of the application, how do I set it up to make Qxorm compatible with the new fields?

@QxOrm
Copy link
Owner

QxOrm commented Apr 30, 2024

Hello,
Not sure to understand what is exactly your issue : you just have to register new fields in your qx::register_class() method.

@qht1003077897
Copy link
Author

sorry,
chinese:
比如我的软件现在是1.0版本,我现在需要升级到2.0版本,但是我需要在我的数据库表中增加一个新的字段,我的代码应该如何处理以兼容数据库的升级?

English:
Let's say my software is now version 1.0, I need to upgrade to version 2.0, but I need to add a new field to my database table, what should my code do to make it compatible with the database upgrade?

@qht1003077897
Copy link
Author

If I had simply added a new field to register_class like this:

class KeyModel
{
public:
    KeyModel() { }
    virtual ~KeyModel() { }

    QString name;
    QString oldfield;
    QString newfield;
};
template <> void register_class(QxClass<KeyModel>& t)
{
    t.id(&KeyModel::name, "name");
    t.data(&KeyModel::oldfield, "oldfield");
    t.data(&KeyModel::newfiled, "newfield");
}

When I execute 'QSqlError daoError = qx::dao::fetch_all(list); 'When I will get an error:

no such; column: KeyModel.newfiled

@qht1003077897
Copy link
Author

If I had simply added a new field to register_class like this:

class KeyModel
{
public:
    KeyModel() { }
    virtual ~KeyModel() { }

    QString name;
    QString oldfield;
    QString newfield;
};
template <> void register_class(QxClass<KeyModel>& t)
{
    t.id(&KeyModel::name, "name");
    t.data(&KeyModel::oldfield, "oldfield");
    t.data(&KeyModel::newfiled, "newfield");
}

When I execute 'QSqlError daoError = qx::dao::fetch_all(list); 'When I will get an error:

no such; column: KeyModel.newfiled

“oldfield” is a field in V1.0 and “newfield” is a field that needs to be added in V2.0

@QxOrm
Copy link
Owner

QxOrm commented May 6, 2024

Hello,

When you write :
t.data(&KeyModel::oldfield, "oldfield");

You are using the method qx::QxClass::data which provides other parameters :
IxDataMember * data(V U::* pData, const QString & sKey, long lVersion = 0, bool bSerialize = true, bool bDao = true);

For example, if you write :
t.data(&KeyModel::oldfield, "oldfield", 0, true, false);
Passing bDao=false means that this field will be not used to build SQL query and communicate with database.

And using the QxOrm introspection engine, you can change this property dynamically at runtime (so not in qx_register_class function).
For example : imagine you fetch your database version, if it is 2.0, then you can write :

qx::IxDataMember * pDataMember = qx::QxClassX::getDataMember("KeyModel", "oldfield");
pDataMember->setDao(false);

qx::IxDataMember * pDataMember = qx::QxClassX::getDataMember("KeyModel", "newfield");
pDataMember->setDao(true);

@qht1003077897
Copy link
Author

Sorry, I described the requirements a bit too complicated, you may have misunderstood my meaning.
My requirement is like issue #66 , how to upgrade the database and add new fields to the table?

My current approach is to add the following code snippet, as the new field was added in version 110. Therefore, I determined that when the APP version is less than 110, I will use Alter to add the field.

Although this method is feasible, I feel that it is not very elegant, and I think there should be a better way.

Looking forward to a reply~

void SqlHelper::upgradeTable(const QString& tableName,const QString& columnName)
{
    QString appVersion = qApp->property(APP_VERSION_STR).toString();
        if (appVersion.toInt() <= 110) {
            QSqlQuery query(db);
            QString execs = "ALTER TABLE " + tableName+ " ADD COLUMN " + columnName + " TEXT;";
            query.exec(execs);
        }
}

@QxOrm
Copy link
Owner

QxOrm commented May 7, 2024

Hello,
I think a better way is to use QxEntityEditor application or another tool dedicated to your SGBD.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants