@@ -562,7 +562,7 @@ public function addTableAlias(string $table)
562562 /**
563563 * Executes the query against the database.
564564 *
565- * @return mixed
565+ * @return bool|object|resource
566566 */
567567 abstract protected function execute (string $ sql );
568568
@@ -882,7 +882,12 @@ public function table($tableName)
882882 */
883883 public function newQuery (): BaseBuilder
884884 {
885- return $ this ->table (', ' )->from ([], true );
885+ // save table aliases
886+ $ tempAliases = $ this ->aliasedTables ;
887+ $ builder = $ this ->table (', ' )->from ([], true );
888+ $ this ->aliasedTables = $ tempAliases ;
889+
890+ return $ builder ;
886891 }
887892
888893 /**
@@ -1405,10 +1410,41 @@ public function listTables(bool $constrainByPrefix = false)
14051410
14061411 /**
14071412 * Determine if a particular table exists
1413+ *
1414+ * @param bool $cached Whether to use data cache
14081415 */
1409- public function tableExists (string $ tableName ): bool
1416+ public function tableExists (string $ tableName, bool $ cached = true ): bool
14101417 {
1411- return in_array ($ this ->protectIdentifiers ($ tableName , true , false , false ), $ this ->listTables (), true );
1418+ if ($ cached === true ) {
1419+ return in_array ($ this ->protectIdentifiers ($ tableName , true , false , false ), $ this ->listTables (), true );
1420+ }
1421+
1422+ if (false === ($ sql = $ this ->_listTables (false , $ tableName ))) {
1423+ if ($ this ->DBDebug ) {
1424+ throw new DatabaseException ('This feature is not available for the database you are using. ' );
1425+ }
1426+
1427+ return false ;
1428+ }
1429+
1430+ $ tableExists = $ this ->query ($ sql )->getResultArray () !== [];
1431+
1432+ // if cache has been built already
1433+ if (! empty ($ this ->dataCache ['table_names ' ])) {
1434+ $ key = array_search (
1435+ strtolower ($ tableName ),
1436+ array_map ('strtolower ' , $ this ->dataCache ['table_names ' ]),
1437+ true
1438+ );
1439+
1440+ // table doesn't exist but still in cache - lets reset cache, it can be rebuilt later
1441+ // OR if table does exist but is not found in cache
1442+ if (($ key !== false && ! $ tableExists ) || ($ key === false && $ tableExists )) {
1443+ $ this ->resetDataCache ();
1444+ }
1445+ }
1446+
1447+ return $ tableExists ;
14121448 }
14131449
14141450 /**
@@ -1575,9 +1611,11 @@ abstract public function insertID();
15751611 /**
15761612 * Generates the SQL for listing tables in a platform-dependent manner.
15771613 *
1614+ * @param string|null $tableName If $tableName is provided will return only this table if exists.
1615+ *
15781616 * @return false|string
15791617 */
1580- abstract protected function _listTables (bool $ constrainByPrefix = false );
1618+ abstract protected function _listTables (bool $ constrainByPrefix = false , ? string $ tableName = null );
15811619
15821620 /**
15831621 * Generates a platform-specific query string so that the column names can be fetched.
0 commit comments