Skip to content

Commit

Permalink
Merge pull request #1522 from AlexKerman/master
Browse files Browse the repository at this point in the history
Make table alias accessible from MySqlDbColumn.
  • Loading branch information
bgrainger authored Oct 28, 2024
2 parents 5ec770b + 31c0852 commit e132a64
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
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

0 comments on commit e132a64

Please sign in to comment.