Example #1
0
        /// <summary>
        ///     新增一个对象
        /// </summary>
        /// <param name="entity"></param>
        public void Add(T entity)
        {
            DbCommand cmd = entity.GetInsertCmd();
            var       idp = EntitiesProxyHelper.GetProxyInfo <T>().IdentitesProp;

            if (!string.IsNullOrEmpty(idp))
            {
                object v = null;
                cmd.CommandText += "; SELECT @@IDENTITY ";
                var ds = Session.ExecCmd(cmd);
                if (ds.Tables.Count == 0)
                {
                    return;
                }

                if (ds.Tables[0].Rows.Count == 0)
                {
                    return;
                }

                v = ds.Tables[0].Rows[0][0];
                try
                {
                    EntitiesProxyHelper.GetDynamicMethod <T>().SetValue(entity, idp, v.ToString().ToLong());
                }
                catch (Exception)
                {
                    EntitiesProxyHelper.GetDynamicMethod <T>().SetValue(entity, idp, v.ToString().ToInt());
                }
            }
            else
            {
                Session.AddCommands(cmd);
            }
        }
        public OnExpression(Expression <Func <T1, TP1> > prop1, Expression <Func <T2, TP2> > prop2, Expression <Func <T2, bool> > func = null)
        {
            var t1TableName = EntitiesProxyHelper.GetProxyInfo <T1>().TableName;
            var t2TableName = EntitiesProxyHelper.GetProxyInfo <T2>().TableName;

            var t1Prop = ExpressionHelper.GetProps(prop1)[0];
            var t2Prop = ExpressionHelper.GetProps(prop2)[0];
        }
Example #3
0
        public ICompositeQuery OnWhere <T>(Expression <Func <T, bool> > where) where T : BaseEntity <T>, new()
        {
            var tblName = EntitiesProxyHelper.GetProxyInfo <T>().TableName;
            var h       = new ExpressionHelper();

            //解析表达式
            h.ResolveExpression(where, tblName);
            var sqlwhere = h.SqlWhere;
            var paras    = h.GetParameters();

            _parameters.AddRange(paras);
            _joinBuilder.Append(" and " + sqlwhere);
            return(this);
        }
        public static SqlServerQueryable <T> AsLinqQueryable <T>(this IDBContext <T> context) where T : BaseEntity <T>, new()
        {
            ISQLContext sqlContext = new SQLContext(context.Session);
            var         tblName    = "";
            var         proxy      = EntitiesProxyHelper.GetProxyInfo <T>();

            if (proxy.IsCompositeQuery)
            {
                tblName = string.Format(" ({0})  ", proxy.CompositeSql);
            }
            else
            {
                tblName = proxy.TableName;
            }
            return(new SqlServerQueryable <T>(QueryParser.CreateDefault(), new SqlServerQueryExecutor(sqlContext, tblName)));
        }
Example #5
0
        public ICompositeQuery On <T1, T2, TP1, TP2>(Expression <Func <T1, TP1> > left, Expression <Func <T2, TP2> > right)
            where T1 : BaseEntity <T1>, new()
            where T2 : BaseEntity <T2>, new()
        {
            var tbl1  = EntitiesProxyHelper.GetProxyInfo <T1>().TableName;
            var prop1 = ExpressionHelper.GetProps(left)[0];
            var tbl2  = EntitiesProxyHelper.GetProxyInfo <T2>().TableName;
            var prop2 = ExpressionHelper.GetProps(right)[0];

            if (_firstJoin)
            {
                _joinBuilder.AppendFormat(" inner join {2} on {0}.{1} = {2}.{3} ", tbl1, prop1, tbl2, prop2);
            }
            else
            {
                _firstJoin = true;
                _joinBuilder.AppendFormat(" from {0} inner join {2} on {0}.{1} = {2}.{3} ", tbl1, prop1, tbl2, prop2);
            }

            return(this);
        }
Example #6
0
 public ICompositeQuery Fetch <T>() where T : BaseEntity <T>, new()
 {
     _tblNames.Add(EntitiesProxyHelper.GetProxyInfo <T>().TableName);
     return(this);
 }
Example #7
0
        static void Main(string[] args)
        {
            // Parallel.For(0, 10, x => Csl.Wl(NOGenter.Instance.GenOrderNo("BZY")));


            TestSku();

            Stopwatch sw = new Stopwatch();

            sw.Start();

            /* DbTest.Oct_FrameworkEntities e = new Oct_FrameworkEntities();
             *
             * var list = e.TestTs.ToList();
             *
             * sw.Stop();
             * e.Dispose();*/
            /* EntityContext context = new EntityContext();
            *  var tss = context.Find<TestTs>(1);
            *  var ssssss=  ActiveRecordLinq.AsQueryable<TestTs>().First();
            *
            *  var ss1 = context.Find    One<TestTs>(1);
            *  sw.Stop();
            *  Console.WriteLine("Nhibernate" + sw.Elapsed);*/
            EntitiesProxyHelper.Register(Assembly.GetExecutingAssembly());

            /*
             *
             * TestTs t = new TestTs();
             *
             *
             * var m = dbContext.TestTsContext.GetModel(1);
             * m.EntryUpdateStack();
             * m.DD = "ss1";
             *
             * var id = EntitiesProxyHelper.GetDynamicMethod<TestTs>().GetValue(m, "Id");
             *
             * dbContext.TestTsContext.Update(m);
             *
             * var ts = new TestTs();
             * ts.DD = "new";
             * dbContext.TestTsContext.Add(ts);
             * dbContext.SaveChanges();
             *
             * var models = dbContext.GetContext<UserAction>().Query(" MenuName='我'");
             *
             * /*sw.Restart();
             * var list = dbContext.TestTsContext.Query("");
             *
             * sw.Stop();
             *
             * Console.WriteLine("Oct.DB" + sw.Elapsed);
             * //   var count1 = cc.Count;
             *
             * //compoise
             *
             * /* dbContext.SQLContext.AsCompositeQuery()
             *   .Fetch<TestTs, TestTs>((x, y) => x.Id == y.Id,(x,y)=>x.DD.Contains("的"));*/
            // Console.ReadLine();

            /* sw.Restart();
             * var list1 = dbContext.TestTsContext.Query("");
             * sw.Stop();*/

            DbContext dbContext = new DbContext("User id=Octopus_Framework;Password=JSJQH8819!(K;Server=192.168.2.20;database=Oct_Framework;");
            var       dd        = new TestTs()
            {
                DD = "ss"
            };

            dbContext.TestTsContext.Add(dd);
            var ddd = dd;

            /*  var ds = dbContext.SQLContext.AsCompositeQuery()
             *    .Fetch<TestTs>()
             *    //第一个
             *    .Fetch<DemoTable>()
             *    .On<TestTs, DemoTable, int, int>(p => p.Id, p => p.Id)
             *    .OnWhere<DemoTable>(p => p.Name == "chenzhiyin")
             *    //第二个
             *     .Fetch<DemoTable>()
             *      .On<TestTs, DemoTable, int, int>(p => p.Id, p => p.Id)
             *      //返回数据
             *    .Query();*/


            // GenDb.Gen(Assembly.GetAssembly(typeof(DbContext)),dbContext.SQLContext);
            //var sql = GenTbl.Gen<TestTs>(dbContext.SQLContext);
            // var m = dbContext.TestTsContext.GetModel(1);

            /*sw.Restart();
             * var count111 = dbContext.TestTsContext.Query();
             * sw.Stop();
             * var lis21 = dbContext.GetContext<UserAction>().AsLinqQueryable().ToList();
             * var count = dbContext.TestTsContext.AsLinqQueryable().Count();
             * // var a = dbContext.GetContext<TestTs>().AsLinqQueryable().FirstOrDefault();
             * sw.Restart();
             * var lis = dbContext.GetContext<TestTs>().AsLinqQueryable().Select(p => new TestTs() { DD = p.DD }).ToList();
             * sw.Stop();
             * var ss = sw.Elapsed;
             * var a = dbContext.GetContext<TestTs>().AsLinqQueryable().FirstOrDefault();
             * sw.Restart();
             * var lis1 = dbContext.GetContext<TestTs>().AsLinqQueryable().Select(p => new TestTs() { DD = p.DD }).ToList();
             * var stes = dbContext.GetContext<TestTs>().AsLinqQueryable().Select(p => new { p.DD, p.Id }).ToList();
             * sw.Stop();
             * var qq = sw.Elapsed;
             * var order = dbContext.TestTsContext.AsLinqQueryable().Where(p => p.DD.Contains("ss")).ToList();
             * var part = dbContext.TestTsContext.AsLinqQueryable().Where(p => p.DD.Contains("ss")).ToList();
             *
             * var ret = (from dd in dbContext.GetContext<TestTs>().AsLinqQueryable() where dd.Id == 12 select new TestTs() { Id = dd.Id }).ToList();
             *
             * var rets = dbContext.TestTsContext.AsLinqQueryable().OrderBy(p => p.Id).Skip(10).Take(10).ToList();
             *
             * new TestTs() { DD = "ss" }.Insert();
             * new TestTs() { DD = "ss", Id = 1 }.Update();
             * new TestTs() { Id = 1 }.Delete();
             * dbContext.SaveChanges();
             * // Console.WriteLine("Oct.DB :" + sql);
             */
            Console.ReadLine();
        }
Example #8
0
        internal static string GetGenTblSql(Type t)
        {
            var props   = ReflectionUtils.GetPublicFieldsAndProperties(t);
            var tblname = EntitiesProxyHelper.GetProxyInfo(t).TableName;

            string script     = @"
                            CREATE TABLE {0} (
                            {1}
                            ) ";
            string fieldsText = "";
            var    idx        = 0;

            foreach (var field in props)
            {
                if (field is PropertyInfo)
                {
                    var col  = " [{0}] {1} {2} {3} ";
                    var prop = (PropertyInfo)field;

                    var name = prop.Name;

                    object defaultValue = null;
                    if (prop.PropertyType == typeof(string))
                    {
                        defaultValue = "";
                    }
                    else
                    {
                        defaultValue = Activator.CreateInstance(prop.PropertyType);
                    }
                    var    para      = new SqlParameter(name, defaultValue);
                    var    dbtype    = para.SqlDbType;
                    string dbtypeStr = "";
                    if (dbtype == SqlDbType.NVarChar)
                    {
                        dbtypeStr = "[" + dbtype.ToString() + "](200)";
                    }
                    else
                    {
                        dbtypeStr = "[" + dbtype.ToString() + "]";
                    }

                    var    isnull   = para.IsNullable;
                    string nullable = " NOT NULL";
                    if (isnull)
                    {
                        nullable = " NULL ";
                    }

                    string keyTrue = "";
                    if (prop.IsPrimaryKey())
                    {
                        keyTrue += "  PRIMARY KEY ";
                    }

                    if (prop.IsAutoIncrease())
                    {
                        keyTrue += " IDENTITY(1,1) ";
                    }

                    if (idx == props.Length - 1)
                    {
                        fieldsText += string.Format(col, name, dbtypeStr, nullable, keyTrue);
                    }
                    else
                    {
                        fieldsText += string.Format(col, name, dbtypeStr, nullable, keyTrue) + ",";
                    }

                    idx++;
                }
            }

            var sql = string.Format(script, tblname, fieldsText);

            return(sql);
        }