Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/MySqlConnector/MySqlDbColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ internal MySqlDbColumn(int ordinal, ColumnDefinitionPayload column, bool allowZe
}
NumericScale = column.Decimals;
ProviderType = mySqlDbType;
TableName = column.Table;
}

public MySqlDbType ProviderType { get; }

/// <summary>
/// Gets the name of the table that the column belongs to. This will be the alias if the table is aliased in the query.
/// </summary>
public string TableName { get; }
}
42 changes: 42 additions & 0 deletions tests/IntegrationTests/QueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,48 @@ public void GetColumnSchemaAfterNextResult()
Assert.False(reader.NextResult());
Assert.Empty(reader.GetColumnSchema());
}

[Fact]
public void GetColumnSchemaForTableAlias()
{
using var cmd = m_database.Connection.CreateCommand();
cmd.CommandText = """
drop table if exists column_schema_table2;
drop table if exists column_schema_table1;
create table column_schema_table1(id int not null primary key);
create table column_schema_table2(id int not null primary key,
table1_id1 int not null,
table1_id2 int not null,
foreign key (table1_id1) references column_schema_table1(id),
foreign key (table1_id2) references column_schema_table1(id));
insert into column_schema_table1(id) values(1),(2);
insert into column_schema_table2(id, table1_id1, table1_id2) values(3, 1, 2);
""";
cmd.ExecuteNonQuery();

cmd.CommandText = """
select t2.id as id2, t1a.id as ida, t1b.id as idb
from column_schema_table2 t2
join column_schema_table1 t1a on t2.table1_id1 = t1a.id
join column_schema_table1 t1b on t2.table1_id2 = t1b.id;
""";
using var reader = cmd.ExecuteReader();
Assert.True(reader.Read());
var schema = reader.GetColumnSchema();
Assert.Equal(3, schema.Count);
Assert.Equal("id2", schema[0].ColumnName);
Assert.Equal("ida", schema[1].ColumnName);
Assert.Equal("idb", schema[2].ColumnName);
Assert.Equal("id", schema[0].BaseColumnName);
Assert.Equal("id", schema[1].BaseColumnName);
Assert.Equal("id", schema[2].BaseColumnName);
Assert.Equal("column_schema_table2", schema[0].BaseTableName);
Assert.Equal("column_schema_table1", schema[1].BaseTableName);
Assert.Equal("column_schema_table1", schema[2].BaseTableName);
Assert.Equal("t2", ((MySqlDbColumn) schema[0]).TableName);
Assert.Equal("t1a", ((MySqlDbColumn) schema[1]).TableName);
Assert.Equal("t1b", ((MySqlDbColumn) schema[2]).TableName);
}
#endif

private void UseReaderWithoutDisposingThread(object obj)
Expand Down