Procházet zdrojové kódy

Common:

      3.12.2019 v.1.0.0.4
      New
      - Add SqlCommand extension
      Update
      - Change classes location to propper namespaces
Dalibor Votruba před 6 roky
rodič
revize
121db8bcd5

+ 5 - 3
Common/Common.csproj

@@ -41,14 +41,16 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="Extensions\XmlNodeExt.cs" />
-    <Compile Include="Extensions\XmlReaderExt.cs" />
-    <Compile Include="Extensions\XPathNavigatorExt.cs" />
+    <Compile Include="Data\Extensions\SqlCommandExt.cs" />
+    <Compile Include="Xml\Extensions\XmlNodeExt.cs" />
+    <Compile Include="Xml\Extensions\XmlReaderExt.cs" />
+    <Compile Include="Xml\Extensions\XPathNavigatorExt.cs" />
     <Compile Include="Object\DisposableObject.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Security\Secure.cs" />
   </ItemGroup>
   <ItemGroup>
+    <Folder Include="Extensions\" />
     <Folder Include="Helpers\" />
   </ItemGroup>
   <ItemGroup>

+ 119 - 0
Common/Data/Extensions/SqlCommandExt.cs

@@ -0,0 +1,119 @@
+using System;
+using System.Data;
+using System.Data.SqlClient;
+using System.Text;
+
+namespace Crestyl.DMS.Console.Migration.Commands.Migration.Assets
+{
+    public static class SqlCommandExt
+    {
+        private static string ParameterValueForSQL(this SqlParameter sp)
+        {
+            var result = "";
+            if (sp.Value == null || sp.Value == DBNull.Value)
+                return "NULL";
+
+            switch (sp.SqlDbType)
+            {
+                case SqlDbType.UniqueIdentifier:
+                    result = "'" + ((Guid)sp.Value).ToString("B") + "'";
+                    break;
+                case SqlDbType.Char:
+                case SqlDbType.NChar:
+                case SqlDbType.NText:
+                case SqlDbType.NVarChar:
+                case SqlDbType.Text:
+                case SqlDbType.Time:
+                case SqlDbType.VarChar:
+                case SqlDbType.Xml:
+                    result = "'" + sp.Value.ToString().Replace("'", "''") + "'";
+                    break;
+                case SqlDbType.Date:
+                case SqlDbType.DateTime:
+                case SqlDbType.DateTime2:
+                case SqlDbType.DateTimeOffset:
+                    result = "'" + ((DateTime)sp.Value).ToString("s").Replace("'", "''") + "'";
+                    break;
+
+                case SqlDbType.Bit:
+                    result = ((int)sp.Value == 1) ? "1" : "0";
+                    break;
+
+                default:
+                    result = sp.Value.ToString().Replace("'", "''");
+                    break;
+            }
+
+            return result;
+        }
+
+        public static string CommandAsSql(this SqlCommand sc)
+        {
+            StringBuilder sql = new StringBuilder();
+            var isFirst = true;
+
+            sql.AppendLine("use " + sc.Connection.Database + ";");
+            switch (sc.CommandType)
+            {
+                case CommandType.StoredProcedure:
+                    sql.AppendLine("declare @return_value int;");
+
+                    foreach (SqlParameter sp in sc.Parameters)
+                    {
+                        if ((sp.Direction == ParameterDirection.InputOutput) || (sp.Direction == ParameterDirection.Output))
+                        {
+                            sql.Append("declare " + sp.ParameterName + "\t" + sp.SqlDbType.ToString() + "\t= ");
+
+                            sql.AppendLine(((sp.Direction == ParameterDirection.Output) ? "null" : sp.ParameterValueForSQL()) + ";");
+
+                        }
+                    }
+
+                    sql.AppendLine("exec [" + sc.CommandText + "]");
+
+                    foreach (SqlParameter sp in sc.Parameters)
+                    {
+                        if (sp.Direction != ParameterDirection.ReturnValue)
+                        {
+                            sql.Append((isFirst) ? "\t" : "\t, ");
+
+                            if (isFirst) isFirst = false;
+
+                            if (sp.Direction == ParameterDirection.Input)
+                                sql.AppendLine(sp.ParameterName + " = " + sp.ParameterValueForSQL());
+                            else
+
+                                sql.AppendLine(sp.ParameterName + " = " + sp.ParameterName + " output");
+                        }
+                    }
+                    sql.AppendLine(";");
+
+                    sql.AppendLine("select 'Return Value' = convert(varchar, @return_value);");
+
+                    foreach (SqlParameter sp in sc.Parameters)
+                    {
+                        if ((sp.Direction == ParameterDirection.InputOutput) || (sp.Direction == ParameterDirection.Output))
+                        {
+                            sql.AppendLine("select '" + sp.ParameterName + "' = convert(varchar, " + sp.ParameterName + ");");
+                        }
+                    }
+                    break;
+                case CommandType.Text:
+                    foreach (SqlParameter sp in sc.Parameters)
+                    {
+                        if (sp.Direction == ParameterDirection.Input)
+                        {
+                            sql.Append("declare " + sp.ParameterName + "\t" + sp.SqlDbType.ToString() + "\t= ");
+
+                            sql.AppendLine(((sp.Direction == ParameterDirection.Output) ? "null" : sp.ParameterValueForSQL()) + ";");
+
+                        }
+                    }
+                    sql.AppendLine(sc.CommandText);
+                    break;
+            }
+
+            return sql.ToString();
+        }
+    }
+}

+ 6 - 2
Common/QDR.FND.Common.nuspec

@@ -2,7 +2,7 @@
 <package>
   <metadata>
     <id>Quadarax.Foundation.Common</id>
-    <version>1.0.0.3</version>
+    <version>1.0.0.4</version>
     <title>Quadarax.Foundation.Common</title>
     <authors>Dalibor Votruba, Quadarax</authors>
     <owners>Dalibor Votruba</owners>
@@ -11,7 +11,11 @@
     <requireLicenseAcceptance>false</requireLicenseAcceptance>
     <description>Implementations of simple patterns and misc. extensions.</description>
     <releaseNotes>
-      2.12.2019 v.1.0.0.3 - New: Add XmlNode extension
+      3.12.2019 v.1.0.0.4
+      New
+      - Add SqlCommand extension
+      Update
+      - Change classes location to propper namespaces
     </releaseNotes>
     <copyright>Copyright (c) 2019 Quadarax</copyright>
     <tags>quadarax, foundarion, common, qdr, fnd, library, extensions</tags>

+ 6 - 0
Common/ReleaseNote.txt

@@ -1,3 +1,9 @@
+3.12.2019 v.1.0.0.4
+New
+ - Add SqlCommand extension
+Update
+ - Change classes location to propper namespaces
+
 2.12.2019 v.1.0.0.3
 New
  - Add XmlNode extension

+ 0 - 0
Common/Extensions/XPathNavigatorExt.cs → Common/Xml/Extensions/XPathNavigatorExt.cs


+ 0 - 0
Common/Extensions/XmlNodeExt.cs → Common/Xml/Extensions/XmlNodeExt.cs


+ 0 - 0
Common/Extensions/XmlReaderExt.cs → Common/Xml/Extensions/XmlReaderExt.cs