/// 
		/// <param name="seqName"></param>
		public bool CreateSequence(string seqName){
            var seq = new DbSequence(seqName);
            var sql = string.Format(this.CreateSequenceSql, seq.name, seq.min_value, seq.max_value, seq.start_with,
                seq.increment_by, seq.cache);
            this.Context.Ado.ExecuteCommand(sql);
            return true;
        }
示例#2
0
        public async Task <bool> CreateSequenceAsync <T>(DbSequence <T> sequence, CancellationToken ct = default)
            where T : struct
        {
            var connection = this.context.Database.GetDbConnection();

            if (connection.State != ConnectionState.Open)
            {
                await connection.OpenAsync(ct);
            }

            var name      = sequence.Schema != null ? $"{sequence.Schema}.{sequence.Name} " : sequence.Name;
            var start     = sequence.StartValue != null ? $"START WITH {sequence.StartValue} " : "";
            var increment = sequence.IncrementBy != null ? $"INCREMENT BY {sequence.IncrementBy} " : "";
            var min       = sequence.MinValue != null ? $"MINVALUE {sequence.MinValue} " : "NO MINVALUE ";
            var max       = sequence.MaxValue != null ? $"MAXVALUE {sequence.MaxValue} " : "NO MAXVALUE ";
            var cycle     = sequence.Cycle ? "CYCLE " : "NO CYCLE ";
            var cache     = sequence.CacheSize != null ? $"CACHE {sequence.CacheSize} " : "NO CACHE ";

            string type;

            if (typeof(T) == typeof(byte))
            {
                type = SqlServerSequenceDataTypes.TinyInt;
            }
            else if (typeof(T) == typeof(short))
            {
                type = SqlServerSequenceDataTypes.SmallInt;
            }
            else if (typeof(T) == typeof(int))
            {
                type = SqlServerSequenceDataTypes.Int;
            }
            else if (typeof(T) == typeof(long))
            {
                type = SqlServerSequenceDataTypes.BigInt;
            }
            else if (typeof(T) == typeof(decimal))
            {
                type = SqlServerSequenceDataTypes.Decimal;
            }
            else
            {
                throw new NotSupportedException($"Numeric type '{typeof(T).Name}' not supported");
            }

            var sql =
                $"CREATE SEQUENCE {name} " +
                $"AS {type} " +
                $"{start}" +
                $"{increment}" +
                $"{min}" +
                $"{max}" +
                $"{cycle}" +
                $"{cache}";

            var result = await this.context.Database.ExecuteSqlRawAsync(sql, ct);

            return(result == -1);
        }
        public bool CreateSequence <T>(DbSequence <T> sequence) where T : struct
        {
            var connection = this.context.Database.GetDbConnection();

            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }

            var name      = sequence.Schema != null ? $"{sequence.Schema}.{sequence.Name}" : sequence.Name;
            var start     = sequence.StartValue != null ? $"START WITH {sequence.StartValue} " : "";
            var increment = sequence.IncrementBy != null ? $"INCREMENT BY {sequence.IncrementBy} " : "";
            var min       = sequence.MinValue != null ? $"MINVALUE {sequence.MinValue} " : "NO MINVALUE ";
            var max       = sequence.MaxValue != null ? $"MAXVALUE {sequence.MaxValue} " : "NO MAXVALUE ";
            var cycle     = sequence.Cycle ? "CYCLE " : "NO CYCLE ";
            var cache     = sequence.CacheSize != null ? $"CACHE {sequence.CacheSize} " : null;
            var owner     = sequence.Owner != null ? $"OWNED BY {sequence.Owner} " : "OWNED BY NONE";

            string type;

            if (typeof(T) == typeof(short))
            {
                type = NpgsqlSequenceDataTypes.SmallInt;
            }
            else if (typeof(T) == typeof(int))
            {
                type = NpgsqlSequenceDataTypes.Integer;
            }
            else if (typeof(T) == typeof(long))
            {
                type = NpgsqlSequenceDataTypes.BigInt;
            }
            else if (typeof(T) == typeof(decimal))
            {
                type = NpgsqlSequenceDataTypes.Decimal;
            }
            else
            {
                throw new NotSupportedException($"Numeric type '{typeof(T).Name}' not supported");
            }

            var sql =
                $"CREATE SEQUENCE {name} " +
                $"{start}" +
                $"{increment}" +
                $"{min}" +
                $"{max}" +
                $"{cycle}" +
                $"{cache}" +
                $"{owner}";

            var result = this.context.Database.ExecuteSqlRaw(sql);

            return(result == -1);
        }
        public async Task <IActionResult> CreateSequence([FromBody] DbSequence <long> sequence)
        {
            var res = await this.context.CreateSequenceAsync(sequence, this.HttpContext.RequestAborted);

            return(this.Ok());
        }