// Button clicks private void btnGenerateCode_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; try { // make sure we have a solid target to push to if (Directory.Exists(txtOutputPath.Text)) { _OutputPath = txtOutputPath.Text; } else { DisplayMessage("Specified output directory does not exist, halting code generation.", true); return; } SqlDatabase sql_database = new SqlDatabase(); StringBuilder sb = new StringBuilder(); OutputObject output = new OutputObject(); string regex_string = txtTableNameRegex.Text; string output_file_name = string.Empty; string generated_code = string.Empty; string file_name = string.Empty; sql_database.LoadDatabaseMetadata(_DatabaseName, _Conn.ToString()); // check for loading errors foreach (var item in sql_database.ErrorList) DisplayMessage(item.Message, true); if (clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptMiscRemoveExistingScripts)) DeleteOldScriptFiles(); #region Generate output if (clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptSQLCreateHelperSp)) { string script = CodeGenerator.GenerateGetTableDetailsStoredProcedure(); file_name = Combine(_OutputPath, ConfigurationManager.AppSettings["DefaultTableDetailsSPName"]); FileIo.WriteToFile(file_name, script); } if (clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptCsharpCreateBaseClass)) { output = CodeGenerator.GenerateCSharpBaseClass(_DatabaseName); file_name = Combine(_OutputPath, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptAspCreateMasterPage)) { // grab a list of all the selected tables List<string> selected_tables = new List<string>(); foreach (string table_name in clbAspSqlTables.CheckedItems) selected_tables.Add(table_name); output = CodeGenerator.GenerateMasterPageCodeInFront(sql_database.Name, selected_tables); file_name = Combine(_OutputPath, s_DirectoryAspPages, output.Name); FileIo.WriteToFile(file_name, output.Body); output = CodeGenerator.GenerateMasterPageCodeBehind(sql_database.Name); file_name = Combine(_OutputPath, s_DirectoryAspPages, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptAspCreateCSSPage)) { output = CodeGenerator.GenerateCSSSheet(_DatabaseName); // this file is a css file output.Name += ".css"; file_name = Combine(_OutputPath, s_DirectoryAspPages, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptAspCreateWebConfig)) { output = CodeGenerator.GenerateWebConfig(_Conn); file_name = Combine(_OutputPath, s_DirectoryAspPages, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptAspCreateDefaultPage)) { output = CodeGenerator.GenerateDefaultPageCodeInFront(sql_database.Name); file_name = Combine(_OutputPath, s_DirectoryAspPages, output.Name); FileIo.WriteToFile(file_name, output.Body); output = CodeGenerator.GenerateDefaultPageCodeBehind(sql_database.Name); FileIo.WriteToFile(file_name, output.Body); } #endregion #region Sql Code Generation if (clbTsqlSqlTables.CheckedItems.Count > 0 && clbTsqlSqlObjects.CheckedItems.Count > 0) { sb.Append(CodeGenerator.GenerateSqlScriptHeader(_DatabaseName)); foreach (string table_name in clbTsqlSqlTables.CheckedItems) { SqlTable sql_table = sql_database.Tables[table_name]; List<string> sort_fields = new List<string>(); List<string> search_fields = new List<string>(); List<string> select_fields = new List<string>(); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelMany) || clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelManyByX) || clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelAll) || clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelAllPage)) { FieldSelector field_selector = new FieldSelector(sort_fields, sql_table, string.Format(Properties.Resource.SelectSortField, sql_table.Name)); field_selector.ShowDialog(this); } if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelAllPage) || clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlCountSearch)) { FieldSelector field_selector = new FieldSelector(search_fields, sql_table, string.Format(Properties.Resource.SelectSearchField, sql_table.Name)); field_selector.ShowDialog(this); } if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelManyByX)) { FieldSelector field_selector = new FieldSelector(select_fields, sql_table, string.Format(Properties.Resource.SelectSelectField, sql_table.Name)); field_selector.ShowDialog(this); } // We are placeing all the SQl scripts in a temp dictionary // object so we dont have to write them to a file untill they // are all completely built. List<OutputObject> sql_procedures = new List<OutputObject>(); // should we be generating permissisons with the sp? bool create_sql_permissions = clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptSQLCreateSqlSpPerms); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelSingle)) sql_procedures.Add(CodeGenerator.GenerateSelectSingleProc(sql_table, create_sql_permissions)); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelMany)) sql_procedures.Add(CodeGenerator.GenerateSelectManyProc(sql_table, sort_fields, create_sql_permissions)); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelManyByX)) sql_procedures.Add(CodeGenerator.GenerateSelectManyByXProc(sql_table, sort_fields, select_fields, create_sql_permissions)); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelAll)) sql_procedures.Add(CodeGenerator.GenerateSelectAllProc(sql_table, sort_fields, create_sql_permissions)); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlSelAllPage)) sql_procedures.Add(CodeGenerator.GenerateSelectAllPaginatedProc(sql_table, sort_fields, search_fields, create_sql_permissions)); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlUpdIns)) sql_procedures.Add(CodeGenerator.GenerateSetProc(sql_table, create_sql_permissions)); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlDelSingle)) sql_procedures.Add(CodeGenerator.GenerateDeleteSingleProc(sql_table, create_sql_permissions)); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlDelMany)) sql_procedures.Add(CodeGenerator.GenerateDeleteManyProc(sql_table, create_sql_permissions)); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlDelAll)) sql_procedures.Add(CodeGenerator.GenerateDeleteAllProc(sql_table, create_sql_permissions)); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlCountAll)) sql_procedures.Add(CodeGenerator.GenerateCountAllProc(sql_table, create_sql_permissions)); if (clbTsqlSqlObjects.CheckedItems.Contains(Properties.Resource.SqlCountSearch)) sql_procedures.Add(CodeGenerator.GenerateCountSearchProc(sql_table, search_fields, create_sql_permissions)); // build one script or many? if (this.clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptSQLSeperateFiles)) { string sql_header = sb.ToString(); foreach (var item in sql_procedures) { file_name = Combine(_OutputPath, item.Name + ".sql"); FileIo.WriteToFile(file_name, sql_header + item.Body); } } else { foreach (var item in sql_procedures) sb.Append(item.Body); file_name = Combine(_OutputPath, _DatabaseName + ConfigurationManager.AppSettings["DefaultSQLScriptFilename"]); FileIo.WriteToFile(file_name, sb.ToString()); } } DisplayMessage("SQL objects created.", false); } #endregion #region C# Code Generation if (clbCsharpSqlTables.CheckedItems.Count > 0 && clbCsharpObjects.CheckedItems.Count > 0) { foreach (string table_name in clbCsharpSqlTables.CheckedItems) { if (!sql_database.Tables.ContainsKey(table_name)) continue; SqlTable current_table = sql_database.Tables[table_name]; if (clbCsharpObjects.CheckedItems.Contains(Properties.Resource.CsharpOrm)) { output = CodeGenerator.GenerateCSharpOrmClass(current_table, _NamespaceIncludes, clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptCsharpIncludeSqlClassDecoration), clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptCsharpIncludeBaseClassRefrence), clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptCsharpIncludeIsDirtyFlag)); file_name = Combine(_OutputPath, s_DirectoryOrm, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbCsharpObjects.CheckedItems.Contains(Properties.Resource.CsharpSimpleOrm)) { output = CodeGenerator.GenerateCSharpSimpleOrmClass(current_table); file_name = Combine(_OutputPath, s_DirectoryOrm, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbCsharpObjects.CheckedItems.Contains(Properties.Resource.CsharpOrmExtension)) { output = CodeGenerator.GenerateCSharpExternalOrmClass(current_table, _NamespaceIncludes); file_name = Combine(_OutputPath, s_DirectoryOrmExt, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbCsharpObjects.CheckedItems.Contains(Properties.Resource.CsharpInterface)) { output = CodeGenerator.GenerateCSharpClassInterface(current_table, _NamespaceIncludes, clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptCsharpIncludeIsDirtyFlag)); file_name = Combine(_OutputPath, s_DirectoryInterface, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbCsharpObjects.CheckedItems.Contains(Properties.Resource.CsharpDal)) { output = CodeGenerator.GenerateCSharpDalClass(current_table, _NamespaceIncludes, clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptCsharpConvertNullableFields), clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptCsharpIncludeIsDirtyFlag)); file_name = Combine(_OutputPath, s_DirectoryDal, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbCsharpObjects.CheckedItems.Contains(Properties.Resource.CsharpExtensionDal)) { output = CodeGenerator.GenerateCSharpExternalDalClass(current_table, _NamespaceIncludes); file_name = Combine(_OutputPath, s_DirectoryDalExt, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbCsharpObjects.CheckedItems.Contains(Properties.Resource.CsharpEnum)) { List<string> key_fields = new List<string>(); List<string> value_fields = new List<string>(); FieldSelector field_selector; field_selector = new FieldSelector(key_fields, current_table, Properties.Resource.SelectEnumNameField, 1, 1); field_selector.ShowDialog(this); field_selector = new FieldSelector(value_fields, current_table, Properties.Resource.SelectEnumValueField, 1, 1); field_selector.ShowDialog(this); // Make sure that table has correct format if (key_fields.Count != 1 || value_fields.Count != 1) { DisplayMessage("Cannot generate enum for " + table_name + ".", true); } else { // we have table metadata, get table data DataTable data_table = CodeGenerator.GenerateSqlTableData(_DatabaseName, table_name, this._Conn.ToString()); output = CodeGenerator.GenerateCSharpEnumeration(current_table, key_fields[0], value_fields[0], data_table); file_name = Combine(_OutputPath, s_DirectoryEnums, output.Name); FileIo.WriteToFile(file_name, output.Body); } } } DisplayMessage("C# objects created", false); } #endregion #region Winform Code Generation if (clbWinformSqlTables.CheckedItems.Count > 0 && clbWinformObjects.CheckedItems.Count > 0) { foreach (string table_name in clbWinformSqlTables.CheckedItems) { if (!sql_database.Tables.ContainsKey(table_name)) continue; SqlTable current_table = sql_database.Tables[table_name]; if (clbWinformObjects.CheckedItems.Contains(Properties.Resource.WinformsEditPage)) { output = CodeGenerator.GenerateWinformEditCode(current_table); file_name = Combine(_OutputPath, s_DirectoryWinforms, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbWinformObjects.CheckedItems.Contains(Properties.Resource.WinformsEditDesigner)) { output = CodeGenerator.GenerateWinformEditCodeDesigner(current_table); file_name = Combine(_OutputPath, s_DirectoryWinforms, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbWinformObjects.CheckedItems.Contains(Properties.Resource.WinformsViewPage)) { output = CodeGenerator.GenerateWinformViewCode(current_table); file_name = Combine(_OutputPath, s_DirectoryWinforms, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbWinformObjects.CheckedItems.Contains(Properties.Resource.WinformsViewDesigner)) { output = CodeGenerator.GenerateWinformViewCodeDesigner(current_table); file_name = Combine(_OutputPath, s_DirectoryWinforms, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbWinformObjects.CheckedItems.Contains(Properties.Resource.WinformsMainPage)) { output = CodeGenerator.GenerateWinformMainCode(current_table); file_name = Combine(_OutputPath, s_DirectoryWinforms, output.Name); FileIo.WriteToFile(file_name, output.Body); } if (clbWinformObjects.CheckedItems.Contains(Properties.Resource.WinformsMainDesigner)) { output = CodeGenerator.GenerateWinformMainCodeDesigner(current_table); file_name = Combine(_OutputPath, s_DirectoryWinforms, output.Name); FileIo.WriteToFile(file_name, output.Body); } } DisplayMessage("Winform objects created", false); } #endregion #region WebService Code Generation if (clbWebServiceSqlTables.CheckedItems.Count > 0 && clbWebServiceObjects.CheckedItems.Count > 0) { foreach (string table_name in clbWebServiceSqlTables.CheckedItems) { SqlTable current_table = sql_database.Tables[table_name]; output = CodeGenerator.GenerateWebServiceCodeInfrontClass(current_table); file_name = Combine(_OutputPath, s_DirectoryWebService, output.Name); FileIo.WriteToFile(file_name, output.Body); output = CodeGenerator.GenerateWebServiceCodeBehindClass(current_table, _NamespaceIncludes); file_name = Combine(_OutputPath, s_DirectoryWebService, output.Name); FileIo.WriteToFile(file_name, output.Body); } DisplayMessage("Webservice objects created", false); } #endregion #region Aspx Code Generation if (clbAspSqlTables.CheckedItems.Count > 0 && clbAspObjects.CheckedItems.Count > 0) { foreach (string table_name in clbAspSqlTables.CheckedItems) { SqlTable current_table = sql_database.Tables[table_name]; if (clbAspObjects.CheckedItems.Contains(Properties.Resource.AspCreateEditPage)) { output = CodeGenerator.GenerateWebEditPageCodeInFront(current_table, clbCsharpObjects.CheckedItems.Contains(Properties.Resource.OptAspCreatePageAsConrol)); file_name = Combine(_OutputPath, s_DirectoryAspPages, output.Name); FileIo.WriteToFile(file_name, output.Body); output = CodeGenerator.GenerateWebEditPageCodeBehind(current_table, _NamespaceIncludes); FileIo.WriteToFile(file_name, output.Body); } if (clbAspObjects.CheckedItems.Contains(Properties.Resource.AspCreateListPage)) { output = CodeGenerator.GenerateWebListPageCodeInFront(current_table, clbCsharpObjects.CheckedItems.Contains(Properties.Resource.OptAspCreatePageAsConrol)); file_name = Combine(_OutputPath, s_DirectoryAspPages, output.Name); FileIo.WriteToFile(file_name, output.Body); output = CodeGenerator.GenerateWebListPageCodeBehind(current_table, _NamespaceIncludes); FileIo.WriteToFile(file_name, output.Body); } if (clbAspObjects.CheckedItems.Contains(Properties.Resource.AspCreateViewPage)) { output = CodeGenerator.GenerateWebViewPageCodeInFront(current_table, clbCsharpObjects.CheckedItems.Contains(Properties.Resource.OptAspCreatePageAsConrol)); file_name = Combine(_OutputPath, s_DirectoryAspPages, output.Name); FileIo.WriteToFile(file_name, output.Body); output = CodeGenerator.GenerateWebViewPageCodeBehind(current_table, _NamespaceIncludes); FileIo.WriteToFile(file_name, output.Body); } } DisplayMessage("ASP objects created.", false); } #endregion #region XML Generation // XML Output if (clbXmlSqlTables.CheckedItems.Count > 0 && this.clbXmlOptionsTables.CheckedItems.Count > 0) { bool generate_with_attributes = clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptXmlFormat); string name_space = string.Empty; if (clbOutputOptions.CheckedItems.Contains(Properties.Resource.OptXmlIncludeNs)) name_space = _DatabaseName; foreach (string table_name in clbXmlSqlTables.CheckedItems) { if (!sql_database.Tables.ContainsKey(table_name)) continue; SqlTable current_table = sql_database.Tables[table_name]; // Xml Export if (clbXmlOptionsTables.CheckedItems.Contains(Properties.Resource.XmlExportData)) { DataTable data_table = CodeGenerator.GenerateSqlTableData(_DatabaseName, table_name, _Conn.ToString()); if (generate_with_attributes) { foreach (DataColumn column in data_table.Columns) { column.ColumnMapping = MappingType.Attribute; } } data_table.TableName = table_name; string xml_output = XmlConverter.DataTableToXmlString(data_table, name_space); file_name = Combine(_OutputPath, s_DirectoryXMlData, table_name + ConfigurationManager.AppSettings["XMLExportSuffix"]); FileIo.WriteToFile(file_name, xml_output); } // Xml Loader object if (clbXmlOptionsTables.CheckedItems.Contains(Properties.Resource.XmlImportObject)) { output = CodeGenerator.GenerateXmlLoader(current_table); file_name = Combine(_OutputPath, s_DirectoryXMlData, output.Name); FileIo.WriteToFile(file_name, output.Body); } } DisplayMessage("XML objects created.", false); } #endregion } catch (Exception ex) { LogError(ex, string.Empty); DisplayMessage(ex.Message, true); } finally { DisplayMessage("Code generation complete.", false); this.Cursor = Cursors.Default; System.Media.SystemSounds.Exclamation.Play(); } }
public SqlTable(SqlDatabase sql_database, string table_name) { _Database = sql_database; _Name = table_name; _Columns = new Dictionary<string,SqlColumn>(); }
public SqlTable(SqlDatabase sql_database, string table_name) { Database = sql_database; Name = table_name; Columns = new Dictionary <string, SqlColumn>(); }
public void Reset() { _Database = null; _Name = string.Empty; _Columns = new Dictionary<string,SqlColumn>(); }