/// <summary> /// This method gets the name of this class. /// </summary> /// <param name="dataTable"></param> /// <param name="procType"></param> /// <returns></returns> private string GetClassName(DataTable dataTable, StoredProcedureTypes procType) { // create StringBuilder StringBuilder sb = new StringBuilder(procType.ToString()); // set the class name string className = dataTable.ClassName; // if this is a fetch all proc if (procType == StoredProcedureTypes.FetchAll) { // get the plural name className = PluralWordHelper.GetPluralName(className, false); } // Append table name sb.Append(className); // append the words stored procedure sb.Append("StoredProcedure"); // set the return value className = sb.ToString(); // return value return(className); }
/// <summary> /// Create the file name for this reader. /// </summary> /// <param name="dataTable"></param> /// <returns></returns> private string CreateFileName(DataTable dataTable, StoredProcedureTypes procType) { // Create StringBuilder StringBuilder sb = new StringBuilder(this.RootStoredProceduresPath); // If does not end with backslash if (!this.RootStoredProceduresPath.EndsWith(@"\")) { // Append Backslash \ sb.Append(@"\"); } // determine folder to add string procFolder = GetProcFolder(procType, false, true); // append procFold sb.Append(procFolder); // Append the word for proc type (Delete, FetchAll, Find, etc.) sb.Append(procType.ToString()); // set the clas name string className = dataTable.ClassName; // if this is a fetch all (a collection is retured) if (procType == StoredProcedureTypes.FetchAll) { // get the class name in plural className = PluralWordHelper.GetPluralName(dataTable.ClassName, false); } // Apend the class name sb.Append(className); // Add The Word DataOperationsManager sb.Append("StoredProcedure.cs"); // return value return(sb.ToString()); }
/// <summary> /// event is fired when Create Blazor Services Button _ Link Clicked /// </summary> private void CreateBlazorServicesButton_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { // locals int attempts = 0; string path = ""; string path2 = ""; bool filesCreated = false; string message = ""; try { // if the value for HasServicesFolder is true if ((HasServicesFolder) && (HasTable)) { // Setup the Graph Graph.Maximum = 20; Graph.Minimum = 0; Graph.Value = 0; // Show the graph Graph.Visible = true; // Create a Process to launch a command window (hidden) to create the item templates Process process = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.FileName = "cmd.exe"; startInfo.WorkingDirectory = Project.ServicesFolder; startInfo.Arguments = "/C " + CreateServices; process.StartInfo = startInfo; process.Start(); // next I am performing a wait for the files to be here, which on different people's machines // speeds I am allowing up to 5 seconds. My machine takes about half a second maybe more, // so 5 should be enough or give up. If it doesn't work for and you haven't upgraded your // computer in a while, maybe you should buy a new computer. // If your internet is slow and this takes longer than 5 seconds let me know and I will extend // the wait. do { // Increment the value for attempt attempts++; // Show the graph Graph.Value = attempts; // refresh everything this.Refresh(); // Try every half second System.Threading.Thread.Sleep(500); // get the path path = Path.Combine(Project.ServicesFolder, DataWatcherFileName); // if the file exists if (File.Exists(path)) { // The files were created filesCreated = true; // break out of the loop break; } }while (attempts < 20); // Wait one extra half second after the file is avialable before trying to modify it System.Threading.Thread.Sleep(500); // if the files were created if (filesCreated) { // *************************************** // ************** DataWatcher.cs Class ************* // *************************************** // Now the file must be read string fileText = File.ReadAllText(path); // now create the replaceParameter values string tableName = table.TableName; string variableName = CSharpClassWriter.CapitalizeFirstCharEx(table.TableName, true); string pluralTableName = PluralWordHelper.GetPluralName(table.TableName, false); string pluralVariableName = PluralWordHelper.GetPluralName(table.TableName, true); string primaryKeyDataType = ""; string primaryKeyVariableName = ""; string primaryKeyPropertyName = ""; // Update 12.12.2020: The DataService (for Blazor) requires the PrimaryKey // cataType and field name. DTNField field = FindPrimaryKey(table); // if the field was found if (NullHelper.Exists(field)) { // get the dataType primaryKeyDataType = field.DataType.ToString().ToLower(); primaryKeyVariableName = CSharpClassWriter.CapitalizeFirstCharEx(field.FieldName, true); primaryKeyPropertyName = CSharpClassWriter.CapitalizeFirstCharEx(field.FieldName, false); // if an autonumber identity field (which most will be) if (primaryKeyDataType == "autonumber") { // switch to int primaryKeyDataType = "int"; } } // string parameterDataType = FindPrimaryKey // Replace out the fileText replacement parameters fileText = fileText.Replace("[TableName]", tableName); fileText = fileText.Replace("[VariableName]", variableName); fileText = fileText.Replace("[PluralVariableName]", pluralVariableName); // Delete the current file at path File.Delete(path); // rename the file path = path.Replace("DataWatcher.cs", Table.TableName + "DataWatcher.cs"); // Write out the next text File.WriteAllText(path, fileText); // *************************************** // ************** Service.cs Class ************* // *************************************** // now change path to the Service class path2 = Path.Combine(Project.ServicesFolder, ServiceFileName); // Now the file must be read fileText = File.ReadAllText(path2); // Replace out the fileText replacement parameters fileText = fileText.Replace("[TableName]", tableName); fileText = fileText.Replace("[VariableName]", variableName); fileText = fileText.Replace("[PluralVariableName]", pluralVariableName); fileText = fileText.Replace("[PluralTableName]", pluralTableName); fileText = fileText.Replace("[ParameterDataType]", primaryKeyDataType); fileText = fileText.Replace("[PrimaryKey]", primaryKeyVariableName); fileText = fileText.Replace("[PrimaryKeyPropertyName]", primaryKeyPropertyName); // Delete the current file at path2 File.Delete(path2); // rename the file path2 = path2.Replace("Service.cs", Table.TableName + "Service.cs"); // Write out the next text File.WriteAllText(path2, fileText); // Show a message message = "The following classes were created:" + Environment.NewLine + path + Environment.NewLine + path2; // Hide the graph Graph.Visible = false; // Show the user a message MessageBoxHelper.ShowMessage(message, "Files Created"); } else { // change the message message = "Oops, something went wrong. Step through the code in DataTier.Net.Client.Controls.BlazorDataServices.cs, event name CreateBlazorServicesButton_Click."; // Show the user a message MessageBoxHelper.ShowMessage(message, "Files Could Not Be Created", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } catch (Exception error) { // Set the error DebugHelper.WriteDebugError("CreateBlazorServicesButton_LinkClicked", this.Name, error); // show the user a message MessageBoxHelper.ShowMessage("The Blazor Data Services Either Were Not Installed Or You Do Not Have Permission To Create Files In This Directory", "Create Data Services Failed"); } }
/// <summary> /// This method writes the LoadCollection method for a DataTable. /// </summary> /// <param name="dataTable"></param> private void WriteLoadCollectionMethod(DataTable dataTable) { // Write Blank Line WriteLine(); // Write BeginRegion BeginRegion("LoadCollection(DataTable dataTable)"); // Write Load Collection Summary WriteLoadCollectionSummary(dataTable); // get objectName string className = dataTable.ClassName; // get collection return type string collectionReturnType = "List<" + className + ">"; // if there is a conflict with this class name if (ConflictHelper.CheckForConflict(className)) { // resolve the conflict collectionReturnType = ConflictHelper.ResolveConflict(collectionReturnType, dataTable.ObjectNameSpaceName); } // get the return variable name string returnVariableName = CapitalizeFirstChar(className, true); // fix any pluralization issues returnVariableName = PluralWordHelper.GetPluralName(className, true); // Create loadMethodLine string loadMethodLine = "public static " + collectionReturnType + " LoadCollection(DataTable dataTable)"; // Write LoadMethodLine WriteLine(loadMethodLine); // Write Open Bracket WriteOpenBracket(); // Increase Indent Indent++; // Write Comment For Initial Value WriteComment("Initial Value"); // Create Object Line string objectLine = CreateCollectionObjectLine(collectionReturnType, returnVariableName); // Write Object Line WriteLine(objectLine); // Write Blank Line WriteLine(); // Write try WriteLine("try"); // Write Open Bracket WriteOpenBracket(); // Increase Indent Indent++; // Write Comment Load Each row In dataTable WriteComment("Load Each row In DataTable"); // Now Write For Each Line WriteLine("foreach (DataRow row in dataTable.Rows)"); // Write Open Bracket WriteOpenBracket(); // Incrase Indent Indent++; // Write the comment to create object from data row WriteComment("Create '" + dataTable.ClassName + "' from rows"); // get the single variable name string singleVariableName = CapitalizeFirstChar(className, true); // if this object ends in "s" it can't be singular if (singleVariableName.EndsWith("s")) { // remove the last s singleVariableName = singleVariableName.Substring(0, singleVariableName.Length - 1); } // Write Line To Load This Object string line = dataTable.ClassName + " " + singleVariableName + " = Load(row);"; // write this line WriteLine(line); // Write Blank Line WriteLine(); // Write Comment add this object to collection WriteComment("Add this object to collection"); // Write addToCollection Line string addToCollection = returnVariableName + ".Add(" + singleVariableName + ");"; // write line WriteLine(addToCollection); // Decrease Indent Indent--; // Write Close Bracket WriteLine("}"); // Decrease Indent Indent--; // Write Close Bracket WriteLine("}"); // Write catch WriteLine("catch"); // Write Open Bracket WriteOpenBracket(); // Write Close Bracket WriteLine("}"); // Write Blank Line WriteLine(); // write Comment For Return Value WriteComment("return value"); // Write Return Value string returnValue = "return " + returnVariableName + ";"; WriteLine(returnValue); // Decrease Indent Indent--; // Write Close Bracket WriteLine("}"); // Write end Region WriteLine("#endregion"); // Write Blank Line WriteLine(); }
/// <summary> /// This method creates the FetchAll proc for a DataTabe /// </summary> /// <param name="dataTable"></param> /// <param name="nameSpace"></param> private void CreateFetchAllMethod(DataTable dataTable) { // get dataType variable string dataType = "List<" + dataTable.ClassName + ">"; // local string variableName = dataTable.ClassName + "Collection"; // create the name for the data object (must be set before the conflict is fixed) string dataObject = this.CapitalizeFirstChar(variableName, true); // if there is a conflict with this name if (ConflictHelper.CheckForConflict(dataTable.ClassName)) { // fix the conflict dataType = ConflictHelper.ResolveConflict(dataType, dataTable.ObjectNameSpaceName); } // get a variable for className string className = PluralWordHelper.GetPluralName(dataTable.ClassName, false); // local string queryType = "procedure"; // get procName & procType string procName = "fetchAll" + className + "Proc"; string procType = "FetchAll" + className + "StoredProcedure"; // Write Blank Line WriteLine(); // Begin Region BeginRegion("FetchAll" + className + "()"); // Write FetchAll Summary WriteLine("/// <summary>"); WriteLine("/// This method fetches a '" + dataType + "' object."); WriteLine("/// This method uses the '" + className + "_FetchAll' " + queryType + "."); WriteLine("/// </summary>"); WriteLine("/// <returns>A '" + dataType + "'</returns>"); WriteLine("/// </summary>"); // get class declaration line string classLine = "public " + dataType + " FetchAll" + className + "(" + procType + " " + procName + ", DataConnector databaseConnector)"; // Write class line WriteLine(classLine); // Write Open Bracket WriteOpenBracket(true); // Write Comment Initial Value WriteComment("Initial Value"); // Write line to set initial value string initialValue = dataType + " " + dataObject + " = null;"; WriteLine(initialValue); // Write Blank Line WriteLine(); // Write Comment Verify database connection is connected WriteComment("Verify database connection is connected"); // get line to test connection string ifConnected = "if ((databaseConnector != null) && (databaseConnector.Connected))"; // Write ifConnected WriteLine(ifConnected); // Write Open Bracket WriteOpenBracket(true); // Write Comment First Get Dataset WriteComment("First Get Dataset"); // get line to get data set string dataSetName = "all" + className + "DataSet"; string dataSetLine = "DataSet " + dataSetName + " = this.DataHelper.LoadDataSet(" + procName + ", databaseConnector);"; // Write set dataSetLine WriteLine(dataSetLine); // Write Blank Line WriteLine(); // Write Comment Verify DataSet Exists WriteComment("Verify DataSet Exists"); // line to test if DataSet exists string ifDataSetExists = "if(" + dataSetName + " != null)"; WriteLine(ifDataSetExists); // Write Open Bracket WriteOpenBracket(true); // Write Comment Get DataTable From DataSet WriteComment("Get DataTable From DataSet"); // get line to get first table out of data set string dataTableLine = "DataTable table = this.DataHelper.ReturnFirstTable(" + dataSetName + ");"; WriteLine(dataTableLine); // Write Blank Line WriteLine(); // Write Comment if table exists WriteComment("if table exists"); // now write line to test if table exists WriteLine("if(table != null)"); // Write Open Bracket WriteOpenBracket(true); // Write Comment Load Collection WriteComment("Load Collection"); // get reader name string readerName = dataTable.ClassName + "Reader"; // get line to load collection string loadCollection = dataObject + " = " + readerName + ".LoadCollection(table);"; // Write loadCollection WriteLine(loadCollection); // Write Close Bracket WriteCloseBracket(true); // WRite Close Bracket WriteCloseBracket(true); // Write Close Bracket WriteCloseBracket(true); // Write Blank Line WriteLine(); // Write Comment Return Value WriteComment("return value"); // Write Return Value WriteLine("return " + dataObject + ";"); // Write Close Bracket WriteCloseBracket(true); // Write EndRegion for DeleteMethod EndRegion(); // Write Blank Line WriteLine(); }