private void GenerateXMLFromMDB(string pFilename) { mfSysTableExport = false; mfSysTablePVEntityGenerator = false; mDBDefinition = new dbdefinition(); mDBDefinition.generatedon = DateTime.Now; // Generate entities ArrayList alEntities = new ArrayList(); ADODB.Connection con = new ADODB.ConnectionClass(); try { con.Open(DBServerHelper.getDatabaseOleDbConnectionString(pFilename), "", "", 0); } catch (Exception ex) { throw new Exception("Unable to open database file:\n" + ex.Message); } ADOX.Catalog cat = new ADOX.Catalog(); cat.ActiveConnection = con; mStatusHandler.InitStatus("Analyzing MDB table structure...", cat.Tables.Count); int intTable = 0; int intTableRealCount = 0; foreach (ADOX.Table tbl in cat.Tables) { intTable++; if (!IsSystemTableName(tbl.Name)) { intTableRealCount++; mStatusHandler.SetStatus("Analyzing table '" + tbl.Name + "'...", intTable); alEntities.Add(GenerateEntity(tbl, con)); } else { // Check if settings from older versions of SQLExporter or PVEntityGenerator exist if (!mfSysTableExport) { mfSysTableExport = tbl.Name.ToLower().Equals("_export"); } if (!mfSysTablePVEntityGenerator) { mfSysTablePVEntityGenerator = tbl.Name.ToLower().Equals("_pventitygenerator"); } } } con.Close(); if (alEntities.Count != 0) { mDBDefinition.entities = (dbdefinitionEntity[])alEntities.ToArray(typeof(dbdefinitionEntity)); } mStatusHandler.ClearStatus("Analyzed " + intTableRealCount + " table(s)."); }
public void GenerateFiles_EntityGeneration() { string strPlatformDefPath = mConfigDir; foreach (pventitygeneratorconfigPlatform p in mConfig.platforms) { if (p.name.Equals(mPlatformDef.name)) { if (p.definitiondir != null && p.definitiondir.Length != 0) { strPlatformDefPath += p.definitiondir; } break; } } if (strPlatformDefPath.Length != 0) { strPlatformDefPath += "\\"; } int intSelectedEntityCount = mSelectedEntities.Count; if (mSelectedEntities.Contains(GLOBAL_ITEMS)) { intSelectedEntityCount--; } // Gesamtzahl der Schritte ermitteln int intStepsTotal = 0; if (mPlatformDef.entitygeneration != null && mPlatformDef.entitygeneration.generateentity != null && mPlatformDef.entitygeneration.generateentity.generatefile != null) { intStepsTotal += mPlatformDef.entitygeneration.generateentity.generatefile.Length * intSelectedEntityCount; } if (mSelectedEntities.Contains(GLOBAL_ITEMS)) { if (mPlatformDef.entitygeneration != null && mPlatformDef.entitygeneration.generateglobal != null && mPlatformDef.entitygeneration.generateglobal.generatefile != null) { intStepsTotal += mPlatformDef.entitygeneration.generateglobal.generatefile.Length; } } if (mProjectSettings.dbplatforms != null && mProjectSettings.dbplatforms.dbplatform != null) { foreach (projectsettingsDbplatformsDbplatform dbplatform in mProjectSettings.dbplatforms.dbplatform) { dbplatformdefinition dbplatformdef = (dbplatformdefinition)mhashDbPlatformDef[dbplatform.name]; if (dbplatformdef != null && dbplatformdef.scriptgeneration != null && dbplatformdef.scriptgeneration.generateglobal != null && dbplatformdef.scriptgeneration.generateglobal.generatefile != null) { foreach (generatefile file in dbplatformdef.scriptgeneration.generateglobal.generatefile) { if (!file.name.Equals("entity-create") && !file.name.Equals("entity-patch")) { intStepsTotal++; } } } } } mStatusHandler.InitStatus("Generating files...", intStepsTotal); int intStep = 0; // Generate entity files if (mPlatformDef.entitygeneration != null && mPlatformDef.entitygeneration.generateentity != null && mPlatformDef.entitygeneration.generateentity.generatefile != null) { foreach (generatefile file in mPlatformDef.entitygeneration.generateentity.generatefile) { XslCompiledTransform transform = GetTransform(strPlatformDefPath + file.transform); string strPath = ExpandPlatformParameters(file.path); if (strPath.Length != 0 && !strPath.EndsWith("\\")) { strPath = strPath += "\\"; } foreach (dbdefinitionEntity entity in mDbDefinition.entities) { if (mSelectedEntities.Contains(entity.name)) { string strFilename = ExpandPlatformParameters(strPath + file.filename, entity.name); GenerateFile(transform, strFilename, mPlatformDef.parameterdefinitions, entity.name, file.encoding); mStatusHandler.SetStatus("Generated: " + strFilename, ++intStep); } } } } // Generate global if (mSelectedEntities.Contains(GLOBAL_ITEMS)) { // Generate global files if (mPlatformDef.entitygeneration != null && mPlatformDef.entitygeneration.generateglobal != null && mPlatformDef.entitygeneration.generateglobal.generatefile != null) { foreach (generatefile file in mPlatformDef.entitygeneration.generateglobal.generatefile) { XslCompiledTransform transform = GetTransform(strPlatformDefPath + file.transform); string strPath = ExpandPlatformParameters(file.path); if (strPath.Length != 0 && !strPath.EndsWith("\\")) { strPath = strPath += "\\"; } string strFilename = ExpandPlatformParameters(strPath + file.filename); GenerateFile(transform, strFilename, mPlatformDef.parameterdefinitions, file.encoding); mStatusHandler.SetStatus("Generated: " + strFilename, ++intStep); } } // Generate global scripts if (mProjectSettings.dbplatforms != null && mProjectSettings.dbplatforms.dbplatform != null) { foreach (projectsettingsDbplatformsDbplatform dbplatform in mProjectSettings.dbplatforms.dbplatform) { dbplatformdefinition dbplatformdef = (dbplatformdefinition)mhashDbPlatformDef[dbplatform.name]; if (dbplatformdef != null && dbplatform != null) { string strDbPlatformDefPath = mConfigDir; foreach (pventitygeneratorconfigDbplatform p in mConfig.dbplatforms) { if (p.name.Equals(dbplatformdef.name)) { if (p.definitiondir != null && p.definitiondir.Length != 0) { strDbPlatformDefPath += p.definitiondir; } break; } } if (strDbPlatformDefPath.Length != 0) { strDbPlatformDefPath += "\\"; } if (dbplatformdef.scriptgeneration != null && dbplatformdef.scriptgeneration.generateglobal != null && dbplatformdef.scriptgeneration.generateglobal.generatefile != null) { foreach (generatefile file in dbplatformdef.scriptgeneration.generateglobal.generatefile) { if (!file.name.Equals("entity-create") && !file.name.Equals("entity-patch")) { XslCompiledTransform transform = GetTransform(strDbPlatformDefPath + file.transform); string strPath = ExpandDbPlatformParameters(file.path, dbplatformdef, dbplatform); if (strPath.Length != 0 && !strPath.EndsWith("\\")) { strPath = strPath += "\\"; } string strFilename = ExpandDbPlatformParameters(strPath + file.filename, dbplatformdef, dbplatform); GenerateFile(transform, strFilename, dbplatformdef.parameterdefinitions, file.encoding); mStatusHandler.SetStatus("Generated: " + strFilename, ++intStep); } } } } } } } mStatusHandler.ClearStatus("Generated " + intStep + " file(s)."); }
public static void ExecuteScript(dbplatformdefinition pDbPlatformDef, projectsettingsDbplatformsDbplatform pDbPlatform, string pScript, StatusHandler pStatusHandler) { bool fInComment = false; bool fSimpleComment = false; bool fInString = false; int intStartPos = 0; if (pScript == null || pScript.Length <= 0) { pStatusHandler.InitStatus("Empty script.", 0); return; } pScript = pScript.Replace("\r\n", "\n"); string strDelim = GetDelimiter(pDbPlatformDef, pScript); ArrayList aSQL = new ArrayList(); for (int i = 0; i < pScript.Length; i++) { char ch = pScript[i]; if (fInComment) { if (fSimpleComment && ch == '\n') { fInComment = false; intStartPos = ++i; continue; } else if (!fSimpleComment && ch == '*' && (i + 1) < pScript.Length && pScript[i + 1] == '/') { fInComment = false; i++; intStartPos = ++i; continue; } else { continue; } } else { if (ch == '/' && (i + 1) < pScript.Length && pScript[i + 1] == '*') { fInComment = true; fSimpleComment = false; continue; } else if (ch == '-' && (i + 1) < pScript.Length && pScript[i + 1] == '-') { fInComment = true; fSimpleComment = true; continue; } } if (ch == '\'') { fInString = !fInString; continue; } int intLenDelim = strDelim.Length; if (!fInComment && !fInString) { if (i + intLenDelim >= pScript.Length) { string strSQL = pScript.Substring(intStartPos, pScript.Length - intStartPos - intLenDelim). Trim(new char[] { ' ', '\n' }); if (strSQL != null && strSQL.Length > 0) { aSQL.Add(strSQL); } break; } else if (pScript.Substring(i, intLenDelim + 1).ToLower().Equals(strDelim.ToLower() + "\n")) { string strSQL = pScript.Substring(intStartPos, i - intStartPos).Trim(new char[] { ' ', '\n' }); if (strSQL != null && strSQL.Length > 0) { aSQL.Add(strSQL); } intStartPos = i + intLenDelim; i += intLenDelim; } } } int intStep = 0; pStatusHandler.InitStatus("Executing SQL...", aSQL.Count); IDbConnection con = GetConnection(pDbPlatformDef, pDbPlatform); bool fError = false; int intCounter = 0; IDbCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.Text; while (true) { if (intCounter >= aSQL.Count) { break; } pStatusHandler.SetStatus("Executing SQL...", ++intStep); string strSQL = (string)aSQL[intCounter]; try { cmd.CommandText = strSQL; cmd.ExecuteNonQuery(); intCounter++; } catch (Exception pex) { string strMsg = pex.Message + "\n\n"; // append first 1000 chars of sql string to error message if (strSQL.Length > 1000) { strMsg += strSQL.Substring(0, 1000) + "..."; } else { strMsg += strSQL; } // ask if user wants to retry, ignore and continue or cancel DialogResult res = pStatusHandler.OnError(strMsg); if (res == DialogResult.Ignore) { intCounter++; } else if (res != DialogResult.Retry) { pStatusHandler.ClearStatus("Execution aborted with errors."); fError = true; break; } } } con.Close(); if (!fError) { pStatusHandler.ClearStatus("Execution completed sucessfully."); } }