@@ -84,7 +84,7 @@ public function rollbackTransaction(): bool
8484 return $ result ;
8585 }
8686
87- private function execute (mixed $ stmt ): bool
87+ protected function execute (mixed $ stmt ): bool
8888 {
8989 $ pdo = $ this ->getPDO ();
9090
@@ -1042,160 +1042,6 @@ public function createDocument(string $collection, Document $document): Document
10421042 return $ document ;
10431043 }
10441044
1045- /**
1046- * Create Documents in batches
1047- *
1048- * @param string $collection
1049- * @param array<Document> $documents
1050- *
1051- * @return array<Document>
1052- *
1053- * @throws DuplicateException
1054- * @throws \Throwable
1055- */
1056- public function createDocuments (string $ collection , array $ documents ): array
1057- {
1058- if (empty ($ documents )) {
1059- return $ documents ;
1060- }
1061-
1062- try {
1063- $ name = $ this ->filter ($ collection );
1064-
1065- $ attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS ;
1066-
1067- $ hasSequence = null ;
1068- foreach ($ documents as $ document ) {
1069- $ attributes = $ document ->getAttributes ();
1070- $ attributeKeys = [...$ attributeKeys , ...\array_keys ($ attributes )];
1071-
1072- if ($ hasSequence === null ) {
1073- $ hasSequence = !empty ($ document ->getSequence ());
1074- } elseif ($ hasSequence == empty ($ document ->getSequence ())) {
1075- throw new DatabaseException ('All documents must have an sequence if one is set ' );
1076- }
1077- }
1078-
1079- $ attributeKeys = array_unique ($ attributeKeys );
1080-
1081- if ($ hasSequence ) {
1082- $ attributeKeys [] = '_id ' ;
1083- }
1084-
1085- if ($ this ->sharedTables ) {
1086- $ attributeKeys [] = '_tenant ' ;
1087- }
1088-
1089- $ columns = [];
1090- foreach ($ attributeKeys as $ key => $ attribute ) {
1091- $ columns [$ key ] = $ this ->quote ($ this ->filter ($ attribute ));
1092- }
1093-
1094- $ columns = '( ' . \implode (', ' , $ columns ) . ') ' ;
1095-
1096- $ bindIndex = 0 ;
1097- $ batchKeys = [];
1098- $ bindValues = [];
1099- $ permissions = [];
1100- $ documentIds = [];
1101- $ documentTenants = [];
1102-
1103- foreach ($ documents as $ index => $ document ) {
1104- $ attributes = $ document ->getAttributes ();
1105- $ attributes ['_uid ' ] = $ document ->getId ();
1106- $ attributes ['_createdAt ' ] = $ document ->getCreatedAt ();
1107- $ attributes ['_updatedAt ' ] = $ document ->getUpdatedAt ();
1108- $ attributes ['_permissions ' ] = \json_encode ($ document ->getPermissions ());
1109-
1110- if (!empty ($ document ->getSequence ())) {
1111- $ attributes ['_id ' ] = $ document ->getSequence ();
1112- } else {
1113- $ documentIds [] = $ document ->getId ();
1114- }
1115-
1116- if ($ this ->sharedTables ) {
1117- $ attributes ['_tenant ' ] = $ document ->getTenant ();
1118- $ documentTenants [] = $ document ->getTenant ();
1119- }
1120-
1121- $ bindKeys = [];
1122-
1123- foreach ($ attributeKeys as $ key ) {
1124- $ value = $ attributes [$ key ] ?? null ;
1125- if (\is_array ($ value )) {
1126- $ value = \json_encode ($ value );
1127- }
1128- $ value = (\is_bool ($ value )) ? (int )$ value : $ value ;
1129- $ bindKey = 'key_ ' . $ bindIndex ;
1130- $ bindKeys [] = ': ' . $ bindKey ;
1131- $ bindValues [$ bindKey ] = $ value ;
1132- $ bindIndex ++;
1133- }
1134-
1135- $ batchKeys [] = '( ' . \implode (', ' , $ bindKeys ) . ') ' ;
1136-
1137- foreach (Database::PERMISSIONS as $ type ) {
1138- foreach ($ document ->getPermissionsByType ($ type ) as $ permission ) {
1139- $ tenantBind = $ this ->sharedTables ? ", :_tenant_ {$ index }" : '' ;
1140- $ permission = \str_replace ('" ' , '' , $ permission );
1141- $ permission = "(' {$ type }', ' {$ permission }', :_uid_ {$ index } {$ tenantBind }) " ;
1142- $ permissions [] = $ permission ;
1143- }
1144- }
1145- }
1146-
1147- $ batchKeys = \implode (', ' , $ batchKeys );
1148-
1149- $ stmt = $ this ->getPDO ()->prepare ("
1150- INSERT INTO {$ this ->getSQLTable ($ name )} {$ columns }
1151- VALUES {$ batchKeys }
1152- " );
1153-
1154- foreach ($ bindValues as $ key => $ value ) {
1155- $ stmt ->bindValue ($ key , $ value , $ this ->getPDOType ($ value ));
1156- }
1157-
1158- $ this ->execute ($ stmt );
1159-
1160- if (!empty ($ permissions )) {
1161- $ tenantColumn = $ this ->sharedTables ? ', _tenant ' : '' ;
1162- $ permissions = \implode (', ' , $ permissions );
1163-
1164- $ sqlPermissions = "
1165- INSERT INTO {$ this ->getSQLTable ($ name . '_perms ' )} (_type, _permission, _document {$ tenantColumn })
1166- VALUES {$ permissions };
1167- " ;
1168-
1169- $ stmtPermissions = $ this ->getPDO ()->prepare ($ sqlPermissions );
1170-
1171- foreach ($ documents as $ index => $ document ) {
1172- $ stmtPermissions ->bindValue (":_uid_ {$ index }" , $ document ->getId ());
1173- if ($ this ->sharedTables ) {
1174- $ stmtPermissions ->bindValue (":_tenant_ {$ index }" , $ document ->getTenant ());
1175- }
1176- }
1177-
1178- $ this ->execute ($ stmtPermissions );
1179- }
1180-
1181- $ sequences = $ this ->getSequences (
1182- $ collection ,
1183- $ documentIds ,
1184- $ documentTenants
1185- );
1186-
1187- foreach ($ documents as $ document ) {
1188- if (isset ($ sequences [$ document ->getId ()])) {
1189- $ document ['$sequence ' ] = $ sequences [$ document ->getId ()];
1190- }
1191- }
1192- } catch (PDOException $ e ) {
1193- throw $ this ->processException ($ e );
1194- }
1195-
1196- return $ documents ;
1197- }
1198-
11991045 /**
12001046 * Update Document
12011047 *
0 commit comments