private bool SendToDb <T>(T data, EntityPropertyGetFunc <T> getFunc, EnttiyPostColumnFunc <T> postColumnFunc, bool synchronous) where T : ISqlEntity { if (Equals(data, null)) { return(false); } SchemaTable schemaTable = EntitySchemaSet.Get(data.GetType()); DbBaseProvider dbProvider = DbConnectionProvider.CreateDbProvider(_connectKey ?? schemaTable.ConnectKey); if (dbProvider == null) { return(false); } CommandStruct command = GenerateCommand(dbProvider, data, schemaTable, getFunc, postColumnFunc); if (command != null) { int result; if (synchronous) { //同时采集 ProfileManager.PostSqlOfMessageQueueTimes(command.TableName, 1); ProfileManager.ProcessSqlOfMessageQueueTimes(command.TableName); result = dbProvider.ExecuteQuery(CommandType.Text, command.Sql, command.Parameters); } else { //put into pool result = dbProvider.ExecuteNonQuery(data.GetMessageQueueId(), CommandType.Text, command.TableName, command.Sql, command.Parameters); } data.ResetState(); return(result > 0); } return(false); }
private bool SendToDb <T>(T data, EntityPropertyGetFunc <T> getFunc, EnttiyPostColumnFunc <T> postColumnFunc, bool synchronous) where T : ISqlEntity { if (Equals(data, null)) { return(false); } SchemaTable schemaTable = EntitySchemaSet.Get(data.GetType()); DbBaseProvider dbProvider = DbConnectionProvider.CreateDbProvider(_connectKey ?? schemaTable.ConnectKey); if (dbProvider == null) { return(false); } CommandStruct command = GenerateCommand(dbProvider, data, schemaTable, getFunc, postColumnFunc); if (command != null) { bool result = (synchronous ? dbProvider.ExecuteQuery(CommandType.Text, command.Sql, command.Parameters) : dbProvider.ExecuteNonQuery(data.GetMessageQueueId(), CommandType.Text, command.Sql, command.Parameters)) > 0; data.ResetState(); return(result); } return(false); }
/// <summary> /// Send entity to db saved. /// </summary> /// <param name="postColumnFunc"></param> /// <param name="getPropertyFunc"></param> /// <param name="entityList"></param> public static void SendToDb <T>(EntityPropertyGetFunc <T> getPropertyFunc, EnttiyPostColumnFunc <T> postColumnFunc, params T[] entityList) where T : ISqlEntity { string key = ""; try { if (entityList == null || entityList.Length == 0) { return; } var sender = new SqlDataSender(false); var groupList = entityList.GroupBy(t => t.GetMessageQueueId()); var sqlList = new List <KeyValuePair <string, KeyValuePair <byte[], long> > >(); foreach (var g in groupList) { key = g.Key.ToString(); var valueList = g.ToList(); foreach (var entity in valueList) { if (entity == null) { continue; } SqlStatement statement = sender.GenerateSqlQueue <T>(entity, getPropertyFunc, postColumnFunc); if (statement == null) { throw new Exception(string.Format("Generate sql of \"{0}\" entity error", entity.GetType().FullName)); } var sqlValueBytes = ProtoBufUtils.Serialize(statement); string sqlQueueKey = SqlStatementManager.GetSqlQueueKey(statement.IdentityID); sqlList.Add(new KeyValuePair <string, KeyValuePair <byte[], long> >(sqlQueueKey, new KeyValuePair <byte[], long>(sqlValueBytes, DateTime.Now.Ticks))); } } RedisConnectionPool.ProcessPipeline(p => { bool hasPost = false; var groupSqlList = sqlList.GroupBy(t => t.Key); foreach (var g in groupSqlList) { var pairs = g.Select(t => t.Value).ToList(); p.QueueCommand(client => ((RedisClient)client).ZAdd(g.Key, pairs)); hasPost = true; } if (hasPost) { p.Flush(); } }); } catch (Exception ex) { TraceLog.WriteError("Send To Db key:{0} error:{1}", key, ex); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dataList"></param> /// <param name="isChange"></param> /// <param name="getPropertyFunc"></param> /// <param name="postColumnFunc"></param> /// <param name="synchronous">is sync data to db</param> /// <returns></returns> public static bool SendSql <T>(IEnumerable <T> dataList, bool isChange, EntityPropertyGetFunc <T> getPropertyFunc, EnttiyPostColumnFunc <T> postColumnFunc = null, bool synchronous = false) where T : ISqlEntity { return(new SqlDataSender(isChange).Send(dataList, getPropertyFunc, postColumnFunc, synchronous)); }
/// <summary> /// Generate sql statement /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <param name="getFunc"></param> /// <param name="postColumnFunc"></param> /// <returns></returns> internal SqlStatement GenerateSqlQueue <T>(T data, EntityPropertyGetFunc <T> getFunc = null, EnttiyPostColumnFunc <T> postColumnFunc = null) where T : ISqlEntity { SchemaTable schemaTable = EntitySchemaSet.Get(data.GetType()); DbBaseProvider dbProvider = DbConnectionProvider.CreateDbProvider(schemaTable.ConnectKey); if (dbProvider != null) { //process all columns. CommandStruct command = GenerateCommand(dbProvider, data, schemaTable, getFunc, postColumnFunc); if (command != null) { var identityId = data.GetMessageQueueId(); return(dbProvider.GenerateSql(identityId, command)); } } return(null); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="dataList"></param> /// <param name="getFunc"></param> /// <param name="postColumnFunc"></param> /// <param name="synchronous">是否同步</param> /// <returns></returns> public bool Send <T>(IEnumerable <T> dataList, EntityPropertyGetFunc <T> getFunc, EnttiyPostColumnFunc <T> postColumnFunc, bool synchronous = false) where T : ISqlEntity { bool result = true; foreach (var data in dataList) { var r = SendToDb(data, getFunc, postColumnFunc, synchronous); if (!r) { result = r; } } return(result); }
private CommandStruct GenerateCommand <T>(DbBaseProvider dbProvider, T data, SchemaTable schemaTable, EntityPropertyGetFunc <T> getFunc, EnttiyPostColumnFunc <T> postColumnFunc) where T : ISqlEntity { CommandStruct command; if (!(schemaTable.StorageType.HasFlag(StorageType.ReadOnlyDB) || schemaTable.StorageType.HasFlag(StorageType.ReadWriteDB) || schemaTable.StorageType.HasFlag(StorageType.WriteOnlyDB)) || (string.IsNullOrEmpty(schemaTable.ConnectKey) && string.IsNullOrEmpty(schemaTable.ConnectionString))) { return(null); } if (getFunc == null) { getFunc = GetPropertyValue; } IList <string> columns = postColumnFunc != null ? postColumnFunc(data, schemaTable, _isChange) : schemaTable.GetColumnNames(); if (columns == null || columns.Count == 0) { TraceLog.WriteError("Class:{0} is not change column.", data.GetType().FullName); return(null); } string tableName = schemaTable.GetTableName(data.GetCreateTime()); if (data.IsDelete) { command = dbProvider.CreateCommandStruct(tableName, CommandMode.Delete); } else if (schemaTable.AccessLevel == AccessLevel.WriteOnly) { command = dbProvider.CreateCommandStruct(tableName, CommandMode.Insert); } else { command = dbProvider.CreateCommandStruct(tableName, CommandMode.ModifyInsert); } //StringBuilder changeLog = new StringBuilder(); //changeLog.AppendFormat("\"Keys\":\"{0}\"", data.GetKeyCode()); //处理列 foreach (string columnName in columns) { if (columnName.IsEmpty()) { continue; } try { SchemaColumn schemaColumn; if (schemaTable.Columns.TryGetValue(columnName, out schemaColumn)) { if (schemaColumn.Disable || schemaColumn.IsIdentity) { continue; } object value = getFunc(data, schemaColumn); IDataParameter parameter = CreateParameter(dbProvider, columnName, schemaColumn.DbType, value); command.AddParameter(parameter); } } catch (Exception ex) { throw new Exception(string.Format("get {0} column val error.", columnName), ex); } } //处理条件 string[] keyList = schemaTable.Keys; if (keyList.Length == 0) { throw new ArgumentNullException(string.Format("Table:{0} key is empty.", schemaTable.EntityName)); } string condition = string.Empty; command.Filter = dbProvider.CreateCommandFilter(); foreach (string columnName in keyList) { try { SchemaColumn schemaColumn; if (schemaTable.Columns.TryGetValue(columnName, out schemaColumn)) { string keyName = columnName; string paramName = "F_" + columnName; if (condition.Length > 0) { condition += " AND "; } condition += dbProvider.FormatFilterParam(schemaColumn.Name, "", paramName); object value = getFunc(data, schemaColumn); IDataParameter parameter = CreateParameter(dbProvider, paramName, schemaColumn.DbType, value); command.Filter.AddParam(parameter); //主键且自增列更新时,MSSQL与MySql处理不同,MySql需要有主键列 command.AddKey(CreateParameter(dbProvider, keyName, schemaColumn.DbType, value), schemaColumn.IsIdentity); } } catch (Exception ex) { throw new Exception(string.Format("get {0} column val error.", columnName), ex); } } command.Filter.Condition = condition; command.Parser(); return(command); }
/// <summary> /// Send entity to db saved. /// </summary> /// <param name="postColumnFunc"></param> /// <param name="getPropertyFunc"></param> /// <param name="entityList"></param> public static void SendToDb <T>(string connectKey, EntityPropertyGetFunc <T> getPropertyFunc, EnttiyPostColumnFunc <T> postColumnFunc, params T[] entityList) where T : ISqlEntity { string key = ""; try { if (entityList == null || entityList.Length == 0) { return; } var sender = new SqlDataSender(false, connectKey); var groupList = entityList.GroupBy(t => t.GetMessageQueueId()); var sqlList = new List <KeyValuePair <string, byte[]> >(); foreach (var g in groupList) { key = g.Key.ToString(); var valueList = g.ToList(); foreach (var entity in valueList) { if (entity == null) { continue; } SqlStatement statement = sender.GenerateSqlQueue <T>(entity, getPropertyFunc, postColumnFunc); if (statement == null) { throw new Exception(string.Format("Generate sql of \"{0}\" entity error", entity.GetType().FullName)); } var sqlValueBytes = ProtoBufUtils.Serialize(statement); string sqlQueueKey = SqlStatementManager.GetSqlQueueKey(statement.IdentityID); //修复顺序重复导致Sql执行乱序问题 sqlList.Add(new KeyValuePair <string, byte[]>(sqlQueueKey, sqlValueBytes)); TraceLog.WriteDebug($"Send to polls[{sqlQueueKey}]-> sql:{statement.ToString()}"); } } RedisConnectionPool.ProcessPipeline(p => { bool hasPost = false; var groupSqlList = sqlList.GroupBy(t => t.Key); int sqlCount = sqlList.Count; foreach (var g in groupSqlList) { var values = g.Select(t => t.Value).ToArray(); p.QueueCommand(client => ((RedisClient)client).LPush(g.Key, values), () => {//onSuccess ProfileManager.PostSqlOfMessageQueueTimes(g.Key, sqlCount); }); hasPost = true; } if (hasPost) { p.Flush(); } }); } catch (Exception ex) { TraceLog.WriteError("Send To Db key:{0} error:{1}", key, ex); } }
public static void SendToDb <T>(EntityPropertyGetFunc <T> getPropertyFunc, EnttiyPostColumnFunc <T> postColumnFunc, params T[] entityList) where T : ISqlEntity { SendToDb <T>(null, null, null, entityList); }