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

Adapting lansuite to abide STRICT_TRANS_TABLES #458

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
20 changes: 19 additions & 1 deletion inc/Classes/MasterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ public function SendForm($BaseURL, $table, $idname = '', $id = 0)
$postFieldValue = $_POST[$val] ?? '';
if (($SQLFieldTypes[$val] == 'datetime' or $SQLFieldTypes[$val] == 'date') and $_POST[$val] == 'NOW()') {
$db_query .= "$val = NOW(), ";
} elseif ($SQLFieldTypes[$val] == 'tinyint(1)') {
} elseif ($this->is_field_int($SQLFieldTypes[$val])) {
$intValue = $_POST[$val] ?? 0;
$db_query .= $val .' = '. (int) $intValue .', ';
} elseif ($SQLFieldTypes[$val] == 'varbinary(16)' and $val == 'ip') {
Expand All @@ -1184,6 +1184,10 @@ public function SendForm($BaseURL, $table, $idname = '', $id = 0)
$db_query .= "$val = $val + 1, ";
} elseif ($postFieldValue == '--' and str_contains($SQLFieldTypes[$val], 'int')) {
$db_query .= "$val = $val - 1, ";
} elseif ($this->is_field_enum($SQLFieldTypes[$val])) {
if ($_POST[$val] != '') {
$db_query .= "$val = '{$_POST[$val]}', ";
} // otherwise ignore value; default has to be defined
} else {
$db_query .= "$val = '{$postFieldValue}', ";
}
Expand Down Expand Up @@ -1266,4 +1270,18 @@ private function get_fieldlength($string)

return 0;
}

private function is_field_enum($field_type)
{
return str_starts_with($field_type, 'enum');
}

private function is_field_int($field_type)
{
return str_starts_with($field_type, 'tinyint')
|| str_starts_with($field_type, 'smallint')
|| str_starts_with($field_type, 'mediumint')
|| str_starts_with($field_type, 'int')
|| str_starts_with($field_type, 'bigint');
}
}
10 changes: 6 additions & 4 deletions modules/install/Classes/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,12 @@ public function ImportXML($rewrite = null)
$default = 'default CURRENT_TIMESTAMP';
$default_xml = 'CURRENT_TIMESTAMP';
$extra = 'on update CURRENT_TIMESTAMP';
} elseif ($type == 'datetime' or $type == 'date' or $type == 'time' or $type == 'blob') {
} elseif ($type == 'datetime' or $type == 'date' or $type == 'time') {
$default = '';
} elseif ($type == 'text' or $type == 'tinytext' or $type == 'mediumtext' or $type == 'longtext') {
// Text fields can't have a default in MySQL
if ($default_xml != '') {
$default = "default '$default_xml'";
}
} elseif ($type == 'text' or $type == 'tinytext' or $type == 'mediumtext' or $type == 'longtext' or $type == 'blob') {
$default = '';
} else {
// E.g. fields with type varchar will land here
Expand Down Expand Up @@ -222,7 +224,7 @@ public function ImportXML($rewrite = null)
// Handle structure changes in general
} elseif ($db_field["Type"] != $type
or (!($db_field["Null"] == $null or ($db_field["Null"] == 'YES' and $null == 'NULL')))
or ($db_field["Default"] != $default_xml and !($db_field["Default"] == 0 and $default_xml == '') and !($db_field["Default"] == '' and $default_xml == 0))
or ($db_field["Default"] != $default_xml and !($db_field["Default"] == 0 and $default_xml == '') and !($db_field["Default"] == '' and $default_xml === 0))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this ever work without casting/double checks?

$default_xml is set by

$default_xml = $this->xml->getFirstTagContent("default", $field);

getFirstTagContent will always return a string, even is 0 is in it.

Maybe it is better to run something like $default_xml === '0'?

See

public function getFirstTagContent($tag, $input, $save = false)
{
$start = strpos($input, '<' . $tag . '>') + strlen($tag) + 2;
// If the tag has attributes, remove them, for the end tag won't contain them
if (strpos($tag, ' ') > 0) {
$tag = substr($tag, 0, strpos($tag, ' '));
}
$end = strpos($input, '</' . $tag . '>') - $start;
if ($end <= 0) {
return '';
}
if ($save) {
return trim(
str_replace(
'--lt--',
'<',
str_replace('--gt--', '>', substr($input, $start, $end))
)
);
}
return trim(substr($input, $start, $end));
}

or $db_field["Extra"] != $extra) {
$db->qry("ALTER TABLE %prefix%%plain% CHANGE %plain% %plain% %plain% %plain% %plain% %plain%", $table_name, $name, $name, $type, $null, $default, $extra);
}
Expand Down
8 changes: 4 additions & 4 deletions modules/install/mod_settings/db.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
<type>datetime</type>
<null></null>
<key></key>
<default>0</default>
<default></default>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate a bit why removing the default is necessary here? (same question applies to the other datetime fields)

<extra></extra>
</field>
<field>
Expand Down Expand Up @@ -451,7 +451,7 @@
<type>datetime</type>
<null></null>
<key></key>
<default>0</default>
<default></default>
<extra></extra>
</field>
<field>
Expand Down Expand Up @@ -1159,7 +1159,7 @@
</field>
<field>
<name>module</name>
<type>char(20)</type>
<type>char(255)</type>
<null></null>
<key>IND</key>
<default></default>
Expand Down Expand Up @@ -1483,7 +1483,7 @@
<type>datetime</type>
<null></null>
<key></key>
<default>0</default>
<default></default>
<extra></extra>
</field>
<field>
Expand Down
8 changes: 4 additions & 4 deletions modules/mail/mod_settings/db.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,31 +152,31 @@
<type>datetime</type>
<null></null>
<key></key>
<default>0</default>
<default></default>
<extra></extra>
</field>
<field>
<name>rx_date</name>
<type>datetime</type>
<null></null>
<key></key>
<default>0</default>
<default></default>
<extra></extra>
</field>
<field>
<name>tx_deleted</name>
<type>tinyint(1)</type>
<null></null>
<key>IND</key>
<default>0</default>
<default></default>
<extra></extra>
</field>
<field>
<name>rx_deleted</name>
<type>tinyint(1)</type>
<null></null>
<key>IND</key>
<default>0</default>
<default></default>
<extra></extra>
</field>
</structure>
Expand Down
2 changes: 1 addition & 1 deletion modules/party/mod_settings/db.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<type>datetime</type>
<null></null>
<key></key>
<default></default>
<default>0000-00-00 00:00:00</default>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What makes this datetime field different from the others when it comes to the default value?

<extra></extra>
</field>
<field>
Expand Down
2 changes: 1 addition & 1 deletion modules/partylist/mod_settings/db.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<type>datetime</type>
<null></null>
<key></key>
<default>0</default>
<default></default>
<extra></extra>
</field>
<field>
Expand Down
2 changes: 1 addition & 1 deletion modules/poll/mod_settings/db.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<type>datetime</type>
<null></null>
<key></key>
<default>0</default>
<default></default>
<extra></extra>
</field>
<field>
Expand Down
4 changes: 2 additions & 2 deletions modules/tournament2/mod_settings/db.xml
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@
<type>datetime</type>
<null></null>
<key></key>
<default></default>
<default>0000-00-00 00:00:00</default>
<extra></extra>
</field>
<field>
Expand Down Expand Up @@ -333,7 +333,7 @@
<field>
<name>rules</name>
<type>text</type>
<null></null>
<null>YES</null>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate why this is needed?

<key></key>
<default></default>
<extra></extra>
Expand Down