Commit 299a8319 authored by Helgi Hafþórsson's avatar Helgi Hafþórsson
Browse files

Added stylecop. Added Sample1.

parent f14cb2a2
......@@ -16,6 +16,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "codeAnalysis", "codeAnalysi
codeAnalysis\MarsonsoftRecommended.ruleset = codeAnalysis\MarsonsoftRecommended.ruleset
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{ED35E57D-7432-4DFB-99E3-9EAFD4812242}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample1", "samples\Sample1\Sample1.csproj", "{83B95ECA-576D-44F1-9FDA-31E582D61187}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -30,6 +34,10 @@ Global
{7D9B2CBD-DB01-4EE6-9743-68B0590D844D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7D9B2CBD-DB01-4EE6-9743-68B0590D844D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7D9B2CBD-DB01-4EE6-9743-68B0590D844D}.Release|Any CPU.Build.0 = Release|Any CPU
{83B95ECA-576D-44F1-9FDA-31E582D61187}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{83B95ECA-576D-44F1-9FDA-31E582D61187}.Debug|Any CPU.Build.0 = Debug|Any CPU
{83B95ECA-576D-44F1-9FDA-31E582D61187}.Release|Any CPU.ActiveCfg = Release|Any CPU
{83B95ECA-576D-44F1-9FDA-31E582D61187}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......@@ -37,5 +45,6 @@ Global
GlobalSection(NestedProjects) = preSolution
{5A8FB352-10B9-4EB2-9297-49C9A66A65CE} = {871C6815-F7D6-4747-8B65-F39063EF1DBF}
{7D9B2CBD-DB01-4EE6-9743-68B0590D844D} = {22873875-9BB7-423E-9B7B-E8565A69D525}
{83B95ECA-576D-44F1-9FDA-31E582D61187} = {ED35E57D-7432-4DFB-99E3-9EAFD4812242}
EndGlobalSection
EndGlobal
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Marsonsoft Recommended" Description="Based on the AllRules ruleset, excluding those deemed irrelvant." ToolsVersion="14.0">
<Include Path="AllRules.ruleset" Action="Default" />
<RuleSet Name="Marsonsoft Recommended" Description="Based on the AllRules ruleset, excluding those deemed irrelvant." ToolsVersion="15.0">
<Include Path="allrules.ruleset" Action="Default" />
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1000" Action="None" />
<Rule Id="CA1006" Action="None" />
......@@ -24,4 +24,17 @@
<Rule Id="CA2204" Action="None" />
<Rule Id="CA2210" Action="None" />
</Rules>
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
<Rule Id="SA1005" Action="None" />
<Rule Id="SA1101" Action="None" />
<Rule Id="SA1200" Action="None" />
<Rule Id="SA1311" Action="None" />
<Rule Id="SA1404" Action="None" />
<Rule Id="SA1501" Action="None" />
<Rule Id="SA1503" Action="None" />
<Rule Id="SA1518" Action="None" />
<Rule Id="SA1600" Action="None" />
<Rule Id="SA1633" Action="None" />
<Rule Id="SX1101" Action="Warning" />
</Rules>
</RuleSet>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="DefaultConnection" providerName="Oracle.ManagedDataAccess.Client" connectionString="Data Source=isgen01;User Id=SERV_PING_SUPPORT_USR;Password=F8QMHPKI578W;" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client"/>
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</DbProviderFactories>
</system.data>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<publisherPolicy apply="no"/>
<assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
<bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.122.1.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
\ No newline at end of file
using Marsonsoft.Database;
using Marsonsoft.Database.Oracle;
namespace Sample1
{
class Program
{
static void Main(string[] args)
{
var environmentProvider = DataEnvironmentProvider.Default;
//environmentProvider.AddOracle();
var dataEnvironment = environmentProvider.GetDefault();
using (var connection = dataEnvironment.ConnectionProvider.OpenConnection())
{
//...
}
}
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Sample1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Landsbankinn")]
[assembly: AssemblyProduct("Sample1")]
[assembly: AssemblyCopyright("Copyright © Landsbankinn 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("83b95eca-576d-44f1-9fda-31e582d61187")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{83B95ECA-576D-44F1-9FDA-31E582D61187}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Sample1</RootNamespace>
<AssemblyName>Sample1</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Oracle.ManagedDataAccess, Version=4.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=MSIL">
<HintPath>..\..\packages\Oracle.ManagedDataAccess.12.2.1100\lib\net40\Oracle.ManagedDataAccess.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Marsonsoft.Database\Marsonsoft.Database.csproj">
<Project>{5a8fb352-10b9-4eb2-9297-49c9a66a65ce}</Project>
<Name>Marsonsoft.Database</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Oracle.ManagedDataAccess" version="12.2.1100" targetFramework="net462" />
</packages>
\ No newline at end of file
......@@ -12,7 +12,7 @@ namespace Marsonsoft.Database
/// </summary>
public class DataEnvironmentProvider
{
private static readonly DataEnvironmentProvider defaultInstance = new DataEnvironmentProvider();
private static readonly Lazy<DataEnvironmentProvider> defaultInstanceLazy = new Lazy<DataEnvironmentProvider>(CreateDefaultEnvironmentProvider, false);
private readonly IConnectionStringInfoProvider connectionStringInfoProvider;
private readonly Dictionary<string, Type> dataEnvironmentDictionary = new Dictionary<string, Type>();
......@@ -42,7 +42,7 @@ namespace Marsonsoft.Database
{
get
{
return defaultInstance;
return defaultInstanceLazy.Value;
}
}
......@@ -58,7 +58,7 @@ namespace Marsonsoft.Database
}
/// <summary>
/// Adds an environment type to the list of types provided. The <see cref="DbProviderFactory"/> types supported are
/// Adds an environment type to the list of types provided. The <see cref="DbProviderFactory"/> types supported are
/// automatically detected by looking for the <see cref="ProviderSupportAttribute"/> attributes of the type.
/// </summary>
/// <param name="type">The environment type, implementing <see cref="IDataEnvironment"/>, that should be added.</param>
......@@ -73,36 +73,29 @@ namespace Marsonsoft.Database
}
/// <summary>
/// Adds an environment type to the list of types provided using the specified provider invariant name as an e
/// Adds an environment type to the list of types provided along with the provider invariant name of the <see cref="DbProviderFactory"/>
/// it supports.
/// </summary>
/// <param name="providerInvariantName"></param>
/// <param name="type"></param>
/// <param name="providerInvariantName">Name of the data provider (<see cref="DbProviderFactory"/>) to that <paramref name="type"/> supports.</param>
/// <param name="type">The environment type, implementing <see cref="IDataEnvironment"/>, that should be added.</param>
public void Add(string providerInvariantName, Type type)
{
ValidateTypeToAdd(type);
dataEnvironmentDictionary.Add(providerInvariantName, type);
}
private static void ValidateTypeToAdd(Type type)
{
if (!typeof(IDataEnvironment).IsAssignableFrom(type))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Type must be assignable to {0}.", nameof(IDataEnvironment)), nameof(type));
}
}
/// <summary>
/// Gets an environment for the specified data provider and connection string.
/// </summary>
/// <param name="dataProviderName">Name of the data provider (<see cref="DbProviderFactory"/>) to use.</param>
/// <param name="providerInvariantName">Name of the data provider (<see cref="DbProviderFactory"/>) to use.</param>
/// <param name="connectionString">The connection string to use.</param>
/// <returns>An object implementing <see cref="IDataEnvironment"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="connectionString"/> or <paramref name="connectionString"/></exception>
public IDataEnvironment GetByConnectionSetting(string dataProviderName, string connectionString)
public IDataEnvironment GetByConnectionSetting(string providerInvariantName, string connectionString)
{
if (dataProviderName == null)
if (providerInvariantName == null)
{
throw new ArgumentNullException(nameof(dataProviderName));
throw new ArgumentNullException(nameof(providerInvariantName));
}
if (connectionString == null)
......@@ -110,7 +103,7 @@ namespace Marsonsoft.Database
throw new ArgumentNullException(nameof(connectionString));
}
return CreateDataEnvironment(dataProviderName, connectionString);
return CreateDataEnvironment(providerInvariantName, connectionString);
}
/// <summary>
......@@ -153,6 +146,29 @@ namespace Marsonsoft.Database
return CreateDataEnvironment(info.ProviderName, info.ConnectionString);
}
private static DataEnvironmentProvider CreateDefaultEnvironmentProvider()
{
var defaultDataEnvironmentProvider = new DataEnvironmentProvider();
var dataEnvironmentTypes = GetAllTypesImplementingIDataEnvironment();
foreach (var dataEnvironmentType in dataEnvironmentTypes)
{
var providerNames = GetAllProviderNamesSupportedBy(dataEnvironmentType);
foreach (var providerName in providerNames)
{
defaultDataEnvironmentProvider.Add(providerName, dataEnvironmentType);
}
}
return defaultDataEnvironmentProvider;
}
private static IEnumerable<Type> GetAllTypesImplementingIDataEnvironment()
{
return from type in Assembly.GetExecutingAssembly().GetTypes()
where type.IsClass && !type.IsAbstract && type.GetInterfaces().Contains(typeof(IDataEnvironment))
select type;
}
private static IEnumerable<string> GetAllProviderNamesSupportedBy(Type dataEnvironmentType)
{
return
......@@ -160,6 +176,14 @@ namespace Marsonsoft.Database
select attribute.ProviderName;
}
private static void ValidateTypeToAdd(Type type)
{
if (!typeof(IDataEnvironment).IsAssignableFrom(type))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Type must be assignable to {0}.", nameof(IDataEnvironment)), nameof(type));
}
}
private IDataEnvironment CreateDataEnvironment(string providerInvariantName, string connectionString)
{
if (!dataEnvironmentDictionary.TryGetValue(providerInvariantName, out Type dataEnvironmentType))
......
......@@ -4,7 +4,7 @@ namespace Marsonsoft.Database
{
/// <summary>
/// Contains extension methods for the <see cref="IDbConnection"/> interface.
/// </summary>
/// </summary>
public static class DbConnectionExtensions
{
/// <summary>
......
namespace Marsonsoft.Database
{
/// <summary>
/// Represents the ability to create SQL elements.
/// </summary>
public interface ISqlFactory
{
/// <summary>
/// Represents the ability to create SQL elements.
/// </summary>
public interface ISqlFactory
{
/// <summary>
/// Gets or sets a value controlling if and how identifiers created by the class implementing this interface
/// are quoted.
......@@ -22,7 +22,7 @@
/// </remarks>
/// <param name="name">The name to represent as an SQL identifier.</param>
/// <returns>The <paramref name="name"/> as an SQL identifier.</returns>
string Identifier(string name);
string Identifier(string name);
/// <summary>
/// Returns the specified tble name as an identifier based on the rules set in <see cref="IdentifierQuoting"/>.
......@@ -37,11 +37,11 @@
/// <returns>The <paramref name="tableName"/> as an SQL identifier.</returns>
string Identifier(TableName tableName);
/// <summary>
/// Returns a valid SQL literal representation of the specified value.
/// </summary>
/// <param name="value">The value to get a literal for.</param>
/// <returns>A valid SQL literal.</returns>
string Literal(object value);
}
/// <summary>
/// Returns a valid SQL literal representation of the specified value.
/// </summary>
/// <param name="value">The value to get a literal for.</param>
/// <returns>A valid SQL literal.</returns>
string Literal(object value);
}
}
namespace Marsonsoft.Database
{
/// <summary>
/// Determines how identifiers are quoted.
/// </summary>
public enum IdentifierQuoting
{
/// <summary>
/// Only quote identifiers when syntax rules require it, for example when identifiers include spaces.
/// </summary>
OnlyWhenNeeded,
/// <summary>
/// Determines how identifiers are quoted.
/// </summary>
public enum IdentifierQuoting
{
/// <summary>
/// Only quote identifiers when syntax rules require it, for example when identifiers include spaces.
/// </summary>
OnlyWhenNeeded,
/// <summary>
/// Never quote identifiers.
/// </summary>
Never,
/// <summary>
/// Never quote identifiers.
/// </summary>
Never,
/// <summary>
/// Always quote identifiers.
/// </summary>
Always,
}
/// <summary>
/// Always quote identifiers.
/// </summary>
Always,
}
}
......@@ -85,5 +85,12 @@
<Compile Include="SqlFactoryBase.cs" />
<Compile Include="TableName.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\..\packages\StyleCop.Analyzers.1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
<Analyzer Include="..\..\packages\StyleCop.Analyzers.1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
......@@ -19,6 +19,7 @@ namespace Marsonsoft.Database.MySql
{
throw new ArgumentNullException(nameof(name));
}
return "`" + name + "`";
}
}
......
......@@ -2,20 +2,20 @@
namespace Marsonsoft.Database.Oracle
{
internal static class AdapterFactory
{
public static IAdapter GetAdapter(string typeName)
{
if (ManagedAdapter.Instance.IsMyType(typeName))
{
return ManagedAdapter.Instance;
}
else if (NativeAdapter.Instance.IsMyType(typeName))
{
return NativeAdapter.Instance;
}
internal static class AdapterFactory
{
public static IAdapter GetAdapter(string typeName)
{
if (ManagedAdapter.Instance.IsMyType(typeName))
{
return ManagedAdapter.Instance;
}
else if (NativeAdapter.Instance.IsMyType(typeName))
{
return NativeAdapter.Instance;
}
throw new ArgumentException("Unknown type name");
}
}
throw new ArgumentException("Unknown type name");
}
}
}
......@@ -5,22 +5,22 @@ namespace Marsonsoft.Database.Oracle
/// <summary>
/// Contains extensions methods for the <see cref="DataEnvironmentProvider"/> class.
/// </summary>
public static class DataEnvironmentProviderExtensions
{
public static class DataEnvironmentProviderExtensions
{
/// <summary>
/// Adds the <see cref="Oracle.DataEnvironment"/> to the data environment provider's list of environments.
/// </summary>
/// <param name="dataEnvironmentProvider">The <see cref="DataEnvironmentProvider"/>.</param>
public static void AddOracle(this DataEnvironmentProvider dataEnvironmentProvider)
{
try
{
dataEnvironmentProvider.Add(typeof(DataEnvironment));
}
catch (ArgumentException)
{
// Silently continue if type has already been added.
}
}
}
public static void AddOracle(this DataEnvironmentProvider dataEnvironmentProvider)
{
try
{
dataEnvironmentProvider.Add(typeof(DataEnvironment));
}
catch (ArgumentException)
{
// Silently continue if type has already been added.
}
}
}
}
......@@ -24,7 +24,7 @@ namespace Marsonsoft.Database.Oracle
public DbParameter CreateParameterAsRefCursor(string parameterName)
{
// This is the statement we would use if we were referencing Oracle.ManagedDataAccess:
// return new OracleParameter(parameterName, Oracle.ManagedDataAccess.Client.OracleDbType.RefCursor, ParameterDirection.Output);
// return new OracleParameter(parameterName, Oracle.ManagedDataAccess.Client.OracleDbType.RefCursor, ParameterDirection.Output);
// But this is what we do when we don't:
return (DbParameter)Activator.CreateInstance(oracleParameterType.Value, parameterName, Enum.ToObject(oracleDbType.Value, RefCursorIntValue), ParameterDirection.Output);
}
......
......@@ -24,7 +24,7 @@ namespace Marsonsoft.Database.Oracle
public DbParameter CreateParameterAsRefCursor(string parameterName)
{
// This is the statement we would use if we were referencing Oracle.DataAccess:
// return new OracleParameter(parameterName, Oracle.DataAccess.Client.OracleDbType.RefCursor, ParameterDirection.Output);
// return new OracleParameter(parameterName, Oracle.DataAccess.Client.OracleDbType.RefCursor, ParameterDirection.Output);
// But this is what we do when we don't:
return (DbParameter)Activator.CreateInstance(oracleParameterType.Value, Enum.ToObject(oracleDbType.Value, RefCursorIntValue), ParameterDirection.Output);
}
......
......@@ -21,6 +21,7 @@ namespace Marsonsoft.Database
{
throw new ArgumentNullException(nameof(name));
}
return name.Any(c => char.IsWhiteSpace(c) || c == '\'');
}
......@@ -36,6 +37,7 @@ namespace Marsonsoft.Database
{
throw new ArgumentNullException(nameof(name));
}
return "\"" + name + "\"";
}
}
......
......@@ -10,22 +10,6 @@ namespace Marsonsoft.Database
/// </summary>
public abstract class SqlFactoryBase : ISqlFactory
{
/// <summary>
/// Determines if the specified name, when used as an identifier in SQL, needs to be quoted.
/// </summary>
/// <param name="name">The name to check.</param>
/// <returns><c>true</c> if the name needs to be quoted, <c>false</c> otherwise.</returns>
/// <exception cref="ArgumentNullException">name</exception>
protected abstract bool IdentifierNeedsQuoting(string name);
/// <summary>
/// Returnes the specified name as a quoted identifier.
/// </summary>
/// <param name="name">The name to quote.</param>
/// <returns><paramref name="name"/> as a quoted identifier.</returns>
/// <exception cref="ArgumentNullException">name</exception>
protected abstract string QuoteIdentifier(string name);
/// <summary>
/// Gets or sets a value controlling if and how identifiers created by the class implementing this interface
/// are quoted.
......@@ -47,7 +31,7 @@ namespace Marsonsoft.Database
{
bool quote = false;