ILIAS  trunk Revision v12.0_alpha-1329-g1094ddb0c33
ilDBPdoMySQLFieldDefinition Class Reference

Class ilDBPdoMySQLFieldDefinition. More...

+ Inheritance diagram for ilDBPdoMySQLFieldDefinition:
+ Collaboration diagram for ilDBPdoMySQLFieldDefinition:

Public Member Functions

 __construct (protected \ilDBInterface $db_instance)
 
 checkTableName (string $table_name)
 
 isReserved (string $table_name)
 
 getAllReserved ()
 
 getReservedMysql ()
 
 setReservedMysql (array $reserved_mysql)
 
 checkColumnName (string $column_name)
 
 checkIndexName (string $a_name)
 
 checkColumnDefinition (array $a_def)
 
 isAllowedAttribute (string $attribute, string $type)
 
 getAvailableTypes ()
 
 setAvailableTypes (array $available_types)
 
 getAllowedAttributes ()
 
 setAllowedAttributes (array $allowed_attributes)
 
 getMaxLength ()
 
 setMaxLength (array $max_length)
 
 getValidTypes ()
 
 getDeclaration (string $type, string $name, array $field)
 
 compareDefinition (array $current, array $previous)
 
 quote ($value, ?string $type=null, bool $quote=true, bool $escape_wildcards=false)
 
 writeLOBToFile ($lob, string $file)
 
 destroyLOB ($lob)
 
 matchPattern (array $pattern, $operator=null, $field=null)
 
 patternEscapeString ()
 
 mapNativeDatatype (array $field)
 
 mapPrepareDatatype (string $type)
 
 getTypeDeclaration (array $field)
 
 checkTableName (string $table_name)
 
 isReserved (string $table_name)
 
 getAllReserved ()
 
 getReservedMysql ()
 
 setReservedMysql (array $reserved_mysql)
 
 checkColumnName (string $column_name)
 
 checkIndexName (string $a_name)
 
 checkColumnDefinition (array $a_def)
 
 isAllowedAttribute (string $attribute, string $type)
 
 getAvailableTypes ()
 
 setAvailableTypes (array $available_types)
 
 getAllowedAttributes ()
 
 setAllowedAttributes (array $allowed_attributes)
 
 getMaxLength ()
 
 setMaxLength (array $max_length)
 
 getValidTypes ()
 
 getDeclaration (string $type, string $name, array $field)
 
 getTypeDeclaration (array $field)
 
 compareDefinition (array $current, array $previous)
 
 quote ($value, ?string $type=null, bool $quote=true, bool $escape_wildcards=false)
 
 writeLOBToFile ($lob, string $file)
 
 destroyLOB ($lob)
 
 matchPattern (array $pattern, $operator=null, $field=null)
 
 patternEscapeString ()
 
 mapNativeDatatype (array $field)
 
 mapPrepareDatatype (string $type)
 

Data Fields

array $lobs
 
const DEFAULT_DECIMAL_PLACES = 2
 
const DEFAULT_TEXT_LENGTH = 4000
 
const DEFINITION_COLUMN_NAME = "/^[a-z]+[_a-z0-9]*$/"
 
const DEFINITION_TABLE_NAME = "/^[a-z]+[_a-z0-9]*$/"
 
array $allowed_attributes_old
 
array $allowed_attributes
 
- Data Fields inherited from ILIAS\Database\FieldDefinition
const INDEX_FORMAT = '%s_idx'
 
const SEQUENCE_COLUMNS_NAME = 'sequence'
 
const SEQUENCE_FORMAT = '%s_seq'
 
const T_BLOB = 'blob'
 
const T_CLOB = 'clob'
 
const T_DATE = 'date'
 
const T_DATETIME = 'datetime'
 
const T_FLOAT = 'float'
 
const T_INTEGER = 'integer'
 
const T_TEXT = 'text'
 
const T_TIME = 'time'
 
const T_TIMESTAMP = 'timestamp'
 

Protected Attributes

array $max_length
 
array $available_types
 
array $reserved_mysql
 
array $reserved_postgres
 
ilMySQLQueryUtils $query_utils = null
 
array $valid_default_values
 

Static Protected Attributes

static self $instance
 

Private Member Functions

 getDBInstance ()
 
 getDeclarationOptions (array $field)
 
 mapNativeDatatypeInternal (array $field)
 

Private Attributes

const MAX_TABLE_IDENTIFIER_LENGTH = 63
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ilDBPdoMySQLFieldDefinition::__construct ( protected \ilDBInterface  $db_instance)

Definition at line 815 of file ilDBPdoMySQLFieldDefinition.php.

816 {
817 }

Member Function Documentation

◆ checkColumnDefinition()

ilDBPdoMySQLFieldDefinition::checkColumnDefinition ( array  $a_def)
Exceptions

ilDatabaseException

Implements ILIAS\Database\FieldDefinition.

Definition at line 935 of file ilDBPdoMySQLFieldDefinition.php.

935 : bool
936 {
937 // check valid type
938 if (!in_array($a_def["type"], $this->getAvailableTypes(), true)) {
939 switch ($a_def["type"]) {
940 case "boolean":
941 throw new ilDatabaseException("Invalid column type '" . $a_def["type"] . "'. Use integer(1) instead.");
942
943 case "decimal":
944 throw new ilDatabaseException("Invalid column type '" . $a_def["type"] . "'. Use float or integer instead.");
945
946 default:
947 throw new ilDatabaseException("Invalid column type '" . $a_def["type"] . "'. Allowed types are: "
948 . implode(', ', $this->getAvailableTypes()));
949 }
950 }
951
952 // check used attributes
954 foreach (array_keys($a_def) as $k) {
955 if ($k !== "type" && !in_array($k, $allowed_attributes[$a_def["type"]], true)) {
956 throw new ilDatabaseException("Attribute '" . $k . "' is not allowed for column type '" . $a_def["type"] . "'.");
957 }
958 }
959
960 // type specific checks
961 $max_length = $this->getMaxLength();
962 switch ($a_def["type"]) {
963 case self::T_TEXT:
964 if ((!isset($a_def["length"]) || $a_def["length"] < 1 || $a_def["length"] > $max_length[self::T_TEXT]) && isset($a_def["length"])) {
965 throw new ilDatabaseException("Invalid length '" . $a_def["length"] . "' for type text." . " Length must be >=1 and <= "
966 . $max_length[self::T_TEXT] . ".");
967 }
968 break;
969
970 case self::T_INTEGER:
971 if (isset($a_def["length"]) && !in_array((int) $a_def["length"], $max_length[self::T_INTEGER], true)) {
972 throw new ilDatabaseException("Invalid length '" . $a_def["length"] . "' for type integer." . " Length must be "
973 . implode(', ', $max_length[self::T_INTEGER]) . " (bytes).");
974 }
975 if ($a_def["unsigned"] ?? null) {
976 throw new ilDatabaseException("Unsigned attribut must not be true for type integer.");
977 }
978 break;
979 }
980
981 return true;
982 }
Class ilDatabaseException.

References $allowed_attributes, $max_length, getAllowedAttributes(), getAvailableTypes(), getMaxLength(), ILIAS\Database\FieldDefinition\T_INTEGER, and ILIAS\Database\FieldDefinition\T_TEXT.

+ Here is the call graph for this function:

◆ checkColumnName()

ilDBPdoMySQLFieldDefinition::checkColumnName ( string  $column_name)
Exceptions

ilDatabaseException

Implements ILIAS\Database\FieldDefinition.

Definition at line 890 of file ilDBPdoMySQLFieldDefinition.php.

890 : bool
891 {
892 if (!preg_match("/^[a-z]+[_a-z0-9]*$/", $column_name)) {
893 throw new ilDatabaseException("Invalid column name '" . $column_name
894 . "'. Column name must only contain _a-z0-9 and must start with a-z.");
895 }
896
897 if ($this->isReserved($column_name)) {
898 throw new ilDatabaseException("Invalid column name '" . $column_name . "' (Reserved Word).");
899 }
900
901 if (stripos($column_name, "sys_") === 0) {
902 throw new ilDatabaseException("Invalid column name '" . $column_name . "'. Name must not start with 'sys_'.");
903 }
904
905 if (strlen($column_name) > 30) {
906 throw new ilDatabaseException("Invalid column name '" . $column_name . "'. Maximum column identifer length is 30 bytes.");
907 }
908
909 return true;
910 }

References isReserved().

+ Here is the call graph for this function:

◆ checkIndexName()

ilDBPdoMySQLFieldDefinition::checkIndexName ( string  $a_name)
Exceptions

ilDatabaseException

Implements ILIAS\Database\FieldDefinition.

Definition at line 915 of file ilDBPdoMySQLFieldDefinition.php.

915 : bool
916 {
917 if (!preg_match("/^[a-z]+[_a-z0-9]*$/", $a_name)) {
918 throw new ilDatabaseException("Invalid column name '" . $a_name . "'. Column name must only contain _a-z0-9 and must start with a-z.");
919 }
920
921 if ($this->isReserved($a_name)) {
922 throw new ilDatabaseException("Invalid column name '" . $a_name . "' (Reserved Word).");
923 }
924
925 if (strlen($a_name) > 3) {
926 throw new ilDatabaseException("Invalid index name '" . $a_name . "'. Maximum index identifer length is 3 bytes.");
927 }
928
929 return true;
930 }

References isReserved().

+ Here is the call graph for this function:

◆ checkTableName()

ilDBPdoMySQLFieldDefinition::checkTableName ( string  $table_name)
Exceptions

ilDatabaseException

Implements ILIAS\Database\FieldDefinition.

Definition at line 835 of file ilDBPdoMySQLFieldDefinition.php.

835 : bool
836 {
837 if (!preg_match(self::DEFINITION_TABLE_NAME, $table_name)) {
838 throw new ilDatabaseException('Table name must only contain _a-z0-9 and must start with a-z.');
839 }
840
841 if ($this->isReserved($table_name)) {
842 throw new ilDatabaseException("Invalid table name '" . $table_name . "' (Reserved Word).");
843 }
844
845 if (stripos($table_name, "sys_") === 0) {
846 throw new ilDatabaseException("Invalid table name '" . $table_name . "'. Name must not start with 'sys_'.");
847 }
848
849 if (strlen($table_name) > self::MAX_TABLE_IDENTIFIER_LENGTH) {
850 throw new ilDatabaseException("Invalid table name '" . $table_name
851 . "'. Maximum table identifer length is " . self::MAX_TABLE_IDENTIFIER_LENGTH . " bytes.");
852 }
853
854 return true;
855 }

References isReserved().

+ Here is the call graph for this function:

◆ compareDefinition()

ilDBPdoMySQLFieldDefinition::compareDefinition ( array  $current,
array  $previous 
)
Exceptions

ilDatabaseException

Returns
array<string, bool>

Implements ILIAS\Database\FieldDefinition.

Definition at line 1142 of file ilDBPdoMySQLFieldDefinition.php.

1142 : array
1143 {
1144 $type = empty($current['type']) ? null : $current['type'];
1145
1146 if (!method_exists($this, "compare{$type}Definition")) {
1147 $db = $this->getDBInstance();
1148
1149 if (!empty($db->options['datatype_map_callback'][$type])) {
1150 $parameter = ['current' => $current, 'previous' => $previous];
1151
1152 return call_user_func_array(
1153 $db->options['datatype_map_callback'][$type],
1154 [&$db, __FUNCTION__, $parameter]
1155 );
1156 }
1157
1158 throw new ilDatabaseException('type "' . $current['type'] . '" is not yet supported');
1159 }
1160
1161 if (empty($previous['type']) || $previous['type'] != $type) {
1162 return $current;
1163 }
1164
1165 $change = $this->{"compare{$type}Definition"}($current, $previous);
1166
1167 if ($previous['type'] != $type) {
1168 $change['type'] = true;
1169 }
1170
1171 $previous_notnull = empty($previous['notnull']) ? false : $previous['notnull'];
1172 $notnull = empty($current['notnull']) ? false : $current['notnull'];
1173 if ($previous_notnull !== $notnull) {
1174 $change['notnull'] = true;
1175 }
1176
1177 $alt = $previous_notnull ? '' : null;
1178 $previous_default = array_key_exists(
1179 'default',
1180 $previous
1181 ) ? $previous['default'] : $alt;
1182 $alt = $notnull ? '' : null;
1183 $default = array_key_exists('default', $current) ? $current['default'] : $alt;
1184 if ($previous_default !== $default) {
1185 $change['default'] = true;
1186 }
1187
1188 return $change;
1189 }

References getDBInstance().

+ Here is the call graph for this function:

◆ destroyLOB()

ilDBPdoMySQLFieldDefinition::destroyLOB (   $lob)
Parameters
resource$lob

Implements ILIAS\Database\FieldDefinition.

Definition at line 1230 of file ilDBPdoMySQLFieldDefinition.php.

1230 : bool
1231 {
1232 $lob_data = stream_get_meta_data($lob);
1233 $lob_index = $lob_data['wrapper_data']->lob_index;
1234 fclose($lob);
1235 if (isset($this->lobs[$lob_index])) {
1236 unset($this->lobs[$lob_index]);
1237 }
1238
1239 return true;
1240 }

◆ getAllowedAttributes()

ilDBPdoMySQLFieldDefinition::getAllowedAttributes ( )
Returns
array<string, string[]>

Implements ILIAS\Database\FieldDefinition.

Definition at line 1008 of file ilDBPdoMySQLFieldDefinition.php.

1008 : array
1009 {
1011 }

References $allowed_attributes.

Referenced by checkColumnDefinition().

+ Here is the caller graph for this function:

◆ getAllReserved()

ilDBPdoMySQLFieldDefinition::getAllReserved ( )
Returns
string[]

Implements ILIAS\Database\FieldDefinition.

Definition at line 865 of file ilDBPdoMySQLFieldDefinition.php.

865 : array
866 {
867 return $this->getReservedMysql();
868 }

References getReservedMysql().

+ Here is the call graph for this function:

◆ getAvailableTypes()

ilDBPdoMySQLFieldDefinition::getAvailableTypes ( )
Returns
string[]

Implements ILIAS\Database\FieldDefinition.

Definition at line 992 of file ilDBPdoMySQLFieldDefinition.php.

992 : array
993 {
995 }

References $available_types.

Referenced by checkColumnDefinition().

+ Here is the caller graph for this function:

◆ getDBInstance()

ilDBPdoMySQLFieldDefinition::getDBInstance ( )
private

Definition at line 1031 of file ilDBPdoMySQLFieldDefinition.php.

1032 {
1033 return $this->db_instance;
1034 }
Interface ilDBInterface.

Referenced by compareDefinition(), getDeclaration(), getDeclarationOptions(), getTypeDeclaration(), getValidTypes(), mapNativeDatatype(), mapPrepareDatatype(), matchPattern(), quote(), and writeLOBToFile().

+ Here is the caller graph for this function:

◆ getDeclaration()

ilDBPdoMySQLFieldDefinition::getDeclaration ( string  $type,
string  $name,
array  $field 
)
Returns
mixed
Exceptions

ilDatabaseException

Implements ILIAS\Database\FieldDefinition.

Definition at line 1068 of file ilDBPdoMySQLFieldDefinition.php.

1069 {
1070 $db = $this->getDBInstance();
1071
1072 if (!empty($db->options['datatype_map'][$type])) {
1073 $type = $db->options['datatype_map'][$type];
1074 if (!empty($db->options['datatype_map_callback'][$type])) {
1075 $parameter = ['type' => $type, 'name' => $name, 'field' => $field];
1076
1077 return call_user_func_array(
1078 $db->options['datatype_map_callback'][$type],
1079 [&$db, __FUNCTION__, $parameter]
1080 );
1081 }
1082 $field['type'] = $type;
1083 }
1084
1085 if (!array_key_exists($type, $this->getValidTypes())) {
1086 throw new ilDatabaseException('type not defined: ' . $type);
1087 }
1088
1089 $quoted_name = $db->quoteIdentifier($name, true);
1090 $type_declaration = $this->getTypeDeclaration($field);
1091
1092 if ($type === 'clob' || $type === 'blob') {
1093 $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
1094 return $quoted_name . ' ' . $type_declaration . $notnull;
1095 }
1096
1097 return $quoted_name . ' ' . $type_declaration . $this->getDeclarationOptions($field);
1098 }

References getDBInstance(), getDeclarationOptions(), getTypeDeclaration(), and getValidTypes().

+ Here is the call graph for this function:

◆ getDeclarationOptions()

ilDBPdoMySQLFieldDefinition::getDeclarationOptions ( array  $field)
private

Definition at line 1100 of file ilDBPdoMySQLFieldDefinition.php.

1100 : string
1101 {
1102 $charset = empty($field['charset']) ? '' : ' ' . $field['charset'];
1103
1104 $default = '';
1105 if (array_key_exists('default', $field)) {
1106 if ($field['default'] === '') {
1107 $db = $this->getDBInstance();
1108
1109 if (empty($field['notnull'])) {
1110 $field['default'] = null;
1111 } else {
1113 $field['default'] = $valid_default_values[$field['type']];
1114 }
1115 if ($field['default'] === ''
1116 && isset($db->options['portability'])
1117 && ($db->options['portability'] & 32)
1118 ) {
1119 $field['default'] = ' ';
1120 }
1121 }
1122 $default = ' DEFAULT ' . $this->quote($field['default'], $field['type']);
1123 } elseif (empty($field['notnull'])) {
1124 $default = ' DEFAULT NULL';
1125 }
1126
1127 $notnull = empty($field['notnull']) ? '' : ' NOT NULL';
1128 if (isset($field['notnull']) && $field['notnull'] === false) {
1129 $notnull = ' NULL';
1130 }
1131
1132 $collation = empty($field['collation']) ? '' : ' ' . $field['collation'];
1133
1134 return $charset . $default . $notnull . $collation;
1135 }
quote($value, ?string $type=null, bool $quote=true, bool $escape_wildcards=false)

References $valid_default_values, getDBInstance(), getValidTypes(), and quote().

Referenced by getDeclaration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getMaxLength()

ilDBPdoMySQLFieldDefinition::getMaxLength ( )

Implements ILIAS\Database\FieldDefinition.

Definition at line 1021 of file ilDBPdoMySQLFieldDefinition.php.

1021 : array
1022 {
1023 return $this->max_length;
1024 }

References $max_length.

Referenced by checkColumnDefinition().

+ Here is the caller graph for this function:

◆ getReservedMysql()

ilDBPdoMySQLFieldDefinition::getReservedMysql ( )
Returns
string[]

Implements ILIAS\Database\FieldDefinition.

Definition at line 873 of file ilDBPdoMySQLFieldDefinition.php.

873 : array
874 {
876 }

References $reserved_mysql.

Referenced by getAllReserved().

+ Here is the caller graph for this function:

◆ getTypeDeclaration()

ilDBPdoMySQLFieldDefinition::getTypeDeclaration ( array  $field)

Implements ILIAS\Database\FieldDefinition.

Definition at line 1331 of file ilDBPdoMySQLFieldDefinition.php.

1331 : string
1332 {
1333 $db = $this->getDBInstance();
1334
1335 switch ($field['type']) {
1336 case 'text':
1337 if (empty($field['length']) && array_key_exists('default', $field)) {
1338 $field['length'] = $db->varchar_max_length ?? null;
1339 }
1340 $length = empty($field['length']) ? false : $field['length'];
1341 $fixed = empty($field['fixed']) ? false : $field['fixed'];
1342 if ($fixed) {
1343 return $length ? 'CHAR(' . $length . ')' : 'CHAR(255)';
1344 }
1345 return $length ? 'VARCHAR(' . $length . ')' : 'TEXT';
1346
1347 case 'clob':
1348 if (!empty($field['length'])) {
1349 $length = $field['length'];
1350 if ($length <= 255) {
1351 return 'TINYTEXT';
1352 }
1353
1354 if ($length <= 65532) {
1355 return 'TEXT';
1356 }
1357
1358 if ($length <= 16_777_215) {
1359 return 'MEDIUMTEXT';
1360 }
1361 }
1362
1363 return 'LONGTEXT';
1364 case 'blob':
1365 if (!empty($field['length'])) {
1366 $length = $field['length'];
1367 if ($length <= 255) {
1368 return 'TINYBLOB';
1369 }
1370
1371 if ($length <= 65532) {
1372 return 'BLOB';
1373 }
1374
1375 if ($length <= 16_777_215) {
1376 return 'MEDIUMBLOB';
1377 }
1378 }
1379
1380 return 'LONGBLOB';
1381 case 'integer':
1382 if (!empty($field['length'])) {
1383 $length = $field['length'];
1384 if ($length <= 1) {
1385 return 'TINYINT';
1386 }
1387
1388 if ($length === 2) {
1389 return 'SMALLINT';
1390 }
1391
1392 if ($length === 3) {
1393 return 'MEDIUMINT';
1394 }
1395
1396 if ($length === 4) {
1397 return 'INT';
1398 }
1399
1400 if ($length > 4) {
1401 return 'BIGINT';
1402 }
1403 }
1404
1405 return 'INT';
1406 case 'boolean':
1407 return 'TINYINT(1)';
1408 case 'date':
1409 return 'DATE';
1410 case 'time':
1411 return 'TIME';
1412 case 'timestamp':
1413 return 'DATETIME';
1414 case 'float':
1415 return 'DOUBLE';
1416 case 'decimal':
1417 $length = empty($field['length']) ? 18 : $field['length'];
1418 // @Todo: Change property access to method call.
1419 $scale = empty($field['scale']) ? $db->options['decimal_places'] : $field['scale'];
1420
1421 return 'DECIMAL(' . $length . ',' . $scale . ')';
1422 }
1423
1424 return '';
1425 }

References getDBInstance().

Referenced by getDeclaration().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getValidTypes()

ilDBPdoMySQLFieldDefinition::getValidTypes ( )
Returns
string[]

Implements ILIAS\Database\FieldDefinition.

Definition at line 1039 of file ilDBPdoMySQLFieldDefinition.php.

1039 : array
1040 {
1042 $db = $this->getDBInstance();
1043
1044 if (!empty($db->options['datatype_map'])) {
1045 foreach ($db->options['datatype_map'] as $type => $mapped_type) {
1046 if (array_key_exists($mapped_type, $types)) {
1047 $types[$type] = $types[$mapped_type];
1048 } elseif (!empty($db->options['datatype_map_callback'][$type])) {
1049 $parameter = ['type' => $type, 'mapped_type' => $mapped_type];
1050 $default = call_user_func_array(
1051 $db->options['datatype_map_callback'][$type],
1052 [&$db, __FUNCTION__, $parameter]
1053 );
1054 $types[$type] = $default;
1055 }
1056 }
1057 }
1058
1059 return $types;
1060 }

References $valid_default_values, and getDBInstance().

Referenced by getDeclaration(), and getDeclarationOptions().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isAllowedAttribute()

ilDBPdoMySQLFieldDefinition::isAllowedAttribute ( string  $attribute,
string  $type 
)

Implements ILIAS\Database\FieldDefinition.

Definition at line 984 of file ilDBPdoMySQLFieldDefinition.php.

984 : bool
985 {
986 return in_array($attribute, $this->allowed_attributes[$type], true);
987 }

◆ isReserved()

ilDBPdoMySQLFieldDefinition::isReserved ( string  $table_name)

Implements ILIAS\Database\FieldDefinition.

Definition at line 857 of file ilDBPdoMySQLFieldDefinition.php.

857 : bool
858 {
859 return false;
860 }

Referenced by checkColumnName(), checkIndexName(), and checkTableName().

+ Here is the caller graph for this function:

◆ mapNativeDatatype()

ilDBPdoMySQLFieldDefinition::mapNativeDatatype ( array  $field)
Returns
mixed

Implements ILIAS\Database\FieldDefinition.

Definition at line 1297 of file ilDBPdoMySQLFieldDefinition.php.

1298 {
1299 $db = $this->getDBInstance();
1300 $db_type = strtok($field['type'], '(), ');
1301 if (!empty($db->options['nativetype_map_callback'][$db_type])) {
1302 return call_user_func_array($db->options['nativetype_map_callback'][$db_type], [$db, $field]);
1303 }
1304
1305 return $this->mapNativeDatatypeInternal($field);
1306 }

References getDBInstance(), and mapNativeDatatypeInternal().

+ Here is the call graph for this function:

◆ mapNativeDatatypeInternal()

ilDBPdoMySQLFieldDefinition::mapNativeDatatypeInternal ( array  $field)
private
Exceptions

ilDatabaseException

Definition at line 1430 of file ilDBPdoMySQLFieldDefinition.php.

1430 : array
1431 {
1432 $db_type = strtolower((string) $field['type']);
1433 $db_type = strtok($db_type, '(), ');
1434 if ($db_type === 'national') {
1435 $db_type = strtok('(), ');
1436 }
1437 if (!empty($field['length'])) {
1438 $length = strtok($field['length'], ', ');
1439 $decimal = strtok(', ');
1440 } else {
1441 $length = strtok('(), ');
1442 $decimal = strtok('(), ');
1443 }
1444 $type = [];
1445 $unsigned = $fixed = null;
1446 switch ($db_type) {
1447 case 'tinyint':
1448 $type[] = 'integer';
1449 $type[] = 'boolean';
1450 if (preg_match('/^(is|has)/', (string) $field['name'])) {
1451 $type = array_reverse($type);
1452 }
1453 $unsigned = preg_match('/ unsigned/i', (string) $field['type']);
1454 $length = 1;
1455 break;
1456 case 'smallint':
1457 $type[] = 'integer';
1458 $unsigned = preg_match('/ unsigned/i', (string) $field['type']);
1459 $length = 2;
1460 break;
1461 case 'mediumint':
1462 $type[] = 'integer';
1463 $unsigned = preg_match('/ unsigned/i', (string) $field['type']);
1464 $length = 3;
1465 break;
1466 case 'int':
1467 case 'integer':
1468 $type[] = 'integer';
1469 $unsigned = preg_match('/ unsigned/i', (string) $field['type']);
1470 $length = 4;
1471 break;
1472 case 'bigint':
1473 $type[] = 'integer';
1474 $unsigned = preg_match('/ unsigned/i', (string) $field['type']);
1475 $length = 8;
1476 break;
1477 case 'tinytext':
1478 case 'mediumtext':
1479 case 'longtext':
1480 case 'text':
1481 case 'varchar':
1482 $fixed = false;
1483 // no break
1484 case 'string':
1485 case 'char':
1486 $type[] = 'text';
1487 if ($length == '1') {
1488 $type[] = 'boolean';
1489 if (preg_match('/^(is|has)/', (string) $field['name'])) {
1490 $type = array_reverse($type);
1491 }
1492 } elseif (str_contains($db_type, 'text')) {
1493 $type[] = 'clob';
1494 if ($decimal === 'binary') {
1495 $type[] = 'blob';
1496 }
1497 }
1498 if ($fixed !== false) {
1499 $fixed = true;
1500 }
1501 break;
1502 case 'enum':
1503 $type[] = 'text';
1504 preg_match_all('/\'.+\'/U', (string) $field['type'], $matches);
1505 $length = 0;
1506 $fixed = false;
1507 if (is_array($matches)) {
1508 foreach ($matches[0] as $value) {
1509 $length = max($length, strlen($value) - 2);
1510 }
1511 if ($length == '1' && count($matches[0]) === 2) {
1512 $type[] = 'boolean';
1513 if (preg_match('/^(is|has)/', (string) $field['name'])) {
1514 $type = array_reverse($type);
1515 }
1516 }
1517 }
1518 $type[] = 'integer';
1519 // no break
1520 case 'set':
1521 $fixed = false;
1522 $type[] = 'text';
1523 $type[] = 'integer';
1524 break;
1525 case 'date':
1526 $type[] = 'date';
1527 $length = null;
1528 break;
1529 case 'datetime':
1530 case 'timestamp':
1531 $type[] = 'timestamp';
1532 $length = null;
1533 break;
1534 case 'time':
1535 $type[] = 'time';
1536 $length = null;
1537 break;
1538 case 'float':
1539 case 'double':
1540 case 'real':
1541 $type[] = 'float';
1542 $unsigned = preg_match('/ unsigned/i', (string) $field['type']);
1543 break;
1544 case 'unknown':
1545 case 'decimal':
1546 case 'numeric':
1547 $type[] = 'decimal';
1548 $unsigned = preg_match('/ unsigned/i', (string) $field['type']);
1549 if ($decimal !== false) {
1550 $length = $length . ',' . $decimal;
1551 }
1552 break;
1553 case 'tinyblob':
1554 case 'mediumblob':
1555 case 'longblob':
1556 case 'blob':
1557 $type[] = 'blob';
1558 $length = null;
1559 break;
1560 case 'binary':
1561 case 'varbinary':
1562 $type[] = 'blob';
1563 break;
1564 case 'year':
1565 $type[] = 'integer';
1566 $type[] = 'date';
1567 $length = null;
1568 break;
1569 default:
1570 throw new ilDatabaseException('unknown database attribute type: ' . $db_type);
1571 }
1572
1573 if ((int) $length <= 0) {
1574 $length = null;
1575 }
1576
1577 return [ $type, $length, $unsigned, $fixed ];
1578 }

Referenced by mapNativeDatatype().

+ Here is the caller graph for this function:

◆ mapPrepareDatatype()

ilDBPdoMySQLFieldDefinition::mapPrepareDatatype ( string  $type)
Returns
mixed

Implements ILIAS\Database\FieldDefinition.

Definition at line 1311 of file ilDBPdoMySQLFieldDefinition.php.

1312 {
1313 $db = $this->getDBInstance();
1314
1315 if (!empty($db->options['datatype_map'][$type])) {
1316 $type = $db->options['datatype_map'][$type];
1317 if (!empty($db->options['datatype_map_callback'][$type])) {
1318 $parameter = ['type' => $type];
1319
1320 return call_user_func_array(
1321 $db->options['datatype_map_callback'][$type],
1322 [&$db, __FUNCTION__, $parameter]
1323 );
1324 }
1325 }
1326
1327 return $type;
1328 }

References getDBInstance().

+ Here is the call graph for this function:

◆ matchPattern()

ilDBPdoMySQLFieldDefinition::matchPattern ( array  $pattern,
  $operator = null,
  $field = null 
)
Exceptions

ilDatabaseException

Implements ILIAS\Database\FieldDefinition.

Definition at line 1246 of file ilDBPdoMySQLFieldDefinition.php.

1246 : string
1247 {
1248 $db = $this->getDBInstance();
1249
1250 $match = '';
1251 if (!is_null($operator)) {
1252 $operator = strtoupper((string) $operator);
1253 switch ($operator) {
1254 // case insensitive
1255 case 'ILIKE':
1256 if (is_null($field)) {
1257 throw new ilDatabaseException('case insensitive LIKE matching requires passing the field name');
1258 }
1259 $db->loadModule('Function');
1260 $match = $db->lower($field) . ' LIKE ';
1261 break;
1262 // case sensitive
1263 case 'LIKE':
1264 $match = is_null($field) ? 'LIKE ' : $field . ' LIKE ';
1265 break;
1266 default:
1267 throw new ilDatabaseException('not a supported operator type:' . $operator);
1268 }
1269 }
1270 $match .= "'";
1271 foreach ($pattern as $key => $value) {
1272 if ($key % 2 !== 0) {
1273 $match .= $value;
1274 } else {
1275 if ($operator === 'ILIKE') {
1276 $value = strtolower((string) $value);
1277 }
1278 // @Todo: ilDBPdo::escape & escapePattern do nothing, probably quote instead.
1279 $escaped = $db->escape($value);
1280 $match .= $db->escapePattern($escaped);
1281 }
1282 }
1283 $match .= "'";
1284 $match .= $this->patternEscapeString();
1285
1286 return $match;
1287 }

References getDBInstance(), and patternEscapeString().

+ Here is the call graph for this function:

◆ patternEscapeString()

ilDBPdoMySQLFieldDefinition::patternEscapeString ( )

Implements ILIAS\Database\FieldDefinition.

Definition at line 1289 of file ilDBPdoMySQLFieldDefinition.php.

1289 : string
1290 {
1291 return '';
1292 }

Referenced by matchPattern().

+ Here is the caller graph for this function:

◆ quote()

ilDBPdoMySQLFieldDefinition::quote (   $value,
?string  $type = null,
bool  $quote = true,
bool  $escape_wildcards = false 
)
Parameters
mixed$value

Implements ILIAS\Database\FieldDefinition.

Definition at line 1194 of file ilDBPdoMySQLFieldDefinition.php.

1194 : string
1195 {
1196 return $this->getDBInstance()->quote($value, $type ?? '');
1197 }

References getDBInstance().

Referenced by getDeclarationOptions().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setAllowedAttributes()

ilDBPdoMySQLFieldDefinition::setAllowedAttributes ( array  $allowed_attributes)
Parameters
array<string,string[]>$allowed_attributes

Implements ILIAS\Database\FieldDefinition.

Definition at line 1016 of file ilDBPdoMySQLFieldDefinition.php.

1016 : void
1017 {
1018 $this->allowed_attributes = $allowed_attributes;
1019 }

References $allowed_attributes.

◆ setAvailableTypes()

ilDBPdoMySQLFieldDefinition::setAvailableTypes ( array  $available_types)
Parameters
string[]$available_types

Implements ILIAS\Database\FieldDefinition.

Definition at line 1000 of file ilDBPdoMySQLFieldDefinition.php.

1000 : void
1001 {
1002 $this->available_types = $available_types;
1003 }

References $available_types.

◆ setMaxLength()

ilDBPdoMySQLFieldDefinition::setMaxLength ( array  $max_length)

Implements ILIAS\Database\FieldDefinition.

Definition at line 1026 of file ilDBPdoMySQLFieldDefinition.php.

1026 : void
1027 {
1028 $this->max_length = $max_length;
1029 }

References $max_length.

◆ setReservedMysql()

ilDBPdoMySQLFieldDefinition::setReservedMysql ( array  $reserved_mysql)
Parameters
string[]$reserved_mysql

Implements ILIAS\Database\FieldDefinition.

Definition at line 881 of file ilDBPdoMySQLFieldDefinition.php.

881 : void
882 {
883 $this->reserved_mysql = $reserved_mysql;
884 }

References $reserved_mysql.

◆ writeLOBToFile()

ilDBPdoMySQLFieldDefinition::writeLOBToFile (   $lob,
string  $file 
)
Parameters
resource$lob
Exceptions

ilDatabaseException

Implements ILIAS\Database\FieldDefinition.

Definition at line 1204 of file ilDBPdoMySQLFieldDefinition.php.

1204 : bool
1205 {
1206 $db = $this->getDBInstance();
1207
1208 if (preg_match('/^(\w+:\/\/)(.*)$/', $file, $match) && $match[1] === 'file://') {
1209 $file = $match[2];
1210 }
1211
1212 $fp = @fopen($file, 'wb');
1213 while (!@feof($lob)) {
1214 $result = @fread($lob, $db->options['lob_buffer_length']);
1215 $read = strlen($result);
1216 if (@fwrite($fp, $result, $read) !== $read) {
1217 @fclose($fp);
1218
1219 throw new ilDatabaseException('could not write to the output file');
1220 }
1221 }
1222 @fclose($fp);
1223
1224 return true;
1225 }

References getDBInstance().

+ Here is the call graph for this function:

Field Documentation

◆ $allowed_attributes

array ilDBPdoMySQLFieldDefinition::$allowed_attributes
Initial value:
= [
"text" => ["length", "notnull", "default", "fixed"],
"integer" => ["length", "notnull", "default", "unsigned"],
"float" => ["notnull", "default"],
"date" => ["notnull", "default"],
"time" => ["notnull", "default"],
"timestamp" => ["notnull", "default"],
"clob" => ["length", "notnull", "default"],
"blob" => ["length", "notnull", "default"],
]

Definition at line 55 of file ilDBPdoMySQLFieldDefinition.php.

Referenced by checkColumnDefinition(), getAllowedAttributes(), and setAllowedAttributes().

◆ $allowed_attributes_old

array ilDBPdoMySQLFieldDefinition::$allowed_attributes_old
Initial value:
= [
self::T_TEXT => ['length', 'notnull', 'default', 'fixed'],
self::T_INTEGER => ['length', 'notnull', 'default', 'unsigned'],
self::T_FLOAT => ['notnull', 'default'],
self::T_DATE => ['notnull', 'default'],
self::T_TIME => ['notnull', 'default'],
self::T_TIMESTAMP => ['notnull', 'default'],
self::T_CLOB => ['notnull', 'default'],
self::T_BLOB => ['notnull', 'default'],
]

Definition at line 42 of file ilDBPdoMySQLFieldDefinition.php.

◆ $available_types

◆ $instance

self ilDBPdoMySQLFieldDefinition::$instance
staticprotected

Definition at line 38 of file ilDBPdoMySQLFieldDefinition.php.

◆ $lobs

array ilDBPdoMySQLFieldDefinition::$lobs

Definition at line 30 of file ilDBPdoMySQLFieldDefinition.php.

◆ $max_length

array ilDBPdoMySQLFieldDefinition::$max_length
protected
Initial value:
= [
self::T_INTEGER => [1, 2, 3, 4, 8],
self::T_TEXT => 4000,
]

Definition at line 65 of file ilDBPdoMySQLFieldDefinition.php.

Referenced by checkColumnDefinition(), getMaxLength(), and setMaxLength().

◆ $query_utils

ilMySQLQueryUtils ilDBPdoMySQLFieldDefinition::$query_utils = null
protected

Definition at line 813 of file ilDBPdoMySQLFieldDefinition.php.

◆ $reserved_mysql

array ilDBPdoMySQLFieldDefinition::$reserved_mysql
protected

Definition at line 85 of file ilDBPdoMySQLFieldDefinition.php.

Referenced by getReservedMysql(), and setReservedMysql().

◆ $reserved_postgres

array ilDBPdoMySQLFieldDefinition::$reserved_postgres
protected

Definition at line 715 of file ilDBPdoMySQLFieldDefinition.php.

◆ $valid_default_values

array ilDBPdoMySQLFieldDefinition::$valid_default_values
protected
Initial value:
= [
'text' => '',
'boolean' => true,
'integer' => 0,
'decimal' => 0.0,
'float' => 0.0,
'timestamp' => '1970-01-01 00:00:00',
'time' => '00:00:00',
'date' => '1970-01-01',
'clob' => '',
'blob' => '',
]

Definition at line 819 of file ilDBPdoMySQLFieldDefinition.php.

Referenced by getDeclarationOptions(), and getValidTypes().

◆ DEFAULT_DECIMAL_PLACES

const ilDBPdoMySQLFieldDefinition::DEFAULT_DECIMAL_PLACES = 2

Definition at line 31 of file ilDBPdoMySQLFieldDefinition.php.

◆ DEFAULT_TEXT_LENGTH

const ilDBPdoMySQLFieldDefinition::DEFAULT_TEXT_LENGTH = 4000

Definition at line 32 of file ilDBPdoMySQLFieldDefinition.php.

◆ DEFINITION_COLUMN_NAME

const ilDBPdoMySQLFieldDefinition::DEFINITION_COLUMN_NAME = "/^[a-z]+[_a-z0-9]*$/"

Definition at line 33 of file ilDBPdoMySQLFieldDefinition.php.

◆ DEFINITION_TABLE_NAME

const ilDBPdoMySQLFieldDefinition::DEFINITION_TABLE_NAME = "/^[a-z]+[_a-z0-9]*$/"

Definition at line 34 of file ilDBPdoMySQLFieldDefinition.php.

◆ MAX_TABLE_IDENTIFIER_LENGTH

const ilDBPdoMySQLFieldDefinition::MAX_TABLE_IDENTIFIER_LENGTH = 63
private

Definition at line 36 of file ilDBPdoMySQLFieldDefinition.php.


The documentation for this class was generated from the following file: