Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Synonyms for tables/views/stored procedures #351

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Simple.Data.Ado/ProcedureExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public IEnumerable<ResultSet> Execute(IDictionary<string, object> suppliedParame
using (var command = cn.CreateCommand(_adapter.AdoOptions))
{
command.Transaction = transaction;
command.CommandText = procedure.QualifiedName;
command.CommandText = string.IsNullOrEmpty(procedure.AliasedProcedure) ? procedure.QualifiedName : procedure.AliasedProcedure;
command.CommandType = CommandType.StoredProcedure;
SetParameters(procedure, command, suppliedParameters);
try
Expand Down
4 changes: 2 additions & 2 deletions Simple.Data.Ado/Schema/DatabaseSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private string DefaultSchema
private TableCollection CreateTableCollection()
{
return new TableCollection(_schemaProvider.GetTables()
.Select(table => new Table(table.ActualName, table.Schema, table.Type, this)));
.Select(table => new Table(table.ActualName, table.Schema, table.Type, this, table.AliasedTable)));
}

private ProcedureCollection CreateProcedureCollection()
Expand All @@ -107,7 +107,7 @@ private ProcedureCollection CreateProcedureCollection()
.Select(
proc =>
new Procedure(proc.Name, proc.SpecificName, proc.Schema,
this)), _schemaProvider.GetDefaultSchema());
this, proc.AliasedProcedure)), _schemaProvider.GetDefaultSchema());
}

public string QuoteObjectName(string unquotedName)
Expand Down
12 changes: 10 additions & 2 deletions Simple.Data.Ado/Schema/Procedure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,26 @@ public class Procedure
private readonly string _name;
private readonly string _specificName;
private readonly string _schema;
private readonly string _aliasedProc;
private readonly Lazy<ParameterCollection> _lazyParameters;

public Procedure(string name, string specificName, string schema)
public Procedure(string name, string specificName, string schema, string aliasedProc = "")
{
_name = name;
_specificName = specificName;
_schema = schema.NullIfWhitespace();
_aliasedProc = aliasedProc;
_lazyParameters = new Lazy<ParameterCollection>(() => new ParameterCollection(GetParameters()));
}

internal Procedure(string name, string specificName, string schema, DatabaseSchema databaseSchema)
internal Procedure(string name, string specificName, string schema, DatabaseSchema databaseSchema, string aliasedProc = "")
{
_name = name;
_specificName = specificName;
_schema = schema.NullIfWhitespace();
_lazyParameters = new Lazy<ParameterCollection>(() => new ParameterCollection(GetParameters()));
_databaseSchema = databaseSchema;
_aliasedProc = aliasedProc;
}

private IEnumerable<Parameter> GetParameters()
Expand All @@ -52,6 +55,11 @@ public string Name
get { return _name; }
}

public string AliasedProcedure
{
get { return _aliasedProc; }
}

public ParameterCollection Parameters
{
get { return _lazyParameters.Value; }
Expand Down
14 changes: 11 additions & 3 deletions Simple.Data.Ado/Schema/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ public class Table : IEquatable<Table>
private readonly string _actualName;
private readonly string _schema;
private readonly TableType _type;
private readonly string _aliasedTable;
private readonly DatabaseSchema _databaseSchema;
private readonly Lazy<ColumnCollection> _lazyColumns;
private readonly Lazy<Key> _lazyPrimaryKey;
private readonly Lazy<ForeignKeyCollection> _lazyForeignKeys;

public Table(string name, string schema, TableType type)
public Table(string name, string schema, TableType type, string aliasedTable = "")
{
_actualName = name;
_schema = string.IsNullOrWhiteSpace(schema) ? null : schema;
_type = type;
_aliasedTable = aliasedTable;
}

internal Table(string name, string schema, TableType type, DatabaseSchema databaseSchema)
internal Table(string name, string schema, TableType type, DatabaseSchema databaseSchema, string aliasedTable = "")
{
_actualName = name;
_databaseSchema = databaseSchema;
Expand All @@ -32,6 +34,7 @@ internal Table(string name, string schema, TableType type, DatabaseSchema databa
_lazyColumns = new Lazy<ColumnCollection>(GetColumns);
_lazyPrimaryKey = new Lazy<Key>(GetPrimaryKey);
_lazyForeignKeys = new Lazy<ForeignKeyCollection>(GetForeignKeys);
_aliasedTable = aliasedTable;
}

public TableType Type
Expand Down Expand Up @@ -74,6 +77,11 @@ public string QualifiedName
}
}

public string AliasedTable
{
get { return _aliasedTable; }
}

public IEnumerable<Column> Columns
{
get { return _lazyColumns.Value.AsEnumerable(); }
Expand Down Expand Up @@ -195,7 +203,7 @@ public bool Equals(Table other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other._actualName, _actualName) && Equals(other._schema, _schema) && Equals(other._type, _type);
return Equals(other._actualName, _actualName) && Equals(other._schema, _schema) && Equals(other._type, _type) && Equals(other._aliasedTable, _aliasedTable);
}

/// <summary>
Expand Down
34 changes: 33 additions & 1 deletion Simple.Data.SqlServer/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Simple.Data.SqlServer/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,10 @@
<data name="PrimaryKeySql" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\PrimaryKeySql.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="RemoteForeignKeysSql" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\RemoteForeignKeysSql.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
</data>
<data name="RemotePrimaryKeySql" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\RemotePrimaryKeySql.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
</root>
6 changes: 6 additions & 0 deletions Simple.Data.SqlServer/Simple.Data.SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@
<ItemGroup>
<None Include="Resources\ForeignKeysSql.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\RemotePrimaryKeySql.txt" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\RemoteForeignKeysSql.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading