public override QueryDTO Execute(QueryParameters parameters) { if (parameters.Database.Schema == null) { throw new ApplicationException("Schema can not be null!"); } Class selectedClass = parameters.Database.Schema.Classes.Values.SingleOrDefault(c => c.Name == Name); if (selectedClass == null) { Class errorClass = new Class() { Name = Name }; DTOQueryResult errorResult = new DTOQueryResult { NextResult = null, QueryResultType = ResultType.StringResult, IsError = true, StringOutput = "Unknown class: " + Name }; return(new QueryDTO { Result = errorResult, QueryClass = errorClass }); } return(new QueryDTO { QueryClass = selectedClass }); }
public override QueryDTO Execute(QueryParameters parameters) { var indexedClassReslut = Element(ElementType.CLASS_NAME).Execute(parameters); if (indexedClassReslut.Result != null) { return(indexedClassReslut); } parameters.Subquery = new QueryDTO { QueryClass = indexedClassReslut.QueryClass }; var indexNameReslut = Element(ElementType.INDEX_NAME).Execute(parameters); if (indexNameReslut.Result != null) { return(indexNameReslut); } var index = parameters.IndexMechanism.GetIndexes(). Where(p => p.Value.Equals(indexNameReslut.Value)).Single(); var attributes = AllElements(ElementType.INDEX_ATTRIBUTE) .Select(p => (string)p.Execute(parameters).Value.Name); var indexedAttributes = parameters.IndexMechanism.GetIndexedAttribiutesForType(index.Key, indexedClassReslut.QueryClass.Name); if (indexedAttributes != null && indexedAttributes.All(p => attributes.Contains(p))) { var errorResult = new DTOQueryResult() { QueryResultType = ResultType.StringResult, StringOutput = "That attributes has already been indexed" }; return(new QueryDTO() { Result = errorResult }); } var classParameter = parameters.Database.Schema.ClassProperties(indexedClassReslut.QueryClass); var objs = parameters.Storage.GetAll(parameters.Database.DatabaseId); var oids = objs?.Where(s => s.Properties.All(p => classParameter.Any(cp => cp.PropertyId.Id == p.Key.PropertyId.Id))) .Select(obj => obj.Oid); parameters.IndexMechanism.IndexObjects(index.Key, oids.ToArray(), attributes.ToArray(), parameters); var result = new DTOQueryResult() { QueryResultType = ResultType.StringResult, StringOutput = "Class " + indexedClassReslut.QueryClass.Name + " indexed" }; return(new QueryDTO() { Result = result }); }
public override QueryDTO Execute(QueryParameters parameters) { try { var did = parameters.Storage.RemoveDatabase(new DatabaseRemoveParameters { DatabaseToRemove = parameters.Database.DatabaseId }); parameters.Log(string.Format("database {0} removed", did), MessageLevel.Info); var sb = new StringBuilder(); var sw = new StringWriter(sb); var xmlSerializer = new XmlSerializer(typeof(SystemInfo)); parameters.SystemInfo.Databases = parameters.Storage.GetDatabases().Select(d => new DatabaseInfo { Name = d.Name, Classes = d.Schema.Classes.Select( c => new DatabaseClass { Name = c.Value.Name, Interface = c.Value.Interface, Fields = d.Schema.ClassProperties(c.Value).Select(f => new Field { Name = f.Name, Type = f.Type, Reference = !f.IsValueType, IsArray = f.IsArray }).ToList(), Methods = d.Schema.ClassMethods(c.Value).Any() ? d.Schema.ClassMethods(c.Value).Select( m => new ClassMethod { Name = m.Name, ReturnType = m.ReturnType }).ToList() : new List <ClassMethod>() }).ToList() }).ToList(); xmlSerializer.Serialize(sw, parameters.SystemInfo); var result = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.SystemInfo, StringOutput = sb.ToString() }; return(new QueryDTO { Result = result }); } catch (Exception ex) { var errorResult = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error during database creation: " + ex.ToString() }; return(new QueryDTO { Result = errorResult }); } }
public override QueryDTO Execute(QueryParameters parameters) { var classProperties = parameters.Database.Schema.ClassProperties(parameters.Subquery.QueryClass); var parametersWithName = classProperties.Where(prop => prop.Name == Name); if (!parametersWithName.Any()) { var errorMessage = new DTOQueryResult() { QueryResultType = ResultType.StringResult, StringOutput = "No attribute with name: " + Name }; return(new QueryDTO() { Result = errorMessage }); } var parameterToDrop = parametersWithName.SingleOrDefault(pId => pId.ParentClassId == parameters.Subquery.QueryClass.ClassId.Id); if (parameterToDrop == null) { var parentClassParameter = parametersWithName.Single(); var parentClass = parameters.Database.Schema.Classes.Values.Single(cl => cl.ClassId.Id == parentClassParameter.ParentClassId); var errorMessage = new DTOQueryResult() { QueryResultType = ResultType.StringResult, StringOutput = "Attribute with name: " + Name + " belongs to parent class(interface): " + parentClass.Name }; return(new QueryDTO() { Result = errorMessage }); } Boolean success = parameters.Database.Schema.Properties.TryRemove(parameterToDrop.PropertyId, out Property dropedProperty); if (!success) { var errorMessage = new DTOQueryResult() { QueryResultType = ResultType.StringResult, StringOutput = "Error while droping attribute: " + Name }; return(new QueryDTO() { Result = errorMessage }); } return(new QueryDTO() { Value = dropedProperty }); }
public override QueryDTO Execute(QueryParameters parameters) { IQueryElement leftElement = Element(ElementType.LEFT_OPERAND); IQueryElement rightElement = Element(ElementType.RIGHT_OPERAND); IQueryElement operation = Element(ElementType.OPERATOR); Func <IStorable, bool> expression = delegate(IStorable databaseObject) { QueryDTO subquery = new QueryDTO { QueryClass = parameters.Subquery.QueryClass, QueryObjects = new List <IStorable> { databaseObject } }; QueryParameters singleParameter = new QueryParameters { Database = parameters.Database, Storage = parameters.Storage, Subquery = subquery }; var left = leftElement.Execute(singleParameter).Value; var right = rightElement.Execute(singleParameter).Value; QueryDTO comparisionSubquery = new QueryDTO { Value = left, AdditionalValue = right }; QueryParameters comparisionParameter = new QueryParameters { Subquery = comparisionSubquery }; return((Boolean)operation.Execute(comparisionParameter).Value); }; try { IEnumerable <IStorable> objects = parameters.Subquery.QueryObjects; QueryDTO query = new QueryDTO() { QueryClass = parameters.Subquery.QueryClass, QueryObjects = objects.Where(obj => expression(obj)).ToList() }; return(query); } catch (NoClassPropertyException exc) { new DTOQueryResult { }; DTOQueryResult errorResult = new DTOQueryResult { NextResult = null, QueryResultType = ResultType.StringResult, StringOutput = "Unknown propertyName: " + exc.PropertyName }; return(new QueryDTO { Result = errorResult }); } }
public override QueryDTO Execute(QueryParameters parameters) { IQueryElement classNameElement = Element(ElementType.CLASS_NAME); QueryDTO classResult = classNameElement.Execute(parameters); if (classResult.Result != null) { return(classResult); } var classToGet = classResult.QueryClass; var classParameter = parameters.Database.Schema.ClassProperties(classToGet); var objs = parameters.Storage.GetAll(parameters.Database.DatabaseId); objs = objs.Where(s => s.Properties.All(p => classParameter.Any(cp => cp.PropertyId.Id == p.Key.PropertyId.Id))); var selectDto = new QueryDTO { QueryClass = classToGet, QueryObjects = objs }; parameters.Subquery = selectDto; if (TryGetElement(ElementType.WHERE, out IQueryElement searchCriteria)) { QueryDTO whereDto = searchCriteria.Execute(parameters); if (whereDto.Result?.QueryResultType == ResultType.StringResult) { return(whereDto); } objs = whereDto.QueryObjects; parameters.Subquery.QueryObjects = objs; } if (TryGetElement(ElementType.CLASS_PROPERTY, out IQueryElement property)) { var propertyValueDto = property.Execute(parameters); if (propertyValueDto.Result?.QueryResultType == ResultType.StringResult) { return(propertyValueDto); } objs = propertyValueDto.QueryObjects; } var getDto = new DTOQueryResult { NextResult = null, QueryResultType = ResultType.ReferencesOnly, QueryResults = objs.Select(o => o.Oid).ToList() }; selectDto.Result = getDto; selectDto.QueryClass = classToGet; selectDto.QueryObjects = objs; return(selectDto); }
public override QueryDTO Execute(QueryParameters parameters) { try { var did = parameters.Storage.RenameDatabase(parameters.Database, NewDatabaseName, parameters.SettingsManager); parameters.Log(string.Format("database renamed as {0}", did), MessageLevel.Info); var sb = new StringBuilder(); var sw = new StringWriter(sb); var xmlSerializer = new XmlSerializer(typeof(SystemInfo)); parameters.SystemInfo.Databases = parameters.Storage.GetDatabases().Select(d => new DatabaseInfo { Name = d.Name, Classes = d.Schema.Classes.Select( c => new DatabaseClass { Name = c.Value.Name, Interface = c.Value.Interface, Fields = d.Schema.ClassProperties(c.Value).Select(f => new Field { Name = f.Name, Type = f.Type }).ToList(), Methods = d.Schema.Methods.ContainsKey(c.Key) ? d.Schema.Methods[c.Key] : new List <string>() }).ToList() }).ToList(); xmlSerializer.Serialize(sw, parameters.SystemInfo); var result = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.SystemInfo, StringOutput = sb.ToString() }; return(new QueryDTO { Result = result }); } catch (Exception ex) { var errorResult = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error during database creation: " + ex.ToString() }; return(new QueryDTO { Result = errorResult }); } }
public override QueryDTO Execute(QueryParameters parameters) { if (!parameters.IndexMechanism.GetIndexes().Any(p => p.Value.Equals(Name))) { DTOQueryResult errorResult = new DTOQueryResult { NextResult = null, QueryResultType = ResultType.StringResult, StringOutput = "Unknown index: " + Name }; return(new QueryDTO { Result = errorResult }); } return(new QueryDTO { Value = Name }); }
public override QueryDTO Execute(QueryParameters parameters) { var sb = new StringBuilder(); var sw = new StringWriter(sb); var xmlSerializer = new XmlSerializer(typeof(SystemInfo)); xmlSerializer.Serialize(sw, parameters.SystemInfo); var result = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.SystemInfo, StringOutput = sb.ToString() }; return(new QueryDTO { Result = result }); }
public override QueryDTO Execute(QueryParameters parameters) { var typeElement = SingleElement(); var typeDao = typeElement.Execute(parameters); var propertyId = new PropertyId { Id = 1 + parameters.Database.Schema.Properties.Max(p => (long?)p.Key.Id) ?? 0, Name = Name, ParentClassId = parameters.Subquery.QueryClass.ClassId.Id }; var property = new Property { ParentClassId = parameters.Subquery.QueryClass.ClassId.Id, Name = Name, PropertyId = propertyId, Type = typeDao.Value, IsValueType = typeDao.AdditionalValue, IsArray = IsArray }; Boolean success = parameters.Database.Schema.Properties.TryAdd(propertyId, property); if (!success) { var errorMessage = new DTOQueryResult() { QueryResultType = ResultType.StringResult, StringOutput = "Error while creating new attribute: " + Name }; return(new QueryDTO() { Result = errorMessage }); } return(new QueryDTO() { Value = property }); }
public override QueryDTO Execute(QueryParameters parameters) { IQueryElement valueElement = SingleElement(); Func <IStorable, bool> expression = delegate(IStorable databaseObject) { QueryDTO subquery = new QueryDTO { QueryClass = parameters.Subquery.QueryClass, QueryObjects = new List <IStorable> { databaseObject } }; QueryParameters singleParameter = new QueryParameters { Database = parameters.Database, Storage = parameters.Storage, Subquery = subquery }; return(valueElement.Execute(singleParameter).Value == null); }; try { IEnumerable <IStorable> objects = parameters.Subquery.QueryObjects; QueryDTO query = new QueryDTO() { QueryClass = parameters.Subquery.QueryClass, QueryObjects = objects.Where(obj => expression(obj)).ToList() }; return(query); } catch (NoClassPropertyException exc) { DTOQueryResult errorResult = new DTOQueryResult { NextResult = null, QueryResultType = ResultType.StringResult, StringOutput = "Unknown propertyName: " + exc.PropertyName }; return(new QueryDTO { Result = errorResult }); } }
public override QueryDTO Execute(QueryParameters parameters) { if (parameters.Database == null) { parameters.Log("Database is required!", MessageLevel.Error); var errorResult = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while interface creation" }; return(new QueryDTO() { Result = errorResult }); } bool nameExists = parameters.Database.Schema.Classes.Values.Any(c => c.Name == Name); if (nameExists) { var classExistsResult = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Class or interface with name: " + Name + " arleady exists!" }; return(new QueryDTO() { Result = classExistsResult }); } var classId = new ClassId { Name = Name, Id = (parameters.Database.Schema.Classes.Max(d => (long?)d.Key.Id) ?? 0) + 1 }; var classDef = new Class { ClassId = classId, Name = Name, Interface = true }; parameters.Subquery = new QueryDTO { QueryClass = classDef }; if (TryGetElement(ElementType.PARENT_CLASSES, out IQueryElement parentClassesElement)) { var parentClasses = parentClassesElement.Execute(parameters); if (parentClasses.Result != null) { return(parentClasses); } classDef.Parent = parentClasses.Value; } foreach (var attr in AllElements(ElementType.ATTRIBUTE_DECLARATION)) { var attributeResult = attr.Execute(parameters); if (attributeResult.Result != null) { return(attributeResult); } } /* * _database.Schema.Methods.TryAdd(classId, new List<string>()); * * foreach (var meth in queryTree.ProductionsList.Where(t => t.TokenName == TokenName.METHOD_DEC_STM)) * { * var methName = (isClass * ? meth.ProductionsList.Single(t => t.TokenName == TokenName.METHOD_NAME) * : meth) * .ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue; * _database.Schema.Methods[classId].Add(methName); * } * foreach (var rel in queryTree.ProductionsList.Where(t => t.TokenName == TokenName.RELATION_DEC_STM)) * { * var attrName = rel.ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue; * string typeName = * rel.ProductionsList.Single(t => t.TokenName == TokenName.DATA_TYPE) * .ProductionsList.Single() * .TokenValue; * var propertyId = new PropertyId * { * Id = 1 + (_database.Schema.Properties.Max(p => (long?)p.Key.Id) ?? 0), * Name = attrName, * ParentClassId = classId.Id * }; * _database.Schema.Properties.TryAdd(propertyId, new Property * { * ParentClassId = classId.Id, * Name = attrName, * PropertyId = propertyId, * Type = typeName, * IsValueType = false * }); * } */ if (!parameters.Database.Schema.Classes.TryAdd(classId, classDef)) { parameters.Log("Could not define new interface", MessageLevel.Error); var errorResult = new DTOQueryResult() { QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while interface creation" }; return(new QueryDTO { Result = errorResult }); } parameters.Storage.SaveSchema(parameters.Database.Schema); parameters.Log("Defined new interface: " + Name, MessageLevel.QueryExecution); var result = new DTOQueryResult() { QueryResultType = ResultType.StringResult, StringOutput = "New interface: " + Name + " created." }; return(new QueryDTO() { Result = result }); }
public override QueryDTO Execute(QueryParameters parameters) { if (parameters.Database == null) { parameters.Log("Database is required!", MessageLevel.Error); return(new QueryDTO() { Result = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while class droping" } }); } IQueryElement classNameElement = Element(ElementType.CLASS_NAME); QueryDTO classResult = classNameElement.Execute(parameters); if (classResult.Result != null || classResult.QueryClass.Interface) { var noInterfaceResult = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "No class with name: " + classResult.QueryClass.Name }; return(new QueryDTO() { Result = noInterfaceResult }); } Class classToDrop = classResult.QueryClass; var childClasses = parameters.Database.Schema.Classes.Values.Where(cl => cl.Parent != null && cl.Parent.Where(p => p.ClassId.Id == classToDrop.ClassId.Id).Any()); if (childClasses.Any()) { var noInterfaceResult = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Can't drop class with childs: " + String.Join(", ", childClasses.Select(c => c.Name)) + ". Drop childs first." }; return(new QueryDTO() { Result = noInterfaceResult }); } IEnumerable <Property> propeteries = parameters.Database.Schema.Properties.Values.Where(p => p.ParentClassId == classToDrop.ClassId.Id); foreach (var parameterToDrop in propeteries) { Boolean success = parameters.Database.Schema.Properties.TryRemove(parameterToDrop.PropertyId, out Property dropedProperty); if (!success) { var errorMessage = new DTOQueryResult() { QueryResultType = ResultType.StringResult, StringOutput = "Error while droping attribute: " + dropedProperty.Name }; return(new QueryDTO() { Result = errorMessage }); } } parameters.Database.Schema.Methods.TryRemove(classToDrop.ClassId, out List <IMethod> dropedMethods); if (!parameters.Database.Schema.Classes.TryRemove(classToDrop.ClassId, out Class dropedClass)) { parameters.Log("Could not drop class", MessageLevel.Error); return(new QueryDTO() { Result = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while class droping" } }); } parameters.Storage.SaveSchema(parameters.Database.Schema); parameters.Log("Droped class: " + dropedClass.Name, MessageLevel.QueryExecution); return(new QueryDTO() { Result = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Class:" + dropedClass.Name + " droped." } }); }
public override QueryDTO Execute(QueryParameters parameters) { if (parameters.Database == null) { parameters.Log("Database is required!", MessageLevel.Error); var errorResult = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while interface creation" }; return(new QueryDTO() { Result = errorResult }); } IQueryElement classNameElement = Element(ElementType.CLASS_NAME); QueryDTO classResult = classNameElement.Execute(parameters); if (classResult.Result != null || classResult.QueryClass.Interface) { var noInterfaceResult = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "No class with name: " + classResult.QueryClass.Name }; return(new QueryDTO() { Result = noInterfaceResult }); } var classDef = classResult.QueryClass; var classId = classDef.ClassId; var interfaceName = classDef.Name; parameters.Subquery = new QueryDTO { QueryClass = classDef }; foreach (var attr in AllElements(ElementType.ATTRIBUTE_DECLARATION)) { var attributeResult = attr.Execute(parameters); if (attributeResult.Result != null) { return(attributeResult); } } foreach (var attr in AllElements(ElementType.DROP_ATTRIBUTE)) { var dropAttributeResult = attr.Execute(parameters); if (dropAttributeResult.Result != null) { return(dropAttributeResult); } } /* * _database.Schema.Methods.TryAdd(classId, new List<string>()); * * foreach (var meth in queryTree.ProductionsList.Where(t => t.TokenName == TokenName.METHOD_DEC_STM)) * { * var methName = (isClass * ? meth.ProductionsList.Single(t => t.TokenName == TokenName.METHOD_NAME) * : meth) * .ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue; * _database.Schema.Methods[classId].Add(methName); * } * foreach (var rel in queryTree.ProductionsList.Where(t => t.TokenName == TokenName.RELATION_DEC_STM)) * { * var attrName = rel.ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue; * string typeName = * rel.ProductionsList.Single(t => t.TokenName == TokenName.DATA_TYPE) * .ProductionsList.Single() * .TokenValue; * var propertyId = new PropertyId * { * Id = 1 + (_database.Schema.Properties.Max(p => (long?)p.Key.Id) ?? 0), * Name = attrName, * ParentClassId = classId.Id * }; * _database.Schema.Properties.TryAdd(propertyId, new Property * { * ParentClassId = classId.Id, * Name = attrName, * PropertyId = propertyId, * Type = typeName, * IsValueType = false * }); * } */ parameters.Storage.SaveSchema(parameters.Database.Schema); parameters.Log("Altered class: " + interfaceName, MessageLevel.QueryExecution); var result = new DTOQueryResult() { QueryResultType = ResultType.StringResult, StringOutput = "Alter class: " + interfaceName + " complited." }; return(new QueryDTO() { Result = result }); }
internal override DTOQueryResult Execute(IQueryTree queryTree) { switch (queryTree.TokenName) { case TokenName.STATEMENT: DTOQueryResult qr = null; DTOQueryResult lastQr = null; foreach (var subTree in queryTree.ProductionsList.Where(p => p.TokenName != TokenName.SEMICOLON)) { var res = Execute(subTree); if (qr == null) { lastQr = qr = res; } else { lastQr.NextResult = res; lastQr = lastQr.NextDTOResult; } } return(qr); case TokenName.SYSTEM_OPERATION: return(Execute(queryTree.ProductionsList[0])); case TokenName.GET_SYSTEM_INFO: var sb = new StringBuilder(); var sw = new StringWriter(sb); var xmlSerializer = new XmlSerializer(typeof(SystemInfo)); xmlSerializer.Serialize(sw, _systemInfo); return(new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.SystemInfo, StringOutput = sb.ToString() }); case TokenName.CREATE_DATABASE: try { var dbName = queryTree.ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue; var did = _storage.CreateDatabase(new DatabaseParameters(dbName, _settingsManager)); _log(string.Format("new database created as {0}", did), MessageLevel.Info); var sb2 = new StringBuilder(); var sw2 = new StringWriter(sb2); var xmlSerializer2 = new XmlSerializer(typeof(DatabaseInfo)); var db = _storage.GetDatabase(did); xmlSerializer2.Serialize(sw2, new DatabaseInfo() { Name = db.Name, Classes = db.Schema.Classes.Select(c => new DatabaseClass { Name = c.Value.Name, Interface = c.Value.Interface, Fields = db.Schema.Properties.Values.Where(p => p.ParentClassId == c.Value.ClassId.Id) .Select(f => new Field { Name = f.Name, Type = f.Type }) .ToList(), Methods = db.Schema.Methods.ContainsKey(c.Key) ? db.Schema.Methods[c.Key] : new List <string>() }).ToList() }); return(new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.DatabaseInfo, StringOutput = sb2.ToString() }); } catch (Exception ex) { return(new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error during database creation: " + ex.ToString() }); } case TokenName.CLASS_DECLARATION: case TokenName.INTERFACE_DECLARATION: if (_database == null) { _log("Database is required!", MessageLevel.Error); return(new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while class creation" }); } bool isClass = queryTree.TokenName == TokenName.CLASS_DECLARATION; var desc = isClass ? "Class" : "Interface"; var newClassName = (queryTree.TokenName == TokenName.CLASS_DECLARATION ? queryTree.ProductionsList.Single(t => t.TokenName == TokenName.CLASS_NAME) : queryTree) .ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue; if (_database.Schema.Classes.Any(c => c.Value.Name.ToUpper() == newClassName.ToUpper())) { return new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = desc + " with name: " + newClassName + " arleady exists!" } } ; var classId = new ClassId { Name = newClassName, Id = (_database.Schema.Classes.Max(d => (long?)d.Key.Id) ?? 0) + 1 }; var classDef = new Class { ClassId = classId, Name = newClassName }; foreach (var attr in queryTree.ProductionsList.Where(t => t.TokenName == TokenName.ATTRIBUTE_DEC_STM) ) { var attrName = (isClass ? attr.ProductionsList.Single(t => t.TokenName == TokenName.ATTRIBUTE_NAME) : attr) .ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue; var typeName = attr.ProductionsList.Single(t => t.TokenName == TokenName.DATA_TYPE) .ProductionsList.Single(); var propertyId = new PropertyId { Id = 1 + _database.Schema.Properties.Max(p => (long?)p.Key.Id) ?? 0, Name = attrName, ParentClassId = classId.Id }; _database.Schema.Properties.TryAdd(propertyId, new Property { ParentClassId = classId.Id, Name = attrName, PropertyId = propertyId, Type = typeName.TokenValue, IsValueType = typeName.TokenName != TokenName.NAME }); } _database.Schema.Methods.TryAdd(classId, new List <string>()); foreach (var meth in queryTree.ProductionsList.Where(t => t.TokenName == TokenName.METHOD_DEC_STM)) { var methName = (isClass ? meth.ProductionsList.Single(t => t.TokenName == TokenName.METHOD_NAME) : meth) .ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue; _database.Schema.Methods[classId].Add(methName); } foreach (var rel in queryTree.ProductionsList.Where(t => t.TokenName == TokenName.RELATION_DEC_STM)) { var attrName = rel.ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue; string typeName = rel.ProductionsList.Single(t => t.TokenName == TokenName.DATA_TYPE) .ProductionsList.Single() .TokenValue; var propertyId = new PropertyId { Id = 1 + (_database.Schema.Properties.Max(p => (long?)p.Key.Id) ?? 0), Name = attrName, ParentClassId = classId.Id }; _database.Schema.Properties.TryAdd(propertyId, new Property { ParentClassId = classId.Id, Name = attrName, PropertyId = propertyId, Type = typeName, IsValueType = false }); } if (!_database.Schema.Classes.TryAdd(classId, classDef)) { _log("Could not define new " + desc.ToLower(), MessageLevel.Error); return(new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while class creation" }); } _storage.SaveSchema(_database.Schema); _log("Defined new class: " + newClassName, MessageLevel.QueryExecution); return(new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "New " + desc.ToLower() + ": " + newClassName + " created." }); case TokenName.DROP: if (_database == null) { _log("Database is required!", MessageLevel.Error); return(new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while class droping" }); } var className = queryTree.ProductionsList.Single(t => t.TokenName == TokenName.CLASS_NAME) .ProductionsList.Single(t => t.TokenName == TokenName.NAME).TokenValue; Class classToDrop = GetClass(className); Class dropedClass; if (!_database.Schema.Classes.TryRemove(classToDrop.ClassId, out dropedClass)) { _log("Could not define new", MessageLevel.Error); return(new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while class droping" }); } _storage.SaveSchema(_database.Schema); _log("Droped class: " + dropedClass.Name, MessageLevel.QueryExecution); return(new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Class:" + dropedClass.Name + " droped." }); case TokenName.NEW_OBJECT: return(base.Execute(queryTree)); default: //if (_doOnDataServers != null) // _doOnDataServers(queryTree); return(base.Execute(queryTree)); } }
public override QueryDTO Execute(QueryParameters parameters) { IQueryElement leftElement = Element(ElementType.LEFT_OPERAND); IQueryElement rightElement = Element(ElementType.RIGHT_OPERAND); IQueryElement operation = Element(ElementType.OPERATOR); Func <IStorable, bool> expression = delegate(IStorable databaseObject) { QueryDTO subquery = new QueryDTO { QueryClass = parameters.Subquery.QueryClass, QueryObjects = new List <IStorable> { databaseObject } }; QueryParameters singleParameter = new QueryParameters { Database = parameters.Database, Storage = parameters.Storage, Subquery = subquery }; QueryDTO leftResult = leftElement.Execute(singleParameter); if (leftResult.Result?.IsError == true) { throw new SelectExecutionException(leftResult); } QueryDTO rightResult = rightElement.Execute(singleParameter); if (rightResult.Result?.IsError == true) { throw new SelectExecutionException(rightResult); } if (leftResult.Result?.QueryResultType == ResultType.ReferencesOnly && rightResult.Result?.QueryResultType == ResultType.ReferencesOnly) { bool containsCommonObject = leftResult.QueryObjects.Any(x => rightResult.QueryObjects.Contains(x)); return(containsCommonObject); } QueryDTO comparisionSubquery = new QueryDTO { Value = leftResult.Value, AdditionalValue = rightResult.Value }; QueryParameters comparisionParameter = new QueryParameters { Subquery = comparisionSubquery }; return((Boolean)operation.Execute(comparisionParameter).Value); }; try { IEnumerable <IStorable> objects = parameters.Subquery.QueryObjects; QueryDTO query = new QueryDTO() { QueryClass = parameters.Subquery.QueryClass, QueryObjects = objects.Where(obj => expression(obj)).ToList() }; return(query); } catch (NoClassPropertyException exc) { DTOQueryResult errorResult = new DTOQueryResult { NextResult = null, QueryResultType = ResultType.StringResult, IsError = true, StringOutput = "Unknown propertyName: " + exc.PropertyName }; return(new QueryDTO { Result = errorResult }); } catch (SelectExecutionException exc) { var errorDto = exc.ErrorDto; return(errorDto); } catch (InvalidOperationException exc) { DTOQueryResult errorResult = new DTOQueryResult { NextResult = null, QueryResultType = ResultType.StringResult, StringOutput = "Can't compare diferent types!" }; return(new QueryDTO { Result = errorResult }); } }
public override QueryDTO Execute(QueryParameters parameters) { if (parameters.Database == null) { parameters.Log("Database is required!", MessageLevel.Error); return(new QueryDTO() { Result = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while class droping" } }); } IQueryElement classNameElement = Element(ElementType.CLASS_NAME); QueryDTO classResult = classNameElement.Execute(parameters); if (classResult.Result != null || !classResult.QueryClass.Interface) { var noInterfaceResult = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "No interface with name: " + classResult.QueryClass.Name }; return(new QueryDTO() { Result = noInterfaceResult }); } Class classToDrop = classResult.QueryClass; var childClasses = parameters.Database.Schema.Classes.Values.Where(cl => cl.Parent != null && cl.Parent.Where(p => p.ClassId.Id == classToDrop.ClassId.Id).Any()); if (childClasses.Any()) { var noInterfaceResult = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Can't drop interface with childs: " + String.Join(", ", childClasses.Select(c => c.Name)) + ". Drop childs first." }; return(new QueryDTO() { Result = noInterfaceResult }); } if (!parameters.Database.Schema.Classes.TryRemove(classToDrop.ClassId, out Class dropedClass)) { parameters.Log("Could not drop interface", MessageLevel.Error); return(new QueryDTO() { Result = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Error ocured while interface droping" } }); } parameters.Storage.SaveSchema(parameters.Database.Schema); parameters.Log("Droped interface: " + dropedClass.Name, MessageLevel.QueryExecution); return(new QueryDTO() { Result = new DTOQueryResult() { NextResult = null, QueryResults = null, QueryResultType = ResultType.StringResult, StringOutput = "Interface:" + dropedClass.Name + " droped." } }); }