// Equals(object obj, columnOrderIsImportant)

        /*public bool ExactEquals(DatabaseTableStructure other)
         * {
         *  if (other == null)
         *  {
         *      return false;
         *  }
         *
         *  var myDefinition = ToString().ToUpper();
         *  var otherTableDefinition = other.ToString().ToUpper();
         *
         *  if (TableName == other.TableName)
         *  {
         *      if (myDefinition != otherTableDefinition)
         *      {
         *          InfoBox.Show($"Tables are not the same: {TableName}", $"{ToString().ToUpper()}{Environment.NewLine}{otherTableDefinition}");
         *      }
         *  }
         *  return myDefinition == otherTableDefinition;
         * }*/

        public bool PermissiveEquals(DatabaseTableStructure other)
        {
            if (other != null)
            {
                return(TableName == other.TableName && Columns.All(t => t == other[t.ColumnName]));
            }

            return(false);
        }
        private bool NeedToRecreatePrimaryIds(DatabaseTableStructure destinationDts)
        {
            if (PrimaryIds.Count != destinationDts.PrimaryIds.Count)
            {
                return(true);
            }

            return(destinationDts.PrimaryIds.Any(destinationPrimaryId =>
                                                 PrimaryIds.All(primaryId => destinationPrimaryId.ColumnName != primaryId.ColumnName)));
        }
Beispiel #3
0
        public static string CorrectWildcardColumnsIfNecessary(DatabaseContext dc, string query, string coloumnNames)
        {
            if (coloumnNames == "*")
            {
                var table = query.ToUpper().Substring("FROM ");
                var index = table.IndexOf(' ');
                if (index != NotFound)
                {
                    table = table.Substring(0, index);
                }

                var tableStructureProvider = new TableStructureProvider();
                var tableStructure         = new DatabaseTableStructure(table, tableStructureProvider.GetDatabaseTableStructure(dc, table));
                coloumnNames = tableStructure.GetCommaSeparatedColumnNames();
            }
            return(coloumnNames);
        }
        public void ModifyTableStructure(DatabaseContext dc, DatabaseTableStructure destinationDts)
        {
            foreach (var destinationColumnStructure in destinationDts.Columns)
            {
                var dcs = this[destinationColumnStructure.ColumnName];
                if (dcs == null) // This database column must be created
                {
                    DatabaseColumnStructure.CreateColumn(dc, TableName, destinationColumnStructure.ToString());
                }
                else // Check column structures
                {
                    dcs.ModifyColumnStructure(dc, TableName, destinationColumnStructure);
                }
            }
            foreach (var columnStructure in Columns)
            {
                if (destinationDts[columnStructure.ColumnName] == null) // This database column does not exists anymore
                {
                    columnStructure.DeleteColumn(dc, TableName);
                }
            }

            if (!NeedToRecreatePrimaryIds(destinationDts))
            {
                return;
            }

            for (var i = 0; i < PrimaryIds.Count; i++)
            {
                dc.ExecuteNonQuery("DECLARE @CMD VARCHAR(MAX); SET @CMD = 'ALTER TABLE Options DROP CONSTRAINT ' + (SELECT constraint_name FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1 AND TABLE_NAME = @C AND COLUMN_NAME = @C2); EXEC(@CMD);", TableName, PrimaryIds[i].ColumnName);
            }

            var primaryKeys = GetPrimaryIdsInStringFormat(destinationDts.PrimaryIds);
            var alterTable  = $"ALTER TABLE {TableName} ADD PRIMARY KEY ({primaryKeys});";

            dc.ExecuteNonQuery(alterTable);
        }