public static void ProcessCurrentProject() { IProject project = ApplicationContext.Current.Services.Get <IProjectContextService>().ActiveProject; List <MethodDescription> methodsToCreate = CollectMethodDescriptions(project.Models.Get <PortalModel>()); if (methodsToCreate.Count == 0) { LOG.Info("No auto-wire method found"); } OrmModel model = project.Models.Get <OrmModel>(); foreach (OrmPackage package in model.Packages) { foreach (OrmEntity entity in package.Entities) { List <MethodDescription> methodsForEntity = methodsToCreate.Where(x => x.TargetEntity == entity.Name).ToList(); bool modified = false; modified = CleanAutowireMethods(entity, methodsForEntity); foreach (MethodDescription method in methodsForEntity) { AddMethodToEntity(entity, method); modified = true; } if (modified) { entity.Save(); } } } }
/// <summary> /// Load the relationship info from the project (uses the ProjectContextService to determine the current project) /// </summary> /// <param name="project"></param> public void LoadRelationships() { ThreadPool.QueueUserWorkItem(delegate { IProject project = ApplicationContext.Current.Services.Get <IProjectContextService>().ActiveProject; IsBusy = true; try { Relationships = new ObservableCollection <Relationship>(); OrmModel model = project.Models.Get <OrmModel>(); foreach (OrmRelationship relationship in model.Relationships) { if (relationship.Columns.Count > 1) { continue; } _dispatcher.Invoke(DispatcherPriority.Normal, new Action <OrmRelationship>(AddRelationship), relationship); } } finally { IsBusy = false; } }); }
protected override OrmResult MappedCheckExists <T>(T objModel) { OrmResult result = null; if (objModel != null) { StringBuilder sqlText = new StringBuilder(); Dictionary <string, DbParameter> parameters = new Dictionary <string, DbParameter>(); OrmModel objMappedModel = this.GetMappedModel <T>(); /**/ sqlText.Append("select count(*) from `{Table}` where 1 = 1 {Where}"); sqlText.Replace("{Table}", objMappedModel.Table.TableName); /**/ StringBuilder whereText = new StringBuilder(); string parameterName = null; object parameterValue = null; MySqlParameter parameter = null; foreach (KeyValuePair <string, OrmColumnAttribute> column in objMappedModel.Columns) { if (column.Value.IsUnique) { parameterName = this.DbCharacter + column.Value.ColumName; parameterValue = column.Value.MappedPropertyInfo.GetValue(objModel, null); parameter = new MySqlParameter(parameterName, column.Value.MySqlDataType); parameter.Value = parameterValue; parameters.Add(parameterName, parameter); /**/ whereText.Append(" and " + column.Value.ColumName + " = " + parameterName); } } sqlText.Replace("{Where}", whereText.ToString()); result = new OrmResult(sqlText.ToString(), parameters); } return(result); } protected override OrmResult MappedInsert <T>(T objModel) { OrmResult result = null; if (objModel != null) { StringBuilder sqlText = new StringBuilder(); Dictionary <string, DbParameter> parameters = new Dictionary <string, DbParameter>(); OrmModel objMappedModel = this.GetMappedModel <T>(); /**/ sqlText.Append("insert into `{Table}`({Column}) values({Value})"); sqlText.Replace("{Table}", objMappedModel.Table.TableName); /**/ StringBuilder columnText = new StringBuilder(); StringBuilder valueText = new StringBuilder(); string parameterName = null; object parameterValue = null; MySqlParameter parameter = null; foreach (KeyValuePair <string, OrmColumnAttribute> column in objMappedModel.Columns) { if (!column.Value.IsAutoincrement && column.Value.IsInsert) { parameterName = this.DbCharacter + column.Value.ColumName; parameterValue = column.Value.MappedPropertyInfo.GetValue(objModel, null); if (parameterValue != null || (!column.Value.IsNullable)) { parameter = new MySqlParameter(parameterName, column.Value.MySqlDataType); parameter.Value = parameterValue; parameters.Add(parameterName, parameter); /**/ columnText.Append(column.Value.ColumName + ", "); valueText.Append(parameterName + ", "); } } } columnText.Remove(columnText.Length - 2, 2); valueText.Remove(valueText.Length - 2, 2); sqlText.Replace("{Column}", columnText.ToString()); sqlText.Replace("{Value}", valueText.ToString()); result = new OrmResult(sqlText.ToString(), parameters); }
public void PersistEntities() { _ormModel = _projectContext.ActiveProject.Models.Get<OrmModel>(); using (_context.Status.BeginStep("Persisting entities...", _context.Tables.Count*2 + 1)) { foreach (StepHandler step in _steps) { step(); if (!_context.Status.Advance()) { break; } } } _ormModel = null; }