-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDatabaseUtility.php
More file actions
121 lines (112 loc) · 3.72 KB
/
DatabaseUtility.php
File metadata and controls
121 lines (112 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace In2code\Lux\Utility;
use Doctrine\DBAL\Exception as ExceptionDbal;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class DatabaseUtility
{
public static function getQueryBuilderForTable(string $tableName, bool $removeRestrictions = false): QueryBuilder
{
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
if ($removeRestrictions === true) {
$queryBuilder->getRestrictions()->removeAll();
}
return $queryBuilder;
}
public static function getConnectionForTable(string $tableName): Connection
{
return GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
}
/**
* @param string $tableName
* @return bool
* @throws ExceptionDbal
*/
public static function isTableExisting(string $tableName): bool
{
$existing = false;
$connection = self::getConnectionForTable($tableName);
$queryResult = $connection->query('show tables;')->fetchAll();
foreach ($queryResult as $tableProperties) {
if (in_array($tableName, array_values($tableProperties))) {
$existing = true;
break;
}
}
return $existing;
}
/**
* @param string $fieldName
* @param string $tableName
* @return bool
* @throws ExceptionDbal
*/
public static function isFieldExistingInTable(string $fieldName, string $tableName): bool
{
$found = false;
$connection = self::getConnectionForTable($tableName);
$queryResult = $connection->query('describe ' . $tableName . ';')->fetchAll();
foreach ($queryResult as $fieldProperties) {
if ($fieldProperties['Field'] === $fieldName) {
$found = true;
break;
}
}
return $found;
}
/**
* @param string $tableName
* @return bool
* @throws ExceptionDbal
*/
public static function isTableFilled(string $tableName): bool
{
$queryBuilder = self::getQueryBuilderForTable($tableName);
return $queryBuilder
->select('*')
->from($tableName)
->setMaxResults(1)
->executeQuery()
->fetchAssociative() > 0;
}
/**
* @param string $fieldName
* @param string $tableName
* @return bool
* @throws ExceptionDbal
*/
public static function isFieldInTableFilled(string $fieldName, string $tableName): bool
{
$queryBuilder = self::getQueryBuilderForTable($tableName);
return (string)$queryBuilder
->select($fieldName)
->from($tableName)
->setMaxResults(1)
->executeQuery()
->fetchOne() !== '';
}
/**
* @param string $fieldName
* @param string $tableName
* @return bool
* @throws ExceptionDbal
*/
public static function isAnyFieldFilledInTable(string $fieldName, string $tableName): bool
{
$queryBuilder = self::getQueryBuilderForTable($tableName);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$result = $queryBuilder
->select($fieldName)
->from($tableName)
->where($fieldName . ' = \'\'')
->setMaxResults(1)
->executeQuery()
->fetchOne();
return $result === false;
}
}