public QueryControllerModel LoadQueryFromStoreQuery(StoreSchema schema, StoreQuery q) { var result = new QueryControllerModel() { SubQueryList = new List <IQueryModel>(), Schema = schema }; var loadedSubQueryList = new List <IQueryModel>(); if (q.Query.SubQueries != null) { foreach (var qName in q.Query.SubQueries.Keys) { var subQuery = ReadQueryTables(loadedSubQueryList, qName, schema, null, q.Query.SubQueries); result.SubQueryList.Add(subQuery); } } result.MainQuery = ReadQueryTables(loadedSubQueryList, "Main", schema, q.Query, q.Query.SubQueries); result.StoreParameters = new StoreQueryParameters(); result.StoreParameters.DataService = q.DataService; result.StoreParameters.QueryName = q.Name; result.StoreParameters.Namespace = q.Namespace; result.StoreParameters.QueryReturnType = q.ReturnTypeName; result.Schema.Name = q.SchemaName; result.Schema.Version = q.SchemaVersion; return(result); }
public StoreSchema ReadSchema(IStoreSchemaReaderParameters parameters) { var dr = parameters as EntityFrameworkStoreSchemaReaderParameters; if (dr == null) { throw new Exception($"EntityFrameworkStoreSchemaReader expects EntityFrameworkStoreSchemaReaderParameters."); } var schema = new StoreSchema { Definitions = new Dictionary <string, StoreDefinition>(), DbContextName = dr.DbContextType.FullName }; using (var db = GetDbContext(dr.DbContextType)) { var entities = db.Model.GetEntityTypes(); foreach (var entityMetadata in entities) { var def = new StoreDefinition { Properties = new Dictionary <string, StoreProperty>() }; def.Name = entityMetadata.ClrType.Name; schema.Definitions.Add(def.Name, def); var props = entityMetadata.GetProperties().ToList(); for (int i = 0; i < props.Count; i++) { var prop = props[i]; var storeProp = new StoreProperty { Name = prop.Name, Order = i, Type = GetTypeString(prop.ClrType), Pk = prop.IsPrimaryKey(), Fk = prop.IsForeignKey(), MaxLength = prop.GetMaxLength(), AutoIncrement = prop.ValueGenerated == ValueGenerated.OnAdd }; if (prop.IsForeignKey()) { var fks = prop.GetContainingForeignKeys(); storeProp.ForeignKeys = prop.GetContainingForeignKeys().Select(f => new StoreForeignKey { DefinitionName = f.PrincipalEntityType.ClrType.Name, PropertyName = f.PrincipalKey.Properties.First().Name, CompositePropertyNames = f.PrincipalKey.Properties.Select(p => p.Name).ToList() }).ToList(); } def.Properties.Add(storeProp.Name, storeProp); } } } return(schema); }
public Store(IndexedDBManager db, StoreSchema schema, StoreSchemaExtOptions options) { _db = db; _schema = schema; _options = options; _parameters = new QueryParameters <T>(); _provider = this; }
public static StoreSchema ToStoreSchema(DesignSchema s) { var schema = new StoreSchema { Name = s.Name, Version = s.Version, VersionKey = s.VersionKey, DbContextName = s.DataContextName, Namespace = s.Namespace, Definitions = new Dictionary <string, StoreDefinition>(), Settings = new Dictionary <string, string>(), }; var tags = new List <string>(); schema.Settings.Add("UseIntId", s.UseIntId.ToString()); //schema.Definitions = Schema.Tables.ToDictionary(t => t.Name, t => new StoreDefinition //{ // Name = t.Name, // Properties = t.Columns.ToDictionary(c => c.Name, c => new StoreProperty // { // Name = c.Name, // Type = c.Type, // Pk = c.Pk, // Fk = !string.IsNullOrWhiteSpace(c.Reference), // Nullable = c.Nullable, // AutoIncrement = c.Pk, // }) //}); foreach (var t in s.Tables) { int i = 0; schema.Definitions[t.Name] = new StoreDefinition { Name = t.Name, Order = t.Order, Properties = new Dictionary <string, StoreProperty>() }; schema.Definitions[t.Name].Properties = t.Columns.Where(c => !c.IsEmpty()).ToDictionary(c => c.Name, c => new StoreProperty { Name = c.Name, Type = c.Type, Pk = c.Pk, Fk = !string.IsNullOrWhiteSpace(c.Reference), Nullable = c.Nullable, AutoIncrement = c.Pk, ForeignKeys = GetForeignKeysForColumn(s, t, c), Order = i++ }); } schema.Tags = tags.ToArray(); return(schema); }
/// <summary> /// Injection constructor. /// </summary> /// <param name="documentWriter">Instance injected by DI.</param> /// <param name="listener">Instance injected by DI.</param> /// <param name="schema">Instance injected by DI.</param> /// <param name="cosmos">Injected instance of the gateway for setting up the database</param> /// <param name="stores">Injected instance of the repository for setting up the database</param> /// <param name="tools">Injected instance of the repository for setting up the database</param> public QueryService(IDocumentWriter documentWriter, DataLoaderDocumentListener listener, StoreSchema schema, CosmosGateway cosmos, StoreRepository stores, ToolRepository tools) { _documentWriter = documentWriter; _listener = listener; _schema = schema; _cosmos = cosmos; _stores = stores; _tools = tools; }
public async Task AddSchemaAsync(string storeName, IEnumerable <string> indexSpecs = null, string primaryKey = "id", bool autoIncrement = true) { var newStoreSchema = new StoreSchema { Name = storeName, PrimaryKey = new IndexSpec { Name = primaryKey, KeyPath = primaryKey, Auto = autoIncrement } }; await _dbManager.AddNewStore(newStoreSchema); }
public IStore <T> Store <T>(string name) where T : new() { StoreSchema schema = _db.Stores.FirstOrDefault(x => Utils.EqualsOrdinal(x.Name, name)); if (schema == null) { throw new InvalidOperationException($"No {name} schema defined."); } if (!_options.Stores.TryGetValue(name, out StoreSchemaExtOptions options)) { throw new InvalidOperationException($"No {name} schema options defined."); } return((Store <T>)Activator.CreateInstance(typeof(Store <>).MakeGenericType(options.ModelType), _db, schema, options)); //new Store(schema,options); }
public void SaveSchema(StoreSchema schema, StorageParameters parameters) { var options = new JsonSerializerOptions() { WriteIndented = true }; var json = System.Text.Json.JsonSerializer.Serialize(schema, options); using (var sw = new StreamWriter(parameters.FileName, false)) { sw.Write(json); } }
public KVRelationalStoreTests() { _storeSchema = new StoreSchema(); _storeSchema.Add( new ObjectTypeSchema(typeof(TestObjA), new Dictionary <Type, string>() { { typeof(TestObjB), "ObjB" } })); _storeSchema.Add( new ObjectTypeSchema(typeof(TestObjB), new Dictionary <Type, string>() { { typeof(TestObjA), "ObjA" } })); }
public List <TemplateJoin> ReadJoins(StoreQueryDefinition query, StoreSchema schema) { var result = new List <TemplateJoin>(); var rightTables = new HashSet <string>(); foreach (var join in query.Joins) { var leftTable = query.Tables[join.LeftObjectAlias].TableName; var rightTable = query.Tables[join.RightObjectAlias].TableName; TemplateJoin tj; if (rightTables.Contains(join.RightObjectAlias)) { // flip tj = new TemplateJoin { LeftField = join.RightField, LeftObjectAlias = join.RightObjectAlias, LeftObject = rightTable, RightField = join.LeftField, RightObjectAlias = join.LeftObjectAlias, RightObject = leftTable }; } else { tj = new TemplateJoin { LeftField = join.LeftField, LeftObjectAlias = join.LeftObjectAlias, LeftObject = leftTable, RightField = join.RightField, RightObjectAlias = join.RightObjectAlias, RightObject = rightTable }; rightTables.Add(join.RightObjectAlias); } result.Add(tj); } return(result); }
void Generate() { var parser = new JsonStoreSchemaParser(); var path = Path.Combine(GetSolutionDirectory(), JsonStorePath); var files = Directory.GetFiles(path); foreach (var file in files) { var json = File.ReadAllText(file); if (Path.GetFileName(file).ToLower() == "schema.json") { Schema = parser.ReadSchema(json); } else { var q = parser.ReadQuery(json); Queries.Add(q); } } }
public TemplateJoin ReadFrom(StoreQueryDefinition query, StoreSchema schema) { TemplateJoin result; var join = query.Joins.FirstOrDefault(); if (join != null) { var table = query.Tables[join.LeftObjectAlias].TableName; result = new TemplateJoin { LeftField = join.LeftField, LeftObjectAlias = join.LeftObjectAlias, LeftObject = table }; } else { var table = query.Tables.Values.First(); result = new TemplateJoin { LeftObjectAlias = table.ObjectAlias, LeftObject = table.TableName }; } return(result); }
public static DesignSchema FromStoreSchema(StoreSchema s) { var ds = new DesignSchema { Name = s.Name, DataContextName = s.DbContextName, Namespace = s.Namespace, Version = s.Version, VersionKey = s.VersionKey, }; if (s.Settings.ContainsKey("UseIntId")) { ds.UseIntId = bool.Parse(s.Settings["UseIntId"]); } foreach (var d in s.Definitions.Values) { var t = new DesignTable() { Name = d.Name, Order = d.Order, }; ds.Tables.Add(t); t.Columns = d.Properties.Select(x => x.Value).OrderBy(x => x.Order).Select(c => new DesignColumn() { Name = c.Name, Pk = c.Pk, Type = c.Type, Nullable = c.Nullable, Reference = GetDesignForeignKey(c), }).ToList(); } return(ds); }
//public List<StoreQuery> ReadSubQueries(StoreQuery query, StoreSchema schema) { } public TemplateJoin ReadFrom(StoreQuery query, StoreSchema schema) { return(ReadFrom(query.Query, schema)); }
/// <summary> /// Builds the schema /// </summary> /// <param name="dbStore"></param> private void Build(DbStore dbStore) { // Get all "tables" var storeSchemaProperties = this.GetType().GetProperties() .Where(x => x.PropertyType.IsGenericType && x.PropertyType.GetGenericTypeDefinition() == typeof(IndexedSet <>)); // For all tables foreach (var schemaProperty in storeSchemaProperties) { // Create schema var schema = new StoreSchema() { Name = schemaProperty.Name, Indexes = new List <IndexSpec>(), }; // Get generic parameter of list<T> (type T, only supports IndexedSet<T> ergo 1 parameter) var propertyType = schemaProperty.PropertyType.GetGenericArguments()[0]; // Get all properties of the generic type T var properties = propertyType.GetProperties(); Dictionary <string, List <string> > compoundIndices = new Dictionary <string, List <string> >(); foreach (var property in properties) { // If any non supported object is used throw exception here if (property.PropertyType.IsGenericType && !property.PropertyType.Namespace.StartsWith("System")) { throw new NotSupportedException(property.PropertyType.FullName); } // Get attributes from the entity property, ergo column var attributes = property.CustomAttributes; var id = false; var unique = false; var autoIncrement = false; var foreignKey = false; var multiEntry = false; // Check for settings via attributes here (additonal attributes have to be checked here) if (attributes.Any(x => x.AttributeType == typeof(KeyAttribute))) { id = true; } if (attributes.Any(x => x.AttributeType == typeof(UniqueAttribute))) { unique = true; } if (attributes.Any(x => x.AttributeType == typeof(MultiEntryAttribute))) { multiEntry = true; } if (attributes.Any(x => x.AttributeType == typeof(AutoIncrementAttribute))) { autoIncrement = true; } if (attributes.Any(x => x.AttributeType == typeof(ForeignKeyAttribute))) { if (id) { throw new NotSupportedException("PK cannot be FK"); } foreignKey = true; } var columnName = this.FirstToLower(property.Name); foreach (var attributeData in attributes.Where(x => x.AttributeType == typeof(CompoundIndexAttribute))) //foreach (var attribute in attributes.OfType<CompoundIndexAttribute>()) { var name = attributeData.ConstructorArguments[0].Value as string; name = this.FirstToLower(name); compoundIndices.TryGetValue(name, out var list); if (list == null) { list = new List <string>(); } list.Add(columnName); compoundIndices[name] = list; Debug.WriteLine($"{nameof(IndexedDb)} -Compound key detected: {name}:{columnName}={string.Join(",",list)}"); } // Define index var index = new IndexSpec { Name = columnName, KeyPath = columnName, Auto = autoIncrement, Unique = unique, MultiEntry = multiEntry }; // Register index if (id) { // Throw invalid operation when index has already been defined if (schema.PrimaryKey != null) { throw new InvalidOperationException("PK already defined"); } Debug.WriteLine($"{nameof(IndexedDb)} - {schemaProperty.Name} - PK-> {columnName}"); index.Auto = true; schema.PrimaryKey = index; } else if (!foreignKey) { Debug.WriteLine($"{nameof(IndexedDb)} - {schemaProperty.Name} - Property -> {columnName}"); schema.Indexes.Add(index); } } foreach (var compound in compoundIndices) { Debug.WriteLine($"{nameof(IndexedDb)} -Compound key queued: {compound.Key} : {string.Join(",", compound.Value)}"); var index = new IndexSpec { Name = compound.Key, KeyPaths = compound.Value.ToArray(), Compound = true /*Unique = unique, MultiEntry = multiEntry*/ }; schema.Indexes.Add(index); } // Create PK when not defined if (schema.PrimaryKey == null) { var idPropertyName = "Id"; var idColumnName = this.FirstToLower(idPropertyName); // Check for registered id property without declared key attribute if (properties.Any(x => x.Name == idPropertyName)) { var idProperty = schema.Indexes.Single(x => x.Name == idColumnName); // Remove from schema schema.Indexes.Remove(idProperty); // And set as primary key schema.PrimaryKey = idProperty; // Set to auto, default setting schema.PrimaryKey.Auto = true; } else { throw new NotSupportedException("Missing id property"); // Not supported because no implementation for changed check when deleted (missing id to resolve object in store) //schema.PrimaryKey = new IndexSpec { Name = idColumnName, KeyPath = idColumnName, Auto = true }; } } // Add schema to registered schemas dbStore.Stores.Add(schema); } Debug.WriteLine($"{nameof(IndexedDb)} - Schema has been built"); }
private static IQueryModel ReadQueryTables(List <IQueryModel> loadedSubQueryList, string name, StoreSchema schema, StoreQueryDefinition queryDef, Dictionary <string, StoreQueryDefinition> subQueries) { var result = loadedSubQueryList.FirstOrDefault(l => l.Name == name); if (result != null) { return(result); } queryDef = queryDef ?? subQueries[name]; result = new QueryModel() { Name = name }; foreach (var t in queryDef.Tables) { if (schema.Definitions.ContainsKey(t.Value.TableName)) { // not subquery table var schemaTable = schema.Definitions[t.Value.TableName]; var ft = new QueryFromTable(schemaTable); ft.Alias = t.Value.ObjectAlias; result.FromTables.Add(ft); } else { if (!subQueries.ContainsKey(t.Value.TableName)) { throw new Exception($"Table or SubQuery with name '{t.Value.TableName}' not found"); } var subQuery = ReadQueryTables(loadedSubQueryList, t.Value.TableName, schema, null, subQueries); var ft = new QueryFromTable(subQuery); ft.Alias = t.Value.ObjectAlias; result.FromTables.Add(ft); } } loadedSubQueryList.Add(result); // fields foreach (var f in queryDef.Fields.Values) { var table = result.FromTables.FirstOrDefault(t => t.Alias == f.Field.ObjectAlias); if (table == null) { throw new Exception($"Table with alias '{f.Field.ObjectAlias}' not found"); } var storeProperty = table.Properties.FirstOrDefault(p => p.Name == f.Field.FieldName); if (storeProperty == null) { throw new Exception($"Field '{f.Field.FieldName}' not found in table with alias '{f.Field.ObjectAlias}'"); } var newSelectionProperty = new QuerySelectProperty(table, storeProperty.OriginalStoreProperty) { IsOutput = f.IsOutput, GroupByFunction = f.GroupByFunction, Having = f.GroupByFilter, Alias = f.FieldAlias != f.Field.FieldName ? f.FieldAlias : "" // ToDo: Filter is not stored and cannot be loaded }; result.SelectionProperties.Add(newSelectionProperty); } // Joins foreach (var j in queryDef.Joins) { } result.FromTableJoins = queryDef.Joins.Select(j => new TableJoinModel() { IsDeleted = false, JoinType = j.JoinType, Source = j }).ToList(); // Where result.WhereClause = QueryExpressionHelper.QueryExprToString(queryDef.Where.Expression); return(result); }
public Trade(StoreSchema store, CardSchema card) { Store = store; Card = card; }
public List <TemplateJoin> ReadJoins(StoreQuery query, StoreSchema schema) { return(ReadJoins(query.Query, schema)); }