You've already forked linux-packaging-mono
Imported Upstream version 5.2.0.179
Former-commit-id: a536d4f20e27294d8bbc2184d75f3a22364f7ba1
This commit is contained in:
parent
966bba02bb
commit
fad71374d0
9
external/entityframework/samples/Provider/FunctionStubGenerator/App.config
vendored
Normal file
9
external/entityframework/samples/Provider/FunctionStubGenerator/App.config
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0"?>
|
||||
<configuration>
|
||||
<system.data>
|
||||
<DbProviderFactories>
|
||||
<clear />
|
||||
<add name="Sample Entity Framework Provider" invariant="SampleEntityFrameworkProvider" description="Sample Entity Framework Provider" type="SampleEntityFrameworkProvider.SampleFactory, SampleEntityFrameworkProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=095b6aea3a01b8e0"/>
|
||||
</DbProviderFactories>
|
||||
</system.data>
|
||||
</configuration>
|
||||
485
external/entityframework/samples/Provider/FunctionStubGenerator/FunctionStubFileWriter.cs
vendored
Normal file
485
external/entityframework/samples/Provider/FunctionStubGenerator/FunctionStubFileWriter.cs
vendored
Normal file
@@ -0,0 +1,485 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
|
||||
using System;
|
||||
using System.Data.Spatial;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Metadata.Edm;
|
||||
|
||||
namespace LinqFunctionStubsGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Class that writes the files using the input function metadata.
|
||||
/// </summary>
|
||||
class FunctionStubFileWriter
|
||||
{
|
||||
private readonly IEnumerable<EdmFunction> _functions;
|
||||
private readonly Dictionary<String, String> _funcDictionaryToUse;
|
||||
private readonly Dictionary<String, String> _paramDictionaryToUse;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes member fields.
|
||||
/// </summary>
|
||||
/// <param name="functions">Metadata about functions</param>
|
||||
/// <param name="functionNames">Dictionary containing better function names</param>
|
||||
/// <param name="parameterNames">Dictionary containing better parameter names</param>
|
||||
public FunctionStubFileWriter(IEnumerable<EdmFunction> functions, Dictionary<String, String> functionNames, Dictionary<String, String> parameterNames)
|
||||
{
|
||||
_functions = functions;
|
||||
_funcDictionaryToUse = functionNames;
|
||||
_paramDictionaryToUse = parameterNames;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates code and writes to the specified file.
|
||||
/// </summary>
|
||||
/// <param name="destinationFile">Filepath of the destination file</param>
|
||||
/// <param name="namespaceString">Namespace where the class will reside</param>
|
||||
/// <param name="className">Generated class name</param>
|
||||
/// <param name="attributeNamespace">The 'EdmFunction' attribute parameter</param>
|
||||
/// <param name="pascalCaseFunctionNames">If the input function names need to be pascal cased.</param>
|
||||
public void GenerateToFile(String destinationFile, string namespaceString, string className, string attributeNamespace, Boolean pascalCaseFunctionNames)
|
||||
{
|
||||
//Use passed in class information to generate the class definition.
|
||||
StringWriter newCode = GenerateCode(namespaceString, className, attributeNamespace, pascalCaseFunctionNames);
|
||||
|
||||
//Write to file.
|
||||
try
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(destinationFile, false))
|
||||
{
|
||||
writer.Write(newCode.ToString());
|
||||
writer.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Main code generator method.
|
||||
/// </summary>
|
||||
/// <param name="namespaceString">Namespace where the class will reside</param>
|
||||
/// <param name="className">Generated class name</param>
|
||||
/// <param name="attributeNamespace">The 'EdmFunction' attribute parameter</param>
|
||||
/// <param name="pascalCaseFunctionNames">If the input function names need to be pascal cased.</param>
|
||||
public StringWriter GenerateCode(string namespaceString, string className, string attributeNamespace, Boolean pascalCaseFunctionNames)
|
||||
{
|
||||
StringWriter newCode = new StringWriter();
|
||||
Boolean isAggregateFunction;
|
||||
bool hasSByteParameterOrReturnType;
|
||||
bool hasStringInParameterName;
|
||||
String separator;
|
||||
|
||||
GenerateFileHeader(newCode, className);
|
||||
GenerateUsingStatements(newCode);
|
||||
|
||||
newCode.WriteLine("namespace " + namespaceString);
|
||||
newCode.WriteLine("{");
|
||||
|
||||
GenerateClassHeader(newCode, className, attributeNamespace);
|
||||
|
||||
foreach (System.Data.Metadata.Edm.EdmFunction function in _functions)
|
||||
{
|
||||
isAggregateFunction = false;
|
||||
hasSByteParameterOrReturnType = false;
|
||||
hasStringInParameterName = false;
|
||||
separator = "";
|
||||
|
||||
String functionNameToUse = FindCorrectFunctionName(function.Name, pascalCaseFunctionNames);
|
||||
GenerateFunctionHeader(newCode,attributeNamespace,function.Name);
|
||||
Type returnType = ((PrimitiveType)(function.ReturnParameter.TypeUsage.EdmType)).ClrEquivalentType;
|
||||
|
||||
//Suppress warning that 'SByte' is not CLS-compliant.
|
||||
if (returnType == typeof(SByte))
|
||||
{
|
||||
hasSByteParameterOrReturnType = true;
|
||||
}
|
||||
StringBuilder functionSignatureString = new StringBuilder();
|
||||
AppendSpaces(functionSignatureString, 8);
|
||||
functionSignatureString.Append("public static ");
|
||||
WriteType(functionSignatureString, returnType);
|
||||
functionSignatureString.Append(functionNameToUse + "(");
|
||||
|
||||
ReadOnlyMetadataCollection<FunctionParameter> functionParameters = function.Parameters;
|
||||
Type parameterType;
|
||||
foreach (System.Data.Metadata.Edm.FunctionParameter parameter in functionParameters)
|
||||
{
|
||||
String parameterNameToUse = parameter.Name;
|
||||
parameterNameToUse = FindCorrectParameterName(parameterNameToUse);
|
||||
|
||||
//Detect aggregate functions. They have just one parameter and so stub can be generated here.
|
||||
if (parameter.TypeUsage.EdmType.GetType() == typeof(System.Data.Metadata.Edm.CollectionType))
|
||||
{
|
||||
isAggregateFunction = true;
|
||||
if (parameterNameToUse.ToLowerInvariant().Contains("string"))
|
||||
{
|
||||
hasStringInParameterName = true;
|
||||
}
|
||||
|
||||
System.Data.Metadata.Edm.CollectionType collectionType = (System.Data.Metadata.Edm.CollectionType)parameter.TypeUsage.EdmType;
|
||||
parameterType = ((PrimitiveType)(collectionType.TypeUsage.EdmType)).ClrEquivalentType;
|
||||
//Detect if there is an 'SByte' parameter to suppress non-CLS-compliance warning.
|
||||
//Generate the attribute only once for each function.
|
||||
if (parameterType == typeof(SByte))
|
||||
{
|
||||
hasSByteParameterOrReturnType = true;
|
||||
}
|
||||
|
||||
//Generate stub for non-nullable input parameters
|
||||
functionSignatureString.Append("IEnumerable<" + parameterType.ToString());
|
||||
//Supress fxcop message and CLS non-compliant attributes
|
||||
GenerateFunctionAttributes(newCode, hasStringInParameterName, hasSByteParameterOrReturnType);
|
||||
//Use the constructed function signature
|
||||
newCode.Write(functionSignatureString.ToString());
|
||||
GenerateAggregateFunctionStub(newCode,parameterType, returnType, parameterNameToUse, false);
|
||||
|
||||
//Generate stub for nullable input parameters
|
||||
//Special Case: Do not generate nullable stub for input parameter of types Byte[]
|
||||
//and String, since they are nullable.
|
||||
if (!IsNullableType(parameterType))
|
||||
{
|
||||
GenerateFunctionHeader(newCode, attributeNamespace, function.Name);
|
||||
//Supress fxcop message and CLS non-compliant attributes
|
||||
GenerateFunctionAttributes(newCode, hasStringInParameterName, hasSByteParameterOrReturnType);
|
||||
//Use the constructed function signature
|
||||
newCode.Write(functionSignatureString.ToString());
|
||||
GenerateAggregateFunctionStub(newCode, parameterType, returnType, parameterNameToUse, true);
|
||||
}
|
||||
} //End of processing parameters for aggregate functions.
|
||||
//Process each parameter in case of non-aggregate functions.
|
||||
else
|
||||
{
|
||||
parameterType = ((PrimitiveType)(parameter.TypeUsage.EdmType)).ClrEquivalentType;
|
||||
functionSignatureString.Append(separator);
|
||||
WriteType(functionSignatureString, parameterType);
|
||||
functionSignatureString.Append(parameterNameToUse);
|
||||
separator = ", ";
|
||||
//Detect if there is an 'SByte' parameter to suppress non-CLS-compliance warning.
|
||||
if (parameterType == typeof(SByte))
|
||||
{
|
||||
hasSByteParameterOrReturnType = true;
|
||||
}
|
||||
if (parameterNameToUse.ToLowerInvariant().Contains("string"))
|
||||
{
|
||||
hasStringInParameterName = true;
|
||||
}
|
||||
}
|
||||
} //End for each parameter
|
||||
|
||||
//Generate stub for Non-aggregate functions after all input parameters are found.
|
||||
if (!isAggregateFunction)
|
||||
{
|
||||
//Supress fxcop supression and CLS non-compliant attributes
|
||||
GenerateFunctionAttributes(newCode, hasStringInParameterName, hasSByteParameterOrReturnType);
|
||||
newCode.WriteLine(functionSignatureString.ToString() + ")");
|
||||
AppendSpaces(newCode, 8);
|
||||
newCode.WriteLine("{");
|
||||
WriteExceptionStatement(newCode);
|
||||
}
|
||||
} //End for each function
|
||||
|
||||
AppendSpaces(newCode, 4);
|
||||
newCode.WriteLine("}");
|
||||
newCode.WriteLine("}");
|
||||
newCode.Close();
|
||||
return newCode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates the content for aggregate function stubs.
|
||||
/// </summary>
|
||||
/// <param name="newCode">Buffer used to store the code constructed so far</param>
|
||||
/// <param name="parameterType">Type of parameter</param>
|
||||
/// <param name="returnType">Return type of function</param>
|
||||
/// <param name="parameterNameToUse">Parameter name</param>
|
||||
/// <param name="isNullable">If this invokation is for generating the nullable/non-nullable stub</param>
|
||||
private void GenerateAggregateFunctionStub(StringWriter newCode, Type parameterType, Type returnType, string parameterNameToUse, bool isNullable)
|
||||
{
|
||||
GenerateQuestionMark(newCode, isNullable);
|
||||
newCode.Write("> ");
|
||||
newCode.WriteLine(parameterNameToUse + ")");
|
||||
AppendSpaces(newCode, 8);
|
||||
newCode.WriteLine("{");
|
||||
AppendSpaces(newCode, 12);
|
||||
newCode.Write("ObjectQuery<" + parameterType.ToString());
|
||||
GenerateQuestionMark(newCode, isNullable);
|
||||
newCode.Write("> objectQuerySource = " + parameterNameToUse);
|
||||
newCode.Write(" as ObjectQuery<" + parameterType.ToString());
|
||||
GenerateQuestionMark(newCode, isNullable);
|
||||
newCode.WriteLine(">;");
|
||||
|
||||
AppendSpaces(newCode, 12);
|
||||
newCode.WriteLine("if (objectQuerySource != null)");
|
||||
AppendSpaces(newCode, 12);
|
||||
newCode.WriteLine("{");
|
||||
AppendSpaces(newCode, 16);
|
||||
newCode.Write("return ((IQueryable)objectQuerySource).Provider.Execute<" + returnType.ToString());
|
||||
|
||||
//Special case: Byte[], String are nullable
|
||||
if (!IsNullableType(returnType))
|
||||
{
|
||||
newCode.Write("?");
|
||||
}
|
||||
newCode.Write(">(Expression.Call((MethodInfo)MethodInfo.GetCurrentMethod(),Expression.Constant(" + parameterNameToUse);
|
||||
newCode.WriteLine(")));");
|
||||
AppendSpaces(newCode, 12);
|
||||
newCode.WriteLine("}");
|
||||
WriteExceptionStatement(newCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a question mark in generated code for nullable parameters
|
||||
/// </summary>
|
||||
/// <param name="newCode">Buffer used to store the code constructed so far</param>
|
||||
/// <param name="isNullable">Is this invokation for generating the nullable/non-nullable stub</param>
|
||||
public void GenerateQuestionMark(StringWriter newCode, bool isNullable)
|
||||
{
|
||||
if (isNullable)
|
||||
{
|
||||
newCode.Write("?");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates fxcop suppression and CLS non-compliant attributes.
|
||||
/// </summary>
|
||||
/// <param name="newCode"></param>
|
||||
/// <param name="hasStringInParameterName"></param>
|
||||
/// <param name="hasSByteParameterOrReturnType"></param>
|
||||
private void GenerateFunctionAttributes(StringWriter newCode, bool hasStringInParameterName, bool hasSByteParameterOrReturnType)
|
||||
{
|
||||
//Supress fxcop message about 'string' in argument names.
|
||||
if (hasStringInParameterName)
|
||||
{
|
||||
GenerateFxcopSuppressionAttribute(newCode);
|
||||
}
|
||||
//Suppress warning that 'SByte' is not CLS-compliant, generate the attribute only once.
|
||||
if (hasSByteParameterOrReturnType)
|
||||
{
|
||||
GenerateSByteCLSNonComplaintAttribute(newCode);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates the output file header.
|
||||
/// </summary>
|
||||
/// <param name="newCode">Buffer used to store the code constructed so far</param>
|
||||
/// <param name="className">Generated class name</param>
|
||||
private void GenerateFileHeader(StringWriter newCode, String className)
|
||||
{
|
||||
DateTime theTime = DateTime.Now;
|
||||
newCode.WriteLine("//------------------------------------------------------------------------------");
|
||||
newCode.WriteLine("// <auto-generated>");
|
||||
newCode.WriteLine("// This code was generated by a tool.");
|
||||
newCode.Write("// Generation date and time : ");
|
||||
newCode.WriteLine(theTime.Date.ToShortDateString() + " " + theTime.TimeOfDay.ToString());
|
||||
newCode.WriteLine("//");
|
||||
newCode.WriteLine("// Changes to this file will be lost if the code is regenerated.");
|
||||
newCode.WriteLine("// </auto-generated>");
|
||||
newCode.WriteLine("//------------------------------------------------------------------------------");
|
||||
newCode.WriteLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates 'using' statements in the output C# file.
|
||||
/// </summary>
|
||||
/// <param name="newCode">Buffer used to store the code constructed so far</param>
|
||||
private void GenerateUsingStatements(StringWriter newCode)
|
||||
{
|
||||
newCode.WriteLine(@"using System;");
|
||||
newCode.WriteLine(@"using System.Collections.Generic;");
|
||||
newCode.WriteLine(@"using System.Data.Objects;");
|
||||
newCode.WriteLine(@"using System.Data.Objects.DataClasses;");
|
||||
newCode.WriteLine(@"using System.Linq;");
|
||||
newCode.WriteLine(@"using System.Linq.Expressions;");
|
||||
newCode.WriteLine(@"using System.Reflection;");
|
||||
newCode.WriteLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates the function header comment and 'EdmFunction' attribute for every function.
|
||||
/// </summary>
|
||||
/// <param name="newCode">Buffer used to store the code constructed so far</param>
|
||||
/// <param name="attributeNamespace">Namespace parameter to the 'EdmFunction' attribute</param>
|
||||
/// <param name="functionName">Function name</param>
|
||||
private void GenerateFunctionHeader(StringWriter newCode, String attributeNamespace, String functionName)
|
||||
{
|
||||
AppendSpaces(newCode, 8);
|
||||
newCode.WriteLine("/// <summary>");
|
||||
AppendSpaces(newCode, 8);
|
||||
newCode.WriteLine("/// Proxy for the function " + attributeNamespace + "." + functionName);
|
||||
AppendSpaces(newCode, 8);
|
||||
newCode.WriteLine("/// </summary>");
|
||||
AppendSpaces(newCode, 8);
|
||||
newCode.WriteLine("[EdmFunction(\""+attributeNamespace+"\", \""+functionName+"\")]");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the function attribute to suppress warnings about non-CLS compliance due
|
||||
/// to SByte arguments.
|
||||
/// </summary>
|
||||
/// <param name="newCode">Buffer used to store the code constructed so far</param>
|
||||
private void GenerateSByteCLSNonComplaintAttribute(StringWriter newCode)
|
||||
{
|
||||
AppendSpaces(newCode, 8);
|
||||
newCode.WriteLine("[CLSCompliant(false)]");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the function attribute to suppress 'fxcop' errors about argument names
|
||||
/// like 'string1','characterString', etc.
|
||||
/// </summary>
|
||||
/// <param name="newCode">Buffer used to store the code constructed so far</param>
|
||||
private void GenerateFxcopSuppressionAttribute(StringWriter newCode)
|
||||
{
|
||||
AppendSpaces(newCode, 8);
|
||||
newCode.WriteLine("[System.Diagnostics.CodeAnalysis.SuppressMessage(\"Microsoft.Naming\", \"CA1720:IdentifiersShouldNotContainTypeNames\", MessageId = \"string\")]");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns if the given data type is nullable.
|
||||
/// </summary>
|
||||
/// <param name="parameterType">Input data type</param>
|
||||
/// <returns>True of input type is nullable, false otherwise</returns>
|
||||
private Boolean IsNullableType(Type parameterType)
|
||||
{
|
||||
return parameterType == typeof (Byte[]) ||
|
||||
parameterType == typeof (String) ||
|
||||
typeof (DbGeometry).IsAssignableFrom(parameterType) ||
|
||||
typeof (DbGeography).IsAssignableFrom(parameterType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates type information in code according to whether it is nullable
|
||||
/// </summary>
|
||||
/// <param name="code">Buffer used to store the code constructed so far</param>
|
||||
/// <param name="parameterType">Input type</param>
|
||||
private void WriteType(StringBuilder code, Type parameterType)
|
||||
{
|
||||
code.Append(parameterType.ToString());
|
||||
if (!IsNullableType(parameterType))
|
||||
{
|
||||
code.Append("?");
|
||||
}
|
||||
code.Append(" ");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates the exception statement which is thrown from each function stub.
|
||||
/// </summary>
|
||||
/// <param name="code">Buffer used to store the code constructed so far</param>
|
||||
private void WriteExceptionStatement(StringWriter code)
|
||||
{
|
||||
AppendSpaces(code, 12);
|
||||
code.WriteLine("throw new NotSupportedException(\"This function can only be invoked from LINQ to Entities.\");");
|
||||
AppendSpaces(code, 8);
|
||||
code.WriteLine("}");
|
||||
code.WriteLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends spaces to code line, this is avoid tabs as required by coding standards.
|
||||
/// </summary>
|
||||
/// <param name="str">Buffer used to store the code constructed so far</param>
|
||||
/// <param name="num">Number of desired spaces</param>
|
||||
private void AppendSpaces(StringWriter str, int num)
|
||||
{
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
str.Write(" ");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends spaces to code line, this is avoid tabs as required by coding standards.
|
||||
/// </summary>
|
||||
/// <param name="str">Buffer used to store the code constructed so far</param>
|
||||
/// <param name="num">Number of desired spaces</param>
|
||||
private void AppendSpaces(StringBuilder str, int num)
|
||||
{
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
str.Append(" ");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up dictionary to find better function name(that fxcop likes). If not in dictionary
|
||||
/// Pascal cases it if asked to.
|
||||
/// </summary>
|
||||
/// <param name="inputName">Function name from metadata</param>
|
||||
/// <param name="pascalCaseFunctionNames">If required to Pascal case function name</param>
|
||||
/// <returns>Better function name</returns>
|
||||
private String FindCorrectFunctionName(String inputName, Boolean pascalCaseFunctionNames)
|
||||
{
|
||||
if (_funcDictionaryToUse == null)
|
||||
{
|
||||
return inputName;
|
||||
}
|
||||
|
||||
String value;
|
||||
if (_funcDictionaryToUse.TryGetValue(inputName, out value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
else if (pascalCaseFunctionNames)
|
||||
{
|
||||
String interFunctionName = inputName.ToLower();
|
||||
char[] charFuncName = interFunctionName.ToCharArray();
|
||||
charFuncName[0] = System.Char.ToUpper(charFuncName[0]);
|
||||
return new String(charFuncName);
|
||||
}
|
||||
return inputName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up dictionary to find better parameter name(that fxcop likes and one that is friendlier).
|
||||
/// </summary>
|
||||
/// <param name="inputName">Parameter name from metadata</param>
|
||||
/// <returns>Better parameter name</returns>
|
||||
private String FindCorrectParameterName(String inputParameterName)
|
||||
{
|
||||
String value;
|
||||
|
||||
if (_paramDictionaryToUse == null)
|
||||
{
|
||||
return inputParameterName;
|
||||
}
|
||||
if (_paramDictionaryToUse.TryGetValue(inputParameterName, out value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
return inputParameterName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates header for the class.
|
||||
/// </summary>
|
||||
/// <param name="newCode">Buffer used to store the code constructed so far</param>
|
||||
/// <param name="className">Output class name</param>
|
||||
public void GenerateClassHeader(StringWriter newCode, String className, String namespaceString)
|
||||
{
|
||||
AppendSpaces(newCode, 4);
|
||||
newCode.WriteLine("/// <summary>");
|
||||
AppendSpaces(newCode, 4);
|
||||
newCode.Write("/// Contains function stubs that expose " +namespaceString);
|
||||
newCode.WriteLine(" methods in Linq to Entities.");
|
||||
AppendSpaces(newCode, 4);
|
||||
newCode.WriteLine("/// </summary>");
|
||||
AppendSpaces(newCode, 4);
|
||||
newCode.Write("public static ");
|
||||
if (className.Equals("EntityFunctions", StringComparison.Ordinal))
|
||||
{
|
||||
newCode.Write("partial ");
|
||||
}
|
||||
newCode.WriteLine("class " + className);
|
||||
AppendSpaces(newCode, 4);
|
||||
newCode.WriteLine("{");
|
||||
}
|
||||
}
|
||||
}
|
||||
65
external/entityframework/samples/Provider/FunctionStubGenerator/FunctionStubGenerator.csproj
vendored
Normal file
65
external/entityframework/samples/Provider/FunctionStubGenerator/FunctionStubGenerator.csproj
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>8.0.30703</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{AC400458-A998-4D85-8C2B-5F1194CFDB8A}</ProjectGuid>
|
||||
<OutputType>Exe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>FunctionStubGenerator</RootNamespace>
|
||||
<AssemblyName>FunctionStubGenerator</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<StartupObject />
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data.Entity" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="FunctionStubFileWriter.cs" />
|
||||
<Compile Include="LinqFunctionStubsCodeGen.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<PropertyGroup>
|
||||
<PostBuildEvent>
|
||||
</PostBuildEvent>
|
||||
</PropertyGroup>
|
||||
<Target Name="AfterBuild">
|
||||
<Exec Command=""$(TargetPath)" "$(ProjectDir)..\SampleEntityFrameworkProvider\SampleProviderFunctions.cs"" Condition="Exists('$(ProjectDir)..\SampleEntityFrameworkProvider')" />
|
||||
</Target>
|
||||
</Project>
|
||||
144
external/entityframework/samples/Provider/FunctionStubGenerator/LinqFunctionStubsCodeGen.cs
vendored
Normal file
144
external/entityframework/samples/Provider/FunctionStubGenerator/LinqFunctionStubsCodeGen.cs
vendored
Normal file
@@ -0,0 +1,144 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Entity;
|
||||
using System.Data.Metadata.Edm;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
|
||||
namespace LinqFunctionStubsGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Class that gets metadata about the functions and generates the function stubs.
|
||||
/// </summary>
|
||||
public class SampleProviderLinqFunctionStubsCodeGen
|
||||
{
|
||||
/// <summary>
|
||||
/// Main driver function.
|
||||
/// </summary>
|
||||
/// <param name="args"></param>
|
||||
public static void Generate(string outputFileName)
|
||||
{
|
||||
//The following functions are omitted because they have counterparts in the BCL
|
||||
string[] omittedFunctions = new[]
|
||||
{
|
||||
"Sum", "Min", "Max", "Average", "Avg",
|
||||
"Count", "BigCount",
|
||||
"Trim", "RTrim", "LTrim",
|
||||
"Concat", "Length", "Substring",
|
||||
"Replace", "IndexOf", "ToUpper", "ToLower",
|
||||
"Contains", "StartsWith", "EndsWith", "Year", "Month", "Day",
|
||||
"DayOfYear", "Hour", "Minute", "Second", "Millisecond", "CurrentDateTime", "CurrentDateTimeOffset",
|
||||
"CurrentUtcDateTime",
|
||||
"BitwiseAnd", "BitwiseOr", "BitwiseXor", "BitwiseNot",
|
||||
"Round", "Abs", "Power", "NewGuid",
|
||||
"Floor", "Ceiling",
|
||||
};
|
||||
|
||||
//The following functions are omitted from SqlFunctions because they already exist in EntityFunctions
|
||||
string[] omittedSqlFunctions = new[]
|
||||
{
|
||||
"STDEV", "STDEVP", "VAR", "VARP", "COUNT_BIG",
|
||||
"Left", "Right", "Reverse", "GetTotalOffsetMinutes",
|
||||
"TruncateTime", "CreateDateTime",
|
||||
"CreateDateTimeOffset", "CreateTime", "Add", "Diff",
|
||||
"Truncate", "SYSDATETIME", "SYSUTCDATETIME", "SYSDATETIMEOFFSET",
|
||||
"LEN", "LOWER", "UPPER", "NEWID",
|
||||
};
|
||||
|
||||
//Generate Sql Server function stubs
|
||||
String ssdl = @"<Schema Namespace='LinqFunctionStubsGenerator' Alias='Self' Provider='SampleEntityFrameworkProvider' ProviderManifestToken='2008' xmlns='http://schemas.microsoft.com/ado/2006/04/edm/ssdl'></Schema>";
|
||||
XmlReader[] xmlReaders = new XmlReader[1];
|
||||
xmlReaders[0] = XmlReader.Create(new StringReader(ssdl));
|
||||
|
||||
StoreItemCollection storeItemCollection = new StoreItemCollection(xmlReaders);
|
||||
IEnumerable<EdmFunction> sqlFunctions = storeItemCollection.GetItems<EdmFunction>()
|
||||
.Where(f => f.NamespaceName == "SqlServer")
|
||||
.Where(f => !(omittedFunctions.Concat(omittedSqlFunctions)).Contains(f.Name, StringComparer.OrdinalIgnoreCase));
|
||||
|
||||
FunctionStubFileWriter sqlStubsFileWriter = new FunctionStubFileWriter(sqlFunctions, GetFunctionNamingDictionary(), GetParameterNamingDictionary());
|
||||
sqlStubsFileWriter.GenerateToFile(outputFileName, "SampleEntityFrameworkProvider", "SampleSqlFunctions", "SqlServer", true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// These help generate better function and argument names in following cases:
|
||||
/// 1. Generates an fxcop agreeable name
|
||||
/// 2. PasalCases two-worded identifier names.
|
||||
/// </summary>
|
||||
private static Dictionary<string, string> GetFunctionNamingDictionary()
|
||||
{
|
||||
Dictionary<string, string> sqlFunctionNames = new Dictionary<string, string>();
|
||||
sqlFunctionNames.Add("CHECKSUM_AGG", "ChecksumAggregate");
|
||||
sqlFunctionNames.Add("STDEV", "StandardDeviation");
|
||||
sqlFunctionNames.Add("STDEVP", "StandardDeviationP");
|
||||
sqlFunctionNames.Add("VARP", "VarP");
|
||||
sqlFunctionNames.Add("CHARINDEX", "CharIndex");
|
||||
sqlFunctionNames.Add("LTRIM", "LTrim");
|
||||
sqlFunctionNames.Add("NCHAR", "NChar");
|
||||
sqlFunctionNames.Add("PATINDEX", "PatIndex");
|
||||
sqlFunctionNames.Add("QUOTENAME", "QuoteName");
|
||||
sqlFunctionNames.Add("RTRIM", "RTrim");
|
||||
sqlFunctionNames.Add("DATEADD", "DateAdd");
|
||||
sqlFunctionNames.Add("DATEDIFF", "DateDiff");
|
||||
sqlFunctionNames.Add("DATENAME", "DateName");
|
||||
sqlFunctionNames.Add("DATEPART", "DatePart");
|
||||
sqlFunctionNames.Add("GETDATE", "GetDate");
|
||||
sqlFunctionNames.Add("GETUTCDATE", "GetUtcDate");
|
||||
sqlFunctionNames.Add("DATALENGTH", "DataLength");
|
||||
sqlFunctionNames.Add("NEWID", "NewId");
|
||||
sqlFunctionNames.Add("CURRENT_TIMESTAMP", "CurrentTimestamp");
|
||||
sqlFunctionNames.Add("CURRENT_USER", "CurrentUser");
|
||||
sqlFunctionNames.Add("HOST_NAME", "HostName");
|
||||
sqlFunctionNames.Add("USER_NAME", "UserName");
|
||||
sqlFunctionNames.Add("ISNUMERIC", "IsNumeric");
|
||||
sqlFunctionNames.Add("ISDATE", "IsDate");
|
||||
sqlFunctionNames.Add("ATN2", "Atan2");
|
||||
sqlFunctionNames.Add("SOUNDEX", "SoundCode");
|
||||
sqlFunctionNames.Add("SQRT", "SquareRoot");
|
||||
sqlFunctionNames.Add("STR", "StringConvert");
|
||||
|
||||
return sqlFunctionNames;
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> GetParameterNamingDictionary()
|
||||
{
|
||||
Dictionary<string, string> sqlFunctionParameterNames = new Dictionary<string, string>();
|
||||
sqlFunctionParameterNames.Add("strSearch", "toSearch");
|
||||
sqlFunctionParameterNames.Add("strTarget", "target");
|
||||
sqlFunctionParameterNames.Add("datepart", "datePartArg");
|
||||
sqlFunctionParameterNames.Add("enddate", "endDate");
|
||||
sqlFunctionParameterNames.Add("startdate", "startDate");
|
||||
sqlFunctionParameterNames.Add("decimal", "decimalArg");
|
||||
sqlFunctionParameterNames.Add("str1", "string1");
|
||||
sqlFunctionParameterNames.Add("str2", "string2");
|
||||
sqlFunctionParameterNames.Add("str", "stringArg");
|
||||
sqlFunctionParameterNames.Add("string_expression", "stringExpression");
|
||||
sqlFunctionParameterNames.Add("x", "baseArg");
|
||||
sqlFunctionParameterNames.Add("y", "exponentArg");
|
||||
sqlFunctionParameterNames.Add("character_string", "stringArg");
|
||||
sqlFunctionParameterNames.Add("quote_character", "quoteCharacter");
|
||||
sqlFunctionParameterNames.Add("numeric_expression", "numericExpression");
|
||||
sqlFunctionParameterNames.Add("strInput", "stringInput");
|
||||
sqlFunctionParameterNames.Add("strReplacement", "stringReplacement");
|
||||
sqlFunctionParameterNames.Add("strPattern", "stringPattern");
|
||||
sqlFunctionParameterNames.Add("datetimeoffset", "dateTimeOffsetArg");
|
||||
|
||||
return sqlFunctionParameterNames;
|
||||
}
|
||||
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
if(args.Length != 1 || string.IsNullOrEmpty(args[0]))
|
||||
{
|
||||
Console.WriteLine("Usage:\nFunctionStubGenerator.exe targetPath");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Generate(args[0]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
external/entityframework/samples/Provider/FunctionStubGenerator/Properties/AssemblyInfo.cs
vendored
Normal file
37
external/entityframework/samples/Provider/FunctionStubGenerator/Properties/AssemblyInfo.cs
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
|
||||
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("FunctionStubGenerator")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Microsoft")]
|
||||
[assembly: AssemblyProduct("FunctionStubGenerator")]
|
||||
[assembly: AssemblyCopyright("Copyright © Microsoft 2009")]
|
||||
[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("0a81df40-d641-4ca7-83f9-6b2ce471666b")]
|
||||
|
||||
// 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")]
|
||||
Reference in New Issue
Block a user