|
| 1 | +namespace PowerSync.Common.DB.Schema; |
| 2 | + |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | + |
| 6 | +using Newtonsoft.Json; |
| 7 | + |
| 8 | +class CompiledTable |
| 9 | +{ |
| 10 | + public static readonly Regex InvalidSQLCharacters = new Regex(@"[""'%,.#\s\[\]]", RegexOptions.Compiled); |
| 11 | + |
| 12 | + public string Name { get; init; } = null!; |
| 13 | + protected TableOptions Options { get; init; } = null!; |
| 14 | + public IReadOnlyDictionary<string, ColumnType> Columns { get; init; } |
| 15 | + public IReadOnlyDictionary<string, List<string>> Indexes { get; init; } |
| 16 | + |
| 17 | + private readonly ColumnJSON[] ColumnsJSON; |
| 18 | + private readonly IndexJSON[] IndexesJSON; |
| 19 | + |
| 20 | + public CompiledTable(string name, Dictionary<string, ColumnType> columns, TableOptions options) |
| 21 | + { |
| 22 | + ColumnsJSON = |
| 23 | + columns |
| 24 | + .Select(kvp => new ColumnJSON(new ColumnJSONOptions(kvp.Key, kvp.Value))) |
| 25 | + .ToArray(); |
| 26 | + |
| 27 | + IndexesJSON = |
| 28 | + (Options?.Indexes ?? []) |
| 29 | + .Select(kvp => |
| 30 | + new IndexJSON(new IndexJSONOptions( |
| 31 | + kvp.Key, |
| 32 | + kvp.Value.Select(name => |
| 33 | + new IndexedColumnJSON(new IndexedColumnJSONOptions( |
| 34 | + name.Replace("-", ""), !name.StartsWith("-"))) |
| 35 | + ).ToArray() |
| 36 | + )) |
| 37 | + ) |
| 38 | + .ToArray(); |
| 39 | + |
| 40 | + Name = name; |
| 41 | + Columns = columns; |
| 42 | + Options = options; |
| 43 | + Indexes = Options?.Indexes ?? []; |
| 44 | + } |
| 45 | + |
| 46 | + public void Validate() |
| 47 | + { |
| 48 | + if (string.IsNullOrWhiteSpace(Name)) |
| 49 | + { |
| 50 | + throw new Exception($"Table name is required."); |
| 51 | + } |
| 52 | + |
| 53 | + if (!string.IsNullOrWhiteSpace(Options.ViewName) && InvalidSQLCharacters.IsMatch(Options.ViewName!)) |
| 54 | + { |
| 55 | + throw new Exception($"Invalid characters in view name: {Options.ViewName}"); |
| 56 | + } |
| 57 | + |
| 58 | + if (Columns.Count > Table.MAX_AMOUNT_OF_COLUMNS) |
| 59 | + { |
| 60 | + throw new Exception( |
| 61 | + $"Table has too many columns. The maximum number of columns is {Table.MAX_AMOUNT_OF_COLUMNS}."); |
| 62 | + } |
| 63 | + |
| 64 | + if (Options.TrackMetadata && Options.LocalOnly) |
| 65 | + { |
| 66 | + throw new Exception("Can't include metadata for local-only tables."); |
| 67 | + } |
| 68 | + |
| 69 | + if (Options.TrackPreviousValues != null && Options.LocalOnly) |
| 70 | + { |
| 71 | + throw new Exception("Can't include old values for local-only tables."); |
| 72 | + } |
| 73 | + |
| 74 | + var columnNames = new HashSet<string> { "id" }; |
| 75 | + |
| 76 | + foreach (var columnName in Columns.Keys) |
| 77 | + { |
| 78 | + if (columnName == "id") |
| 79 | + { |
| 80 | + throw new Exception("An id column is automatically added, custom id columns are not supported"); |
| 81 | + } |
| 82 | + |
| 83 | + if (InvalidSQLCharacters.IsMatch(columnName)) |
| 84 | + { |
| 85 | + throw new Exception($"Invalid characters in column name: {columnName}"); |
| 86 | + } |
| 87 | + |
| 88 | + columnNames.Add(columnName); |
| 89 | + } |
| 90 | + |
| 91 | + foreach (var kvp in Indexes) |
| 92 | + { |
| 93 | + var indexName = kvp.Key; |
| 94 | + var indexColumns = kvp.Value; |
| 95 | + |
| 96 | + if (InvalidSQLCharacters.IsMatch(indexName)) |
| 97 | + { |
| 98 | + throw new Exception($"Invalid characters in index name: {indexName}"); |
| 99 | + } |
| 100 | + |
| 101 | + foreach (var indexColumn in indexColumns) |
| 102 | + { |
| 103 | + if (!columnNames.Contains(indexColumn)) |
| 104 | + { |
| 105 | + throw new Exception($"Column {indexColumn} not found for index {indexName}"); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public string ToJSON(string Name = "") |
| 112 | + { |
| 113 | + var trackPrevious = Options.TrackPreviousValues; |
| 114 | + |
| 115 | + var jsonObject = new |
| 116 | + { |
| 117 | + view_name = Options.ViewName ?? Name, |
| 118 | + local_only = Options.LocalOnly, |
| 119 | + insert_only = Options.InsertOnly, |
| 120 | + columns = ColumnsJSON.Select(c => c.ToJSONObject()).ToList(), |
| 121 | + indexes = IndexesJSON.Select(i => i.ToJSONObject(this)).ToList(), |
| 122 | + |
| 123 | + include_metadata = Options.TrackMetadata, |
| 124 | + ignore_empty_update = Options.IgnoreEmptyUpdates, |
| 125 | + include_old = (object)(trackPrevious switch |
| 126 | + { |
| 127 | + null => false, |
| 128 | + { Columns: null } => true, |
| 129 | + { Columns: var cols } => cols |
| 130 | + }), |
| 131 | + include_old_only_when_changed = trackPrevious?.OnlyWhenChanged ?? false |
| 132 | + }; |
| 133 | + |
| 134 | + return JsonConvert.SerializeObject(jsonObject); |
| 135 | + } |
| 136 | +} |
0 commit comments