コード例 #1
0
 private object Execute()
 {
     if (InsertObjects != null && InsertObjects.Count() > 0)
     {
         var isIdEntity = IsIdEntity(this.Entity);
         if (!isIdEntity)
         {
             this.Context.Insertable(InsertObjects).ExecuteCommand();
         }
         foreach (var InsertObject in InsertObjects)
         {
             int id = 0;
             if (isIdEntity)
             {
                 id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity();
             }
             var pk = GetPrimaryKey(this.Entity, InsertObject, id);
             AddChildList(this.SubList, InsertObject, pk);
         }
         return(InsertObjects.Count());
     }
     else
     {
         return(0);
     }
 }
コード例 #2
0
 private object Execute()
 {
     if (InsertObjects != null && InsertObjects.Count() > 0)
     {
         var isIdEntity = IsIdEntity(this.Entity);
         if (!isIdEntity)
         {
             this.Context.Insertable(InsertObjects).ExecuteCommand();
         }
         foreach (var InsertObject in InsertObjects)
         {
             int id = 0;
             if (isIdEntity)
             {
                 id = this.Context.Insertable(InsertObject).ExecuteReturnIdentity();
                 this.Entity.Columns.First(it => it.IsIdentity || it.OracleSequenceName.HasValue()).PropertyInfo.SetValue(InsertObject, id);
             }
             var pk = GetPrimaryKey(this.Entity, InsertObject, id);
             AddChildList(this.SubList, InsertObject, pk);
         }
         return(InsertObjects.Count());
     }
     else
     {
         return(0);
     }
 }
コード例 #3
0
ファイル: SubInserable.cs プロジェクト: czhensheng/SqlSugar
 public ISubInsertable <T> AddSubList(Expression <Func <T, object> > items)
 {
     if (InsertObjects != null && InsertObjects.Count() > 0)
     {
         string subMemberName;
         object sublist;
         GetList(InsertObjects, items, out subMemberName, out sublist);
         if (!this.SubList.ContainsKey(subMemberName))
         {
             this.SubList.Add(subMemberName, sublist);
         }
     }
     return(this);
 }
コード例 #4
0
        /// <summary>
        /// Tries to parse a script node.
        /// </summary>
        /// <param name="Parser">Custom parser.</param>
        /// <param name="Result">Parsed Script Node.</param>
        /// <returns>If successful in parsing a script node.</returns>
        public bool TryParse(ScriptParser Parser, out ScriptNode Result)
        {
            Result = null;

            try
            {
                string s = Parser.NextToken().ToUpper();
                if (s != "INTO")
                {
                    return(false);
                }

                if (!SelectParser.TryParseSources(Parser, out SourceDefinition Source))
                {
                    return(false);
                }

                switch (Parser.PeekNextToken().ToUpper())
                {
                case "(":
                    Parser.NextToken();

                    ScriptNode Node = Parser.ParseList();
                    if (!(Node is ElementList Columns))
                    {
                        Columns = new ElementList(new ScriptNode[] { Node }, Node.Start, Node.Length, Node.Expression);
                    }

                    if (Parser.NextToken() != ")")
                    {
                        return(false);
                    }

                    if (Parser.NextToken().ToUpper() != "VALUES")
                    {
                        return(false);
                    }

                    if (Parser.NextToken() != "(")
                    {
                        return(false);
                    }

                    Node = Parser.ParseList();
                    if (!(Node is ElementList Values))
                    {
                        Values = new ElementList(new ScriptNode[] { Node }, Node.Start, Node.Length, Node.Expression);
                    }

                    if (Values.Elements.Length != Columns.Elements.Length)
                    {
                        return(false);
                    }

                    if (Parser.NextToken() != ")")
                    {
                        return(false);
                    }

                    Result = new InsertValues(Source, Columns, Values, Parser.Start, Parser.Length, Parser.Expression);
                    return(true);

                case "SELECT":
                    Node = Parser.ParseStatement();
                    if (!(Node is Select Select))
                    {
                        return(false);
                    }

                    Result = new InsertSelect(Source, Select, Parser.Start, Parser.Position, Parser.Expression);
                    return(true);

                case "OBJECT":
                case "OBJECTS":
                    Parser.NextToken();

                    Node = Parser.ParseList();
                    if (!(Node is ElementList Objects))
                    {
                        Objects = new ElementList(new ScriptNode[] { Node }, Node.Start, Node.Length, Node.Expression);
                    }

                    Result = new InsertObjects(Source, Objects, Parser.Start, Parser.Length, Parser.Expression);
                    return(true);

                default:
                    return(false);
                }
            }
            catch (Exception)
            {
                return(false);
            }
        }
コード例 #5
0
ファイル: SubInserable.cs プロジェクト: czhensheng/SqlSugar
 public object ExecuteReturnPrimaryKey()
 {
     if (InsertObjects != null && InsertObjects.Count() > 0)
     {
         int count = 1;
         foreach (var InsertObject in InsertObjects)
         {
             List <ConditionalModel> conModel = new List <ConditionalModel>();
             int    id        = this.Context.Insertable(InsertObject).ExecuteReturnIdentity();
             object pkValue   = null;
             var    qureyable = this.Context.Queryable <T>();
             if (id.ObjToInt() == 0)
             {
                 var primaryProperty = this.Entity.Columns.FirstOrDefault(it =>
                                                                          it.PropertyName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase) ||
                                                                          it.DbColumnName.Equals(this.Pk, StringComparison.CurrentCultureIgnoreCase)
                                                                          );
                 pkValue = primaryProperty.PropertyInfo.GetValue(InsertObject);
                 qureyable.In(pkValue);
             }
             else
             {
                 qureyable.In(id);
                 pkValue = id;
             }
             var data = qureyable.First();
             foreach (var item in this.SubList)
             {
                 Dictionary <string, object> insertDictionary = new Dictionary <string, object>();
                 if (item.Value == null)
                 {
                     continue;
                 }
                 EntityInfo subEntity = null;
                 if (item.Value is IEnumerable <object> )
                 {
                     var list = item.Value as IEnumerable <object>;
                     if (list.Count() == 0)
                     {
                         continue;
                     }
                     var type = list.First().GetType();
                     this.Context.InitMappingInfo(type);
                     subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
                     foreach (var sbItem in list)
                     {
                         SetItems(insertDictionary, sbItem, subEntity, item.Key, pkValue);
                     }
                 }
                 else if (item.Value.GetType().IsClass())
                 {
                     var type = item.Value.GetType();
                     this.Context.InitMappingInfo(type);
                     subEntity = this.Context.EntityMaintenance.GetEntityInfo(type);
                     SetItems(insertDictionary, item.Value, subEntity, item.Key, pkValue);
                 }
                 count += this.Context.Insertable(insertDictionary).AS(subEntity.DbTableName).ExecuteCommand();
             }
         }
         return(count);
     }
     else
     {
         return(0);
     }
 }