public void Put(string name, JArr v) { if (name != null) { Build(name); } else { throw new NotImplementedException(); } }
JObj ParseObj(ref int pos) { JObj jo = new JObj(); int p = pos; for (;;) { for (;;) { if (p >= length - 1) { throw ParseEx; } int b = this[++p]; if (b == ' ' || b == '\t' || b == '\n' || b == '\r') { continue; } if (b == '"') { break; // meet first quote } if (b == '}') // close early empty { pos = p; return(jo); } throw ParseEx; } str.Clear(); // parse name for (;;) { if (p >= length - 1) { throw ParseEx; } int b = this[++p]; if (b == '"') { break; // meet second quote } str.Add((char)b); } for (;;) // till a colon { if (p >= length - 1) { throw ParseEx; } int b = this[++p]; if (b == ' ' || b == '\t' || b == '\n' || b == '\r') { continue; } if (b == ':') { break; } throw ParseEx; } string name = str.ToString(); // parse the value part for (;;) { if (p >= length - 1) { throw ParseEx; } int b = this[++p]; if (b == ' ' || b == '\t' || b == '\n' || b == '\r') { continue; // skip ws } if (b == '{') { JObj v = ParseObj(ref p); jo.Add(name, v); } else if (b == '[') { JArr v = ParseArr(ref p); jo.Add(name, v); } else if (b == '"') { string v = ParseString(ref p); jo.Add(name, v); } else if (b == 'n') { if (ParseNull(ref p)) { jo.AddNull(name); } } else if (b == 't' || b == 'f') { bool v = ParseBool(ref p, b); jo.Add(name, v); } else if (b == '-' || b >= '0' && b <= '9') { JNumber v = ParseNumber(ref p, b); jo.Add(name, v); } else if (b == '&') // bytes extension { byte[] v = ParseBytes(p); jo.Add(name, v); } else { throw ParseEx; } break; } // comma or end for (;;) { if (p >= length - 1) { throw ParseEx; } int b = this[++p]; if (b == ' ' || b == '\t' || b == '\n' || b == '\r') { continue; } if (b == ',') { break; } if (b == '}') // close normal { pos = p; return(jo); } throw ParseEx; } } }
JArr ParseArr(ref int pos) { JArr ja = new JArr(); int p = pos; for (;;) { if (p >= length - 1) { throw ParseEx; } int b = this[++p]; if (b == ' ' || b == '\t' || b == '\n' || b == '\r') { continue; // skip ws } if (b == ']') // close early empty { pos = p; return(ja); } if (b == '{') { JObj v = ParseObj(ref p); ja.Add(new JMbr(null, v)); } else if (b == '[') { JArr v = ParseArr(ref p); ja.Add(new JMbr(null, v)); } else if (b == '"') { string v = ParseString(ref p); ja.Add(new JMbr(null, v)); } else if (b == 'n') { if (ParseNull(ref p)) { ja.Add(new JMbr()); } } else if (b == 't' || b == 'f') { bool v = ParseBool(ref p, b); ja.Add(new JMbr(null, v)); } else if (b == '-' || b >= '0' && b <= '9') { JNumber v = ParseNumber(ref p, b); ja.Add(new JMbr(null, v)); } else if (b == '&') // bytes extension { byte[] v = ParseBytes(p); ja.Add(new JMbr(null, v)); } else { throw ParseEx; } // comma or return for (;;) { if (p >= length - 1) { throw ParseEx; } b = this[++p]; if (b == ' ' || b == '\t' || b == '\n' || b == '\r') { continue; // skip ws } if (b == ',') { break; } if (b == ']') // close normal { pos = p; return(ja); } throw ParseEx; } } }
internal DbSource(JObj s) { s.Get(nameof(host), ref host); s.Get(nameof(port), ref port); s.Get(nameof(database), ref database); s.Get(nameof(username), ref username); s.Get(nameof(password), ref password); // initialize connection string // var sb = new StringBuilder(); sb.Append("Host=").Append(host); sb.Append(";Port=").Append(port); sb.Append(";Database=").Append(database); sb.Append(";Username="******";Password="******";Read Buffer Size=").Append(1024 * 32); sb.Append(";Write Buffer Size=").Append(1024 * 32); sb.Append(";No Reset On Close=").Append(true); connstr = sb.ToString(); // load public composite types // using (var dc = NewDbContext()) { // composite types dc.QueryAll("SELECT t.oid, t.typname, t.typarray FROM pg_type t, pg_class c WHERE t.oid = c.reltype AND c.relkind = 'c'"); while (dc.Next()) { dc.Let(out uint oid); dc.Let(out string name); dc.Let(out uint typarray); composites.Add(new DbType(oid, name, typarray) { Converter = (n, src, snk) => { JObj v = null; src.Get(n, ref v); snk.Put(n, v); } }); } // add columns and the related array type int count = composites.Count; for (int i = 0; i < count; i++) { var comp = composites[i].Value; dc.QueryAll("SELECT attname AS name, atttypid AS typoid, atthasdef AS def, attnotnull AS notnull FROM pg_attribute WHERE attrelid = @1", p => p.Set(comp.Key)); while (dc.Next()) { comp.AddColumn(new DbField(dc)); } // the corresponding array type dc.Query("SELECT oid, typname, typarray FROM pg_type WHERE oid = @1", p => p.Set(comp.arrayoid)); dc.Let(out uint oid); dc.Let(out string name); composites.Add(new DbType(oid, name) { ElementType = comp, Converter = (n, src, snk) => { JArr v = null; src.Get(n, ref v); snk.Put(n, v); } }); } } }