* Chuck Bell <Charles.Bell@stripped> [09/07/09 02:24]:
> +/**
> + Return a count of databases.
> +
> + This method returns the number of databases excluding the internal
> + databases 'mysql' and 'information_schema'.
> +
> + @Note This method executes based on the current context of the THD
> + and thus returns a value based on the visibility (i.e. SELECT
> + privilege) of the user context.
> +
> + @param[in] THD Current thread
> +
> + @returns uint Number of databases.
> +*/
> +uint get_num_dbs(THD *thd)
> +{
> + Ed_connection ed_connection(thd);
> + Ed_result_set *ed_result_set;
> + String_stream s_stream;
> +
> + s_stream <<
> + "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA " <<
> + "WHERE LCASE(schema_name) != 'mysql' AND " <<
> + "LCASE(schema_name) != 'information_schema'";
> +
> + if (run_service_interface_sql(thd, &ed_connection, s_stream.lex_string()))
> + /* Query failed with an error */
> + return NULL;
> + else if(ed_connection.get_warn_count())
> + /* Push warnings to BACKUP's error stack. Calling method will
> + decide if the warnings are a problem later when serializing the
> + object. */
> + thd->warning_info->append_warnings(thd, ed_connection.get_warn_list());
> +
> + DBUG_ASSERT(ed_connection.get_field_count());
> +
> + /* Use store_result to get ownership of result memory */
> + ed_result_set= ed_connection.store_result_set();
> + uint count= ed_result_set->size();
> + delete ed_result_set;
> + return count;
> +uint get_num_objects(THD *thd, const String *db_name)
> +{
> + Ed_connection ed_connection(thd);
> + Ed_result_set *ed_result_set;
> + String_stream s_stream;
> +
> + /*
> + Build query to select all objects from IS tables.
> + */
> + s_stream <<
> + "SELECT TABLE_NAME "
> + "FROM INFORMATION_SCHEMA.TABLES "
> + "WHERE table_schema = '" << db_name << "' UNION " <<
> + "SELECT TRIGGER_NAME "
> + "FROM INFORMATION_SCHEMA.TRIGGERS "
> + "WHERE trigger_schema = '" << db_name << "' UNION " <<
> + "SELECT ROUTINE_NAME "
> + "FROM INFORMATION_SCHEMA.ROUTINES "
> + "WHERE routine_schema = '" << db_name << "' UNION " <<
> + "SELECT EVENT_NAME "
> + "FROM INFORMATION_SCHEMA.EVENTS "
> + "WHERE event_schema = '" << db_name << "'";
> +
> + if (run_service_interface_sql(thd, &ed_connection, s_stream.lex_string()))
> + /* Query failed with an error */
> + return NULL;
> + else if(ed_connection.get_warn_count())
> + /* Push warnings to BACKUP's error stack. Calling method will
> + decide if the warnings are a problem later when serializing the
> + object. */
> + thd->warning_info->append_warnings(thd, ed_connection.get_warn_list());
> +
> + DBUG_ASSERT(ed_connection.get_field_count());
> +
> + /* Use store_result to get ownership of result memory */
> + ed_result_set= ed_connection.store_result_set();
> + uint count= ed_result_set->size();
> + delete ed_result_set;
> + return count;
> +}
Instead of checking the size of the result set here,
you could SELECT COUNT(*).
There is no way, however, to get
ed_result_set.get_row(0).get_column(0).to_int().
But I think Ed_result_set API could be extended
to support it in a fairly straightforward fashion.
It wasn't done right away since it was not used
anywhere, and we were a bit pressed for time :)
--