/// <summary> /// Elimina la tabella di storage dell'UDS e tutte le tabelle di relazione relativa la specifica UDS. /// </summary> /// <param name="connStr"></param> /// <param name="commitHook">Function Delegate che consente passare una funzione esterna da eseguire prima del commit su DB. Se ritorna False viene fatto rollback della transazione</param> public void DropStorage(string connStr) { try { using (SmoContext ctx = new SmoContext(connStr, DbSchema)) using (SqlTransaction trans = ctx.BeginTransaction()) { try { //prima le tabelle esterne ctx.DropTable(_udsTableBuilder.UDSDocumentsTableName); //ultima la uds perchè ci sono le FK ctx.DropTable(_udsTableBuilder.UDSTableName); _logger.WriteDebug(new LogMessage("CommitTransaction"), LogCategories); trans.Commit(); } catch (Exception ex) { _logger.WriteError(ex, LogCategories); _logger.WriteDebug(new LogMessage("RollbackTransaction"), LogCategories); trans.Rollback(); throw new UDSStorageException(string.Format("DropStorage - Errore durante l'esecuzione: {0}", ex.Message), ex); } } } catch (Exception ex) { _logger.WriteError(ex, LogCategories); throw; } }
private async Task <UDSEntity> UpdateStorageAsync(UDSStorageFacade storage) { UDSEntity entity = null; MetadataEntity metadatas = new MetadataEntity(_logger); try { //verifica esistenza tabella using (SmoContext smo = new SmoContext(ConnectionString, DBSchema)) { if (!smo.TableExist(storage.Builder.UDSTableName)) { _logger.WriteError(new LogMessage(string.Concat(storage.Builder.UDSTableName, " not exist")), LogCategories); throw new Exception(string.Concat(storage.Builder.UDSTableName, " - UDS structures not found")); } } await storage.UpdateStorageAsync(ConnectionString); _logger.WriteInfo(new LogMessage("update storage completed"), LogCategories); entity = metadatas.LoadMetadata(storage); } catch (Exception ex) { _logger.WriteError(ex, LogCategories); throw ex; } return(entity); }
/// <summary> /// Crea la tabella di storage dell'UDS e tutte le tabelle per contenere le relazioni esterne (Documents, Contacts ecc...) /// </summary> /// <param name="connStr"></param> /// <param name="commitHookAsync">Function Delegate che consente passare una funzione esterna da eseguire prima del commit su DB. Se ritorna False viene fatto rollback della transazione</param> public async Task CreateStorageAsync(string connStr, Func <SmoContext, Task <bool> > commitHookAsync = null) { try { using (SmoContext ctx = new SmoContext(connStr, DbSchema)) //using (SqlTransaction trans = ctx.BeginTransaction()) { try { _logger.WriteDebug(new LogMessage("BeginTransaction"), LogCategories); if (!ctx.TableExist(_udsTableBuilder.UDSTableName)) { _logger.WriteDebug(new LogMessage($"creating {_udsTableBuilder.UDSTableName} table"), LogCategories); _udsTableBuilder.CreateUDSTable(ctx); } else { _logger.WriteDebug(new LogMessage($"{_udsTableBuilder.UDSTableName} already exist"), LogCategories); } if (!ctx.TableExist(_udsTableBuilder.UDSDocumentsTableName)) { _logger.WriteDebug(new LogMessage($"creating {_udsTableBuilder.UDSDocumentsTableName} table"), LogCategories); _udsTableBuilder.CreateUDSDocumentsTable(ctx); } else { _logger.WriteDebug(new LogMessage($"{_udsTableBuilder.UDSDocumentsTableName} already exist"), LogCategories); } if (commitHookAsync != null) { _logger.WriteDebug(new LogMessage("invoke commitHook"), LogCategories); if (!await commitHookAsync(ctx)) { _logger.WriteDebug(new LogMessage("RollbackTransaction"), LogCategories); //trans.Rollback(); return; } } _logger.WriteDebug(new LogMessage("CommitTransaction"), LogCategories); //trans.Commit(); } catch (Exception ex) { _logger.WriteDebug(new LogMessage("RollbackTransaction"), LogCategories); _logger.WriteError(ex, LogCategories); //trans.Rollback(); throw new UDSStorageException(string.Format("CreateStorage - Errore durante l'esecuzione: {0}", ex.Message), ex); } } } catch (Exception ex) { _logger.WriteError(ex, LogCategories); throw; } }
public void CreateUDSDocumentsTable(SmoContext ctx) { Table tb = new Table(ctx.DbInstace, UDSDocumentsTableName, _dbSchema); Column col = new Column(tb, UDSDocumentsPK, DataType.UniqueIdentifier) { Nullable = false }; tb.Columns.Add(col); col = new Column(tb, UDSFK, DataType.UniqueIdentifier) { Nullable = false }; tb.Columns.Add(col); col = new Column(tb, UDSDocumentsFK, DataType.UniqueIdentifier) { Nullable = false }; tb.Columns.Add(col); col = new Column(tb, UDSDocumentsDocumentNameField, DataType.NVarChar(256)) { Nullable = true }; tb.Columns.Add(col); col = new Column(tb, UDSDocumentsDocumentTypeField, DataType.SmallInt) { Nullable = false }; tb.Columns.Add(col); col = new Column(tb, UDSDocumentsDocumentLabelField, DataType.NVarChar(256)) { Nullable = true }; tb.Columns.Add(col); AddDateTimeOffsetField(UDSRegistrationDateField, tb); tb.ValidateSchema(); tb.Create(); tb.AddPrimaryKey(UDSDocumentsPK); tb.AddForeignKey(UDSFK, UDSTableName, _dbSchema, UDSPK); tb.AddIndex(UDSFK); tb.AddClusterIndex(UDSRegistrationDateField); }
private string GetLoginName(ADAllowableInputObject id) { if (id.FromADObject && string.IsNullOrEmpty(id.NetBiosName)) { if (!oneTimeNB || string.IsNullOrEmpty(netBiosName)) { netBiosName = SmoContext.GetNetBiosFromDn(id.DistinguishedName); } id.NetBiosName = netBiosName; } return(id.AsLoginName(_server)); }
protected override void ProcessRecord() { if (this.Database != null) { _dbs.Add(this.Database); } else if (SmoContext.Databases != null && SmoContext.Databases.Count > 0) { _dbs.AddRange(SmoContext.Databases); } else { SmoContext.SetDatabases(_server.Databases); _dbs.AddRange(SmoContext.Databases); } }
/// <summary> /// Aggiorna la tabella di storage dell'UDS. Vengono solamente aggiunti i campi mancanti rispetto l'esistente. Qualsiasi altra variazione non viene considerata /// </summary> /// <param name="connStr"></param> /// <param name="commitHook">Function Delegate che consente passare una funzione esterna da eseguire prima del commit su DB. Se ritorna False viene fatto rollback della transazione</param> public async Task UpdateStorageAsync(string connStr, Func <Task <bool> > commitHookAsync = null) { try { using (SmoContext ctx = new SmoContext(connStr, DbSchema)) //using (SqlTransaction trans = ctx.BeginTransaction()) { try { _logger.WriteDebug(new LogMessage("BeginTransaction"), LogCategories); //ctx.BeginTransaction(); _logger.WriteDebug(new LogMessage("UpdateUDSTable"), LogCategories); _udsTableBuilder.UpdateUDSTable(ctx); if (commitHookAsync != null) { _logger.WriteDebug(new LogMessage("invoke commitHook"), LogCategories); if (!(await commitHookAsync())) { _logger.WriteDebug(new LogMessage("RollbackTransaction"), LogCategories); //trans.Rollback(); return; } } _logger.WriteDebug(new LogMessage("CommitTransaction"), LogCategories); //trans.Commit(); } catch (Exception ex) { _logger.WriteError(ex, LogCategories); _logger.WriteDebug(new LogMessage("RollbackTransaction"), LogCategories); //trans.Rollback(); throw new UDSStorageException(string.Format("UpdateStorage - Errore durante l'esecuzione: {0}", ex.Message), ex); } } } catch (Exception ex) { _logger.WriteError(ex, LogCategories); throw; } }
private void ConditionOne() { if (this.LoginName.LoginName.Contains(@"\")) { string[] strs = this.LoginName.LoginName.Split(new string[1] { @"\" }, StringSplitOptions.RemoveEmptyEntries); this.LoginName.LoginName = strs.Last(); this.LoginName.NetBiosName = strs.First().ToUpper(); } string cls = SmoContext.FindADObjectClassFromSamAccountName(this.LoginName.LoginName, out string dn); this.LoginName.Type = SmoContext.FindADLoginFromObjectClass(cls); this.LoginName.FromADObject = true; if (!string.IsNullOrEmpty(dn)) { this.LoginName.DistinguishedName = dn; } }
protected override void ProcessRecord() { if (this.Database != null && this.MyInvocation.PipelinePosition >= 2) { if (this.HasNames()) { WildcardPattern[] patterns = this.PatternsFromNames(this.ViewName); this.FilterByName(ref _views, patterns, this.Database); } else { _views.AddRange(this.Database.Views); } } else if (this.Database != null && this.MyInvocation.PipelinePosition < 2) { NoEnd = true; if (this.HasNames()) { WildcardPattern[] patterns = base.PatternsFromNames(this.ViewName); for (int i = 0; i < this.Database.Views.Count; i++) { View v = this.Database.Views[i]; if (base.NameMatchesPatterns(v.Name, patterns)) { base.WriteObject(v); } } } } else { if (SmoContext.Databases == null) { SmoContext.SetDatabases(_server.Databases); } _views.AddRange(SmoContext.Databases.SelectMany(x => x.Views.Cast <View>())); } }
public void UpdateUDSTable(SmoContext ctx) { Table tb = ctx.GetTable(_tableName); if (tb == null) { throw new Exception(string.Concat("Table ", _tableName, " not found in methods UpdateUDSTable")); } //colonne dei metadati if (_uds.Model.Metadata != null && _uds.Model.Metadata.Length > 0) { foreach (Section section in _uds.Model.Metadata) { foreach (FieldBaseType field in section.Items.Where(f => !ctx.ColumnExist(tb, f.ColumnName, true))) { AddField(field, tb); } } } tb.ValidateSchema(); tb.Alter(); }
public void CreateUDSTable(SmoContext ctx) { Table tb = new Table(ctx.DbInstace, _tableName, _dbSchema); Column col = new Column(tb, UDSPK, DataType.UniqueIdentifier) { Nullable = false }; tb.Columns.Add(col); //colonne dei metadati if (_uds.Model.Metadata != null && _uds.Model.Metadata.Length > 0) { foreach (Section section in _uds.Model.Metadata.Where(f => f.Items != null)) { foreach (FieldBaseType field in section.Items) { AddField(field, tb); } } } col = new Column(tb, UDSRepositoryFK, DataType.UniqueIdentifier) { Nullable = false }; tb.Columns.Add(col); //colonne di default AddDateTimeOffsetField(UDSRegistrationDateField, tb); col = new Column(tb, UDSRegistrationUserField, DataType.NVarChar(256)) { Nullable = false }; tb.Columns.Add(col); AddDateTimeOffsetField(UDSLastChangedDateField, tb, true); col = new Column(tb, UDSLastChangedUserField, DataType.NVarChar(256)) { Nullable = true }; tb.Columns.Add(col); col = new Column(tb, UDSYearField, DataType.SmallInt) { Nullable = false }; tb.Columns.Add(col); col = new Column(tb, UDSNumberField, DataType.Int) { Nullable = false }; tb.Columns.Add(col); col = new Column(tb, UDSSubjectField, DataType.NVarChar(4000)) { Nullable = true }; tb.Columns.Add(col); col = new Column(tb, UDSIdCategoryFK, DataType.SmallInt) { Nullable = false }; tb.Columns.Add(col); col = new Column(tb, UDSStatusField, DataType.SmallInt) { Nullable = false }; col.AddDefaultConstraint().Text = "1"; // Active tb.Columns.Add(col); col = new Column(tb, UDSCancelMotivationField, DataType.NVarChar(1024)) { Nullable = true }; tb.Columns.Add(col); col = new Column(tb, UDSTimestampField, DataType.Timestamp) { Nullable = false }; tb.Columns.Add(col); //Create the table on the instance of SQL Server. tb.ValidateSchema(); tb.Create(); tb.AddPrimaryKey(UDSPK); tb.AddForeignKey(UDSIdCategoryFK, DSWCategoryTableName, "dbo", DSWIdCategoryPK); tb.AddForeignKey(UDSRepositoryFK, UDSRepositoriesTableName, _dbSchema, UDSRepositoryFK); tb.AddClusterIndex(UDSRegistrationDateField); tb.AddIndex(new List <string>() { UDSYearField, UDSNumberField }); }