/// <summary> /// Import relationship / foreign key details from create table statements. /// </summary> /// <returns>Entity meta block.</returns> public DatabaseMetaBlock ImportRelationshipsFromCreateTableStatement() { DatabaseMetaBlock databaseBlock = null; TableRelationshipMetaBlock tableRelationshipBlock = null; TableRelationship relationship = null; string line = string.Empty; int nPosAlterTable = 0; int nPosForeignKey = 0; int nPosReferences = 0; int nPos1 = 0; int nPos2 = 0; bool regionOpened = false; bool regionClosed = false; bool tablePrimaryKeyOpened = false; int lineNumber = 0; while (!regionClosed && lineNumber < _inputLines.Length) { line = _inputLines[lineNumber].Trim(); if (line.Length > 0) { if (!line.StartsWith("//") && !line.StartsWith("--")) { if (!regionOpened) { if (line.Contains("#region Create")) { databaseBlock = new DatabaseMetaBlock(); regionOpened = true; } } else { if (line.EndsWith("#endregion")) { regionClosed = true; } else { if (line.ToUpper().Contains("CREATE TABLE ")) { nPos1 = line.LastIndexOf("["); if (nPos1 > -1) { nPos2 = line.LastIndexOf("]"); if (nPos2 > -1) { if (databaseBlock != null && tableRelationshipBlock != null) { databaseBlock.Tables.Add(tableRelationshipBlock); } tableRelationshipBlock = new TableRelationshipMetaBlock(); relationship = new TableRelationship(); tableRelationshipBlock.TableName = line.Substring(nPos1 + 1, nPos2 - nPos1 - 1); } } } if (line.StartsWith("CONSTRAINT") && line.Contains("PRIMARY KEY")) { tablePrimaryKeyOpened = true; } if (tablePrimaryKeyOpened) { if (line.StartsWith("[")) { tableRelationshipBlock.PrimaryKey = GetParameter(line); tablePrimaryKeyOpened = false; } } //ALTER TABLE [dbo].[tblSource] WITH CHECK ADD CONSTRAINT [FK_tblSource_tblDivorceDetails] FOREIGN KEY([DivorceDetailID]) //REFERENCES [dbo].[tblDivorceDetails] ([ID]) nPosAlterTable = line.IndexOf("ALTER TABLE "); if (nPosAlterTable > -1) { relationship.Dependant.TableName = GetTableName(line.Substring(nPosAlterTable)); } nPosForeignKey = line.IndexOf("FOREIGN KEY"); if (nPosForeignKey > -1) { relationship.Dependant.ColumnName = GetParameter(line.Substring(nPosForeignKey)); } nPosReferences = line.IndexOf("REFERENCES"); if (nPosReferences > -1) { relationship.DependsOn.TableName = GetTableName(line.Substring(nPosReferences)); nPos1 = line.IndexOf("(", nPosReferences); if (nPos1 > -1) { relationship.DependsOn.ColumnName = GetParameter(line.Substring(nPos1)); tableRelationshipBlock.Relationships.Add(relationship); relationship = new TableRelationship(); } } } } } } lineNumber++; } if (tableRelationshipBlock != null) { databaseBlock.Tables.Add(tableRelationshipBlock); } return(databaseBlock); }
/// <summary> /// Import relationship foreign key details from create table statement. /// </summary> private void ImportLines() { _inputLines = ReadFile(@"C:\_N\Notepod\Notepod\Input\Tables.txt"); _databaseBlock = ImportRelationshipsFromCreateTableStatement(); }