|
| 1 | +using SqlServerSimulator.Storage; |
| 2 | +using System.Globalization; |
| 3 | + |
| 4 | +namespace SqlServerSimulator; |
| 5 | + |
| 6 | +internal static partial class BuiltInResources |
| 7 | +{ |
| 8 | + internal static readonly object?[][] SystypesRowData = |
| 9 | + [ |
| 10 | + ["image", 34, 0, 34, 16, 0, 0, 0, 0, 4, 0, null, 20, false, true, 34, null, null, null, null], |
| 11 | + ["text", 35, 0, 35, 16, 0, 0, 0, 0, 4, 0, 872468488, 19, false, true, 35, null, null, null, "SQL_Latin1_General_CP1_CI_AS"], |
| 12 | + ["uniqueidentifier", 36, 0, 36, 16, 0, 0, 0, 0, 4, 0, null, 0, false, true, 37, null, 16, null, null], |
| 13 | + ["date", 40, 0, 40, 3, 10, 0, 0, 0, 4, 0, null, 0, false, true, 0, null, 10, 0, null], |
| 14 | + ["time", 41, 0, 41, 5, 16, 7, 0, 0, 4, 0, null, 0, false, true, 0, null, 16, 7, null], |
| 15 | + ["datetime2", 42, 0, 42, 8, 27, 7, 0, 0, 4, 0, null, 0, false, true, 0, null, 27, 7, null], |
| 16 | + ["datetimeoffset", 43, 0, 43, 10, 34, 7, 0, 0, 4, 0, null, 0, false, true, 0, null, 34, 7, null], |
| 17 | + ["tinyint", 48, 0, 48, 1, 3, 0, 0, 0, 4, 0, null, 5, false, true, 48, null, 3, 0, null], |
| 18 | + ["smallint", 52, 0, 52, 2, 5, 0, 0, 0, 4, 0, null, 6, false, true, 52, null, 5, 0, null], |
| 19 | + ["int", 56, 0, 56, 4, 10, 0, 0, 0, 4, 0, null, 7, false, true, 56, null, 10, 0, null], |
| 20 | + ["smalldatetime", 58, 0, 58, 4, 16, 0, 0, 0, 4, 0, null, 22, false, true, 58, null, 16, 0, null], |
| 21 | + ["real", 59, 0, 59, 4, 24, 0, 0, 0, 4, 0, null, 23, false, true, 59, null, 24, null, null], |
| 22 | + ["money", 60, 0, 60, 8, 19, 4, 0, 0, 4, 0, null, 11, false, true, 60, null, 19, 4, null], |
| 23 | + ["datetime", 61, 0, 61, 8, 23, 3, 0, 0, 4, 0, null, 12, false, true, 61, null, 23, 3, null], |
| 24 | + ["float", 62, 0, 62, 8, 53, 0, 0, 0, 4, 0, null, 8, false, true, 62, null, 53, null, null], |
| 25 | + ["sql_variant", 98, 0, 98, 8016, 0, 0, 0, 0, 4, 0, null, 0, false, true, 39, null, 0, null, null], |
| 26 | + ["ntext", 99, 0, 99, 16, 0, 0, 0, 0, 4, 0, 872468488, 0, false, true, 35, null, null, null, "SQL_Latin1_General_CP1_CI_AS"], |
| 27 | + ["bit", 104, 0, 104, 1, 1, 0, 0, 0, 4, 0, null, 16, false, true, 50, null, 1, null, null], |
| 28 | + ["decimal", 106, 0, 106, 17, 38, 38, 0, 0, 4, 0, null, 24, false, true, 55, null, 38, 38, null], |
| 29 | + ["numeric", 108, 0, 108, 17, 38, 38, 0, 0, 4, 0, null, 10, false, true, 63, null, 38, 38, null], |
| 30 | + ["smallmoney", 122, 0, 122, 4, 10, 4, 0, 0, 4, 0, null, 21, false, true, 122, null, 10, 4, null], |
| 31 | + ["bigint", 127, 0, 127, 8, 19, 0, 0, 0, 4, 0, null, 0, false, true, 63, null, 19, 0, null], |
| 32 | + ["hierarchyid", 240, 0, 128, 892, 0, 0, 0, 0, 4, 0, null, 0, false, true, 0, null, 892, null, null], |
| 33 | + ["geometry", 240, 0, 129, -1, 0, 0, 0, 0, 4, 0, null, 0, false, true, 0, null, -1, null, null], |
| 34 | + ["geography", 240, 0, 130, -1, 0, 0, 0, 0, 4, 0, null, 0, false, true, 0, null, -1, null, null], |
| 35 | + ["varbinary", 165, 0, 165, 8000, 0, 0, 0, 0, 4, 0, null, 4, true, true, 37, null, 8000, null, null], |
| 36 | + ["varchar", 167, 0, 167, 8000, 0, 0, 0, 0, 4, 0, 872468488, 2, true, true, 39, null, 8000, null, "SQL_Latin1_General_CP1_CI_AS"], |
| 37 | + ["binary", 173, 0, 173, 8000, 0, 0, 0, 0, 4, 0, null, 3, false, true, 45, null, 8000, null, null], |
| 38 | + ["char", 175, 0, 175, 8000, 0, 0, 0, 0, 4, 0, 872468488, 1, false, true, 47, null, 8000, null, "SQL_Latin1_General_CP1_CI_AS"], |
| 39 | + ["timestamp", 189, 1, 189, 8, 0, 0, 0, 0, 4, 0, null, 80, false, false, 45, null, 8, null, null], |
| 40 | + ["nvarchar", 231, 0, 231, 8000, 0, 0, 0, 0, 4, 0, 872468488, 0, true, true, 39, null, 4000, null, "SQL_Latin1_General_CP1_CI_AS"], |
| 41 | + ["nchar", 239, 0, 239, 8000, 0, 0, 0, 0, 4, 0, 872468488, 0, false, true, 47, null, 4000, null, "SQL_Latin1_General_CP1_CI_AS"], |
| 42 | + ["xml", 241, 0, 241, -1, 0, 0, 0, 0, 4, 0, null, 0, false, true, 0, null, -1, null, null], |
| 43 | + ["sysname", 231, 1, 256, 256, 0, 0, 0, 0, 4, 0, 872468488, 18, true, false, 39, null, 128, null, "SQL_Latin1_General_CP1_CI_AS"], |
| 44 | + ]; |
| 45 | + |
| 46 | + public static readonly Lazy<Dictionary<string, HeapTable>> SystemHeapTables = new(BuildSystemHeapTables); |
| 47 | + |
| 48 | + private static Dictionary<string, HeapTable> BuildSystemHeapTables() |
| 49 | + { |
| 50 | + HeapColumn[] systypesColumns = |
| 51 | + [ |
| 52 | + new("name", SqlType.SystemName, 128, false), |
| 53 | + new("xtype", SqlType.TinyInt, null, false), |
| 54 | + new("status", SqlType.TinyInt, null, true), |
| 55 | + new("xusertype", SqlType.SmallInt, null, true), |
| 56 | + new("length", SqlType.SmallInt, null, false), |
| 57 | + new("xprec", SqlType.TinyInt, null, false), |
| 58 | + new("xscale", SqlType.TinyInt, null, false), |
| 59 | + new("tdefault", SqlType.Int32, null, false), |
| 60 | + new("domain", SqlType.Int32, null, false), |
| 61 | + new("uid", SqlType.SmallInt, null, true), |
| 62 | + new("reserved", SqlType.SmallInt, null, true), |
| 63 | + new("collationid", SqlType.Int32, null, true), |
| 64 | + new("usertype", SqlType.SmallInt, null, true), |
| 65 | + new("variable", SqlType.Bit, null, false), |
| 66 | + new("allownulls", SqlType.Bit, null, true), |
| 67 | + new("type", SqlType.TinyInt, null, false), |
| 68 | + new("printfmt", SqlType.Varchar, 255, true), |
| 69 | + new("prec", SqlType.SmallInt, null, true), |
| 70 | + new("scale", SqlType.TinyInt, null, true), |
| 71 | + new("collation", SqlType.SystemName, 128, true), |
| 72 | + ]; |
| 73 | + // System tables live outside any user database's id space — they're |
| 74 | + // process-shared and the simulator doesn't expose them via OBJECT_ID |
| 75 | + // (which routes through per-DB schema resolution). A small negative |
| 76 | + // id keeps them distinguishable in debug output. |
| 77 | + var systypes = new HeapTable("systypes", systypesColumns, objectId: -1); |
| 78 | + |
| 79 | + foreach (var row in SystypesRowData) |
| 80 | + { |
| 81 | + var values = new SqlValue[systypesColumns.Length]; |
| 82 | + for (var i = 0; i < systypesColumns.Length; i++) |
| 83 | + values[i] = ObjectToSqlValue(row[i], systypesColumns[i].Type); |
| 84 | + |
| 85 | + _ = systypes.Heap.Insert(RowEncoder.EncodeRow(systypes.Schema, values)); |
| 86 | + } |
| 87 | + |
| 88 | + return new(BuiltInToken.Comparer) { [systypes.Name] = systypes }; |
| 89 | + } |
| 90 | + |
| 91 | + private static SqlValue ObjectToSqlValue(object? value, SqlType type) => |
| 92 | + value is null ? SqlValue.Null(type) |
| 93 | + : type == SqlType.TinyInt ? SqlValue.FromByte(Convert.ToByte(value, CultureInfo.InvariantCulture)) |
| 94 | + : type == SqlType.SmallInt ? SqlValue.FromInt16(Convert.ToInt16(value, CultureInfo.InvariantCulture)) |
| 95 | + : type == SqlType.Int32 ? SqlValue.FromInt32(Convert.ToInt32(value, CultureInfo.InvariantCulture)) |
| 96 | + : type == SqlType.Bit ? SqlValue.FromBoolean((bool)value) |
| 97 | + : type is VarcharSqlType ? SqlValue.FromVarchar((string)value) |
| 98 | + : type == SqlType.SystemName ? SqlValue.FromSystemName((string)value) |
| 99 | + : throw new NotSupportedException($"Built-in resource materializer doesn't know how to convert {value.GetType().Name} to {type}."); |
| 100 | +} |
0 commit comments