private string GenerateScripts() { string tablePluralName = (this.Name.EndsWith("y") ? $"{this.Name.TrimEnd('y')}ies" : $"{this.Name}s"); string tableTypeName = $"[{this.Owner}].[tt_{this.Name}]"; string selectProcName = $"[{this.Owner}].[usp_Select_{this.Name}]"; string selectAllProcName = $"[{this.Owner}].[usp_SelectAll_{tablePluralName}]"; string insertProcName = $"[{this.Owner}].[usp_Insert_{this.Name}]"; string updateProcName = $"[{this.Owner}].[usp_Update_{this.Name}]"; string hardDeleteProcName = null; string softDeleteProcName = null; StringBuilder scriptBuilder = new StringBuilder(); StringBuilder dropScriptBuilder = new StringBuilder(); var tableType = new TableType() { TableTypeName = tableTypeName, AllColumnAndTypeList = this.allColumnAndTypeList }.TransformText().TrimStart('\r', '\n'); scriptBuilder.Append(tableType); if (!string.IsNullOrEmpty(this.KeyColumnCategory) && this.KeyColumnCategory.Equals("primary")) { var selectProc = new SelectProc() { FullTableName = this.fullTableName, SelectProcName = selectProcName, AllColumnList = this.allColumnList, keyColumnAndTypeList = this.keyColumnAndTypeList, WhereClause = this.whereClause }.TransformText().TrimStart('\r', '\n'); scriptBuilder.Append(selectProc); dropScriptBuilder.Append(new DropProc() { ProcName = selectProcName }.TransformText().TrimStart('\r', '\n')); var selectAllProc = new SelectAllProc() { FullTableName = this.fullTableName, SelectAllProcName = selectAllProcName, AllColumnList = this.allColumnList, DeleteFlagWhereClause = this.deleteFlagWhereClause }.TransformText().TrimStart('\r', '\n'); scriptBuilder.Append(selectAllProc); dropScriptBuilder.Append(new DropProc() { ProcName = selectAllProcName }.TransformText().TrimStart('\r', '\n')); var updateProc = new UpdateProc() { TableName = this.Name, FullTableName = this.fullTableName, UpdateProcName = updateProcName, UpdateColumnList = this.updateColumnList, OnClause = this.onClause }.TransformText().TrimStart('\r', '\n'); scriptBuilder.Append(updateProc); dropScriptBuilder.Append(new DropProc() { ProcName = updateProcName }.TransformText().TrimStart('\r', '\n')); if (idNameColumnList != null) { var selectIdNamePairsProcName = $"[{this.Owner}].[usp_Select_{this.Name}_IdNamePairs]"; var selectIdNamePairsProc = new SelectIdNamePairsProc() { FullTableName = this.fullTableName, SelectIdNamePairsProcName = selectIdNamePairsProcName, IdNameColumnList = this.idNameColumnList, DeleteFlagWhereClause = this.deleteFlagWhereClause }.TransformText().TrimStart('\r', '\n'); scriptBuilder.Append(selectIdNamePairsProc); dropScriptBuilder.Append(new DropProc() { ProcName = selectIdNamePairsProcName }.TransformText().TrimStart('\r', '\n')); } } var insertProc = new InsertProc() { TableName = this.Name, FullTableName = this.fullTableName, InsertProcName = insertProcName, InsertIntoColumnList = this.insertIntoColumnList, InsertSelectColumnList = this.insertSelectColumnList, IdentityColumn = this.IdentityColumn, HasCreatedDateColumn = this.hasCreatedDateColumn, IsKeyColumnGuidColumn = (this.keyColumn != null && this.keyColumn.Type.Equals("uniqueidentifier")) }.TransformText().TrimStart('\r', '\n'); scriptBuilder.Append(insertProc); dropScriptBuilder.Append(new DropProc() { ProcName = insertProcName }.TransformText().TrimStart('\r', '\n')); if (this.softDeleteStatement != null) { softDeleteProcName = $"[{this.Owner}].[usp_Delete_{this.Name}]"; var deleteProc = new DeleteProc() { DeleteProcName = softDeleteProcName, keyColumnAndTypeList = this.keyColumnAndTypeList, DeleteStatement = this.softDeleteStatement }.TransformText().TrimStart('\r', '\n'); scriptBuilder.Append(deleteProc); dropScriptBuilder.Append(new DropProc() { ProcName = softDeleteProcName }.TransformText().TrimStart('\r', '\n')); hardDeleteProcName = $"[{this.Owner}].[usp_HardDelete_{this.Name}]"; } else { hardDeleteProcName = $"[{this.Owner}].[usp_Delete_{this.Name}]"; } if (this.hardDeleteStatement != null) { var deleteProc = new DeleteProc() { DeleteProcName = hardDeleteProcName, keyColumnAndTypeList = this.keyColumnAndTypeList, DeleteStatement = this.hardDeleteStatement }.TransformText().TrimStart('\r', '\n'); scriptBuilder.Append(deleteProc); dropScriptBuilder.Append(new DropProc() { ProcName = hardDeleteProcName }.TransformText().TrimStart('\r', '\n')); } if (this.fkColumns.Count > 0 && this.hasIdColumn) { foreach (var c in this.fkColumns) { var procName = $"[{this.Owner}].[usp_Select_{this.Name}Ids_By_{c.ReferencingColumnName}]"; var proc = new SelectPKColumnByFKColumnProc() { FullTableName = this.fullTableName, ProcName = procName, VariablesList = $"@{c.ReferencingColumnName} {c.Type}", ColumnList = string.Join(",", this.keyColumnList), WhereClause = $"[{c.Name}] = @{c.ReferencingColumnName}" + (this.deleteFlagWhereClause == null ? "" : $" AND {this.deleteFlagWhereClause}") }.TransformText().TrimStart('\r', '\n'); scriptBuilder.Append(proc); dropScriptBuilder.Append(new DropProc() { ProcName = procName }.TransformText().TrimStart('\r', '\n')); procName = $"[{this.Owner}].[usp_Select_{tablePluralName}_By_{c.ReferencingColumnName}]"; proc = new SelectPKColumnByFKColumnProc() { FullTableName = this.fullTableName, ProcName = procName, VariablesList = $"@{c.ReferencingColumnName} {c.Type}", ColumnList = this.allColumnList, WhereClause = $"[{c.Name}] = @{c.ReferencingColumnName}" + (this.deleteFlagWhereClause == null ? "" : $" AND {this.deleteFlagWhereClause}") }.TransformText().TrimStart('\r', '\n'); scriptBuilder.Append(proc); dropScriptBuilder.Append(new DropProc() { ProcName = procName }.TransformText().TrimStart('\r', '\n')); } } dropScriptBuilder.Append(new DropType() { TableName = this.Name, TypeName = tableTypeName }.TransformText().TrimStart('\r', '\n')); scriptBuilder.Insert(0, dropScriptBuilder.ToString()); var script = scriptBuilder.ToString(); var solutionRootFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../"); var dbProjectPath = Path.Combine(solutionRootFolder, "DatabaseScriptsGenerator/DatabaseScriptsGenerator.csproj"); var dbScriptPath = Path.Combine(solutionRootFolder, $"DatabaseScriptsGenerator/Tables/{this.Name}.sql"); CommonFunctions.WriteFileToProject("Content", dbScriptPath, dbProjectPath, script); var entity = new Entity() { TableName = this.Name, Columns = allColumns }.TransformText().TrimStart('\r', '\n'); var apiProjectPath = Path.Combine(solutionRootFolder, "AddAppAPI/AddAppAPI.csproj"); var entityPath = Path.Combine(solutionRootFolder, $"AddAppAPI/Models/{this.Name}.cs"); CommonFunctions.WriteFileToProject("Compile", entityPath, apiProjectPath, entity); var lowerCaseTableName = this.Name[0].ToString().ToLower() + this.Name.Substring(1); var entityController = new EntityController() { EntityName = this.Name, PluralEntityName = tablePluralName, LowerCaseEntityName = lowerCaseTableName, PluralEntityVariableName = lowerCaseTableName.EndsWith("y") ? (lowerCaseTableName.TrimEnd('y') + "ies") : (lowerCaseTableName + "s"), KeyColumnType = this.keyColumn.DotNetType, KeyColumnCategory = this.KeyColumnCategory, HasNameColumn = this.nameColumns.Count > 0, KeyColumns = this.primaryOrUniqueKeyColumns, KeyColumnAndDotNetTypeList = this.keyColumnAndDotNetTypeList, KeyColumnDotNetVariableNameList = this.keyColumnDotNetVariableNameList, FkColumns = this.fkColumns, HasIdColumn = this.hasIdColumn }.TransformText().TrimStart('\r', '\n'); var entityControllerPath = Path.Combine(solutionRootFolder, $"AddAppAPI/Controllers/{this.Name}Controller.cs"); CommonFunctions.WriteFileToProject("Compile", entityControllerPath, apiProjectPath, entityController); return(script); }
/* *---------------------------------------------------------------------- * * Tcl_CreateNamespace -- * * Creates a new namespace with the given name. If there is no * active namespace (i.e., the interpreter is being initialized), * the global :: namespace is created and returned. * * Results: * Returns a reference to the new namespace if successful. If the * namespace already exists or if another error occurs, this routine * returns null, along with an error message in the interpreter's * result object. * * Side effects: * If the name contains "::" qualifiers and a parent namespace does * not already exist, it is automatically created. * *---------------------------------------------------------------------- */ internal static Namespace createNamespace(Interp interp, string name, DeleteProc deleteProc) { Namespace ns, ancestor; Namespace parent; Namespace globalNs = getGlobalNamespace(interp); string simpleName; System.Text.StringBuilder buffer1, buffer2; // If there is no active namespace, the interpreter is being // initialized. if ((globalNs == null) && (interp.varFrame == null)) { // Treat this namespace as the global namespace, and avoid // looking for a parent. parent = null; simpleName = ""; } else if (name.Length == 0) { /* TclObject tobj = interp.getResult(); // FIXME : is there a test case to check this error result? TclString.append(tobj, "can't create namespace \"\": only global namespace can have empty name"); */ // FIXME : is there a test case to check this error result? interp.setResult("can't create namespace \"\": only global namespace can have empty name"); return null; } else { // Find the parent for the new namespace. // Java does not support passing an address so we pass // an array of size 1 and then assign arr[0] to the value Namespace[] parentArr = new Namespace[1]; Namespace[] dummyArr = new Namespace[1]; string[] simpleArr = new string[1]; getNamespaceForQualName(interp, name, null, (TCL.VarFlag.CREATE_NS_IF_UNKNOWN | TCL.VarFlag.LEAVE_ERR_MSG), parentArr, dummyArr, dummyArr, simpleArr); // Get the values out of the arrays! parent = parentArr[0]; simpleName = simpleArr[0]; // If the unqualified name at the end is empty, there were trailing // "::"s after the namespace's name which we ignore. The new // namespace was already (recursively) created and is referenced // by parent. if (simpleName.Length == 0) { return parent; } // Check for a bad namespace name and make sure that the name // does not already exist in the parent namespace. if (parent.childTable[simpleName] != null) { /* TclObject tobj = interp.getResult(); // FIXME : is there a test case to check this error result? TclString.append(tobj, "can't create namespace \"" + name + "\": already exists"); */ // FIXME : is there a test case to check this error result? interp.setResult("can't create namespace \"" + name + "\": already exists"); return null; } } // Create the new namespace and root it in its parent. Increment the // count of namespaces created. ns = new Namespace(); ns.name = simpleName; ns.fullName = null; // set below //ns.clientData = clientData; ns.deleteProc = deleteProc; ns.parent = parent; ns.childTable = new Hashtable(); lock (nsMutex) { numNsCreated++; ns.nsId = numNsCreated; } ns.interp = interp; ns.flags = 0; ns.activationCount = 0; // FIXME : there was a problem with the refcount because // when the namespace was deleted the refocount was 0. // We avoid this by just using a refcount of 1 for now. // We can do ignore the refCount because GC will reclaim mem. //ns.refCount = 0; ns.refCount = 1; ns.cmdTable = new Hashtable(); ns.varTable = new Hashtable(); ns.exportArray = null; ns.numExportPatterns = 0; ns.maxExportPatterns = 0; // Jacl does not use these tcl compiler specific members //ns.cmdRefEpoch = 0; //ns.resolverEpoch = 0; ns.resolver = null; if (parent != null) { SupportClass.PutElement(parent.childTable, simpleName, ns); } // Build the fully qualified name for this namespace. buffer1 = new System.Text.StringBuilder(); buffer2 = new System.Text.StringBuilder(); for (ancestor = ns; ancestor != null; ancestor = ancestor.parent) { if (ancestor != globalNs) { buffer1.Append("::"); buffer1.Append(ancestor.name); } buffer1.Append(buffer2); buffer2.Length = 0; buffer2.Append(buffer1); buffer1.Length = 0; } name = buffer2.ToString(); ns.fullName = name; // Return a reference to the new namespace. return ns; }