public void testSimple() { HGridBuilder b = new HGridBuilder(); b.addCol("id"); b.addCol("dis"); b.addCol("area"); b.addRow(new HVal[] { HRef.make("a"), HStr.make("Alpha"), HNum.make(1200) }); b.addRow(new HVal[] { HRef.make("b"), null, HNum.make(1400) }); // meta HGrid g = b.toGrid(); Assert.AreEqual(g.meta.size(), 0); // cols //HCol c; Assert.AreEqual(g.numCols, 3); verifyCol(g, 0, "id"); verifyCol(g, 1, "dis"); verifyCol(g, 2, "area"); // rows Assert.AreEqual(g.numRows, 2); Assert.IsFalse(g.isEmpty()); HRow r; r = g.row(0); Assert.IsTrue(r.get("id").hequals(HRef.make("a"))); Assert.IsTrue(r.get("dis").hequals(HStr.make("Alpha"))); Assert.IsTrue(r.get("area").hequals(HNum.make(1200))); r = g.row(1); Assert.IsTrue(r.get("id").hequals(HRef.make("b"))); Assert.IsNull(r.get("dis", false)); Assert.IsTrue(r.get("area").hequals(HNum.make(1400))); try { r.get("dis"); Assert.Fail(); } catch (UnknownNameException) { Assert.IsTrue(true); } Assert.IsNull(r.get("fooBar", false)); try { r.get("fooBar"); Assert.Fail(); } catch (UnknownNameException) { Assert.IsTrue(true); } // HRow no-nulls HRow it = g.row(0); Assert.IsFalse(it.Size > 3); verifyRowIterator(it, 0, "id", HRef.make("a")); verifyRowIterator(it, 1, "dis", HStr.make("Alpha")); verifyRowIterator(it, 2, "area", HNum.make(1200)); // HRow with nulls it = g.row(1); Assert.IsFalse(it.Size > 3); verifyRowIterator(it, 0, "id", HRef.make("b")); verifyRowIterator(it, 2, "area", HNum.make(1400)); // iterating verifyGridIterator(g); }
////////////////////////////////////////////////////////////////////////// // HGridWriter ////////////////////////////////////////////////////////////////////////// // Write a grid public override void writeGrid(HGrid grid) { // meta p("ver:\"").p(m_iVersion).p(".0\"").writeMeta(grid.meta).nl(); // cols if (grid.numCols == 0) { // technically this shoudl be illegal, but // for robustness handle it here throw new ArgumentException("Grid has no cols", "grid"); } else { for (int i = 0; i < grid.numCols; ++i) { if (i > 0) { p(','); } writeCol(grid.col(i)); } } nl(); // rows for (int i = 0; i < grid.numRows; ++i) { writeRow(grid, grid.row(i)); nl(); } flush(); }
void verifyGridEq(HGrid grid, HDict meta, Object[] cols, HVal[][] rows) { // meta Assert.IsTrue(grid.meta.hequals(meta)); // cols Assert.AreEqual(grid.numCols, cols.Length / 2); for (int i = 0; i < grid.numCols; ++i) { Assert.AreEqual(grid.col(i).Name, cols[i * 2 + 0]); Assert.IsTrue(grid.col(i).meta.hequals(cols[i * 2 + 1])); } // rows Assert.AreEqual(grid.numRows, rows.Length); for (int ri = 0; ri < rows.Length; ++ri) { HVal[] expected = rows[ri]; HRow actual = grid.row(ri); for (int ci = 0; ci < expected.Length; ++ci) { Assert.AreEqual(expected[ci], actual.get(grid.col(ci).Name, false)); } } }
/** * Query one entity record that matches the given filter. If * there is more than one record, then it is undefined which one is * returned. If there are no matches than return null or raise * UnknownRecException based on checked flag. * NOTE: Was final */ public async Task <HDict> readAsync(string filter, bool bChecked) { HGrid grid = await readAllAsync(filter, 1); if (grid.numRows > 0) { return(grid.row(0)); } if (bChecked) { throw new Exception("rec not found for: " + filter); } return(null); }
////////////////////////////////////////////////////////////////////////// // Reads ////////////////////////////////////////////////////////////////////////// protected override HDict onReadById(HRef id) { HGrid res = readByIds(new HRef[] { id }, false); if (res.isEmpty()) { return(null); } HDict rec = res.row(0); if (rec.missing("id")) { return(null); } return(rec); }
/** * Read a list of entity records by their unique identifier. * Return a grid where each row of the grid maps to the respective * id array (indexes line up). If checked is true and any one of the * ids cannot be resolved then raise UnknownRecException for first id * not resolved. If checked is false, then each id not found has a * row where every cell is null. * NOTE: Was final */ public HGrid readByIds(HRef[] ids, bool bChecked) { HGrid grid = readByIds(ids); if (bChecked) { for (int i = 0; i < grid.numRows; ++i) { if (grid.row(i).missing("id")) { throw new Exception("rec not found for: " + ids[i].ToString()); } } } return(grid); }
////////////////////////////////////////////////////////////////////////// // HGridWriter ////////////////////////////////////////////////////////////////////////// // Write a grid public override void writeGrid(HGrid grid) { // grid begin m_swOut.Write("{\n"); // meta m_swOut.Write("\"meta\": {\"ver\":\"2.0\""); writeDictTags(grid.meta, false); m_swOut.Write("},\n"); // columns m_swOut.Write("\"cols\":[\n"); for (int i = 0; i < grid.numCols; i++) { if (i > 0) { m_swOut.Write(",\n"); } HCol col = grid.col(i); m_swOut.Write("{\"name\":"); m_swOut.Write(HStr.toCode(col.Name)); writeDictTags(col.meta, false); m_swOut.Write("}"); } m_swOut.Write("\n],\n"); // rows m_swOut.Write("\"rows\":[\n"); for (int i = 0; i < grid.numRows; i++) { if (i > 0) { m_swOut.Write(",\n"); } writeDict(grid.row(i)); } m_swOut.Write("\n]\n"); // grid end m_swOut.Write("}\n"); m_swOut.Flush(); }
////////////////////////////////////////////////////////////////////////// // HGridWriter ////////////////////////////////////////////////////////////////////////// // Write a grid public override void writeGrid(HGrid grid) { // cols for (int i = 0; i < grid.numCols; ++i) { if (i > 0) { m_swOut.Write(m_cDelimiter); } writeCell(grid.col(i).dis()); } m_swOut.Write('\n'); // rows for (int i = 0; i < grid.numRows; ++i) { writeRow(grid, grid.row(i)); m_swOut.Write('\n'); } }