public static void GeneratePropertyEnum(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); var entityConfig = config.GetEntityConfig(table.Name); var className = entityConfig.ClassName; var properties = GetProperties(table, config, entityConfig); // Definition buffer.Append(@"public"); buffer.Append(@" "); buffer.Append(@"enum"); buffer.Append(@" "); buffer.Append(className); buffer.Append(@"Property"); buffer.AppendLine(); buffer.AppendLine(@"{"); // Entries foreach (var property in properties) { if (property.Column.IsPrimaryKey) continue; buffer.Append(property.Name); buffer.Append(@","); buffer.AppendLine(); } buffer.AppendLine(@"}"); }
private static KeyValuePair<string, bool> MapType(Column column, ProjectConfig config) { if (column.ForeignKey == null) { return MapType(column.Type, column.IsNullable); } return new KeyValuePair<string, bool>(config.GetEntityConfig(column.ForeignKey.TableName).ClassName, true); }
public static void GenerateSortOptionsInitialization(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); var entityConfig = config.GetEntityConfig(table.Name); var properties = GetProperties(table, config, entityConfig); Generate(buffer, properties, p => string.Format(@"this.{0}Option = new SortOption({1}Caption, {2}Property.{0});", p.Name, p.VariableName, entityConfig.ClassName)); }
public static void GenerateSortOptionsProperties(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); var entityConfig = config.GetEntityConfig(table.Name); var properties = GetProperties(table, config, entityConfig); Generate(buffer, properties, p => string.Format(@"public SortOption {0}Option {{ get; }}", p.Name)); }
public static void GenerateParametersClass(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); var entityConfig = config.GetEntityConfig(table.Name); var className = entityConfig.ClassName + @"Parameters"; GenerateClass(buffer, className, new List<Property>(0), null); }
public static void GenerateClass(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); var entityConfig = config.GetEntityConfig(table.Name); var className = entityConfig.ClassName; var properties = GetProperties(table, config, entityConfig); GenerateClass(buffer, className, properties, AppendConstructor); }
public static void GenerateGetAllAsDictionary(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); const string template = @" public static Dictionary<{5}, {0}> Get{1}(IDbContext dbContext, DataCache cache) {{ if (dbContext == null) throw new ArgumentNullException(nameof(dbContext)); if (cache == null) throw new ArgumentNullException(nameof(cache)); var items = new Dictionary<{5}, {0}>(); {4} var query = new Query<{0}>(@""{2}"", r => {{ {3} }}); foreach (var item in dbContext.Execute(query)) {{ items.Add(item.{6}, item); }} return items; }}"; var entityConfig = config.GetEntityConfig(table.Name); var className = entityConfig.ClassName; var properties = GetProperties(table, config, entityConfig); var pk = properties.SingleOrDefault(p => p.Column.IsPrimaryKey); var primaryKeyType = pk.Type; var primaryKey = pk.Name; buffer.AppendFormat(template, className, entityConfig.ClassPluralName, SqlGenerator.Select(table, entityConfig), GetCreator(className, properties), GetDictionariesVariables(properties), primaryKeyType, primaryKey); }
public static Property From(Column column, ProjectConfig config) { var type = MapType(column, config); string name; EntityConfig entityConfig; if (column.ForeignKey == null) { name = NameProvider.GetPropertyName(column.Name); entityConfig = null; } else { name = type.Key; entityConfig = config.GetEntityConfig(column.ForeignKey.TableName); } return new Property(column, type.Key, type.Value, name, entityConfig); }
public static void GenerateSortOptionsArray(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); buffer.AppendLine(@"private SortOption[] SortOptions"); buffer.AppendLine(@"{"); buffer.AppendLine(@"get"); buffer.AppendLine(@"{"); buffer.AppendLine(@"return new[]"); buffer.AppendLine(@"{"); var entityConfig = config.GetEntityConfig(table.Name); var properties = GetProperties(table, config, entityConfig); Generate(buffer, properties, p => $@"this.{p.Name}Option,"); buffer.AppendLine(@"};"); buffer.AppendLine(@"}"); buffer.AppendLine(@"}"); }
public static void GenerateCaptionsClass(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); var entityConfig = config.GetEntityConfig(table.Name); var className = entityConfig.ClassName + @"Captions"; var properties = new List<Property>(table.Columns.Length); foreach (var property in GetProperties(table, config, entityConfig)) { if (property.Column.IsPrimaryKey) continue; var captionProperty = new Property(property.Column, MapType(SqlDataType.String), property.Name, property.ReferenceEntityConfig); properties.Add(captionProperty); } GenerateClass(buffer, className, properties, AppendConstructor); }
public static void GenerateIsTextMatchMethod(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); var entityConfig = config.GetEntityConfig(table.Name); var properties = GetProperties(table, config, entityConfig); buffer.AppendFormat(@"private static bool IsTextMatch({0}ViewModel viewModel, string searchText)", entityConfig.ClassName); buffer.AppendLine(); buffer.AppendLine(@"{"); buffer.AppendLine(@"return"); Generate(buffer, properties, p => string.Format(@"viewModel.{0}.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0 ||", p.Name)); var newLine = Environment.NewLine; buffer[buffer.Length - newLine.Length - 3] = ';'; buffer[buffer.Length - newLine.Length - 2] = ' '; buffer[buffer.Length - newLine.Length - 1] = ' '; buffer.AppendLine(@"}"); }
public static void GenerateViewModel(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); var entityConfig = config.GetEntityConfig(table.Name); var modelClassName = entityConfig.ClassName; var captionsClassName = modelClassName + @"Captions"; var className = modelClassName + @"ViewModel"; var properties = GetProperties(table, config, entityConfig) .Where(v => !v.Column.IsPrimaryKey) .SelectMany(v => new[] { new Property(v.Column, MapType(SqlDataType.String), v.Name, v.ReferenceEntityConfig), new Property(v.Column, MapType(SqlDataType.String), v.Name, v.ReferenceEntityConfig, true), }).ToList(); properties.Insert(0, new Property(null, new KeyValuePair<string, bool>(modelClassName, true), @"Model", null)); properties.Insert(1, new Property(null, new KeyValuePair<string, bool>(captionsClassName, true), @"Captions", null, generateField: false)); GenerateClass(buffer, className, properties, AppendViewModelConstructor, @"ViewModel"); }
public static void GenerateSortMethod(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); var entityConfig = config.GetEntityConfig(table.Name); var properties = GetProperties(table, config, entityConfig); buffer.AppendFormat(@"private static void Sort(List<{0}ViewModel> viewModels, SortOption sortOption, {0}Property property)", entityConfig.ClassName); buffer.AppendLine(); buffer.AppendLine(@"{"); buffer.AppendLine(@"switch (property)"); buffer.AppendLine(@"{"); Generate(buffer, properties, p => { var column = p.Column; if (column.Type == SqlDataType.DateTime && column.IsNullable) { return string.Format(@"case {1}Property.{0}: viewModels.Sort((x, y) => {{ var cmp = (x.Model.{0} ?? DateTime.MinValue).CompareTo((y.Model.{0} ?? DateTime.MinValue)); if (cmp == 0) {{ cmp = x.Model.Id.CompareTo(y.Model.Id); }} return cmp; }}); break;", p.Name, entityConfig.ClassName); } switch (column.Type) { case SqlDataType.Int: case SqlDataType.Long: case SqlDataType.Decimal: case SqlDataType.DateTime: return string.Format(@"case {1}Property.{0}: viewModels.Sort((x, y) => {{ var cmp = x.Model.{0}.CompareTo(y.Model.{0}); if (cmp == 0) {{ cmp = x.Model.Id.CompareTo(y.Model.Id); }} return cmp; }}); break;", p.Name, entityConfig.ClassName); } // Sort by string return string.Format(@"case {1}Property.{0}: viewModels.Sort((x, y) => string.Compare(x.{0}, y.{0}, StringComparison.OrdinalIgnoreCase)); break;", p.Name, entityConfig.ClassName); }); buffer.AppendLine(@"default:"); buffer.AppendLine(@"throw new ArgumentOutOfRangeException();"); buffer.AppendLine(@"}"); buffer.AppendLine(); buffer.AppendLine(@"if ((sortOption.SortDirection ?? SortDirection.Asc) == SortDirection.Desc)"); buffer.AppendLine(@"{"); buffer.AppendLine(@"viewModels.Reverse();"); buffer.AppendLine(@"}"); buffer.AppendLine(@"}"); }
public static void GenerateGetAll(StringBuilder buffer, Table table, ProjectConfig config) { if (buffer == null) throw new ArgumentNullException(nameof(buffer)); if (table == null) throw new ArgumentNullException(nameof(table)); if (config == null) throw new ArgumentNullException(nameof(config)); var entityConfig = config.GetEntityConfig(table.Name); var className = entityConfig.ClassName; var properties = GetProperties(table, config, entityConfig); var dictionaries = GetDictionariesVariables(properties); if (dictionaries == string.Empty) { const string template = @" public static IEnumerable<{0}> Get{1}(IDbContext dbContext) {{ if (dbContext == null) throw new ArgumentNullException(nameof(dbContext)); var query = new Query<{0}>(@""{2}"", r => {{ {3} }}); return dbContext.Execute(query); }}"; buffer.AppendFormat(template, className, entityConfig.ClassPluralName, SqlGenerator.Select(table, entityConfig), GetCreator(className, properties)); } else { const string template = @" public static IEnumerable<{0}> Get{1}(IDbContext dbContext, DataCache cache) {{ if (dbContext == null) throw new ArgumentNullException(nameof(dbContext)); if (cache == null) throw new ArgumentNullException(nameof(cache)); var items = new List<{0}>(); {4} var query = new Query<{0}>(@""{2}"", r => {{ {3} }}); foreach (var item in dbContext.Execute(query)) {{ items.Add(item); }} return items; }}"; buffer.AppendFormat(template, className, entityConfig.ClassPluralName, SqlGenerator.Select(table, entityConfig), GetCreator(className, properties), dictionaries); } }
static void Main(string[] args) { var path = @"C:\Users\PetarPetrov\AppData\Local\Packages\9ed4bf97-9c34-45dc-a217-5f8121aa6dfc_7gbmn9e1bm2jj\LocalState\ifsa.sqlite"; //path = @"C:\Users\PetarPetrov\Desktop\app_studio.sqlite"; var projectConfig = new ProjectConfig(); projectConfig.Add(new EntityConfig(@"ACTIVATION_COMPLIANCES")); // TODO : !!! //projectConfig.Load(null); // load to a file //projectConfig.Save(null); // save to a file var cnString = $@"Data Source = {path}; Version = 3; DateTimeFormat=Ticks;"; var classes = new StringBuilder(); var dataProvider = new StringBuilder(); var register = new StringBuilder(); using (var dbContext = new DbContext(cnString)) { foreach (var table in DataProvider.GetTables(dbContext)) { if (table.Name.Contains('$') || table.Name.IndexOf(@"Asset_Class", StringComparison.OrdinalIgnoreCase) >= 0 || table.Name.IndexOf(@"open_balance", StringComparison.OrdinalIgnoreCase) >= 0 || table.Name.IndexOf(@"factory_cal", StringComparison.OrdinalIgnoreCase) >= 0 || table.Name.IndexOf(@"Visit_dat", StringComparison.OrdinalIgnoreCase) >= 0 || table.Name.IndexOf(@"Equipment", StringComparison.OrdinalIgnoreCase) >= 0 || table.Name.IndexOf(@"Temp_data", StringComparison.OrdinalIgnoreCase) >= 0) { continue; } var buffer = new StringBuilder(4 * 1024); buffer.Clear(); CodeGenerator.GenerateClass(buffer, table, projectConfig); buffer.AppendLine(); //CodeGenerator.GenerateCaptionsClass(buffer, table, projectConfig); //buffer.AppendLine(); //CodeGenerator.GenerateViewModel(buffer, table, projectConfig); //buffer.AppendLine(); //CodeGenerator.GeneratePropertyEnum(buffer, table, projectConfig); //buffer.AppendLine(); //CodeGenerator.GenerateParametersClass(buffer, table, projectConfig); //buffer.AppendLine(); //CodeGenerator.GenerateSortOptionsArray(buffer, table, projectConfig); //buffer.AppendLine(); //CodeGenerator.GenerateSortOptionsProperties(buffer, table, projectConfig); //buffer.AppendLine(); //CodeGenerator.GenerateSortOptionsInitialization(buffer, table, projectConfig); //buffer.AppendLine(); //CodeGenerator.GenerateIsTextMatchMethod(buffer, table, projectConfig); //buffer.AppendLine(); //CodeGenerator.GenerateSortMethod(buffer, table, projectConfig); //buffer.AppendLine(); Console.WriteLine(); Console.WriteLine(buffer.ToString()); classes.AppendLine(buffer.ToString()); buffer.Clear(); CodeGenerator.GenerateGetAll(buffer, table, projectConfig); dataProvider.AppendLine(buffer.ToString()); //register.AppendLine($@"Console.Write(""{table.Name}"" + "" : "");"); //register.AppendLine($@"Console.WriteLine(DataProviders.Get{projectConfig.GetEntityConfig(table.Name).ClassPluralName}(dbContext, cache).Count);"); register.AppendLine($@"s.Add(""{table.Name}"", DataProviders.Get{projectConfig.GetEntityConfig(table.Name).ClassPluralName}(dbContext, cache).Count);"); } var cache = new DataCache(); //cache.Register(DataProviders.GetBrandKinds); //cache.Register(DataProviders.GetBrands); //cache.Register(DataProviders.GetFlavours); //var arts = DataProviders.GetArticles(ctx, cache); //Console.WriteLine(arts.Count); var s = new Dictionary<string, int>(); var vals = s .Where(v => v.Value != 0) .Select(v => Tuple.Create(v.Key, v.Value)) .ToList(); vals.Sort((x, y) => { var cmp = y.Item2.CompareTo(x.Item2); if (cmp == 0) { cmp = string.Compare(x.Item1, y.Item1, StringComparison.OrdinalIgnoreCase); } return cmp; } ); foreach (var v in vals) { Console.WriteLine(v); } dbContext.Complete(); } var code = classes.ToString(); var data = dataProvider.ToString(); //Console.WriteLine(code.Length); //Console.WriteLine(data.Length); foreach (var s in register.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)) { Debug.WriteLine(s); } //Console.WriteLine(code); //Console.WriteLine(data); //return; // File.WriteAllText(@"C:\Atos\AppStudio\AppStudio\Objects.cs", string.Format(@"using System; //using AppCore.ViewModels; //namespace AppStudio //{{ // {0} //}} //", code)); // File.WriteAllText(@"C:\Atos\AppStudio\AppStudio\DataProviders.cs", string.Format(@"using System; //using System.Collections.Generic; //using AppCore; //using AppCore.Data; //namespace AppStudio //{{ // public static class DataProviders // {{ // {0} // }} //}} //", data)); }