/*================================================================================================= * CONSTANTS *================================================================================================*/ /*PUBLIC******************************************************************************************/ /*PRIVATE*****************************************************************************************/ /*================================================================================================= * FIELDS *================================================================================================*/ /*PUBLIC******************************************************************************************/ /*PRIVATE*****************************************************************************************/ /*================================================================================================= * PROPERTIES *================================================================================================*/ /*PUBLIC******************************************************************************************/ /*PRIVATE*****************************************************************************************/ /*================================================================================================= * CONSTRUCTORS *================================================================================================*/ /*================================================================================================= * PUBLIC METHODS *================================================================================================*/ /*************************************************************************************************/ public static IDatabase GetDatabase(Database database) { IDatabase result = null; switch (database) { case Database.InMemory: result = new InMemoryDatabase(); break; case Database.CSV: result = new CsvDatabase( new CSVUserManager( new CSVReader(), new CSVWriter() ), new CSVPasswordManager( new CSVReader(), new CSVWriter() ) ); break; case Database.MySQL: result = new MySQLDatabase(); break; case Database.SQLLite: result = new SQLiteDatabase(); break; } return(result); }
private static List <ExplorerItem> GetSchema(CsvDatabase db) => db.Tables.Select(table => new ExplorerItem(table.DisplayName, ExplorerItemKind.QueryableObject, ExplorerIcon.Table) { DragText = table.CodeName, Tag = table.CodeName, IsEnumerable = true, ToolTipText = table.FilePath, Children = table.Columns .Select(column => new ExplorerItem(column.DisplayName, ExplorerItemKind.Property, ExplorerIcon.Column) { DragText = column.CodeName, ToolTipText = $"{column.Index}:{column.Name}" } ).Concat( table.Relations.Select(relation => new ExplorerItem(relation.DisplayName, ExplorerItemKind.CollectionLink, ExplorerIcon.ManyToMany) { DragText = relation.CodeName, ToolTipText = $"Relation to {relation.TargetTable.CodeName} where {relation.SourceTable.CodeName}.{relation.SourceColumn.CodeName} == {relation.TargetTable.CodeName}.{relation.TargetColumn.CodeName}" }) ).ToList() }).ToList();
/// <summary> /// Get LINQPad Schema from CSV data model /// </summary> /// <param name="db"></param> /// <returns></returns> private static List <ExplorerItem> GetSchema(CsvDatabase db, ICsvDataContextDriverProperties props) { var schema = ( from table in db.Tables ?? Enumerable.Empty <CsvTable>() select new ExplorerItem(table.DisplayName, ExplorerItemKind.QueryableObject, ExplorerIcon.Table) { DragText = table.CodeName, IsEnumerable = true, ToolTipText = table.FilePath, Children = ( from c in table.Columns ?? Enumerable.Empty <CsvColumn>() select new ExplorerItem(c.DisplayName, ExplorerItemKind.Property, ExplorerIcon.Column) { DragText = c.CodeName, ToolTipText = (c.CsvColumnIndex + 1) + ":" + c.CsvColumnName, } ).Concat( from r in table.Relations select new ExplorerItem(r.DisplayName, ExplorerItemKind.CollectionLink, ExplorerIcon.ManyToMany) { DragText = r.CodeName, ToolTipText = string.Format("Relation to {0} where {1}.{2} == {3}.{4}", r.TargetTable.CodeName, r.SourceTable.CodeName, r.SourceColumn.CodeName, r.TargetTable.CodeName, r.TargetColumn.CodeName), } ).ToList(), } ).ToList(); return(schema); }
protected void SetUp() { var filePath = "csv-test.txt"; var database = new CsvDatabase(filePath); _searcher = new UserCondoPermissionsSearcher(database); }
public Application(IConfiguration configuration) { var fileToReadPath = configuration[FileToReadPath]; var fileToWritePath = configuration[FileToWritePath]; _database = new CsvDatabase(fileToReadPath); _searcher = new UserCondoPermissionsSearcher(_database); _writter = new CondoPermissionsWritter(fileToWritePath); }
internal static List <ExplorerItem> GetSchemaAndBuildAssembly(ICsvDataContextDriverProperties props, AssemblyName assemblyName, ref string nameSpace, ref string typeName) { Logger.LogEnabled = props.DebugInfo; Logger.Log("Build started: " + props.Files); var sw = Stopwatch.StartNew(); var sw2 = Stopwatch.StartNew(); CsvDatabase db = CsvDataModelGenerator.CreateModel(props); Logger.Log("Model created. ({0} ms)", sw2.ElapsedMilliseconds); sw2.Restart(); string code = CsvCSharpCodeGenerator.GenerateCode(db, ref nameSpace, ref typeName, props); Logger.Log("Code generated. ({0} ms)", sw2.ElapsedMilliseconds); sw2.Restart(); string[] compileErrors = BuildAssembly(code, assemblyName); Logger.Log("Assembly compiled. ({0} ms)", sw2.ElapsedMilliseconds); sw2.Restart(); List <ExplorerItem> schema = GetSchema(db, props); Logger.Log("Schema tree created. ({0} ms)", sw2.ElapsedMilliseconds); sw2.Restart(); bool anyCompileError = compileErrors != null && compileErrors.Any(); if (anyCompileError || props.DebugInfo) { schema.Insert(0, new ExplorerItem("Context Source Code", ExplorerItemKind.Schema, ExplorerIcon.Schema) { ToolTipText = "Data Context source code. Drag&drop to text window.", DragText = code, }); } if (anyCompileError) { Logger.Log("Errors: {0}", compileErrors.Length); schema.Insert(0, new ExplorerItem("Context Compile ERROR", ExplorerItemKind.Schema, ExplorerIcon.Box) { ToolTipText = "Data context compile failed. Drag&Drop error messages to text window to see them.", DragText = string.Join("\n", compileErrors), }); } if (db.Tables.Count == 0) { schema.Insert(0, new ExplorerItem("No files found.", ExplorerItemKind.Schema, ExplorerIcon.Box)); } Logger.Log("Tables: {0} Columns: {1} Relations: {2}", db.Tables.Count(), db.Tables.Sum(t => t.Columns.Count()), db.Tables.Sum(t => t.Relations.Count())); Logger.Log("Build finished. ({0} ms)", sw.ElapsedMilliseconds); Logger.Log(string.Join("\n", db.Tables.SelectMany(t => t.Relations.Select(r => r.CodeName)))); return(schema); }
public string GenerateSrcFile(CsvDatabase db) { var src = @"using System; using System.Linq; using System.Collections.Generic; namespace " + contextNameSpace + @" { /// <summary>CSV Data Context</summary> public class " + contextTypeName + @" : " + typeof(CsvDataContextBase).GetCodeTypeClassName() + @" { " + string.Join("", from table in db.Tables select @" /// <summary>File: " + System.Security.SecurityElement.Escape(table.FilePath) + @"</summary> public " + typeof(CsvTableBase <>).GetCodeTypeClassName(table.GetCodeRowClassName()) + @" " + table.CodeName + @" { get; private set; }" ) + @" public " + contextTypeName + @"() { //Init tables data " + string.Join("", from table in db.Tables select @" this." + table.CodeName + @" = " + typeof(CsvTableFactory).GetCodeTypeClassName() + @".CreateTable<" + table.GetCodeRowClassName() + @">( " + (properties.IsStringInternEnabled ? "true" : "false") + @", " + (properties.IsCacheEnabled ? "true" : "false") + @", " + table.CsvSeparator.GetCodeCharEscaped() + @", " + table.FilePath.GetCodeStringEscaped() + @", new " + typeof(CsvColumnInfoList <>).GetCodeTypeClassName(table.GetCodeRowClassName()) + @"() { " + string.Join("", from c in table.Columns select @" { " + c.CsvColumnIndex + @", x => x." + c.CodeName + @" }, ") + @" }, r => { " + string.Join("", from r in table.Relations select @" r." + r.CodeName + @" = new " + typeof(LazyEnumerable <>).GetCodeTypeClassName(r.TargetTable.GetCodeRowClassName()) + @"( () => " + r.TargetTable.CodeName + @".WhereIndexed( tr => tr." + r.TargetColumn.CodeName + @" , " + r.TargetColumn.CodeName.GetCodeStringEscaped() + @", r." + r.SourceColumn.CodeName + @") );") + @" } ); " ) + @" } }//context class //Data types " + string.Join("", from table in db.Tables select GenerateTableRowDataTypeClass(table, db, properties.HideRelationsFromDump) ) + @" }//namespace "; return(src); }
internal string GenerateTableRowDataTypeClass(CsvTable table, CsvDatabase db, bool hideRelationsFromDump) { var src = @" public class " + table.GetCodeRowClassName() + @" : " + typeof(CsvRowBase).GetCodeTypeClassName() + @" {" + string.Join("", from c in table.Columns select @" public string " + c.CodeName + @" { get; set; } " ) + string.Join("", from rel in table.Relations select @" /// <summary>" + System.Security.SecurityElement.Escape(rel.DisplayName) + @"</summary> " + (hideRelationsFromDump ? @" [" + typeof(HideFromDumpAttribute).GetCodeTypeClassName() + "]" : "") + @" public IEnumerable<" + rel.TargetTable.GetCodeRowClassName() + @"> " + rel.CodeName + @" { get; set; } " ) + @" } " ; return(src); }
private Result GenerateSrcFile(CsvDatabase csvDatabase) { var csvTables = csvDatabase.Tables; var groups = csvTables .Select(table => GenerateTableRowDataTypeClass(table, _properties.HideRelationsFromDump, _properties.StringComparison)) .GroupBy(typeCode => typeCode.TypeName) .ToImmutableList(); return(new Result($@"namespace {_contextNameSpace} {{ /// <summary>CSV Data Context</summary> public class {_contextTypeName} : {typeof(CsvDataContextBase).GetCodeTypeClassName()} {{{string.Join(string.Empty, csvTables.Select(table => $@" /// <summary>File: {SecurityElement.Escape(table.FilePath)}</summary> public {typeof(CsvTableBase<>).GetCodeTypeClassName(GetClassName(table))} {table.CodeName} {{ get; private set; }}") )} public {_contextTypeName}() {{ // Init tables data {string.Join(string.Empty, csvTables.Select(table => $@" this.{table.CodeName} = {typeof(CsvTableFactory).GetCodeTypeClassName()}.CreateTable<{GetClassName(table)}>( {GetBoolConst(_properties.IsStringInternEnabled)}, {GetBoolConst(_properties.IsCacheEnabled)}, {table.CsvSeparator.AsValidCSharpCode()}, {typeof(NoBomEncoding).GetCodeTypeClassName()}.{_properties.NoBomEncoding}, {GetBoolConst(_properties.AllowComments)}, {GetBoolConst(_properties.IgnoreBadData)}, {table.FilePath.AsValidCSharpCode()}, new {typeof(CsvColumnInfoList<>).GetCodeTypeClassName(GetClassName(table))} {{ {string.Join(string.Empty, table.Columns.Select(c => $@"{{ {c.Index}, x => x.{c.CodeName} }}, "))} }}, r => {{{string.Join(string.Empty, table.Relations.Select(csvRelation => $@" r.{csvRelation.CodeName} = new {typeof(LazyEnumerable<>).GetCodeTypeClassName(GetClassName(csvRelation.TargetTable))}( () => {csvRelation.TargetTable.CodeName}.WhereIndexed(tr => tr.{csvRelation.TargetColumn.CodeName}, {csvRelation.TargetColumn.CodeName.AsValidCSharpCode()}, r.{csvRelation.SourceColumn.CodeName}));"))} }} );") )} }} }} // context class // Data types {string.Join(Environment.NewLine, groups.Select(grouping => grouping.First().Code))} // data types }} // namespace ", groups));
protected void SetUp() { var filePath = "csv-test.txt"; _database = new CsvDatabase(filePath); }
public static string GenerateCode(CsvDatabase db, ref string nameSpace, ref string typeName, ICsvDataContextDriverProperties props) { typeName = DefaultContextTypeName; return(new CsvCSharpCodeGenerator(nameSpace, DefaultContextTypeName, props).GenerateSrcFile(db)); }
GenerateCode(CsvDatabase db, ref string nameSpace, ref string typeName, ICsvDataContextDriverProperties props) =>