core-bundle/src/Resources/contao/library/Contao/Database/Installer.php
public function getFromDb()
	{
		$this->import(Database::class, 'Database');
		$tables = preg_grep('/^tl_/', $this->Database->listTables(null, true));

		if (empty($tables))
		{
			return array();
		}

		$return = array();
		$quote = function ($item) { return '`' . $item . '`'; };

		foreach ($tables as $table)
		{
			$fields = $this->Database->listFields($table, true);

			foreach ($fields as $field)
			{
				$name = $field['name'];
				$field['name'] = $quote($field['name']);

				if ($field['type'] != 'index')
				{
					unset($field['index']);
					unset($field['origtype']);

					// Field type
					if ($field['length'] != '')
					{
						$field['type'] .= '(' . $field['length'] . (($field['precision'] != '') ? ',' . $field['precision'] : '') . ')';

						unset($field['length']);
						unset($field['precision']);
					}

					// Variant collation
					if ($field['collation'] != '' && $field['collation'] != Config::get('dbCollation'))
					{
						$field['collation'] = 'COLLATE ' . $field['collation'];
					}
					else
					{
						unset($field['collation']);
					}

					// Default values
					if (\in_array(strtolower($field['type']), array('text', 'tinytext', 'mediumtext', 'longtext', 'blob', 'tinyblob', 'mediumblob', 'longblob')) || stripos($field['extra'], 'auto_increment') !== false || $field['default'] === null || strtolower($field['null']) == 'null')
					{
						unset($field['default']);
					}
					// Date/time constants (see #5089)
					elseif (\in_array(strtolower($field['default']), array('current_date', 'current_time', 'current_timestamp')))
					{
						$field['default'] = "default " . $field['default'];
					}
					// Everything else
					else
					{
						$field['default'] = "default '" . $field['default'] . "'";
					}

					$return[$table]['TABLE_FIELDS'][$name] = trim(implode(' ', $field));
				}

				// Indexes
				if (isset($field['index']) && $field['index_fields'])
				{
					// Quote the field names
					$index_fields = implode(', ', array_map
					(
						function ($item) use ($quote) {
							if (strpos($item, '(') === false) {
								return $quote($item);
							}

							list($name, $length) = explode('(', rtrim($item, ')'));

							return $quote($name) . '(' . $length . ')';
						},
						$field['index_fields'])
					);

					switch ($field['index'])
					{
						case 'UNIQUE':
							if ($name == 'PRIMARY')
							{
								$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'PRIMARY KEY  ('.$index_fields.')';
							}
							else
							{
								$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'UNIQUE KEY `'.$name.'` ('.$index_fields.')';
							}
							break;

						case 'FULLTEXT':
							$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'FULLTEXT KEY `'.$name.'` ('.$index_fields.')';
							break;

						default:
							$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'KEY `'.$name.'` ('.$index_fields.')';
							break;
					}

					unset($field['index_fields']);
					unset($field['index']);
				}
			}
		}

		// HOOK: allow third-party developers to modify the array (see #3281)
		if (isset($GLOBALS['TL_HOOKS']['sqlGetFromDB']) && \is_array($GLOBALS['TL_HOOKS']['sqlGetFromDB']))
		{
			foreach ($GLOBALS['TL_HOOKS']['sqlGetFromDB'] as $callback)
			{
				$this->import($callback[0]);
				$return = $this->{$callback[0]}->{$callback[1]}($return);
			}
		}

		return $return;
	}
public function getFromDb()
	{
		$this->import(Database::class, 'Database');
		$tables = preg_grep('/^tl_/', $this->Database->listTables(null, true));

		if (empty($tables))
		{
			return array();
		}

		$return = array();
		$quote = function ($item) { return '`' . $item . '`'; };

		foreach ($tables as $table)
		{
			$fields = $this->Database->listFields($table, true);

			foreach ($fields as $field)
			{
				$name = $field['name'];
				$field['name'] = $quote($field['name']);

				if ($field['type'] != 'index')
				{
					unset($field['index']);
					unset($field['origtype']);

					// Field type
					if ($field['length'] != '')
					{
						$field['type'] .= '(' . $field['length'] . (($field['precision'] != '') ? ',' . $field['precision'] : '') . ')';

						unset($field['length']);
						unset($field['precision']);
					}

					// Variant collation
					if ($field['collation'] != '' && $field['collation'] != Config::get('dbCollation'))
					{
						$field['collation'] = 'COLLATE ' . $field['collation'];
					}
					else
					{
						unset($field['collation']);
					}

					// Default values
					if (\in_array(strtolower($field['type']), array('text', 'tinytext', 'mediumtext', 'longtext', 'blob', 'tinyblob', 'mediumblob', 'longblob')) || stripos($field['extra'], 'auto_increment') !== false || $field['default'] === null || strtolower($field['null']) == 'null')
					{
						unset($field['default']);
					}
					// Date/time constants (see #5089)
					elseif (\in_array(strtolower($field['default']), array('current_date', 'current_time', 'current_timestamp')))
					{
						$field['default'] = "default " . $field['default'];
					}
					// Everything else
					else
					{
						$field['default'] = "default '" . $field['default'] . "'";
					}

					$return[$table]['TABLE_FIELDS'][$name] = trim(implode(' ', $field));
				}

				// Indexes
				if (isset($field['index']) && $field['index_fields'])
				{
					// Quote the field names
					$index_fields = implode(', ', array_map
					(
						function ($item) use ($quote) {
							if (strpos($item, '(') === false) {
								return $quote($item);
							}

							list($name, $length) = explode('(', rtrim($item, ')'));

							return $quote($name) . '(' . $length . ')';
						},
						$field['index_fields'])
					);

					switch ($field['index'])
					{
						case 'UNIQUE':
							if ($name == 'PRIMARY')
							{
								$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'PRIMARY KEY  ('.$index_fields.')';
							}
							else
							{
								$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'UNIQUE KEY `'.$name.'` ('.$index_fields.')';
							}
							break;

						case 'FULLTEXT':
							$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'FULLTEXT KEY `'.$name.'` ('.$index_fields.')';
							break;

						default:
							$return[$table]['TABLE_CREATE_DEFINITIONS'][$name] = 'KEY `'.$name.'` ('.$index_fields.')';
							break;
					}

					unset($field['index_fields']);
					unset($field['index']);
				}
			}
		}

		// HOOK: allow third-party developers to modify the array (see #3281)
		if (isset($GLOBALS['TL_HOOKS']['sqlGetFromDB']) && \is_array($GLOBALS['TL_HOOKS']['sqlGetFromDB']))
		{
			foreach ($GLOBALS['TL_HOOKS']['sqlGetFromDB'] as $callback)
			{
				$this->import($callback[0]);
				$return = $this->{$callback[0]}->{$callback[1]}($return);
			}
		}

		return $return;
	}
public function getFromDb ( ) { $ this -> import ( Database :: class , 'Database' ) ; $ tables = preg_grep ( '/^tl_/' , $ this -> Database -> listTables ( null , true ) ) ; if ( empty ( $ tables ) ) { return array ( ) ; } $ return = array ( ) ; $ quote = function ( $ item ) { return '`' . $ item . '`' ; } ; foreach ( $ tables as $ table ) { $ fields = $ this -> Database -> listFields ( $ table , true ) ; foreach ( $ fields as $ field ) { $ name = $ field [ 'name' ] ; $ field [ 'name' ] = $ quote ( $ field [ 'name' ] ) ; if ( $ field [ 'type' ] != 'index' ) { unset ( $ field [ 'index' ] ) ; unset ( $ field [ 'origtype' ] ) ; // Field type if ( $ field [ 'length' ] != '' ) { $ field [ 'type' ] .= '(' . $ field [ 'length' ] . ( ( $ field [ 'precision' ] != '' ) ? ',' . $ field [ 'precision' ] : '' ) . ')' ; unset ( $ field [ 'length' ] ) ; unset ( $ field [ 'precision' ] ) ; } // Variant collation if ( $ field [ 'collation' ] != '' && $ field [ 'collation' ] != Config :: get ( 'dbCollation' ) ) { $ field [ 'collation' ] = 'COLLATE ' . $ field [ 'collation' ] ; } else { unset ( $ field [ 'collation' ] ) ; } // Default values if ( \ in_array ( strtolower ( $ field [ 'type' ] ) , array ( 'text' , 'tinytext' , 'mediumtext' , 'longtext' , 'blob' , 'tinyblob' , 'mediumblob' , 'longblob' ) ) || stripos ( $ field [ 'extra' ] , 'auto_increment' ) !== false || $ field [ 'default' ] === null || strtolower ( $ field [ 'null' ] ) == 'null' ) { unset ( $ field [ 'default' ] ) ; } // Date/time constants (see #5089) elseif ( \ in_array ( strtolower ( $ field [ 'default' ] ) , array ( 'current_date' , 'current_time' , 'current_timestamp' ) ) ) { $ field [ 'default' ] = "default " . $ field [ 'default' ] ; } // Everything else else { $ field [ 'default' ] = "default '" . $ field [ 'default' ] . "'" ; } $ return [ $ table ] [ 'TABLE_FIELDS' ] [ $ name ] = trim ( implode ( ' ' , $ field ) ) ; } // Indexes if ( isset ( $ field [ 'index' ] ) && $ field [ 'index_fields' ] ) { // Quote the field names $ index_fields = implode ( ', ' , array_map ( function ( $ item ) use ( $ quote ) { if ( strpos ( $ item , '(' ) === false ) { return $ quote ( $ item ) ; } list ( $ name , $ length ) = explode ( '(' , rtrim ( $ item , ')' ) ) ; return $ quote ( $ name ) . '(' . $ length . ')' ; } , $ field [ 'index_fields' ] ) ) ; switch ( $ field [ 'index' ] ) { case 'UNIQUE' : if ( $ name == 'PRIMARY' ) { $ return [ $ table ] [ 'TABLE_CREATE_DEFINITIONS' ] [ $ name ] = 'PRIMARY KEY  (' . $ index_fields . ')' ; } else { $ return [ $ table ] [ 'TABLE_CREATE_DEFINITIONS' ] [ $ name ] = 'UNIQUE KEY `' . $ name . '` (' . $ index_fields . ')' ; } break ; case 'FULLTEXT' : $ return [ $ table ] [ 'TABLE_CREATE_DEFINITIONS' ] [ $ name ] = 'FULLTEXT KEY `' . $ name . '` (' . $ index_fields . ')' ; break ; default : $ return [ $ table ] [ 'TABLE_CREATE_DEFINITIONS' ] [ $ name ] = 'KEY `' . $ name . '` (' . $ index_fields . ')' ; break ; } unset ( $ field [ 'index_fields' ] ) ; unset ( $ field [ 'index' ] ) ; } } } // HOOK: allow third-party developers to modify the array (see #3281) if ( isset ( $ GLOBALS [ 'TL_HOOKS' ] [ 'sqlGetFromDB' ] ) && \ is_array ( $ GLOBALS [ 'TL_HOOKS' ] [ 'sqlGetFromDB' ] ) ) { foreach ( $ GLOBALS [ 'TL_HOOKS' ] [ 'sqlGetFromDB' ] as $ callback ) { $ this -> import ( $ callback [ 0 ] ) ; $ return = $ this -> { $ callback [ 0 ] } -> { $ callback [ 1 ] } ( $ return ) ; } } return $ return ; }
Get the current database structure

@return array An array of tables and fields
Get the current database structure