1
1
namespace PowerSync . Common . DB . Schema ;
2
2
3
+ using System . Text . RegularExpressions ;
3
4
using Newtonsoft . Json ;
4
5
5
6
public class TableOptions (
@@ -19,7 +20,10 @@ public class TableOptions(
19
20
20
21
public class Table
21
22
{
22
- protected TableOptions Options { get ; set ; }
23
+ public static readonly Regex InvalidSQLCharacters = new Regex ( @"[""'%,.#\s\[\]]" , RegexOptions . Compiled ) ;
24
+
25
+
26
+ protected TableOptions Options { get ; init ; } = null ! ;
23
27
24
28
public Dictionary < string , ColumnType > Columns ;
25
29
public Dictionary < string , List < string > > Indexes ;
@@ -48,6 +52,53 @@ [.. kv.Value.Select(name =>
48
52
Indexes = Options ? . Indexes ?? [ ] ;
49
53
}
50
54
55
+ public void Validate ( )
56
+ {
57
+ if ( ! string . IsNullOrWhiteSpace ( Options . ViewName ) && InvalidSQLCharacters . IsMatch ( Options . ViewName ! ) )
58
+ {
59
+ throw new Exception ( $ "Invalid characters in view name: { Options . ViewName } ") ;
60
+ }
61
+
62
+ if ( Columns . Count > Column . MAX_AMOUNT_OF_COLUMNS )
63
+ {
64
+ throw new Exception ( $ "Table has too many columns. The maximum number of columns is { Column . MAX_AMOUNT_OF_COLUMNS } .") ;
65
+ }
66
+
67
+ var columnNames = new HashSet < string > { "id" } ;
68
+
69
+ foreach ( var columnName in Columns . Keys )
70
+ {
71
+ if ( columnName == "id" )
72
+ {
73
+ throw new Exception ( "An id column is automatically added, custom id columns are not supported" ) ;
74
+ }
75
+
76
+ if ( InvalidSQLCharacters . IsMatch ( columnName ) )
77
+ {
78
+ throw new Exception ( $ "Invalid characters in column name: { columnName } ") ;
79
+ }
80
+
81
+ columnNames . Add ( columnName ) ;
82
+ }
83
+
84
+ foreach ( var ( indexName , indexColumns ) in Indexes )
85
+ {
86
+
87
+ if ( InvalidSQLCharacters . IsMatch ( indexName ) )
88
+ {
89
+ throw new Exception ( $ "Invalid characters in index name: { indexName } ") ;
90
+ }
91
+
92
+ foreach ( var indexColumn in indexColumns )
93
+ {
94
+ if ( ! columnNames . Contains ( indexColumn ) )
95
+ {
96
+ throw new Exception ( $ "Column { indexColumn } not found for index { indexName } ") ;
97
+ }
98
+ }
99
+ }
100
+ }
101
+
51
102
public string ToJSON ( string Name = "" )
52
103
{
53
104
var jsonObject = new
0 commit comments