55use Exception ;
66use PDO ;
77use PDOException ;
8- use Utopia \Database \Exception as DatabaseException ;
98use Utopia \Database \Database ;
109use Utopia \Database \Document ;
11- use Utopia \Database \Exception \Duplicate ;
12- use Utopia \Database \Exception \Timeout ;
10+ use Utopia \Database \Exception as DatabaseException ;
11+ use Utopia \Database \Exception \Duplicate as DuplicateException ;
12+ use Utopia \Database \Exception \Timeout as TimeoutException ;
1313use Utopia \Database \Query ;
1414use Utopia \Database \Validator \Authorization ;
1515
@@ -641,7 +641,7 @@ public function deleteIndex(string $collection, string $id): bool
641641 * @return Document
642642 * @throws Exception
643643 * @throws PDOException
644- * @throws Duplicate
644+ * @throws DuplicateException
645645 */
646646 public function createDocument (string $ collection , Document $ document ): Document
647647 {
@@ -736,7 +736,7 @@ public function createDocument(string $collection, Document $document): Document
736736 switch ($ e ->getCode ()) {
737737 case 1062 :
738738 case 23000 :
739- throw new Duplicate ('Duplicated document: ' . $ e ->getMessage ());
739+ throw new DuplicateException ('Duplicated document: ' . $ e ->getMessage ());
740740
741741 default :
742742 throw $ e ;
@@ -758,7 +758,7 @@ public function createDocument(string $collection, Document $document): Document
758758 * @return Document
759759 * @throws Exception
760760 * @throws PDOException
761- * @throws Duplicate
761+ * @throws DuplicateException
762762 */
763763 public function updateDocument (string $ collection , Document $ document ): Document
764764 {
@@ -939,7 +939,7 @@ public function updateDocument(string $collection, Document $document): Document
939939 switch ($ e ->getCode ()) {
940940 case 1062 :
941941 case 23000 :
942- throw new Duplicate ('Duplicated document: ' . $ e ->getMessage ());
942+ throw new DuplicateException ('Duplicated document: ' . $ e ->getMessage ());
943943
944944 default :
945945 throw $ e ;
@@ -1058,12 +1058,11 @@ public function deleteDocument(string $collection, string $id): bool
10581058 * @param array<string> $orderTypes
10591059 * @param array<string, mixed> $cursor
10601060 * @param string $cursorDirection
1061- * @param int|null $timeout
10621061 * @return array<Document>
10631062 * @throws DatabaseException
1064- * @throws Timeout
1063+ * @throws TimeoutException
10651064 */
1066- public function find (string $ collection , array $ queries = [], ?int $ limit = 25 , ?int $ offset = null , array $ orderAttributes = [], array $ orderTypes = [], array $ cursor = [], string $ cursorDirection = Database::CURSOR_AFTER , ? int $ timeout = null ): array
1065+ public function find (string $ collection , array $ queries = [], ?int $ limit = 25 , ?int $ offset = null , array $ orderAttributes = [], array $ orderTypes = [], array $ cursor = [], string $ cursorDirection = Database::CURSOR_AFTER ): array
10671066 {
10681067 $ name = $ this ->filter ($ collection );
10691068 $ roles = Authorization::getRoles ();
@@ -1173,11 +1172,8 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
11731172
11741173 $ sql = $ this ->trigger (Database::EVENT_DOCUMENT_FIND , $ sql );
11751174
1176- if ($ timeout || static ::$ timeout ) {
1177- $ sql = $ this ->setTimeoutForQuery ($ sql , $ timeout ? $ timeout : static ::$ timeout );
1178- }
1179-
11801175 $ stmt = $ this ->getPDO ()->prepare ($ sql );
1176+
11811177 foreach ($ queries as $ query ) {
11821178 $ this ->bindConditionValue ($ stmt , $ query );
11831179 }
@@ -1257,7 +1253,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
12571253 * @throws Exception
12581254 * @throws PDOException
12591255 */
1260- public function count (string $ collection , array $ queries = [], ?int $ max = null , ? int $ timeout = null ): int
1256+ public function count (string $ collection , array $ queries = [], ?int $ max = null ): int
12611257 {
12621258 $ name = $ this ->filter ($ collection );
12631259 $ roles = Authorization::getRoles ();
@@ -1287,11 +1283,8 @@ public function count(string $collection, array $queries = [], ?int $max = null,
12871283
12881284 $ sql = $ this ->trigger (Database::EVENT_DOCUMENT_COUNT , $ sql );
12891285
1290- if ($ timeout || self ::$ timeout ) {
1291- $ sql = $ this ->setTimeoutForQuery ($ sql , $ timeout ? $ timeout : self ::$ timeout );
1292- }
1293-
12941286 $ stmt = $ this ->getPDO ()->prepare ($ sql );
1287+
12951288 foreach ($ queries as $ query ) {
12961289 $ this ->bindConditionValue ($ stmt , $ query );
12971290 }
@@ -1322,7 +1315,7 @@ public function count(string $collection, array $queries = [], ?int $max = null,
13221315 * @throws Exception
13231316 * @throws PDOException
13241317 */
1325- public function sum (string $ collection , string $ attribute , array $ queries = [], ?int $ max = null , ? int $ timeout = null ): int |float
1318+ public function sum (string $ collection , string $ attribute , array $ queries = [], ?int $ max = null ): int |float
13261319 {
13271320 $ name = $ this ->filter ($ collection );
13281321 $ roles = Authorization::getRoles ();
@@ -1352,10 +1345,6 @@ public function sum(string $collection, string $attribute, array $queries = [],
13521345
13531346 $ sql = $ this ->trigger (Database::EVENT_DOCUMENT_SUM , $ sql );
13541347
1355- if ($ timeout || self ::$ timeout ) {
1356- $ sql = $ this ->setTimeoutForQuery ($ sql , $ timeout ? $ timeout : self ::$ timeout );
1357- }
1358-
13591348 $ stmt = $ this ->getPDO ()->prepare ($ sql );
13601349
13611350 foreach ($ queries as $ query ) {
@@ -1583,35 +1572,42 @@ public function getSupportForTimeouts(): bool
15831572 }
15841573
15851574 /**
1586- * Returns Max Execution Time
1587- * @param string $sql
1575+ * Set max execution time
15881576 * @param int $milliseconds
1589- * @return string
1577+ * @param string $event
1578+ * @return void
1579+ * @throws DatabaseException
15901580 */
1591- protected function setTimeoutForQuery ( string $ sql , int $ milliseconds ): string
1581+ public function setTimeout ( int $ milliseconds , string $ event = Database:: EVENT_ALL ): void
15921582 {
15931583 if (!$ this ->getSupportForTimeouts ()) {
1594- return $ sql ;
1584+ return ;
1585+ }
1586+ if ($ milliseconds <= 0 ) {
1587+ throw new DatabaseException ('Timeout must be greater than 0 ' );
15951588 }
15961589
15971590 $ seconds = $ milliseconds / 1000 ;
1598- return "SET STATEMENT max_statement_time = {$ seconds } FOR " . $ sql ;
1591+
1592+ $ this ->before ($ event , 'timeout ' , function ($ sql ) use ($ seconds ) {
1593+ return "SET STATEMENT max_statement_time = {$ seconds } FOR " . $ sql ;
1594+ });
15991595 }
16001596
16011597 /**
16021598 * @param PDOException $e
1603- * @throws Timeout
1599+ * @throws TimeoutException
16041600 */
16051601 protected function processException (PDOException $ e ): void
16061602 {
16071603 // Regular PDO
16081604 if ($ e ->getCode () === '70100 ' && isset ($ e ->errorInfo [1 ]) && $ e ->errorInfo [1 ] === 1969 ) {
1609- throw new Timeout ($ e ->getMessage ());
1605+ throw new TimeoutException ($ e ->getMessage ());
16101606 }
16111607
16121608 // PDOProxy switches errorInfo PDOProxy.php line 64
16131609 if ($ e ->getCode () === 1969 && isset ($ e ->errorInfo [0 ]) && $ e ->errorInfo [0 ] === '70100 ' ) {
1614- throw new Timeout ($ e ->getMessage ());
1610+ throw new TimeoutException ($ e ->getMessage ());
16151611 }
16161612
16171613 throw $ e ;
0 commit comments