Exemplo n.º 1
0
        private void BuildCreateTableScriptForVariable(MyQuery Query, String TableName, Object Value)
        {
            if (MyTypeHelper.IsPrimitive(Value))
            {
            }

            else if (Value is IDictionary <string, object> dict)
            {
                Query.Append("declare @").Append(TableName).Append(" table (");
                Int32 index = 0;
                foreach (var keyAndValue in dict)
                {
                    if (index > 0)
                    {
                        Query.Append(", ");
                    }
                    Query.Append(keyAndValue.Key).Append(" ").Append(TSqlHelper.GetSqlType(keyAndValue.Value?.GetType()));
                    index++;
                }
                Query.Append(");");
            }

            else if (Value is IList)
            {
            }

            else if (Value.GetType().IsClass)
            {
                BuildCreateTableScriptForVariable(Query, TableName, ReflectionHelper.ToDictionary(Value));
            }
        }
Exemplo n.º 2
0
        private void BuildScriptForVariable(MyQuery Query, String Name, Object Value, String ParentName = null)
        {
            string name = string.IsNullOrEmpty(ParentName) ? Name : (ParentName + "_" + Name);

            if (MyTypeHelper.IsPrimitive(Value))
            {
                Query.Append($"declare @{name} {TSqlHelper.GetSqlType(Value?.GetType())};\n");
                Query.Append($"set @{name} = {{0}};\n", Value);
            }

            else if (Value is IDictionary <string, object> dict)
            {
                if (string.IsNullOrEmpty(ParentName))
                {
                    foreach (var keyAndValue in dict)
                    {
                        BuildScriptForVariable(Query, keyAndValue.Key, keyAndValue.Value, Name);
                    }
                }
                else
                {
                    BuildCreateTableScriptForVariable(Query, name, dict);
                    BuildInsertIntoTableScriptForVariable(Query, name, dict);
                }
                //BuildCreateTableScriptForVariable(Query, name, dict);
                // BuildInsertIntoTableScriptForVariable(Query, name, dict);
            }

            else if (Value is IList list)
            {
                var firstValue = ReflectionHelper.ToDictionary(list.Count > 0 ? list[0] : null);
                BuildCreateTableScriptForVariable(Query, name, firstValue);
                foreach (var item in list)
                {
                    BuildInsertIntoTableScriptForVariable(Query, name, item);
                }
            }

            else if (Value.GetType().IsClass)
            {
                var classAsDict = ReflectionHelper.ToDictionary(Value);
                if (string.IsNullOrEmpty(ParentName))
                {
                    foreach (var keyAndValue in classAsDict)
                    {
                        BuildScriptForVariable(Query, keyAndValue.Key, keyAndValue.Value, Name);
                    }
                }
                else
                {
                    BuildCreateTableScriptForVariable(Query, name, classAsDict);
                    BuildInsertIntoTableScriptForVariable(Query, name, classAsDict);
                }
            }
        }