public ActionResult <string> insert([FromForm] InsertedNode parameter) { ElementDAO elementDAO = new ElementDAO(); String parent = (parameter.parentid.Equals(Element.getRoot().getId()) ? "" : parameter.parentid + "."); using (var conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;Database=nasca;Encoding=UTF-8;User Id=postgres;Password=nomura;")) { conn.Open(); using (var tran = conn.BeginTransaction()) { try { elementDAO.insert( conn, tran, parent + parameter.id, parameter.name, parameter.type, parameter.remark ); this.updateDependencyID(conn, tran, parameter.parentid, parent + parameter.id); tran.Commit(); } catch { tran.Rollback(); } } } return(JsonConvert.SerializeObject(new { })); }
public ActionResult <string> delete([FromForm] InsertedNode parameter) { ElementDAO elementDAO = new ElementDAO(); String parent = (parameter.parentid.Equals(Element.getRoot().getId()) ? "" : parameter.parentid + "."); using (var conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;Database=nasca;Encoding=UTF-8;User Id=postgres;Password=nomura;")) { conn.Open(); using (var tran = conn.BeginTransaction()) { //更新対象とその子エレメントを取得します。 Element target = elementDAO.selectByID(parent + parameter.id); List <Element> children = target.getChild(); //子エレメントとその依存を更新します。 foreach (Element child in children) { this.deleteDependency(conn, tran, child.getId()); //子エレメントの更新 elementDAO.delete(conn, tran, child.getId()); } this.deleteDependency(conn, tran, parent + parameter.id); elementDAO.delete(conn, tran, parent + parameter.id); tran.Commit(); } } return(JsonConvert.SerializeObject(new { })); }
Result GetResultFromBlock(BlockStatement block) { // For a block, we are tracking 4 possibilities: // a) context is checked, no unchecked block open Cost costCheckedContext = new Cost(0, 0); InsertedNode nodesCheckedContext = null; // b) context is checked, an unchecked block is open Cost costCheckedContextUncheckedBlockOpen = Cost.Infinite; InsertedNode nodesCheckedContextUncheckedBlockOpen = null; Statement uncheckedBlockStart = null; // c) context is unchecked, no checked block open Cost costUncheckedContext = new Cost(0, 0); InsertedNode nodesUncheckedContext = null; // d) context is unchecked, a checked block is open Cost costUncheckedContextCheckedBlockOpen = Cost.Infinite; InsertedNode nodesUncheckedContextCheckedBlockOpen = null; Statement checkedBlockStart = null; Statement statement = block.Statements.FirstOrDefault(); while (true) { // Blocks can be closed 'for free'. We use '<=' so that blocks are closed as late as possible (goal 4) if (costCheckedContextUncheckedBlockOpen <= costCheckedContext) { costCheckedContext = costCheckedContextUncheckedBlockOpen; nodesCheckedContext = nodesCheckedContextUncheckedBlockOpen + new InsertedBlock(uncheckedBlockStart, statement, false); } if (costUncheckedContextCheckedBlockOpen <= costUncheckedContext) { costUncheckedContext = costUncheckedContextCheckedBlockOpen; nodesUncheckedContext = nodesUncheckedContextCheckedBlockOpen + new InsertedBlock(checkedBlockStart, statement, true); } if (statement == null) { break; } // Now try opening blocks. We use '<' so that blocks are opened as early as possible. (goal 4) if (costCheckedContext + new Cost(1, 0) < costCheckedContextUncheckedBlockOpen) { costCheckedContextUncheckedBlockOpen = costCheckedContext + new Cost(1, 0); nodesCheckedContextUncheckedBlockOpen = nodesCheckedContext; uncheckedBlockStart = statement; } if (costUncheckedContext + new Cost(1, 0) < costUncheckedContextCheckedBlockOpen) { costUncheckedContextCheckedBlockOpen = costUncheckedContext + new Cost(1, 0); nodesUncheckedContextCheckedBlockOpen = nodesUncheckedContext; checkedBlockStart = statement; } // Now handle the statement Result stmtResult = GetResult(statement); costCheckedContext += stmtResult.CostInCheckedContext; nodesCheckedContext += stmtResult.NodesToInsertInCheckedContext; costCheckedContextUncheckedBlockOpen += stmtResult.CostInUncheckedContext; nodesCheckedContextUncheckedBlockOpen += stmtResult.NodesToInsertInUncheckedContext; costUncheckedContext += stmtResult.CostInUncheckedContext; nodesUncheckedContext += stmtResult.NodesToInsertInUncheckedContext; costUncheckedContextCheckedBlockOpen += stmtResult.CostInCheckedContext; nodesUncheckedContextCheckedBlockOpen += stmtResult.NodesToInsertInCheckedContext; statement = statement.GetNextStatement(); } return(new Result { CostInCheckedContext = costCheckedContext, NodesToInsertInCheckedContext = nodesCheckedContext, CostInUncheckedContext = costUncheckedContext, NodesToInsertInUncheckedContext = nodesUncheckedContext }); }
public InsertedNodeList(AddCheckedBlocks.InsertedNode child1, AddCheckedBlocks.InsertedNode child2) { this.child1 = child1; this.child2 = child2; }
Result GetResultFromBlock(BlockStatement block) { // For a block, we are tracking 4 possibilities: // a) context is checked, no unchecked block open Cost costCheckedContext = new Cost(0, 0); InsertedNode nodesCheckedContext = null; // b) context is checked, an unchecked block is open Cost costCheckedContextUncheckedBlockOpen = Cost.Infinite; InsertedNode nodesCheckedContextUncheckedBlockOpen = null; Statement uncheckedBlockStart = null; // c) context is unchecked, no checked block open Cost costUncheckedContext = new Cost(0, 0); InsertedNode nodesUncheckedContext = null; // d) context is unchecked, a checked block is open Cost costUncheckedContextCheckedBlockOpen = Cost.Infinite; InsertedNode nodesUncheckedContextCheckedBlockOpen = null; Statement checkedBlockStart = null; Statement statement = block.Statements.FirstOrDefault(); while (true) { // Blocks can be closed 'for free'. We use '<=' so that blocks are closed as late as possible (goal 4b) if (costCheckedContextUncheckedBlockOpen <= costCheckedContext) { costCheckedContext = costCheckedContextUncheckedBlockOpen; nodesCheckedContext = nodesCheckedContextUncheckedBlockOpen + new InsertedBlock(uncheckedBlockStart, statement, false); } if (costUncheckedContextCheckedBlockOpen <= costUncheckedContext) { costUncheckedContext = costUncheckedContextCheckedBlockOpen; nodesUncheckedContext = nodesUncheckedContextCheckedBlockOpen + new InsertedBlock(checkedBlockStart, statement, true); } if (statement == null) { break; } // Now try opening blocks. We use '<=' so that blocks are opened as late as possible. (goal 4a) if (costCheckedContext + new Cost(1, 0) <= costCheckedContextUncheckedBlockOpen) { costCheckedContextUncheckedBlockOpen = costCheckedContext + new Cost(1, 0); nodesCheckedContextUncheckedBlockOpen = nodesCheckedContext; uncheckedBlockStart = statement; } if (costUncheckedContext + new Cost(1, 0) <= costUncheckedContextCheckedBlockOpen) { costUncheckedContextCheckedBlockOpen = costUncheckedContext + new Cost(1, 0); nodesUncheckedContextCheckedBlockOpen = nodesUncheckedContext; checkedBlockStart = statement; } // Now handle the statement Result stmtResult = GetResult(statement); costCheckedContext += stmtResult.CostInCheckedContext; nodesCheckedContext += stmtResult.NodesToInsertInCheckedContext; costCheckedContextUncheckedBlockOpen += stmtResult.CostInUncheckedContext; nodesCheckedContextUncheckedBlockOpen += stmtResult.NodesToInsertInUncheckedContext; costUncheckedContext += stmtResult.CostInUncheckedContext; nodesUncheckedContext += stmtResult.NodesToInsertInUncheckedContext; costUncheckedContextCheckedBlockOpen += stmtResult.CostInCheckedContext; nodesUncheckedContextCheckedBlockOpen += stmtResult.NodesToInsertInCheckedContext; if (statement is LabelStatement || statement is LocalFunctionDeclarationStatement) { // We can't move labels into blocks because that might cause goto-statements // to be unable to jump to the labels. // Also, we can't move local functions into blocks, because that might cause // them to become out of scope from the call-sites. costCheckedContextUncheckedBlockOpen = Cost.Infinite; costUncheckedContextCheckedBlockOpen = Cost.Infinite; } statement = statement.GetNextStatement(); } return(new Result { CostInCheckedContext = costCheckedContext, NodesToInsertInCheckedContext = nodesCheckedContext, CostInUncheckedContext = costUncheckedContext, NodesToInsertInUncheckedContext = nodesUncheckedContext }); }
public InsertedNodeList(InsertedNode child1, InsertedNode child2) { this.child1 = child1; this.child2 = child2; }
public ActionResult <string> update([FromForm] InsertedNode parameter) { ElementDAO elementDAO = new ElementDAO(); String parent = (parameter.parentid.Equals(Element.getRoot().getId()) ? "" : parameter.parentid + "."); using (var conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;Database=nasca;Encoding=UTF-8;User Id=postgres;Password=nomura;")) { conn.Open(); using (var tran = conn.BeginTransaction()) { try { //更新対象とその子エレメントを取得します。 Element target = elementDAO.selectByID(parameter.originalid); List <Element> children = target.getChild(); //ID変更の場合 if (parameter.originalid != parent + parameter.id) { var re = new Regex(parameter.originalid); //子エレメントとその依存を更新します。 foreach (Element child in children) { this.updateDependencyID(conn, tran, child.getId(), re.Replace(child.getId(), parent + parameter.id, 1)); //子エレメントの更新 elementDAO.update( conn, tran, child.getId(), re.Replace(child.getId(), parent + parameter.id, 1), child.getName(), child.getType(), child.getRemark()); } } this.updateDependencyID(conn, tran, parameter.originalid, parent + parameter.id); elementDAO.update( conn, tran, parameter.originalid, parent + parameter.id, parameter.name, parameter.type, parameter.remark ); tran.Commit(); } catch { tran.Rollback(); } } } return(JsonConvert.SerializeObject(new { })); }