Commit d41f0f74 authored by Marc Egger's avatar Marc Egger

Refs #10120 extract sql queries which are used multiple times

parent cf68a1ea
Pipeline #3648 failed with stages
in 7 minutes and 41 seconds
......@@ -22,13 +22,10 @@ use IMATHUZH\Qfq\Core\Store\Store;
// TODO: Carsten Fragen: PHP version von gitlab runner hochstellen, damit ?string und void type syntax fuer funktionen erlaubt ist? verhindert einige fehler
// TODO: Carsten Fragen: Regex fuer erlaubte file names und form names im editor (+ erlauben?): ^([-\.\w]+)$ VS [a-zA-Z0-9._+-]+
// TODO: Carsten Fragen: Muss die neue Column fileStats von Form tabelle in DatabaseUpdateData.php? Form Editor wird ja sowieso neu eingespielt bei einem update.
// TODO: Carsten Fragen: QFQ as file
// TODO: Carsten Fragen: add log messages somewhare?
// TODO: BEFOORE PRUDUCTION
// TODO: extract all sql queries and make them reusable
// TODO: write tests (file test https://medium.com/weebly-engineering/phpunit-mocking-the-file-system-using-vfsstream-5d7d79b1eb2a)
// TODO: maybe: update button in form list which removes forms from DB if their file was deleted
// TODO: Carsten Fragen: add log messages somewhare?
// TODO: Tests:
// new form => form file created
// form field change editor => changed in form file
......@@ -124,12 +121,9 @@ class FormAsFile
self::deleteFormDB($formName, $database);
throw $fileReadException;
}
$F_ID = F_ID; // can't use constants in strings directly
$F_FILE_STATS = F_FILE_STATS; // can't use constants in strings directly
$F_NAME = F_NAME; // can't use constants in strings directly
$TABLE_NAME_FORM = TABLE_NAME_FORM; // can't use constants in strings directly
$formFromDb = $database->sql("SELECT `$F_ID`, `$F_FILE_STATS` FROM `$TABLE_NAME_FORM` AS f WHERE `f`.`$F_NAME` LIKE ? AND `f`.`deleted`='no'", ROW_EXPECT_0_1,
[$formName], "Multiple forms with the same name: '$formName'");
list($sql, $parameterArray) = SqlQuery::selectFormByName($formName, [F_ID, F_FILE_STATS]);
$formFromDb = $database->sql($sql, ROW_EXPECT_0_1,
$parameterArray, "Multiple forms with the same name: '$formName'");
if (array_key_exists(F_FILE_STATS, $formFromDb) && $fileStatsNew === $formFromDb[F_FILE_STATS]) {
return false;
}
......@@ -144,7 +138,7 @@ class FormAsFile
// Delete old form with that name from DB if it exists.
if (array_key_exists(F_ID, $formFromDb)) {
$formId = $formFromDb[$F_ID];
$formId = $formFromDb[F_ID];
self::deleteFormDBWithId($formId, $database);
}
......@@ -208,11 +202,9 @@ class FormAsFile
public static function exportForm(string $formName, Database $database) // : void
{
// Get form from DB
$FORM = TABLE_NAME_FORM; // can't use constants in strings directly
$NAME = F_NAME; // can't use constants in strings directly
$DELETED = F_DELETED; // can't use constants in strings directly
$form = $database->sql("SELECT * FROM `$FORM` AS f WHERE `f`.`$NAME` LIKE ? AND `f`.`$DELETED`='no'", ROW_EXPECT_1,
[$formName], 'Form "' . $formName . '" not found or multiple forms with the same name.'); // array(column name => value)
list($sql, $parameterArray) = SqlQuery::selectFormByName($formName);
$form = $database->sql($sql, ROW_EXPECT_1,
$parameterArray, 'Form "' . $formName . '" not found or multiple forms with the same name.'); // array(column name => value)
// Remove columns: id, name, fileStats
$formId = $form[F_ID];
......@@ -221,11 +213,8 @@ class FormAsFile
unset($form[F_FILE_STATS]);
// Get formElements from DB
$FORM_ELEMENT = TABLE_NAME_FORM_ELEMENT; // can't use constants in strings directly
$FORM_ID = FE_FORM_ID; // can't use constants in strings directly
$ORD = FE_ORD; // can't use constants in strings directly
$ID = FE_ID; // can't use constants in strings directly
$formElements = $database->sql("SELECT * FROM `$FORM_ELEMENT` AS `fe` WHERE `fe`.`$FORM_ID` = ? ORDER BY `fe`.`$ORD`, `fe`.`$ID`", ROW_REGULAR, [$formId]); // array(array(column name => value))
list($sql, $parameterArray) = SqlQuery::selectFormElementById($formId);
$formElements = $database->sql($sql, ROW_REGULAR, $parameterArray); // array(array(column name => value))
// Translate container references (id to name) and remove all id columns
$containerNames = array_reduce($formElements, function ($result, $formElement) {
......@@ -388,11 +377,9 @@ class FormAsFile
}
switch ($recordTable) {
case TABLE_NAME_FORM:
$F_NAME = F_NAME;
$TABLE_NAME_FORM = TABLE_NAME_FORM;
$F_ID = F_ID;
$formFromDb = $database->sql("SELECT `$F_NAME` FROM `$TABLE_NAME_FORM` AS f WHERE `f`.`$F_ID`=? AND `f`.`deleted`='no'", ROW_EXPECT_1,
[$recordId], "Form with id '$recordId' not found. " . self::errorHintFormImport());
list($sql, $parameterArray) = SqlQuery::selectFormById($recordId);
$formFromDb = $database->sql($sql, ROW_EXPECT_1,
$parameterArray, "Form with id '$recordId' not found. " . self::errorHintFormImport());
return $formFromDb[F_NAME];
case TABLE_NAME_FORM_ELEMENT:
$F_NAME = F_NAME;
......@@ -401,6 +388,8 @@ class FormAsFile
$F_ID = F_ID;
$FE_FORM_ID = FE_FORM_ID;
$FE_ID = FE_ID;
// Select form name by formElement id
$formFromDb = $database->sql("SELECT `f`.`$F_NAME` FROM `$TABLE_NAME_FORM` AS f INNER JOIN `$TABLE_NAME_FORM_ELEMENT` AS fe ON f.`$F_ID`=fe.`$FE_FORM_ID` WHERE `fe`.`$FE_ID`=?", ROW_EXPECT_1,
[$recordId], "Form element with id '$recordId' not found. " . self::errorHintFormImport());
return $formFromDb[F_NAME];
......@@ -529,13 +518,11 @@ class FormAsFile
*/
private static function deleteFormDB(string $formName, Database $database) // : void
{
$F_ID = F_ID; // can't use constants in strings directly
$F_NAME = F_NAME; // can't use constants in strings directly
$TABLE_NAME_FORM = TABLE_NAME_FORM; // can't use constants in strings directly
$formFromDb = $database->sql("SELECT `$F_ID` FROM `$TABLE_NAME_FORM` AS f WHERE `f`.`$F_NAME` LIKE ? AND `f`.`deleted`='no'", ROW_EXPECT_0_1,
[$formName], "Multiple forms with the same name: '$formName'");
list($sql, $parameterArray) = SqlQuery::selectFormByName($formName, [F_ID]);
$formFromDb = $database->sql($sql, ROW_EXPECT_0_1,
$parameterArray, "Multiple forms with the same name: '$formName'");
if (array_key_exists(F_ID, $formFromDb)) {
self::deleteFormDBWithId($formFromDb[$F_ID], $database);
self::deleteFormDBWithId($formFromDb[F_ID], $database);
}
}
......
......@@ -57,4 +57,59 @@ class SqlQuery {
$parameterArray = array_values($values);
return array($sql, $parameterArray);
}
/**
* @param string $formName
* @param array|null $columnsToSelect
* @return array
*/
public static function selectFormByName(string $formName, array $columnsToSelect = null): array
{
$NAME = F_NAME; // can't use constants in strings directly
$FORM = TABLE_NAME_FORM;
$DELETED = F_DELETED;
$sql = "SELECT " . self::columnListSelect($columnsToSelect) . " FROM `$FORM` AS f WHERE `f`.`$NAME` LIKE ? AND `f`.`$DELETED`='no'";
return array($sql, [$formName]);
}
/**
* @param int $fId
* @param array|null $columnsToSelect
* @return array
*/
public static function selectFormById(int $fId, array $columnsToSelect = null): array
{
$FORM = TABLE_NAME_FORM; // can't use constants in strings directly
$ID = F_ID;
$DELETED = F_DELETED;
$sql = "SELECT " . self::columnListSelect($columnsToSelect) . " FROM `$FORM` AS f WHERE `f`.`$ID`=? AND `f`.`$DELETED`='no'";
return array($sql, [$fId]);
}
/**
* @param int $feId
* @param array|null $columnsToSelect
* @return array
*/
public static function selectFormElementById(int $feId, array $columnsToSelect = null): array
{
$FORM_ELEMENT = TABLE_NAME_FORM_ELEMENT; // can't use constants in strings directly
$FORM_ID = FE_FORM_ID;
$ORD = FE_ORD;
$ID = FE_ID;
$sql = "SELECT " . self::columnListSelect($columnsToSelect) . " FROM `$FORM_ELEMENT` AS `fe` WHERE `fe`.`$FORM_ID` = ? ORDER BY `fe`.`$ORD`, `fe`.`$ID`";
return array($sql, [$feId]);
}
/**
* @param array|null $columns
* @return string
*/
private static function columnListSelect(array $columns = null) {
if ($columns === null) {
return ' * ';
} else {
return '`' . implode('`, `', $columns) . '`';
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment