public virtual int Insert(IDictionary <string, object> o) { ISqlMapper mapper = new InsertMapper(Factory.CreateConverter(_helper.DBType)); var model = mapper.ObjectToSql(Common.GetTableName(_key, _config.Owner, o.GetType(), _config, o), o, null, _config); int result = 0; result = _helper.ExecNoneQueryWithSQL(model.SQL, model.Parameters.ToArray()); return(result); }
public SqlModel ObjectToSql(string tableName, IDictionary <string, object> o, IList <WhereClause> where, TableConfig config = null) { InsertMapper insertMapper = new InsertMapper(Converter); UpdateByIDMapper updateMapper = new UpdateByIDMapper(Converter); StringBuilder judgement = new StringBuilder(); SqlModel model = new SqlModel(); judgement.AppendFormat(" EXISTS(SELECT 1 FROM {0} WHERE ", tableName); foreach (var key in o.Keys) { var column = config?.ColumnMapping?.Find(t => t.SourceColumn.ToLower().Equals(key.ToLower())); if (column != null && column.Ingore) { continue; } if (column == null || !column.Primarykey) { continue; } if (column != null && column.GenerateID && o[key] == null) { IIDGenerator generator = IDGeneratorFactory.Create(GeneratorType.SnowFlak); o.SetValue(key, generator.Generate()); } string columnName = Common.GetColumnName(key, column); if (string.IsNullOrEmpty(columnName)) { continue; } judgement.AppendFormat("{0}={1} AND ", Converter.FormatColumn(columnName), Converter.FormatParameter(columnName)); } var insertModel = insertMapper.ObjectToSql(tableName, o, where, config); var updateModel = updateMapper.ObjectToSql(tableName, o, where, config); judgement.Remove(judgement.Length - 4, 4); judgement.Append(")"); string sql = Converter.BuildIfElseStatement(judgement.ToString(), updateModel.SQL, insertModel.SQL); model.SQL = sql; model.Parameters = updateModel.Parameters; return(model); }