I have implemented an interface through which one can select, update and remove items of the Account
class in a MySQL database. For the three functions I'm preparing statements.
The database, column and table names are all configured in a settings file.
Because of this I have to call the interface which gets the configuration from the settings file while preparing my query. This gives me a huge code block of code with little string concatenations in between:
updateStmt(connection->prepareStatement(
"INSERT INTO " + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_TABLE_NAME)
+ "(" + GETSET(PTH_STORAGE_SYSTEM_IDCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_OWNERCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_NAMECOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_DESCRIPTIONCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_PASSWORDCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_BALANCECOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_LOCKEDCOL) + ","
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_PASSWORDEDTRANSACTIONSCOL)
+ "," + GETSET(PTH_STORAGE_SYSTEM_CHANGEDCOL) + ","
+ GETSET(PTH_STORAGE_SYSTEM_CREATEDCOL) + ","
+ GETSET(PTH_STORAGE_SYSTEM_DELETEDCOL)
+ ") VALUES (?,?,?,?,?,?,?,?,NOW(),NOW(),NULL) ON DUPLICATE KEY UPDATE"
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_OWNERCOL) + "=VALUES("
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_OWNERCOL) + "), "
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_NAMECOL) + "=VALUES("
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_NAMECOL) + "), "
+ GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_DESCRIPTIONCOL)
+ "=VALUES(" + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_DESCRIPTIONCOL)
+ "), " + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_PASSWORDCOL)
+ "=VALUES(" + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_PASSWORDCOL)
+ "), " + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_BALANCECOL)
+ "=VALUES(" + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_BALANCECOL)
+ "), " + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_LOCKEDCOL)
+ "=VALUES(" + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_LOCKEDCOL)
+ "), " + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_PASSWORDEDTRANSACTIONSCOL)
+ "=VALUES(" + GETSET(PTH_STORAGE_ACCOUNT
PTH_PART_STORAGE_ACCOUNT_PASSWORDEDTRANSACTIONSCOL)
+ "), " + GETSET(PTH_STORAGE_SYSTEM_CHANGEDCOL) + "=NOW()"
+ GETSET(PTH_STORAGE_SYSTEM_DELETEDCOL) + "=NULL"
))
GETSET(x)
is a define for settingsCtrl.readSetting(x)
to shorten the call. PTH_STORAGE_...
and PTH_PART_...
are defines for the XPaths to the configuration value.
PTH_STORAGE_ACCOUNT PTH_PART_STORAGE_TABLE_NAME
is an XPath to the account section and the configuration value for the table name.
I think it is obvious that building a query like this seems a bit wrong, but I'm not sure if there is a more concise way to prepare a query with so much parameters.
Is there?
=NOW()
. Do you have something in mind? \$\endgroup\$