@@ -1365,249 +1365,6 @@ public function updateDocument(string $collection, string $id, Document $documen
13651365 return $ document ;
13661366 }
13671367
1368- /**
1369- * Update documents
1370- *
1371- * Updates all documents which match the given query.
1372- *
1373- * @param string $collection
1374- * @param Document $updates
1375- * @param array<Document> $documents
1376- *
1377- * @return int
1378- *
1379- * @throws DatabaseException
1380- */
1381- public function updateDocuments (string $ collection , Document $ updates , array $ documents ): int
1382- {
1383- if (empty ($ documents )) {
1384- return 0 ;
1385- }
1386-
1387- $ attributes = $ updates ->getAttributes ();
1388-
1389- if (!empty ($ updates ->getUpdatedAt ())) {
1390- $ attributes ['_updatedAt ' ] = $ updates ->getUpdatedAt ();
1391- }
1392-
1393- if (!empty ($ updates ->getPermissions ())) {
1394- $ attributes ['_permissions ' ] = json_encode ($ updates ->getPermissions ());
1395- }
1396-
1397- if (empty ($ attributes )) {
1398- return 0 ;
1399- }
1400-
1401- $ name = $ this ->filter ($ collection );
1402-
1403- $ internalIds = \array_map (fn ($ document ) => $ document ->getInternalId (), $ documents );
1404-
1405- $ bindIndex = 0 ;
1406- $ columns = '' ;
1407- foreach ($ attributes as $ attribute => $ value ) {
1408- $ column = $ this ->filter ($ attribute );
1409- $ columns .= "{$ this ->quote ($ column )} = :key_ {$ bindIndex }" ;
1410-
1411- if ($ attribute !== \array_key_last ($ attributes )) {
1412- $ columns .= ', ' ;
1413- }
1414-
1415- $ bindIndex ++;
1416- }
1417-
1418- $ sql = "
1419- UPDATE {$ this ->getSQLTable ($ name )}
1420- SET {$ columns }
1421- WHERE _id IN ( " . \implode (', ' , \array_map (fn ($ index ) => ":_id_ {$ index }" , \array_keys ($ internalIds ))) . ")
1422- {$ this ->getTenantQuery ($ collection )}
1423- " ;
1424- var_dump ($ this ->sharedTables );
1425- var_dump ($ sql );
1426- $ sql = $ this ->trigger (Database::EVENT_DOCUMENTS_UPDATE , $ sql );
1427- $ stmt = $ this ->getPDO ()->prepare ($ sql );
1428-
1429- if ($ this ->sharedTables ) {
1430- $ stmt ->bindValue (':_tenant ' , $ this ->tenant );
1431- }
1432-
1433- foreach ($ internalIds as $ id => $ value ) {
1434- $ stmt ->bindValue (":_id_ {$ id }" , $ value );
1435- }
1436-
1437- $ attributeIndex = 0 ;
1438- foreach ($ attributes as $ value ) {
1439- if (is_array ($ value )) {
1440- $ value = json_encode ($ value );
1441- }
1442-
1443- $ bindKey = 'key_ ' . $ attributeIndex ;
1444- $ value = (is_bool ($ value )) ? (int )$ value : $ value ;
1445- $ stmt ->bindValue (': ' . $ bindKey , $ value , $ this ->getPDOType ($ value ));
1446- $ attributeIndex ++;
1447- }
1448-
1449- $ stmt ->execute ();
1450- $ affected = $ stmt ->rowCount ();
1451-
1452- // Permissions logic
1453- if (!empty ($ updates ->getPermissions ())) {
1454- $ removeQueries = [];
1455- $ removeBindValues = [];
1456-
1457- $ addQuery = '' ;
1458- $ addBindValues = [];
1459-
1460- /* @var $document Document */
1461- foreach ($ documents as $ index => $ document ) {
1462- // Permissions logic
1463- $ sql = "
1464- SELECT _type, _permission
1465- FROM {$ this ->getSQLTable ($ name . '_perms ' )}
1466- WHERE _document = :_uid
1467- {$ this ->getTenantQuery ($ collection )}
1468- " ;
1469-
1470- $ sql = $ this ->trigger (Database::EVENT_PERMISSIONS_READ , $ sql );
1471-
1472- $ permissionsStmt = $ this ->getPDO ()->prepare ($ sql );
1473- $ permissionsStmt ->bindValue (':_uid ' , $ document ->getId ());
1474-
1475- if ($ this ->sharedTables ) {
1476- $ permissionsStmt ->bindValue (':_tenant ' , $ this ->tenant );
1477- }
1478-
1479- $ permissionsStmt ->execute ();
1480- $ permissions = $ permissionsStmt ->fetchAll ();
1481- $ permissionsStmt ->closeCursor ();
1482-
1483- $ initial = [];
1484- foreach (Database::PERMISSIONS as $ type ) {
1485- $ initial [$ type ] = [];
1486- }
1487-
1488- $ permissions = \array_reduce ($ permissions , function (array $ carry , array $ item ) {
1489- $ carry [$ item ['_type ' ]][] = $ item ['_permission ' ];
1490- return $ carry ;
1491- }, $ initial );
1492-
1493- // Get removed Permissions
1494- $ removals = [];
1495- foreach (Database::PERMISSIONS as $ type ) {
1496- $ diff = array_diff ($ permissions [$ type ], $ updates ->getPermissionsByType ($ type ));
1497- if (!empty ($ diff )) {
1498- $ removals [$ type ] = $ diff ;
1499- }
1500- }
1501-
1502- // Build inner query to remove permissions
1503- if (!empty ($ removals )) {
1504- foreach ($ removals as $ type => $ permissionsToRemove ) {
1505- $ bindKey = 'uid_ ' . $ index ;
1506- $ removeBindKeys [] = ':uid_ ' . $ index ;
1507- $ removeBindValues [$ bindKey ] = $ document ->getId ();
1508-
1509- $ removeQueries [] = "(
1510- _document = :uid_ {$ index }
1511- {$ this ->getTenantQuery ($ collection )}
1512- AND _type = ' {$ type }'
1513- AND _permission IN ( " . \implode (', ' , \array_map (function (string $ i ) use ($ permissionsToRemove , $ index , $ type , &$ removeBindKeys , &$ removeBindValues ) {
1514- $ bindKey = 'remove_ ' . $ type . '_ ' . $ index . '_ ' . $ i ;
1515- $ removeBindKeys [] = ': ' . $ bindKey ;
1516- $ removeBindValues [$ bindKey ] = $ permissionsToRemove [$ i ];
1517-
1518- return ': ' . $ bindKey ;
1519- }, \array_keys ($ permissionsToRemove ))) .
1520- ")
1521- ) " ;
1522- }
1523- }
1524-
1525- // Get added Permissions
1526- $ additions = [];
1527- foreach (Database::PERMISSIONS as $ type ) {
1528- $ diff = \array_diff ($ updates ->getPermissionsByType ($ type ), $ permissions [$ type ]);
1529- if (!empty ($ diff )) {
1530- $ additions [$ type ] = $ diff ;
1531- }
1532- }
1533-
1534- // Build inner query to add permissions
1535- if (!empty ($ additions )) {
1536- foreach ($ additions as $ type => $ permissionsToAdd ) {
1537- foreach ($ permissionsToAdd as $ i => $ permission ) {
1538- $ bindKey = 'uid_ ' . $ index ;
1539- $ addBindValues [$ bindKey ] = $ document ->getId ();
1540-
1541- $ bindKey = 'add_ ' . $ type . '_ ' . $ index . '_ ' . $ i ;
1542- $ addBindValues [$ bindKey ] = $ permission ;
1543-
1544- $ addQuery .= "(:uid_ {$ index }, ' {$ type }', : {$ bindKey }" ;
1545-
1546- if ($ this ->sharedTables ) {
1547- $ addQuery .= ", :_tenant) " ;
1548- } else {
1549- $ addQuery .= ") " ;
1550- }
1551-
1552- if ($ i !== \array_key_last ($ permissionsToAdd ) || $ type !== \array_key_last ($ additions )) {
1553- $ addQuery .= ', ' ;
1554- }
1555- }
1556- }
1557- if ($ index !== \array_key_last ($ documents )) {
1558- $ addQuery .= ', ' ;
1559- }
1560- }
1561- }
1562-
1563- if (!empty ($ removeQueries )) {
1564- $ removeQuery = \implode (' OR ' , $ removeQueries );
1565-
1566- $ stmtRemovePermissions = $ this ->getPDO ()->prepare ("
1567- DELETE
1568- FROM {$ this ->getSQLTable ($ name . '_perms ' )}
1569- WHERE ( {$ removeQuery })
1570- " );
1571-
1572- foreach ($ removeBindValues as $ key => $ value ) {
1573- $ stmtRemovePermissions ->bindValue ($ key , $ value , $ this ->getPDOType ($ value ));
1574- }
1575-
1576- if ($ this ->sharedTables ) {
1577- $ stmtRemovePermissions ->bindValue (':_tenant ' , $ this ->tenant );
1578- }
1579- $ stmtRemovePermissions ->execute ();
1580- }
1581-
1582- if (!empty ($ addQuery )) {
1583- $ sqlAddPermissions = "
1584- INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (`_document`, `_type`, `_permission`
1585- " ;
1586-
1587- if ($ this ->sharedTables ) {
1588- $ sqlAddPermissions .= ', `_tenant`) ' ;
1589- } else {
1590- $ sqlAddPermissions .= ') ' ;
1591- }
1592-
1593- $ sqlAddPermissions .= " VALUES {$ addQuery }" ;
1594-
1595- $ stmtAddPermissions = $ this ->getPDO ()->prepare ($ sqlAddPermissions );
1596-
1597- foreach ($ addBindValues as $ key => $ value ) {
1598- $ stmtAddPermissions ->bindValue ($ key , $ value , $ this ->getPDOType ($ value ));
1599- }
1600-
1601- if ($ this ->sharedTables ) {
1602- $ stmtAddPermissions ->bindValue (':_tenant ' , $ this ->tenant );
1603- }
1604-
1605- $ stmtAddPermissions ->execute ();
1606- }
1607- }
1608-
1609- return $ affected ;
1610- }
16111368
16121369 /**
16131370 * @param string $collection
0 commit comments