/// <summary> /// 获取Insert语句模板 /// </summary> /// <param name="tableInfo">表信息</param> /// <param name="cmd">Insert命令对象</param> public override void PreExecuteInsert(TableInfo tableInfo, DbCommand cmd) { if (!tableInfo.AutoIncrement) return; string sql = cmd.CommandText; int index = sql.IndexOf("VALUES", StringComparison.OrdinalIgnoreCase); cmd.CommandText = sql.Insert(index, String.Format(" OUTPUT INSERTED.{0} ",tableInfo.PrimaryKey)); }
/// <summary> /// 获取Insert语句模板 /// </summary> /// <param name="tableInfo">表信息</param> /// <param name="cmd">Insert命令对象</param> public override void PreExecuteInsert(TableInfo tableInfo, DbCommand cmd) { if (!tableInfo.AutoIncrement) return; cmd.CommandText += string.Format(" returning {0} into :newid", tableInfo.PrimaryKey); var param = cmd.CreateParameter(); param.ParameterName = ":newid"; param.Value = DBNull.Value; param.Direction = ParameterDirection.ReturnValue; param.DbType = DbType.Int64; cmd.Parameters.Add(param); }
/// <summary> /// 获取Insert语句模板 /// </summary> /// <param name="tableInfo">表信息</param> /// <param name="cmd">Insert命令对象</param> public abstract void PreExecuteInsert(TableInfo tableInfo, DbCommand cmd);
/// <summary> /// 读取特性标签获取表信息 /// </summary> /// <param name="t">实体类型</param> /// <returns>返回新创建的表信息</returns> public static TableInfo FromPoco(Type t) { TableInfo ti = new TableInfo(); #region 表信息 var a = t.GetCustomAttributes(typeof(TableAttribute), true); if (a.Length > 0) { var tableAttribute = a[0] as TableAttribute; // ReSharper disable once PossibleNullReferenceException string name = string.IsNullOrEmpty(tableAttribute.Name) ? t.Name : tableAttribute.Name; string schema = tableAttribute.Schema; string tableName = string.IsNullOrEmpty(schema) ? name : string.Format("{0}.{1}", schema, name); ti.TableName = tableName; ti.Caption = tableAttribute.Caption; } else { ti.TableName = t.Name; } #endregion #region 主键信息 a = t.GetCustomAttributes(typeof(PrimaryKeyAttribute), true); if (a.Length > 0) { var primaryKeyAttribute = a[0] as PrimaryKeyAttribute; // ReSharper disable once PossibleNullReferenceException ti.PrimaryKey = primaryKeyAttribute.Name; ti.SequenceName = primaryKeyAttribute.SequenceName; ti.AutoIncrement = primaryKeyAttribute.AutoIncrement; } #endregion #region 父级信息 a = t.GetCustomAttributes(typeof(ParentKeyAttribute), true); if (a.Length > 0) { var parentKeyAttribute = a[0] as ParentKeyAttribute; // ReSharper disable once PossibleNullReferenceException ti.ParentKey = parentKeyAttribute.Name; } #endregion #region 显示字段信息 a = t.GetCustomAttributes(typeof(TextKeyAttribute), true); if (a.Length > 0) { var textKeyAttribute = a[0] as TextKeyAttribute; // ReSharper disable once PossibleNullReferenceException ti.TextKey = textKeyAttribute.Name; } #endregion #region 排序路径 a = t.GetCustomAttributes(typeof(SortPathAttribute), true); if (a.Length > 0) { var attribute = a[0] as SortPathAttribute; // ReSharper disable once PossibleNullReferenceException ti.SortPath = attribute.Name; } #endregion return ti; }
/// <summary> /// 获取Insert语句模板 /// </summary> /// <param name="tableInfo">表信息</param> /// <param name="cmd">Insert命令对象</param> public override void PreExecuteInsert(TableInfo tableInfo, DbCommand cmd) { if (!tableInfo.AutoIncrement) return; cmd.CommandText += ";\nSELECT last_insert_rowid();"; }