ARTICLE AD BOX
int MySQLDatabase::GetTableId(const std::wstring &UNUSED(catalog), const std::wstring &schema, const std::wstring &table, long &id, std::vector<std::wstring> &errors)
{
printf( "Schema in GetTableId is: %s\n\r", m_pimpl->m_myconv.to_bytes( schema.c_str() ).c_str() );
std::wstring query1 = L"SELECT st.table_id FROM information_schema.tables t, information_schema.innodb_tables st WHERE st.name = CONCAT(t.table_schema,'/',t.table_name) AND t.table_schema = ? AND t.table_name = ? AND t.engine = 'InnoDB';";
int result = 0;
long tableId = 0;
unsigned long str_length1 = 0, str_length2 = 0;
MYSQL_RES *prepare_meta_result = nullptr;
auto res1 = mysql_stmt_init( m_db );
if( !res1 )
{
std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_stmt_error( res1 ) );
errors.push_back( err );
result = 1;
}
if( !result )
{
if( mysql_stmt_prepare( res1, m_pimpl->m_myconv.to_bytes( query1.c_str() ).c_str(), query1.length() ) )
{
std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_stmt_error( res1 ) );
errors.push_back( err );
result = 1;
}
}
if( !result )
{
MYSQL_BIND params[2];
memset( params, 0, sizeof( params ) );
str_length1 = strlen( m_pimpl->m_myconv.to_bytes( schema.c_str() ).c_str() ) + 1;
str_length2 = strlen( m_pimpl->m_myconv.to_bytes( table.c_str() ).c_str() ) + 1;
std::unique_ptr<char[]> str_data1( new char[str_length1] );
std::unique_ptr<char[]> str_data2( new char[str_length2] );
memset( str_data1.get(), '\0', str_length1 );
memset( str_data2.get(), '\0', str_length2 );
snprintf( str_data1.get(), str_length1, "%s", m_pimpl->m_myconv.to_bytes( schema.c_str() ).c_str() );
snprintf( str_data2.get(), str_length2, "%s", m_pimpl->m_myconv.to_bytes( table.c_str() ).c_str() );
params[0].buffer_type = MYSQL_TYPE_STRING;
params[0].buffer = (char *) str_data1.get();
params[0].buffer_length = str_length1 - 1;
params[0].is_null = 0;
params[0].length = &str_length1 - 1;
params[1].buffer_type = MYSQL_TYPE_STRING;
params[1].buffer = (char *) str_data2.get();
params[1].buffer_length = str_length2 - 1;
params[1].is_null = 0;
params[1].length = &str_length2 - 1;
if( mysql_stmt_bind_param( res1, params ) )
{
std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_stmt_error( res1 ) );
errors.push_back( err );
result = 1;
}
if( !result )
{
if( mysql_stmt_execute( res1 ) )
{
std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_stmt_error( res1 ) );
errors.push_back( err );
result = 1;
}
}
if( !result )
{
prepare_meta_result = mysql_stmt_result_metadata( res1 );
if( !prepare_meta_result )
{
std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_stmt_error( res1 ) );
errors.push_back( err );
result = 1;
}
}
}
if( !result )
{
MYSQL_BIND results1[1];
bool is_null[1], error[1];
unsigned long length[1];
memset( results1, 0, sizeof( results1 ) );
results1[0].buffer_type = MYSQL_TYPE_LONG;
results1[0].buffer = (char *) &tableId;
results1[0].is_null = &is_null[0];
results1[0].error = &error[0];
results1[0].length = &length[0];
if( mysql_stmt_bind_result( res1, results1 ) )
{
std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_stmt_error( res1 ) );
errors.push_back( err );
result = 1;
}
if( !result )
{
if( mysql_stmt_store_result( res1 ) )
{
std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_stmt_error( res1 ) );
errors.push_back( err );
result = 1;
}
}
auto rows = mysql_stmt_num_rows( res1 );
if( !result )
{
auto fetch = mysql_stmt_fetch( res1 );
switch( fetch )
{
case 1:
{
std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_stmt_error( res1 ) );
errors.push_back( err );
result = 1;
}
break;
case 0:
printf( "Table id is: %ld", tableId );
id = tableId;
break;
case MYSQL_NO_DATA:
break;
case MYSQL_DATA_TRUNCATED:
errors.push_back( L"Data truncated" );
break;
}
}
mysql_free_result( prepare_meta_result );
}
if( !result )
{
if( mysql_stmt_close( res1 ) )
{
std::wstring err = m_pimpl->m_myconv.from_bytes( mysql_stmt_error( res1 ) );
errors.push_back( err );
result = 1;
}
}
return result;
}
I'm getting 0 in the id parameter, so I added couple of printf's in that function.
The first printf does print my schema name when I run that code.
Next I turned on logging on the MySQL.
And the log shows:
12 Execute SELECT st.table_id FROM information_schema.tables t, information_schema.innodb_tables st WHERE st.name = CONCAT(t.table_schema,'/',t.table_name) AND t.table_schema = '' AND t.table_name = 'league' AND t.engine = 'InnoDB'.How can it be? The second parameter converts fine with the same code, but not the first one. Please explain this.
MySQL version is 8.0.32. MySQL-connector version is 8.0.32.
