Ejemplo n.º 1
0
        /// <summary>
        /// 为所有的实体生成 Id。
        /// </summary>
        /// <param name="batch"></param>
        private void GenerateId(EntityBatch batch)
        {
            var dba      = batch.DBA;
            var entities = batch.InsertBatch;

            //如果批量生成 Id 使用的序列号太低,则需要抛出异常。
            var seqName     = OracleMigrationProvider.SequenceName(batch.Table.Name, batch.Table.IdentityColumn.Name);
            var incrementBy = Convert.ToInt32(dba.QueryValue(
                                                  "SELECT INCREMENT_BY FROM ALL_SEQUENCES WHERE SEQUENCE_NAME = {0} AND SEQUENCE_OWNER = {1}",
                                                  seqName,
                                                  DbConnectionSchema.GetOracleUserId(dba.ConnectionSchema).ToUpper()
                                                  ));

            if (incrementBy < 100)
            {
                throw new InvalidOperationException(string.Format("使用批量保存,表 {0} 的序列 {1} 的每次递增数不能少于 100。建议在数据库生成完成后使用 Rafy.Domain.ORM.BatchSubmit.Oracle.OracleBatchImporter.EnableBatchSequence() 来变更序列的每次递增数以批量生成实体聚合中的所有 Id 标识。", batch.Table.Name, seqName));
            }

            //由于每次生成的 Id 号数有限,所以需要分批生成 Id
            var nextSeqValueSql = string.Format("SELECT {0}.NEXTVAL FROM DUAL", seqName);

            foreach (var section in EnumerateAllBatches(batch.InsertBatch, incrementBy))
            {
                var nextValue = Convert.ToInt64(dba.QueryValue(nextSeqValueSql));
                var startId   = nextValue - incrementBy + 1;
                for (int i = 0, c = section.Count; i < c; i++)
                {
                    var item = section[i];
                    item.Id = startId++;
                }
            }
        }
        protected override void RunCore(IDbAccesser db)
        {
            var count = Convert.ToInt32(db.QueryValue(
                                            "SELECT COUNT(0) FROM ALL_SEQUENCES WHERE SEQUENCE_NAME = {0} AND SEQUENCE_OWNER = {1}",
                                            this.SequenceName,
                                            DbConnectionSchema.GetOracleUserId(db.ConnectionSchema).ToUpper()
                                            ));

            if (count <= 0)
            {
                db.ExecuteText(string.Format(
                                   @"CREATE SEQUENCE {0}
MINVALUE 1
MAXVALUE 99999999999999999
START WITH 1
INCREMENT BY 1
NOCACHE
ORDER", this.SequenceName));
            }

            //            var sql = string.Format(@"DECLARE T_COUNT NUMBER;
            //BEGIN
            //    SELECT COUNT(*) INTO T_COUNT FROM DUAL WHERE EXISTS(SELECT * FROM ALL_SEQUENCES WHERE SEQUENCE_NAME='{0}');
            //    IF T_COUNT = 0 THEN
            //        EXECUTE IMMEDIATE '
            //        CREATE SEQUENCE {0}
            //        MINVALUE 1
            //        MAXVALUE 99999999999999999
            //        START WITH 1
            //        INCREMENT BY 1
            //        NOCACHE
            //        ORDER
            //        ';
            //    END IF;
            //END;", this.SEQName(op));
        }