void Update()
        {
            var id      = Form.Find <long>("ID");
            var account = RowAdapter.LoadFirstOrDefault <Account>(p => p.ID == id && p.Deleted == false);

            if (account == null)
            {
                WrapResult(false, "未找到指定账号!");
                return;
            }

            var view = EntitySchemaManager.GetSchema <Account>(true);

            foreach (var column in view.Columns)
            {
                string str;
                if (Form.TryGet(column.Name, out str))
                {
                    var json  = Json.Parse(str);
                    var value = JsonSerializer.Deserialize(json, column.PropertyType);
                    column.SetValue(account, value);
                }
            }

            account.Save();
            var myaccount = JsonSerializer.Deserialize <Account>(Session["account"]);

            if (account.ID == myaccount.ID)
            {
                Session["account"] = JsonSerializer.Serialize(account);
            }

            WrapResult(true, FromAccount(account));
        }
        void GetFirendList()
        {
            var       token       = Form.Find("access_token");
            var       openStrings = Form.Find("opens");
            JsonArray opens;

            if (!Json.TryParse <JsonArray>(openStrings, out opens))
            {
                WrapResult(false, "invaild param: opens!");
                return;
            }
            if (string.IsNullOrEmpty(token))
            {
                WrapResult(false, "nvaild param: access_token");
                return;
            }
            if (opens.Count == 0)
            {
                WrapResult(false, "opens length error!");
                return;
            }

            var access = RowAdapter.LoadFirstOrDefault <Access>(p => p.AccessToken == token);

            if (access == null)
            {
                WrapResult(false, "invaild access_token!");
                return;
            }
            if (!access.Available)
            {
                WrapResult(false, "access_token expire!");
                return;
            }

            var view          = EntitySchemaManager.GetSchema <Account>(true);
            var dbProvider    = DbFactory.Gain(view.ConnectKey);
            var commandStruct = dbProvider.CreateCommand <Account>(view.Name, DbCommandMode.Select);
            var member        = SqlExpression.Member(view.Columns.First(p => p.Name == "OpenID").Name);
            var newArray      = SqlExpression.NewArray(opens.Select(p => SqlExpression.Constant((string)p)).ToArray());

            commandStruct.Condition = SqlExpression.In(member, newArray);
            var accounts = RowAdapter.Load <Account>(dbProvider, commandStruct);
            var array    = new JsonArray();

            foreach (var account in accounts)
            {
                var json = new JsonObject();
                json["open_id"]   = account.OpenID;
                json["nick_name"] = account.Nickname;
                json["real_name"] = account.Realname;
                json["gender"]    = account.Gender;
                json["available"] = account.Available;
                array.Add(json);
            }

            WrapResult(true, array);
        }
Exemple #3
0
        public static List <object> Select(this DbConnectionProvider connectionProvider, Type type, string commandText, CommandType commandType = CommandType.Text, TimeSpan?commandTimeout = null, IEnumerable <IDataParameter>?parameters = null)
        {
            if (connectionProvider == null)
            {
                throw new ArgumentNullException(nameof(connectionProvider));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (commandText == null)
            {
                throw new ArgumentNullException(nameof(commandText));
            }

            var schema = EntitySchemaManager.GetSchema(type);

            if (schema == null)
            {
                throw new InvalidConstraintException($"EntityType:{type} schema not found.");
            }
            if (schema.AccessLevel == AccessLevel.WriteOnly)
            {
                throw new ArgumentException($"Type:[{schema.EntityType}] write only");
            }
            if (schema.EntityType.GetConstructor(Type.EmptyTypes) == null)
            {
                throw new ArgumentException("not found empty type constructor!");
            }
            var result  = new List <object>();
            var columns = schema.Columns.ToDictionary(p => p.Name);

            connectionProvider.ExecuteReader(commandText, commandType, commandTimeout, parameters, reader =>
            {
                object obj;
                object value;
                string name;
                while (reader.Read())
                {
                    obj = Activator.CreateInstance(type);
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        value = reader[i];
                        if (value == DBNull.Value)
                        {
                            continue;
                        }
                        name = reader.GetName(i);
                        if (columns.TryGetValue(name, out ISchemaColumn column))
                        {
                            column.SetValue(obj, value);
                        }
                    }
                    result.Add(obj);
                }
            });
            return(result);
        }
Exemple #4
0
        public static void Delete(this DbConnectionProvider connectionProvider, IEnumerable <object> datas)
        {
            if (connectionProvider == null)
            {
                throw new ArgumentNullException(nameof(connectionProvider));
            }
            if (datas == null)
            {
                throw new ArgumentNullException(nameof(datas));
            }

            Type type;

            using (var e = datas.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    return;
                }
                type = e.Current.GetType();
            }
            var schema = EntitySchemaManager.GetSchema(type);

            if (schema == null)
            {
                throw new InvalidConstraintException($"EntityType:{type} schema not found.");
            }
            if (schema.AccessLevel == AccessLevel.ReadOnly)
            {
                throw new ArgumentException($"Type:[{schema.EntityType}] read only");
            }
            var validColumn = schema.Columns;
            var keyColumn   = validColumn.FirstOrDefault(p => p.IsPrimary);

            if (keyColumn == null)
            {
                throw new ArgumentException($"Type:[{schema.EntityType}] not found parimary");
            }
            var dbCommand = connectionProvider.CreateCommand(schema.Name, DbCommandMode.Delete);

            foreach (var data in datas)
            {
                var value = keyColumn.GetValue(data);
                if (value == null)
                {
                    throw new InvalidOperationException($"schema:{schema.EntityType} primary:{keyColumn.Name} should not be null");
                }

                var parameter = connectionProvider.CreateParameter($"@{keyColumn.Name}", value);
                dbCommand.AddParameter(parameter);
                dbCommand.Condition = SqlExpression.Equal(SqlExpression.Member(keyColumn.Name), SqlExpression.Paramter(keyColumn.Name));

                var commandText = dbCommand.CommandText;
                connectionProvider.ExecuteNonQuery(commandText, parameters: dbCommand.Parameters.ToArray());
                dbCommand.ClearParameter();
            }
        }
Exemple #5
0
        public static void Insert(this DbConnectionProvider connectionProvider, IEnumerable <object> datas)
        {
            if (connectionProvider == null)
            {
                throw new ArgumentNullException(nameof(connectionProvider));
            }
            if (datas == null)
            {
                throw new ArgumentNullException(nameof(datas));
            }

            Type type;

            using (var e = datas.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    return;
                }
                type = e.Current.GetType();
            }
            var schema = EntitySchemaManager.GetSchema(type);

            if (schema == null)
            {
                throw new InvalidConstraintException($"EntityType:{type} schema not found.");
            }
            if (schema.AccessLevel == AccessLevel.ReadOnly)
            {
                throw new ArgumentException($"Type:[{schema.EntityType}] read only");
            }
            var columns       = schema.Columns;
            var validColumn   = columns.Where(p => !p.IsIdentity).ToArray();
            var commandStruct = connectionProvider.CreateCommand(schema.Name, DbCommandMode.Insert, validColumn.Select(p => SqlExpression.Member(p.Name)));

            foreach (var data in datas)
            {
                foreach (var column in validColumn)
                {
                    var value = column.GetValue(data) ?? DBNull.Value;
                    commandStruct.AddParameter(column.Name, value);
                }

                connectionProvider.ExecuteNonQuery(commandStruct);
                commandStruct.ClearParameter();
            }
        }
Exemple #6
0
        static void TestExpression1()
        {
            EntitySchemaManager.LoadEntity <SceneObject>();
            var view          = EntitySchemaManager.GetSchema <SceneObject>(true);
            var dbProvider    = new Framework.Data.MsSql.MsSqlConnectionProvider(1, "");
            var commandStruct = dbProvider.CreateCommand <SceneObject>(view.Name, DbCommandMode.Select);
            var fields        = new List <long>()
            {
            };

            var guid = Guid.NewGuid();

            commandStruct.Where(p => (p.ID == 0 || fields.Contains(p.ID)) && !p.Deleted);
            commandStruct.OrderBy(p => p.Guid, true);
            //commandStruct.Where(p => fields.Contains(p.ID) && !p.Deleted);
            var commandText = commandStruct.CommandText;
        }
Exemple #7
0
        static void TestExpression2()
        {
            EntitySchemaManager.LoadEntity <SceneObject>();
            var view          = EntitySchemaManager.GetSchema <SceneObject>(true);
            var dbProvider    = new Framework.Data.MsSql.MsSqlConnectionProvider(1, "");
            var commandStruct = dbProvider.CreateCommand <SceneObject>(view.Name, DbCommandMode.Select);

            commandStruct.Columns.Add(SqlExpression.As(SqlExpression.Function("count", SqlExpression.Symbol("*")), "Count"));
            var fields = new List <long>()
            {
                4, 3
            };

            commandStruct.Where(p => (p.Function == null || fields.Contains(p.ID)) && !p.Deleted);
            commandStruct.OrderBy(p => p.ID);

            var commandText = commandStruct.CommandText;
        }
Exemple #8
0
        public static List <T> Load <T>(DbConnectionProvider connectionProvider, Expression <Func <T, bool> > condition = null)
            where T : RowAdapter, new()
        {
            if (connectionProvider == null)
            {
                throw new ArgumentNullException(nameof(connectionProvider));
            }

            var view          = EntitySchemaManager.GetSchema <T>(true);
            var validColumns  = view.Columns.Select(p => SqlExpression.Member(p.Name));
            var commandStruct = connectionProvider.CreateCommand <T>(view.Name, DbCommandMode.Select, validColumns);

            if (condition != null)
            {
                commandStruct.Where(condition);
            }
            return(Load <T>(connectionProvider, commandStruct));
        }
Exemple #9
0
        static void TestExpression3()
        {
            var sb = new System.Data.SqlClient.SqlConnectionStringBuilder();

            sb.DataSource     = "115.159.55.137,1999";
            sb.UserID         = "chenguangxu";
            sb.Password       = "******";
            sb.InitialCatalog = "BlingAccount";
            EntitySchemaManager.LoadEntity <SceneObject>();
            var view          = EntitySchemaManager.GetSchema <SceneObject>(true);
            var dbProvider    = new Framework.Data.MsSql.MsSqlConnectionProvider(1, sb.ConnectionString);
            var commandStruct = dbProvider.CreateCommand <SceneObject>(view.Name, DbCommandMode.Select);
            var keys          = new List <long>()
            {
                1, 5, 7, 9
            };

            commandStruct.Where(p => p.Name != null && keys.Contains(p.ID));
            commandStruct.OrderBy(p => p.ID);
            var a = dbProvider.Select(commandStruct);
        }
Exemple #10
0
        public static void Update(this DbConnectionProvider connectionProvider, IEnumerable <object> datas)
        {
            if (connectionProvider == null)
            {
                throw new ArgumentNullException(nameof(connectionProvider));
            }
            if (datas == null)
            {
                throw new ArgumentNullException(nameof(datas));
            }

            Type type;

            using (var e = datas.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    return;
                }
                type = e.Current.GetType();
            }
            var schema = EntitySchemaManager.GetSchema(type);

            if (schema == null)
            {
                throw new InvalidConstraintException($"EntityType:{type} schema not found.");
            }
            if (schema.AccessLevel == AccessLevel.ReadOnly)
            {
                throw new InvalidConstraintException($"Type:[{schema.EntityType}] read only");
            }
            var columns   = schema.Columns;
            var keyColumn = columns.FirstOrDefault(p => p.IsPrimary);

            if (keyColumn == null)
            {
                throw new InvalidConstraintException($"Type:[{schema.EntityType}] not found parimary");
            }
            var aryColumn = columns.Where(p => !p.IsIdentity).Select(p => SqlExpression.Member(p.Name)).ToArray();
            var dbCommand = connectionProvider.CreateCommand(schema.Name, DbCommandMode.Update, aryColumn);

            foreach (var data in datas)
            {
                foreach (var column in columns)
                {
                    if (column.IsIdentity && !column.IsPrimary)
                    {
                        continue;
                    }

                    var val       = column.GetValue(data) ?? DBNull.Value;
                    var parameter = connectionProvider.CreateParameter(column.Name, val);
                    dbCommand.AddParameter(parameter);
                }

                dbCommand.Condition = SqlExpression.Equal(SqlExpression.Member(keyColumn.Name), SqlExpression.Paramter(keyColumn.Name));
                var strComm = dbCommand.CommandText;
                connectionProvider.ExecuteNonQuery(strComm, parameters: dbCommand.Parameters.ToArray());
                dbCommand.ClearParameter();
            }
        }