public T InsertT <T>(string tableName, BeeDataAdapter dataAdapter, bool removeIdentity) { ThrowExceptionUtil.ArgumentNotNullOrEmpty(tableName, "tableName"); ThrowExceptionUtil.ArgumentNotNull(dataAdapter, "dataAdapter"); if (removeIdentity) { TableSchema tableSchema = GetTableSchema(tableName); ThrowExceptionUtil.ArgumentConditionTrue(tableSchema != null, "tableName", "can not find table. Name:{0}".FormatWith(tableName)); if (tableSchema.IdentityColumn != null) { dataAdapter.RemoveKey(tableSchema.IdentityColumn.ColumnName); } else { dataAdapter.RemoveKey(Constants.DefaultIdentityColumnName); } } T result = default(T); InsertBeeCommand insertBeeCommand = new InsertBeeCommand(this, tableName, dataAdapter, Constants.DefaultIdentityColumnName); result = insertBeeCommand.Excute <T>(); return(result); }
/// <summary> /// 保存数据。 /// 默认的表名就是类名, 若T类型上有指定的表名,则以此为准 /// 默认的自增列为Id,若T类型上有指定的自增列, 则以此为准。 /// 若value中的标识列大于0, 则修改。 若小于等于0, 则新增。 /// 注:主键为int /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="value">类型值</param> /// <returns>新增则返回自增列, 修改则返回本身标示列值。</returns> public int Save <T>(T value) where T : class { ThrowExceptionUtil.ArgumentNotNull(value, "value"); string identityColumnName = OrmUtil.GetIdentityColumnName <T>(); string tableName = OrmUtil.GetTableName <T>(); EntityProxy <T> entityProxy = EntityProxyManager.Instance.GetEntityProxy <T>(); object identity = entityProxy.GetPropertyValue(value, identityColumnName); ThrowExceptionUtil.ArgumentConditionTrue(identity != null, string.Empty, "未指定主键列"); if ((int)identity <= 0) { return(Insert <T>(value)); } else { BeeDataAdapter dataAdapter = BeeDataAdapter.From <T>(value); dataAdapter.RemoveKey(identityColumnName); Update(tableName, dataAdapter, SqlCriteria.New.Equal(identityColumnName, identity)); return((int)identity); } }
protected override void BuildCommand() { BeeDataAdapter columnDataAdapter = new BeeDataAdapter(); columnDataAdapter.Merge(this.dataAdapter, true); // 移除不存在的列名 及 计算列 TableSchema tableSchema = owner.GetTableSchema(tableName); ThrowExceptionUtil.ArgumentConditionTrue(tableSchema != null, "tableName", "can not find table. Name:{0}".FormatWith(tableName)); List <string> needToRemovedList = new List <string>(); foreach (string key in columnDataAdapter.Keys) { ColumnSchema columnSchema = tableSchema.GetColumn(key); if (columnSchema == null) { needToRemovedList.Add(key); } else if (columnSchema.IsComputeField) { needToRemovedList.Add(key); } else { // do nothing here. } } if (tableSchema.IdentityColumn != null) { needToRemovedList.Add(tableSchema.IdentityColumn.ColumnName); } foreach (string key in needToRemovedList) { columnDataAdapter.RemoveKey(key); } DataAdapterParser dataAdapterParser = new DataAdapterParser(owner, columnDataAdapter); base.dbCommand.Parameters.AddRange(dataAdapterParser.DbParameterList.ToArray()); string whereClause = string.Empty; if (sqlCriteria != null) { this.sqlCriteria.Owner = owner; whereClause = string.Format("where {0}", sqlCriteria.WhereClause); base.dbCommand.Parameters.AddRange(sqlCriteria.DbParameters.ToArray()); } if (owner.DbDriver.GetType() == typeof(OracleDriver)) { string sqlText = string.Format("update {0} set {1} {2}", owner.DbDriver.FormatField(this.tableName), dataAdapterParser.UpdateClause, whereClause); sqlText = sqlText.Replace(Environment.NewLine, " "); base.dbCommand.CommandText = sqlText; } else { base.dbCommand.CommandText = string.Format("update {0} \r\nset {1} \r\n{2}", owner.DbDriver.FormatField(this.tableName), dataAdapterParser.UpdateClause, whereClause); } }
protected override void BuildCommand() { base.dbCommand.CommandType = CommandType.Text; BeeDataAdapter columnDataAdapter = new BeeDataAdapter(); columnDataAdapter.Merge(this.dataAdapter, true); // 移除不存在的列名 TableSchema tableSchema = owner.GetTableSchema(tableName); ThrowExceptionUtil.ArgumentConditionTrue(tableSchema != null, "tableName", "can not find table. Name:{0}".FormatWith(tableName)); List <string> needToRemovedList = new List <string>(); foreach (string key in columnDataAdapter.Keys) { ColumnSchema columnSchema = tableSchema.GetColumn(key); if (columnSchema == null) { needToRemovedList.Add(key); } else if (columnSchema.IsComputeField) { needToRemovedList.Add(key); } else { // do nothing here. } } foreach (string key in needToRemovedList) { columnDataAdapter.RemoveKey(key); } DataAdapterParser dataAdapterParser = new DataAdapterParser(owner, columnDataAdapter); base.dbCommand.CommandText = string.Format(@" insert into {0} ({1}) values ({2});" , owner.DbDriver.FormatField(this.tableName) , dataAdapterParser.ColumnClause, dataAdapterParser.ParameterClause); if (owner.DbServerType == DBServerType.Oracle) { string sqlText = string.Format(@"begin {0} end;", base.dbCommand.CommandText); sqlText = sqlText.Replace(Environment.NewLine, " "); base.dbCommand.CommandText = sqlText; } else if (owner.DbServerType == DBServerType.Pgsql) { if (!string.IsNullOrEmpty(identityColumnName)) { string identitySelectString = string.Format(@"; SELECT {0} ", owner.DbDriver.IdentitySelectString); identitySelectString = string.Format(identitySelectString, tableName).ToLower(); base.dbCommand.CommandText = base.dbCommand.CommandText + identitySelectString; } } else { if (!string.IsNullOrEmpty(identityColumnName)) { string identitySelectString = string.Format(@" SELECT {0}", owner.DbDriver.IdentitySelectString); base.dbCommand.CommandText = base.dbCommand.CommandText + identitySelectString; } } base.dbCommand.Parameters.AddRange(dataAdapterParser.DbParameterList.ToArray()); }