Created
January 3, 2020 15:51
-
-
Save Siliconrob/b0d9bdc550180e24df42ff59fa3a96b8 to your computer and use it in GitHub Desktop.
Extension methods on IDBConnection to generate random data from Database methods (MS SQL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data; | |
using System.Threading.Tasks; | |
using Dapper; | |
public static class IDbConnectionExtensions | |
{ | |
public static async Task<DateTime> DbGeneratedFakeDateOfBirthAsync(this IDbConnection db, int range = 100) | |
{ | |
if (range < 1 || range > 100) | |
{ | |
range = 100; | |
} | |
return await db.QueryFirstAsync<DateTime>( | |
@"SELECT DATEADD(YEAR, RAND() * @Range * -1, GETDATE())", new | |
{ | |
Range = range | |
}); | |
} | |
public static async Task<string> DbGeneratedFakeNameAsync(this IDbConnection db, string baseName) | |
{ | |
return await db.QueryFirstAsync<string>( | |
@"SELECT @Name + CONVERT(varchar(20), ROUND(RAND() * 1000000, 0))", new | |
{ | |
Name = baseName ?? "Name" | |
}); | |
} | |
public static async Task<string> DbGeneratedFakeSSNAsync(this IDbConnection db) | |
{ | |
return await db.QueryFirstAsync<string>( | |
@"SELECT ROUND(RAND() * 10000000, 0) + 990000000"); | |
} | |
public static async Task<string> FakeTaxIdAsync(this IDbConnection db) | |
{ | |
return await db.QueryFirstAsync<string>( | |
@"SELECT CONVERT(char(9),CONVERT(bigint, (ROUND(ROUND(RAND() * 10000000, 0) + 720000000, 0))))"); | |
} | |
public static async Task<string> DbGeneratedFakeIdentifierAsync(this IDbConnection db) | |
{ | |
return await db.QueryFirstAsync<string>( | |
@"DECLARE @alphabet varchar(100) = 'ACDEFGHJKMNPQRTUVWXY' | |
DECLARE @alphanumeric varchar(100) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' | |
DECLARE @numeric19 varchar(10) = '123456789' | |
DECLARE @numeric09 varchar(10) = '0123456789' | |
SELECT RIGHT(LEFT(@numeric19,ABS(BINARY_CHECKSUM(NEWID()) % 9) + 1 ), 1) + -- Position 1 – numeric values 1 thru 9 | |
RIGHT(LEFT(@alphabet,ABS(BINARY_CHECKSUM(NEWID()) % 20) + 1 ), 1) + -- Position 2 – alphabetic values A thru Z (minus S, L, O, I, B, Z) | |
RIGHT(LEFT(@alphanumeric,ABS(BINARY_CHECKSUM(NEWID()) % 36) + 1 ), 1) + -- Position 3 – alpha-numeric values 0 thru 9 and A thru Z (minus S, L, O, I, B, Z) | |
RIGHT(LEFT(@numeric09,ABS(BINARY_CHECKSUM(NEWID()) % 10) + 1 ), 1) + -- Position 4 – numeric values 0 thru 9 | |
RIGHT(LEFT(@alphabet,ABS(BINARY_CHECKSUM(NEWID()) % 20) + 1 ), 1) + -- Position 5 – alphabetic values A thru Z (minus S, L, O, I, B, Z) | |
RIGHT(LEFT(@alphanumeric,ABS(BINARY_CHECKSUM(NEWID()) % 36) + 1 ), 1) + -- Position 6 – alpha-numeric values 0 thru 9 and A thru Z (minus S, L, O, I, B, Z) | |
RIGHT(LEFT(@numeric09,ABS(BINARY_CHECKSUM(NEWID()) % 10) + 1 ), 1) + -- Position 7 – numeric values 0 thru 9 | |
RIGHT(LEFT(@alphabet,ABS(BINARY_CHECKSUM(NEWID()) % 20) + 1 ), 1) + -- Position 8 – alphabetic values A thru Z (minus S, L, O, I, B, Z) | |
RIGHT(LEFT(@alphabet,ABS(BINARY_CHECKSUM(NEWID()) % 20) + 1 ), 1) + -- Position 9 – alphabetic values A thru Z (minus S, L, O, I, B, Z) | |
RIGHT(LEFT(@numeric09,ABS(BINARY_CHECKSUM(NEWID()) % 10) + 1 ), 1) + -- Position 10 – numeric values 0 thru 9 | |
RIGHT(LEFT(@numeric09,ABS(BINARY_CHECKSUM(NEWID()) % 10) + 1 ), 1) -- Position 11 – numeric values 0 thru 9" | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment