FreeSql/Examples/base_entity/ModAsTableImpl.cs
2024-11-13 18:18:28 +08:00

55 lines
1.8 KiB
C#

using FreeSql;
using FreeSql.DataAnnotations;
using FreeSql.Internal;
using FreeSql.Internal.Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
class ModAsTableImpl : IAsTable
{
public ModAsTableImpl(IFreeSql fsql)
{
AllTables = Enumerable.Range(0, 9).Select(a => $"order_{a}").ToArray();
fsql.Aop.CommandBefore += (_, e) =>
{
e.Command.CommandText = Regex.Replace(e.Command.CommandText, @"/\*astable\([^\)]+\)*\/", "1=1");
};
}
public string[] AllTables { get; }
public string GetTableNameByColumnValue(object columnValue, bool autoExpand = false)
{
var modid = (int)columnValue;
return $"order_{(modid % 10)}";
}
public string[] GetTableNamesByColumnValueRange(object columnValue1, object columnValue2)
{
throw new NotImplementedException();
}
public IAsTableTableNameRangeResult GetTableNamesBySqlWhere(string sqlWhere, List<DbParameter> dbParams, SelectTableInfo tb, CommonUtils commonUtils)
{
var match = Regex.Match(sqlWhere, @"/\*astable\([^\)]+\)*\/");
if (match.Success == false) return new IAsTableTableNameRangeResult(AllTables, null, null);
var tables = match.Groups[1].Value.Split(',').Where(a => AllTables.Contains(a)).ToArray();
if (tables.Any() == false) return new IAsTableTableNameRangeResult(AllTables, null, null);
return new IAsTableTableNameRangeResult(tables, null, null);
}
public IAsTable SetDefaultAllTables(Func<string[], string[]> audit)
{
throw new NotImplementedException();
}
public IAsTable SetTableName(int index, string tableName)
{
throw new NotImplementedException();
}
}