@@ -49,27 +49,47 @@ public function create(string $name): bool
4949
5050 /**
5151 * Check if database exists
52+ * Optionally check if collection exists in database
5253 *
53- * @param string $name
54+ * @param string $database database name
55+ * @param string $collection (optional) collection name
5456 *
5557 * @return bool
5658 */
57- public function exists (string $ name ): bool
59+ public function exists (string $ database , string $ collection = null ): bool
5860 {
59- $ name = $ this ->filter ($ name );
61+ $ database = $ this ->filter ($ database );
62+
63+ if (!\is_null ($ collection )) {
64+ $ collection = $ this ->filter ($ collection );
65+
66+ $ select = 'TABLE_NAME ' ;
67+ $ from = 'INFORMATION_SCHEMA.TABLES ' ;
68+ $ where = 'TABLE_SCHEMA = :schema AND TABLE_NAME = :table ' ;
69+ $ match = "{$ this ->getNamespace ()}_ {$ collection }" ;
70+ } else {
71+ $ select = 'SCHEMA_NAME ' ;
72+ $ from = 'INFORMATION_SCHEMA.SCHEMATA ' ;
73+ $ where = 'SCHEMA_NAME = :schema ' ;
74+ $ match = $ database ;
75+ }
6076
6177 $ stmt = $ this ->getPDO ()
62- ->prepare ("SELECT SCHEMA_NAME
63- FROM INFORMATION_SCHEMA.SCHEMATA
64- WHERE SCHEMA_NAME = :schema; " );
65-
66- $ stmt ->bindValue (':schema ' , $ name , PDO ::PARAM_STR );
78+ ->prepare ("SELECT {$ select }
79+ FROM {$ from }
80+ WHERE {$ where }; " );
81+
82+ $ stmt ->bindValue (':schema ' , $ database , PDO ::PARAM_STR );
83+
84+ if (!\is_null ($ collection )) {
85+ $ stmt ->bindValue (':table ' , "{$ this ->getNamespace ()}_ {$ collection }" , PDO ::PARAM_STR );
86+ }
6787
6888 $ stmt ->execute ();
6989
7090 $ document = $ stmt ->fetch (PDO ::FETCH_ASSOC );
7191
72- return (($ document [' SCHEMA_NAME ' ] ?? '' ) == $ name );
92+ return (($ document [$ select ] ?? '' ) === $ match );
7393 }
7494
7595 /**
0 commit comments