|
|
@@ -5,6 +5,8 @@ using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Data;
|
|
|
+using System.Globalization;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
using Database.Database;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
@@ -12,27 +14,29 @@ namespace Database;
|
|
|
|
|
|
public class DbLogic
|
|
|
{
|
|
|
- public static async Task<int?> GenIdAsync(ModelContext dbContext, string sequence)
|
|
|
+ public static async Task<decimal?> GenIdAsync(ModelContext dbContext, string sequence)
|
|
|
{
|
|
|
+ if (string.IsNullOrWhiteSpace(sequence)
|
|
|
+ || !Regex.IsMatch(sequence, "^[A-Za-z][A-Za-z0-9_]*$"))
|
|
|
+ {
|
|
|
+ throw new ArgumentException("Invalid Oracle sequence name.", nameof(sequence));
|
|
|
+ }
|
|
|
+
|
|
|
var conn = dbContext.Database.GetDbConnection();
|
|
|
if (conn.State == ConnectionState.Closed)
|
|
|
{
|
|
|
await conn.OpenAsync();
|
|
|
}
|
|
|
- var command = conn.CreateCommand();
|
|
|
- string query = "select " + sequence + ".nextval from dual";
|
|
|
- command.CommandText = query;
|
|
|
- var reader = await command.ExecuteReaderAsync();
|
|
|
- while (await reader.ReadAsync())
|
|
|
- {
|
|
|
- var title = reader.GetString(0);
|
|
|
|
|
|
- //await conn.CloseAsync();
|
|
|
- return int.Parse(title);
|
|
|
+ await using var command = conn.CreateCommand();
|
|
|
+ command.CommandText = $"SELECT {sequence}.NEXTVAL FROM DUAL";
|
|
|
+ await using var reader = await command.ExecuteReaderAsync();
|
|
|
+ if (await reader.ReadAsync())
|
|
|
+ {
|
|
|
+ return Convert.ToDecimal(reader.GetValue(0), CultureInfo.InvariantCulture);
|
|
|
}
|
|
|
|
|
|
- //await conn.CloseAsync();
|
|
|
- return 0;
|
|
|
+ return null;
|
|
|
}
|
|
|
}
|
|
|
|