|
12 | 12 | // limitations under the License.
|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
| 15 | +using System; |
15 | 16 | using System.IO;
|
16 | 17 | using System.Collections.Generic;
|
17 | 18 | using System.Management.Automation;
|
@@ -45,43 +46,63 @@ protected override AzureSqlDataSyncAdapter InitModelAdapter(IAzureSubscription s
|
45 | 46 | /// </summary>
|
46 | 47 | /// <param name="filePath">The path of the schema file</param>
|
47 | 48 | /// <returns>A schema object of member database</returns>
|
48 |
| - protected AzureSqlSyncGroupSchemaModel ConstructSchemaFromFile(string filePath) |
| 49 | + protected static AzureSqlSyncGroupSchemaModel ConstructSchemaFromFile(string filePath) |
49 | 50 | {
|
50 |
| - AzureSqlSyncGroupSchemaModel schema = new AzureSqlSyncGroupSchemaModel(); |
51 | 51 | try
|
52 | 52 | {
|
53 | 53 | JObject jSchema = JObject.Parse(File.ReadAllText(filePath));
|
54 |
| - schema.MasterSyncMemberName = jSchema["masterSyncMemberName"] == null ? null : jSchema["masterSyncMemberName"].ToString(); |
55 |
| - List<AzureSqlSyncGroupSchemaTableModel> tables = new List<AzureSqlSyncGroupSchemaTableModel>(); |
56 |
| - JArray jTables = (JArray)jSchema["tables"]; |
57 |
| - if (jTables != null) |
| 54 | + return ConstructSchemaFromJObject(jSchema); |
| 55 | + } |
| 56 | + catch (Newtonsoft.Json.JsonReaderException) |
| 57 | + { |
| 58 | + throw new PSArgumentException("The schema file is empty or invalid!", "SchemaFile"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Construct schema for schema object |
| 64 | + /// </summary> |
| 65 | + /// <param name="jSchema">JObject containing description of the schema</param> |
| 66 | + /// <returns>A schema object of member database</returns> |
| 67 | + public static AzureSqlSyncGroupSchemaModel ConstructSchemaFromJObject(JObject jSchema) |
| 68 | + { |
| 69 | + AzureSqlSyncGroupSchemaModel schema = new AzureSqlSyncGroupSchemaModel(); |
| 70 | + JToken masterSyncMemberName = jSchema.GetValue("masterSyncMemberName", StringComparison.InvariantCultureIgnoreCase); |
| 71 | + schema.MasterSyncMemberName = masterSyncMemberName == null ? null : masterSyncMemberName.ToString(); |
| 72 | + List<AzureSqlSyncGroupSchemaTableModel> tables = new List<AzureSqlSyncGroupSchemaTableModel>(); |
| 73 | + JArray jTables = (JArray)jSchema.GetValue("tables", StringComparison.InvariantCultureIgnoreCase); |
| 74 | + if (jTables != null) |
| 75 | + { |
| 76 | + foreach (var jTableToken in jTables.Children()) |
58 | 77 | {
|
59 |
| - foreach (var jTable in jTables.Children()) |
| 78 | + if (jTableToken.Type == JTokenType.Object) |
60 | 79 | {
|
| 80 | + JObject jTable = (JObject)jTableToken; |
61 | 81 | AzureSqlSyncGroupSchemaTableModel table = new AzureSqlSyncGroupSchemaTableModel();
|
62 |
| - table.QuotedName = jTable["quotedName"] == null ? null : jTable["quotedName"].ToString(); |
| 82 | + JToken tableQuotedNameToken = jTable.GetValue("quotedName", StringComparison.InvariantCultureIgnoreCase); |
| 83 | + table.QuotedName = tableQuotedNameToken == null ? null : tableQuotedNameToken.ToString(); |
63 | 84 | List<AzureSqlSyncGroupSchemaColumnModel> columns = new List<AzureSqlSyncGroupSchemaColumnModel>();
|
64 |
| - JArray jColumns = (JArray)jTable["columns"]; |
| 85 | + JArray jColumns = (JArray)jTable.GetValue("columns", StringComparison.InvariantCultureIgnoreCase); |
65 | 86 | if (jColumns != null)
|
66 | 87 | {
|
67 |
| - foreach (var jColumn in jColumns.Children()) |
| 88 | + foreach (var jColumnToken in jColumns.Children()) |
68 | 89 | {
|
69 |
| - AzureSqlSyncGroupSchemaColumnModel column = new AzureSqlSyncGroupSchemaColumnModel(); |
70 |
| - column.QuotedName = jColumn["quotedName"] == null ? null : jColumn["quotedName"].ToString(); |
71 |
| - columns.Add(column); |
| 90 | + if (jColumnToken.Type == JTokenType.Object) |
| 91 | + { |
| 92 | + AzureSqlSyncGroupSchemaColumnModel column = new AzureSqlSyncGroupSchemaColumnModel(); |
| 93 | + JToken columnQuotedNameToken = ((JObject)jColumnToken).GetValue("quotedName", StringComparison.InvariantCultureIgnoreCase); |
| 94 | + column.QuotedName = columnQuotedNameToken == null ? null : columnQuotedNameToken.ToString(); |
| 95 | + columns.Add(column); |
| 96 | + } |
72 | 97 | }
|
73 | 98 | }
|
74 | 99 | table.Columns = columns;
|
75 | 100 | tables.Add(table);
|
76 | 101 | }
|
77 | 102 | }
|
78 |
| - schema.Tables = tables; |
79 |
| - return schema; |
80 |
| - } |
81 |
| - catch (Newtonsoft.Json.JsonReaderException) |
82 |
| - { |
83 |
| - throw new PSArgumentException("The schema file is empty or invalid!", "SchemaFile"); |
84 | 103 | }
|
| 104 | + schema.Tables = tables; |
| 105 | + return schema; |
85 | 106 | }
|
86 | 107 | }
|
87 | 108 | }
|
0 commit comments