|
| 1 | +using System.Linq.Expressions; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using PandaNuGet.Demo.Context; |
| 4 | +using PandaNuGet.Demo.Entities; |
| 5 | +using Pandatech.Crypto; |
| 6 | +using PostgresDbContext = PandaNuGet.Demo.Context.PostgresDbContext; |
| 7 | + |
| 8 | +namespace PandaNuGet.Demo.Services; |
| 9 | + |
| 10 | +public class GetByFirstBytesService(PostgresContext context) |
| 11 | +{ |
| 12 | + private static byte[] DocumentNumber => Sha3.Hash("1234567890"); |
| 13 | + |
| 14 | + public async Task<int> SeedUser() |
| 15 | + { |
| 16 | + var user = new UserEntity(); |
| 17 | + var documentNumber = DocumentNumber; |
| 18 | + var randomBytes = new byte[10]; |
| 19 | + documentNumber = documentNumber.Concat(randomBytes).ToArray(); |
| 20 | + user.Document = documentNumber; |
| 21 | + context.Users.Add(user); |
| 22 | + await context.SaveChangesAsync(); |
| 23 | + return user.Id; |
| 24 | + } |
| 25 | + |
| 26 | + public async Task GetByFirstBytes() |
| 27 | + { |
| 28 | + var userId = await SeedUser(); |
| 29 | + |
| 30 | + var userByDocument = await context |
| 31 | + .Users |
| 32 | + .WhereStartWithBytes(x => x.Document, 1, 64, DocumentNumber) |
| 33 | + .FirstOrDefaultAsync(); |
| 34 | + |
| 35 | + Console.WriteLine($"AAAAAA {userByDocument.Id}"); |
| 36 | + |
| 37 | + var user = await context.Users.FindAsync(userId); |
| 38 | + if (user != null) |
| 39 | + { |
| 40 | + context.Users.Remove(user); |
| 41 | + await context.SaveChangesAsync(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public async Task GetByFirstBytesDavit() |
| 46 | + { |
| 47 | + var userId = await SeedUser(); |
| 48 | + |
| 49 | + var userByDocument = await context |
| 50 | + .Users |
| 51 | + .Where(u => PostgresDbContext.substr(u.Document, 1, 64).SequenceEqual(DocumentNumber)) |
| 52 | + .FirstOrDefaultAsync(); |
| 53 | + |
| 54 | + Console.WriteLine($"AAAAAA {userByDocument.Id}"); |
| 55 | + |
| 56 | + var user = await context.Users.FindAsync(userId); |
| 57 | + if (user != null) |
| 58 | + { |
| 59 | + context.Users.Remove(user); |
| 60 | + await context.SaveChangesAsync(); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +public static class QueryableExtensions |
| 66 | +{ |
| 67 | + public static IQueryable<T> WhereStartWithBytes<T>( |
| 68 | + this IQueryable<T> source, |
| 69 | + Expression<Func<T, byte[]>> byteArraySelector, |
| 70 | + int start, |
| 71 | + int length, |
| 72 | + byte[] prefix) where T : class |
| 73 | + { |
| 74 | + // Parameter expression for the source element |
| 75 | + var parameter = Expression.Parameter(typeof(T), "x"); |
| 76 | + |
| 77 | + // Expression to get the byte array from the element |
| 78 | + var member = Expression.Invoke(byteArraySelector, parameter); |
| 79 | + |
| 80 | + // MethodInfo for the substr method |
| 81 | + var methodInfo = typeof(PostgresDbContext).GetMethod(nameof(PostgresDbContext.substr), new[] { typeof(byte[]), typeof(int), typeof(int) }); |
| 82 | + |
| 83 | + // Call to substr method |
| 84 | + var call = Expression.Call( |
| 85 | + methodInfo, |
| 86 | + member, |
| 87 | + Expression.Constant(start), |
| 88 | + Expression.Constant(length)); |
| 89 | + |
| 90 | + // MethodInfo for the SequenceEqual method |
| 91 | + var sequenceEqualMethod = typeof(Enumerable).GetMethods() |
| 92 | + .First(m => m.Name == "SequenceEqual" && m.GetParameters().Length == 2) |
| 93 | + .MakeGenericMethod(typeof(byte)); |
| 94 | + |
| 95 | + // Call to SequenceEqual method |
| 96 | + var sequenceEqualCall = Expression.Call( |
| 97 | + sequenceEqualMethod, |
| 98 | + call, |
| 99 | + Expression.Constant(prefix)); |
| 100 | + |
| 101 | + // Lambda expression for the final predicate |
| 102 | + var lambda = Expression.Lambda<Func<T, bool>>(sequenceEqualCall, parameter); |
| 103 | + |
| 104 | + // Apply the predicate to the source IQueryable with client-side evaluation |
| 105 | + return source.AsEnumerable().AsQueryable().Where(lambda); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
0 commit comments