private CmsDependencyMessage[] TestColumn(DBColumnDescription col, DataRowCollection rows, string tableName) { List <CmsDependencyMessage> ret = new List <CmsDependencyMessage>(); bool colFound = false; foreach (DataRow dr in rows) { string fieldName = dr["Field"].ToString(); string fieldType = dr["Type"].ToString(); if (String.Compare(fieldName, col.ColumnName, true) == 0) { //TODO: add comparison of (optional) column types colFound = true; break; } } // foreach if (col.existsMode == ExistsMode.MustExist && !colFound) { ret.Add(CmsDependencyMessage.Error(tableName + " table does not have a required column named '" + col.ColumnName + "'")); } else if (col.existsMode == ExistsMode.MustNotExist && colFound) { ret.Add(CmsDependencyMessage.Error(tableName + " table has a column named '" + col.ColumnName + "' that must be removed.")); } return(ret.ToArray()); }
public CmsDatabaseTableDependency(string mysqlCreateTableStatement, string[] colNamesThatMustNotExist) { initFromMySqlCreateStatement(mysqlCreateTableStatement); List <DBColumnDescription> columns = new List <DBColumnDescription>(); foreach (string colName in colNamesThatMustNotExist) { DBColumnDescription c = new DBColumnDescription(colName, ExistsMode.MustNotExist); columns.Add(c); } Columns = columns.ToArray(); }
protected void initFromMySqlCreateStatement(string mysqlCreateTableStatement) { if (mysqlCreateTableStatement.IndexOf("CREATE TABLE", StringComparison.CurrentCultureIgnoreCase) < 0) { throw new ArgumentException("Error: you have not specified a CREATE TABLE statement for CmsDatabaseTableDependency(" + mysqlCreateTableStatement + ")"); } // -- 1: table name int indexFirstOpenBracket = mysqlCreateTableStatement.IndexOf("(", StringComparison.CurrentCultureIgnoreCase); int indexLastCloseBracket = mysqlCreateTableStatement.LastIndexOf(")", StringComparison.CurrentCultureIgnoreCase); string tNameStartsAfter = "CREATE TABLE "; int tNameStartsAfterIndex = mysqlCreateTableStatement.IndexOf(tNameStartsAfter) + tNameStartsAfter.Length; string tName = mysqlCreateTableStatement.Substring(tNameStartsAfterIndex, (indexFirstOpenBracket - 1) - tNameStartsAfterIndex); tName = RemoveAtStartAndEnd("`", tName); this.TableName = tName.ToLower(); // -- column statements string allColumnStatements = mysqlCreateTableStatement.Substring(indexFirstOpenBracket, indexLastCloseBracket - indexFirstOpenBracket); // remove first open bracket allColumnStatements = allColumnStatements.Substring(1); string[] colStatements = allColumnStatements.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); List <DBColumnDescription> dbColumnDescriptions = new List <DBColumnDescription>(); foreach (string rawColStatement in colStatements) { string colStatement = rawColStatement.Trim(); int firstTickIndex = colStatement.IndexOf("`", StringComparison.CurrentCultureIgnoreCase); int lastTickIndex = colStatement.LastIndexOf("`", StringComparison.CurrentCultureIgnoreCase); bool isKey = false; if (colStatement.IndexOf("PRIMARY KEY", StringComparison.CurrentCultureIgnoreCase) >= 0) { isKey = true; } else if (colStatement.IndexOf("UNIQUE KEY", StringComparison.CurrentCultureIgnoreCase) >= 0) { isKey = true; } else if (colStatement.IndexOf("UNIQUE INDEX", StringComparison.CurrentCultureIgnoreCase) >= 0) { isKey = true; } else if (colStatement.IndexOf("KEY `", StringComparison.CurrentCultureIgnoreCase) >= 0) { isKey = true; } if (!isKey && firstTickIndex >= 0 && lastTickIndex > firstTickIndex) { string cName = colStatement.Substring(0, lastTickIndex); cName = RemoveAtStartAndEnd("`", cName); DBColumnDescription c = new DBColumnDescription(cName, ExistsMode.MustExist); dbColumnDescriptions.Add(c); } } // foreach colStatement this.Columns = dbColumnDescriptions.ToArray(); }