// Use this for initialization void Start () { // create a new server before connecting database var server = new CouchServer (db_host, db_port); var database = server.GetDatabase (db); // Gets entire database. Use GetDocuments("id"); for getting specific document. IEnumerable<StarInfo> stars = database.GetAllDocuments<StarInfo>(); IEnumerator<StarInfo> stars_iter = stars.GetEnumerator (); //iterates thourgh documents while (stars_iter.MoveNext()) { var star = stars_iter.Current; // Examples to reach spesific object starting from star. It is recommended that using a null check when necessary. Debug.Log ("Name: " + star.name + "\nCoordinates: " + star.coordinates[0] + "," + star.coordinates[1] + "," + star.coordinates[2] + "\nPlanets: \n"); foreach(var planet in star.planets){ Debug.Log ("\nPNAME:" + planet.name); Debug.Log ("\nPCOR:" + planet.coordinates[0] + "," + planet.coordinates[1] + "," + planet.coordinates[2]); Debug.Log ("\nPSAT:" + planet.satellites[0].name + ", "+ planet.satellites[0].type); //Such as here we need null check otherwise an exception ill be thrown because first planet does not have any facilities. //Debug.Log ("\nPSATF:" + planet.satellites[0].facilities[0].name + ", "+ planet.satellites[0].facilities[0].type); Debug.Log ("\nPSAT:" + planet.satellites[1].name + ", "+ planet.satellites[1].type); Debug.Log ("\nPSATF:" + planet.satellites[1].facilities[0].name + ", "+ planet.satellites[1].facilities[0].type); Debug.Log ("\nPOWN:" + planet.owner); foreach (var tile in planet.tiles) { Debug.Log ("\nPTILEID:" + tile.id); Debug.Log ("\nPTILET:" + tile.type); Debug.Log ("\nPTILEB:" + tile.building.id + " " + tile.building.name + " " + tile.building.level + " " + tile.building.type); } } } }
public bool UpdateTourToCouch(List <dtoTour> inpar) { try { var server = new CouchServer(host, port); var cdb = server.GetDatabase("dbsendill"); foreach (dtoTour par in inpar) { jtour jt = new jtour(); jt.id = par.id; jt.idcar = par.idcar; jt.idcustomer = par.idcustomer; jt.taddress = par.taddress; jt.tcontact = par.tcontact; jt.tdatetime = par.tdatetime; jt.tnote = par.tnote; jt.tphone = par.tphone; cdb.SaveDocument(jt); } return(true); } catch (Exception) { return(false); } }
static void Main(string[] args) { var dbName = "couch_demo"; string host = "localhost"; int port = 5984; var server = new CouchServer(host, port); if (server.HasDatabase(dbName)) { server.DeleteDatabase(dbName); } var db = server.GetDatabase(dbName); Console.WriteLine("Created database '{0}'", dbName); Car car = null; for (int i = 0; i < 10; i++) { car = new Car("Saab", "93", 170 + i); db.SaveDocument(car); } Console.WriteLine("Saved 10 Cars with 170-180 hps each."); var firstCar = db.GetAllDocuments().First(); Console.WriteLine("Here is the first of the Cars: \n\n" + firstCar); car.HorsePowers = 400; db.SaveDocument(car); Console.WriteLine("Modified last Car with id " + car.Id); var sameCar = db.GetDocument<Car>(car.Id); Console.WriteLine("Loaded last Car " + sameCar.Make + " " + sameCar.Model + " now with " + sameCar.HorsePowers + "hps."); var cars = db.GetAllDocuments<Car>(); Console.WriteLine("Loaded all Cars: " + cars.Count()); var tempView = db.NewTempView("test", "test", "if (doc.docType && doc.docType == 'car') emit(doc.Hps, doc);"); var linqCars = tempView.LinqQuery<Car>(); var fastCars = from c in linqCars where c.HorsePowers >= 175 select c;//.Make + " " + c.Model; foreach (var fastCar in fastCars) Console.WriteLine(fastCar); db.DeleteDocument(tempView.Doc); foreach (var eachCar in cars.Where(x => x.HorsePowers > 175)) { db.DeleteDocument(eachCar); Console.WriteLine("Deleted car with id " + eachCar.Id); } // Get all cars again and see how many are left. Console.WriteLine("Cars left: " + db.GetAllDocuments<Car>().Count()); Console.Write("\r\nPress any key to close. "); Console.ReadKey(false); }
public void SetUp() { var host = ConfigurationManager.AppSettings["CouchHost"] ?? "localhost"; var port = Convert.ToInt32(ConfigurationManager.AppSettings["CouchPort"] ?? "5984"); server = new CouchServer(host, port); db = server.GetNewDatabase(DbName); }
static void Main() { //This is real username and password and must work with them CouchServer server = new CouchServer("testuser.cloudant.com", 5984, "testuser", "testpassword"); ICouchDatabase database = server.GetDatabase("dictionary"); bool isWorking = true; ICouchDocument doc = database.GetDocument("game"); while (isWorking) { Console.WriteLine("For add new word press : 1"); Console.WriteLine("For finding a translation for given word press 2"); Console.WriteLine("For list all words and their translations press 3"); Console.WriteLine("For exit press 4"); string command = Console.ReadLine(); switch (command) { case "1": { Console.WriteLine("Enter word"); string word = Console.ReadLine(); Console.WriteLine("Enter translation"); string translation = Console.ReadLine(); CreateWord(word, translation, database); } break; case "2": { Console.WriteLine("Enter word"); string word = Console.ReadLine(); GetWordTranslation(word, database); } break; case "3": { ListAllLords(database); } break; case "4": { isWorking = false; } break; default: { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Unknown Command"); Console.ResetColor(); } break; } } }
static void Main(string[] args) { string host = "localhost"; int port = 5984; var server = new CouchServer(host, port); var db = server.GetDatabase("messages"); Console.WriteLine("Created database 'trivial'"); }
public static void InsertItemToCloudant(FeedItemSP item) { CouchServer server = new CouchServer("xamfia.cloudant.com", 5984, "xamfia", "123400"); ICouchDatabase database = server.GetDatabase("docs"); //var docs = database.GetAllDocuments(); //string doc = "{'title':'test','tags':'mn,jv'}"; database.WriteDocument(ItemToJson(item), item.FeedItemId.ToString()); }
public ActionResult Index() { ViewData["Message"] = "Welcome to ASP.NET MVC!"; string host = "192.168.1.6"; int port = 5918; var server = new CouchServer(host, port); var db = server.GetDatabase("contacts"); Console.WriteLine("Created database 'contacts'"); return View(); }
public void SetUp() { var host = ConfigurationManager.AppSettings["CouchHost"] ?? "localhost"; var port = Convert.ToInt32(ConfigurationManager.AppSettings["CouchPort"] ?? "5984"); server = new CouchServer(host, port); db = server.GetNewDatabase(DbName); Car car = null; for (int i = 0; i < 10; i++) { car = new Car("Saab", "93", 170 + i); db.SaveDocument(car); } tempView = db.NewTempView("test", "test", "if (doc.docType && doc.docType == 'car') emit(doc.Hps, doc);"); }
public void SetUp() { var host = ConfigurationManager.AppSettings["CouchHost"] ?? "localhost"; var port = Convert.ToInt32(ConfigurationManager.AppSettings["CouchPort"] ?? "5984"); server = new CouchServer(host, port); DbName = GetNewDbName(); db = server.GetNewDatabase(DbName); Car car = null; for (int i = 0; i < 10; i++) { car = new Car("Saab", "93", 170 + i); db.SaveDocument(car); } tempView = db.NewTempView("test", "test", "if (doc.docType && doc.docType == 'car') emit(doc.Hps, doc);"); }
public void SetUp() { var host = ConfigurationManager.AppSettings["CouchHost"] ?? "localhost"; var port = Convert.ToInt32(ConfigurationManager.AppSettings["CouchPort"] ?? "5984"); var username = ConfigurationManager.AppSettings["Username"] ?? String.Empty; var password = ConfigurationManager.AppSettings["Password"] ?? String.Empty; if (username.Equals(String.Empty)) { server = new CouchServer(host, port); } else { server = new CouchServer(host, port, username, password); } DbName = GetNewDbName(); db = server.GetNewDatabase(DbName); }
// Use this for initialization void Awake () { // Singleton if (gameMaster == null) { DontDestroyOnLoad (gameObject); gameMaster = this; } else if (gameMaster != this) Destroy (gameObject); // Establish connection with the database var server = new CouchServer (db_host, db_port); database = server.GetDatabase (db); // Fetch the home planet details from the database // and create a global planet object called myPlanet // If some attribute which belongs to a star will be called, // then it is necessary that a Star object to be created. // starID should be recognized from beginning and it should be // known that which planet we are on. string starID = "star-1"; // Subject to change -> string to int Star currentStar = gameObject.AddComponent<Star>(); selectedStar = currentStar; currentStar.GetStar(database, starID); // Fetch all stars for the galaxy generation currentStar.GetAllStars(database); stars = currentStar.starDBIter; // Get planet from current selected star. Planet currentPlanet = currentStar.planets[2]; myPlanet = currentPlanet; myPlanet.initTiles(); myTiles = currentPlanet.tiles; // Where iz ma tiles? foreach (Tile tile in myTiles) { } // Start Resource Generation InvokeRepeating("UpdateResources", 0, 1); }
public bool UpdateTourToCouch(List <dtoTour> inpar) { try { var server = new CouchServer(host, port); var cdb = server.GetDatabase("dbsendill_newtours"); foreach (dtoTour par in inpar) { jcar jc = new jcar(); cdb.SaveDocument(jc); } return(true); } catch (Exception) { return(false); } }
/// <summary> /// Connects to the server and builds a list of databases. /// </summary> /// <param name="connection">The connection.</param> private void LoadServer(string connection) { try { _svr = null; _currentNode = null; buffer = null; tvMain.Nodes.Clear(); rtSource.Clear(); var parts = connection.Split(':'); _svr = CreateServer(parts); if (_svr == null) { return; } tvMain.Nodes.Clear(); var svrNode = tvMain.Nodes.Add(parts[0], parts[0]); svrNode.Tag = _svr; svrNode.ImageIndex = 5; svrNode.SelectedImageIndex = 5; foreach (var db in _svr.GetDatabaseNames()) { var node = svrNode.Nodes.Add(db, db); node.Tag = new CouchDatabase(db, _svr); node.Nodes.Add(String.Empty); node.ImageIndex = 0; node.SelectedImageIndex = 0; } toolStripStatusLabel1.Text = "Connected to " + connection; settings.Connection = connection; } catch (WebException ex) { MessageBox.Show(ex.Message, "Unable to connect", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public List <dtoCustomer> LoadCustomersFromCouch() { List <dtoCustomer> lcustomer = new List <dtoCustomer>(); var server = new CouchServer(host, port); var cdb = server.GetDatabase("dbsendill_newcustomers"); var tempView = cdb.NewTempView("dbsendill_newcustomerss", "dbsendill_newcustomer", "if (doc.docType && doc.docType == 'customer') emit(doc.carnumber, doc);"); var linqCustomer = tempView.LinqQuery <jcustomer>(); var qalldoc = cdb.QueryAllDocuments().View.LinqQuery <jcustomer>(); var mycustomer = from c in linqCustomer select c; foreach (var par in mycustomer) { dtoCustomer jc = new dtoCustomer(); jc.id = par.id; jc.address = par.address; jc.area = par.area; jc.areacode = par.areacode; jc.cumbaf = par.cumbaf; jc.email = par.email; jc.fax = par.fax; jc.isdel = par.isdel; jc.kt = par.kt; jc.mobile = par.mobile; jc.name = par.name; jc.number = par.number; jc.phone1 = par.phone1; jc.phone2 = par.phone2; jc.streetnr = par.streetnr; jc.svaedi = par.svaedi; jc.umbaf = par.umbaf; jc.url = par.url; lcustomer.Add(jc); } return(lcustomer); //var tempView = cdb.NewTempView("test", "test", "if (doc.docType && doc.docType == 'car') emit(doc.Hps, doc);"); //var linqCars = tempView.LinqQuery<Car>(); }
public bool UpdateCustomersToCouch(List <dtoCustomer> inpar) { try { var server = new CouchServer(host, port); var cdb = server.GetDatabase("dbsendill_customers"); foreach (dtoCustomer par in inpar) { jcustomer jc = new jcustomer(); jc.id = par.id; jc.address = par.address; jc.area = par.area; jc.areacode = par.areacode; jc.cumbaf = par.cumbaf; jc.email = par.email; jc.fax = par.fax; jc.isdel = par.isdel; jc.kt = par.kt; jc.mobile = par.mobile; jc.name = par.name; jc.number = par.number; jc.phone1 = par.phone1; jc.phone2 = par.phone2; jc.streetnr = par.streetnr; jc.svaedi = par.svaedi; jc.umbaf = par.umbaf; jc.url = par.url; cdb.SaveDocument(jc); } return(true); } catch (Exception) { return(false); } }
public bool UpdataCarsToCouch(List <dtoCars> inpar) { try { var server = new CouchServer(host, port); var cdb = server.GetDatabase("dbsendill_newcars"); foreach (dtoCars par in inpar) { jcar jc = new jcar(); if (par.id == null) { jc.id = 9999; } else { jc.id = par.id; }; if (par.stationid == null) { jc.stationid = 9999; } else { jc.stationid = par.stationid; } if (par.carnumber == null) { jc.carnumber = "null"; } else { jc.carnumber = par.carnumber; } if (par.code == null) { jc.code = "null"; } else { jc.code = par.code; } if (par.listed == null) { jc.listed = false; } else { jc.listed = par.listed; } if (par.carname == null) { jc.carname = "null"; } else { jc.carname = par.carname; } if (par.car1 == null) { jc.car1 = false; } else { jc.car1 = par.car1; } if (par.car2 == null) { jc.car2 = false; } else { jc.car2 = par.car2; } if (par.car3 == null) { jc.car3 = false; } else { jc.car3 = par.car3; } if (par.car4 == null) { jc.car4 = false; } else { jc.car4 = par.car4; } if (par.car5 == null) { jc.car5 = false; } else { jc.car5 = par.car5; } if (par.car6 == null) { jc.car6 = false; } else { jc.car6 = par.car6; } if (par.car7 == null) { jc.car7 = false; } else { jc.car7 = par.car7; } if (par.car8 == null) { jc.car8 = false; } else { jc.car8 = par.car8; } if (par.car9 == null) { jc.car9 = false; } else { jc.car9 = par.car9; } if (par.car10 == null) { jc.car10 = false; } else { jc.car10 = par.car10; } if (par.length == null) { jc.length = 9999; } else { jc.length = par.length; } if (par.backdoorheight == null) { jc.backdoorheight = 9999; } else { jc.backdoorheight = par.backdoorheight; } if (par.backdoorlength == null) { jc.backdoorlength = 9999; } else { jc.backdoorlength = par.backdoorlength; } if (par.sidedoorheight == null) { jc.sidedoorheight = 9999; } else { jc.sidedoorheight = par.sidedoorheight; } if (par.sidedoorlength == null) { jc.sidedoorlength = 9999; } else { jc.sidedoorlength = par.sidedoorlength; } if (par.weightlimit == null) { jc.weightlimit = 9999; } else { jc.weightlimit = par.weightlimit; } if (par.liftsize == null) { jc.liftsize = 9999; } else { jc.liftsize = par.liftsize; } if (par.volume == null) { jc.volume = 9999; } else { jc.volume = par.volume; } if (par.width == null) { jc.width = 9999; } else { jc.width = par.width; } if (par.model == null) { jc.model = "null"; } else { jc.model = par.model; } if (par.maxcarry == null) { jc.maxcarry = 9999; } else { jc.maxcarry = par.maxcarry; } if (par.owner == null) { jc.owner = "null"; } else { jc.owner = par.owner; } if (par.kt == null) { jc.kt = "null"; } else { jc.kt = par.kt; } if (par.address == null) { jc.address = "null"; } else { jc.address = par.address; } if (par.town == null) { jc.town = "null"; } else { jc.town = par.town; } if (par.postcode == null) { jc.postcode = "null"; } else { jc.postcode = par.postcode; } if (par.phone == null) { jc.phone = "null"; } else { jc.phone = par.phone; } if (par.mobile == null) { jc.mobile = "null"; } else { jc.mobile = par.mobile; } if (par.driver == null) { jc.driver = "null"; } else { jc.driver = par.driver; } if (par.dkt == null) { jc.dkt = "null"; } else { jc.dkt = par.dkt; } if (par.daddress == null) { jc.daddress = "null"; } else { jc.daddress = par.daddress; } if (par.dtown == null) { jc.dtown = "null"; } else { jc.dtown = par.town; } if (par.postcode == null) { jc.postcode = "null"; } else { jc.postcode = par.postcode; } if (par.dphone == null) { jc.dphone = "null"; } else { jc.dphone = par.dphone; } if (par.dmobile == null) { jc.dmobile = "null"; } else { jc.dmobile = par.dmobile; } if (par.heightofbox == null) { jc.heightofbox = 9999; } else { jc.heightofbox = par.heightofbox; } if (par.isdel == null) { jc.isdel = false; } jc.isdel = par.isdel; if (par.size == null) { jc.size = 9999; } else { jc.size = par.size; } cdb.SaveDocument(jc); } return(true); } catch (Exception) { return(false); } }
public List <dtoCars> LoadCarsFromCouch() { List <dtoCars> lcar = new List <dtoCars>(); var server = new CouchServer(host, port); var cdb = server.GetDatabase("dbsendill_newcars"); var tempView = cdb.NewTempView("dbsendill_cars", "dbsendill_cars", "if (doc.docType && doc.docType == 'car') emit(doc.carnumber, doc);"); var linqCars = tempView.LinqQuery <jcar>(); var mycar = from c in linqCars select c; foreach (var par in mycar) { dtoCars jc = new dtoCars(); jc.id = par.id; jc.stationid = par.stationid; jc.carnumber = par.carnumber; jc.code = par.code; jc.listed = par.listed; jc.carname = par.carname; jc.car1 = par.car1; jc.car2 = par.car2; jc.car3 = par.car3; jc.car4 = par.car4; jc.car5 = par.car5; jc.car6 = par.car6; jc.car7 = par.car7; jc.car8 = par.car8; jc.car9 = par.car9; jc.car10 = par.car10; jc.length = par.length; jc.backdoorheight = par.backdoorheight; jc.backdoorlength = par.backdoorlength; jc.sidedoorheight = par.sidedoorheight; jc.sidedoorlength = par.sidedoorlength; jc.weightlimit = par.weightlimit; jc.liftsize = par.liftsize; jc.volume = par.volume; jc.width = par.width; jc.model = par.model; jc.maxcarry = par.maxcarry; jc.owner = par.owner; jc.kt = par.kt; jc.address = par.address; jc.town = par.town; jc.postcode = par.postcode; jc.phone = par.phone; jc.mobile = par.mobile; jc.driver = par.driver; jc.dkt = par.dkt; jc.daddress = par.daddress; jc.dtown = par.town; jc.postcode = par.postcode; jc.dphone = par.dphone; jc.dmobile = par.dmobile; jc.heightofbox = par.heightofbox; jc.isdel = par.isdel; jc.size = par.size; lcar.Add(jc); } return(lcar); //var tempView = cdb.NewTempView("test", "test", "if (doc.docType && doc.docType == 'car') emit(doc.Hps, doc);"); //var linqCars = tempView.LinqQuery<Car>(); }
protected static void CreateJSON () { StringBuilder json = new StringBuilder(); TempSensor t; json.AppendLine("{"); for (int i = 0; i < TempSensors.Length; i++) { t = TempSensors[i]; json.Append("\"" + t.Description +"\":\"" + t.CurrentValue + "\""); if ( i < TempSensors.Length - 1) { json.Append(","); } json.AppendLine(); } json.AppendLine("}"); TextWriter tw = new StreamWriter("temp.json", false, Encoding.UTF8); tw.WriteLine(json); tw.Close(); // und in couchDB mit Divan var cdb_server = new CouchServer(cdb_host, cdb_port); var db = cdb_server.GetDatabase(cdb_DBName); log("Created database " + cdb_DBName); for (int i = 0; i < TempSensors.Length; i++) { TemperaturClass tc = new TemperaturClass(TempSensors[i]); db.SaveDocument(tc); } log("all values written"); try { var tempView = db.NewTempView("test", "test", "if (doc.docType && doc.docType == 'TemperaturClass') emit(doc.ts.TimeStamp, doc);"); var linqCars = tempView.LinqQuery<TemperaturClass>(); var fastCars = from c in linqCars where c.ts.TimeStamp == "13.09.2015 17:09:30" select c; foreach (var fastCar in fastCars) Console.WriteLine(fastCar); } catch(Exception E) { log("Error reading from couchdb" + E.Message); } }
static void Main(string[] args) { string host = ""; int port = 5984; String username = ""; String password = ""; // Lets you see all HTTP requests made by Divan Trace.Listeners.Add(new ConsoleTraceListener()); // Trivial parse of args to get host and port switch (args.Length) { case 0: Console.WriteLine("Using " + host + ":" + port); break; case 1: Console.WriteLine("Using " + args[0] + ":5984"); host = args[0]; break; case 2: Console.WriteLine("Using " + args[0] + ":" + args[1]); host = args[0]; port = int.Parse(args[1]); break; } // Get a server instance. It only holds host, port and a string database prefix. // For non trivial usage of Divan you typically create your own subclass of CouchServer. var server = new CouchServer(host, port, username, password); /* This has issues with the windows build of couch db - something about file locking * // a little bit of cleanup * if (server.HasDatabase("trivial")) * server.DeleteDatabase("trivial"); */ // Get (creates it if needed) a CouchDB database. This call will create the db in CouchDB // if it does not exist, create a CouchDatabase instance and then send Initialize() to it // before returning it. The base class CouchDatabase also has very little state, it knows // only the server that it belongs to and its own name. var db = server.GetDatabase("trivial"); Console.WriteLine("Created database 'trivial'"); // Create and save 10 Cars with automatically allocated Ids by CouchDB. // Divan stores ICouchDocuments and there are several ways you can go: // - Create a subclass of CouchDocument (like Car is here). // - Create a class that implements ICouchDocument. // - Create a subclass of CouchJsonDocument. Car car = null; for (int i = 0; i < 10; i++) { car = new Car("Saab", "93", 170 + i); db.SaveDocument(car); } Console.WriteLine("Saved 10 Cars with 170-180 hps each."); // Load all Cars as CouchJsonDocuments and print one of them Console.WriteLine("Here is the first of the Cars: \n\n" + db.GetAllDocuments().First().ToString()); // Modify the last Car we saved... car.HorsePowers = 400; // ...and save the change. // We could also have used WriteDocument if we knew it was an existing doc db.SaveDocument(car); Console.WriteLine("Modified last Car with id " + car.Id); // Load a Car by known id (we just pick it from car), the class to instantiate is given using generics (Car) var sameCar = db.GetDocument <Car>(car.Id); Console.WriteLine("Loaded last Car " + sameCar.Make + " " + sameCar.Model + " now with " + sameCar.HorsePowers + "hps."); // Load all Cars. If we dwelve into the GetAllDocuments() method we will see that // QueryAllDocuments() gives us a CouchQuery which we can configure. We tell it to IncludeDocuments() // which means that we will get back not only ids but the actual documents too. GetResult() will perform the // HTTP request to CouchDB and return a CouchGenericViewResult which we in turn can ask to produce objects from JSON, // in this case we pick out the actual documents and instantiate them as instances of the class Car. var cars = db.GetAllDocuments <Car>(); Console.WriteLine("Loaded all Cars: " + cars.Count()); // Now try some linq var tempView = db.NewTempView("test", "test", "if (doc.docType && doc.docType == 'car') emit(doc.Hps, doc);"); var linqCars = tempView.LinqQuery <Car>(); var fastCars = from c in linqCars where c.HorsePowers >= 175 select c; //.Make + " " + c.Model; foreach (var fastCar in fastCars) { Console.WriteLine(fastCar); } var twoCars = from c in linqCars where c.HorsePowers == 175 || c.HorsePowers == 176 select c; //.Make + " " + c.Model; foreach (var twoCar in twoCars) { Console.WriteLine(twoCar); } var hps = new int[] { 176, 177 }; var twoMoreCars = from c in linqCars where hps.Contains(c.HorsePowers) select c.Make + " " + c.Model + " with " + c.HorsePowers + "HPs"; foreach (var twoCar in twoMoreCars) { Console.WriteLine(twoCar); } // cleanup for later db.DeleteDocument(tempView.Doc); // Delete some Cars one by one. CouchDB is an MVCC database which means that for every operation that modifies a document // we need to supply not only its document id, but also the revision that we are aware of. This means that we must supply id/rev // for each document we want to delete. foreach (var eachCar in cars.Where(x => x.HorsePowers > 175)) { db.DeleteDocument(eachCar); Console.WriteLine("Deleted car with id " + eachCar.Id); } cars = db.GetAllDocuments <Car>(); // Get all cars again and see how many are left. Console.WriteLine("Cars left: " + cars.Count()); // We can actually also delete in a single bulk call using DeleteDocuments(). db.DeleteDocuments(cars.ToArray()); Console.WriteLine("Deleted the rest of the cars"); // test out the arbitrary conventions Console.WriteLine("Trying arbitrary documents"); var littleCar = new LittleCar() { docType = "car", Make = "Yugo", Model = "Hell if i know" }; littleCar = db.SaveArbitraryDocument(littleCar); Console.Write("\r\nDelete database (y/n)? "); if (Console.ReadLine().Trim().Equals("y", StringComparison.OrdinalIgnoreCase)) { // Delete the db itself db.Delete(); Console.WriteLine("Deleted database."); } Console.WriteLine("\r\nPress enter to close. "); Console.ReadLine(); }
static void Main(string[] args) { string host = ""; int port = 5984; String username = ""; String password = ""; // Lets you see all HTTP requests made by Divan Trace.Listeners.Add(new ConsoleTraceListener()); // Trivial parse of args to get host and port switch (args.Length) { case 0: Console.WriteLine("Using " + host + ":" + port); break; case 1: Console.WriteLine("Using " + args[0] + ":5984"); host = args[0]; break; case 2: Console.WriteLine("Using " + args[0] + ":" + args[1]); host = args[0]; port = int.Parse(args[1]); break; } // Get a server instance. It only holds host, port and a string database prefix. // For non trivial usage of Divan you typically create your own subclass of CouchServer. var server = new CouchServer(host, port,username,password); /* This has issues with the windows build of couch db - something about file locking // a little bit of cleanup if (server.HasDatabase("trivial")) server.DeleteDatabase("trivial"); */ // Get (creates it if needed) a CouchDB database. This call will create the db in CouchDB // if it does not exist, create a CouchDatabase instance and then send Initialize() to it // before returning it. The base class CouchDatabase also has very little state, it knows // only the server that it belongs to and its own name. var db = server.GetDatabase("trivial"); Console.WriteLine("Created database 'trivial'"); // Create and save 10 Cars with automatically allocated Ids by CouchDB. // Divan stores ICouchDocuments and there are several ways you can go: // - Create a subclass of CouchDocument (like Car is here). // - Create a class that implements ICouchDocument. // - Create a subclass of CouchJsonDocument. Car car = null; for (int i = 0; i < 10; i++) { car = new Car("Saab", "93", 170 + i); db.SaveDocument(car); } Console.WriteLine("Saved 10 Cars with 170-180 hps each."); // Load all Cars as CouchJsonDocuments and print one of them Console.WriteLine("Here is the first of the Cars: \n\n" + db.GetAllDocuments().First().ToString()); // Modify the last Car we saved... car.HorsePowers = 400; // ...and save the change. // We could also have used WriteDocument if we knew it was an existing doc db.SaveDocument(car); Console.WriteLine("Modified last Car with id " + car.Id); // Load a Car by known id (we just pick it from car), the class to instantiate is given using generics (Car) var sameCar = db.GetDocument<Car>(car.Id); Console.WriteLine("Loaded last Car " + sameCar.Make + " " + sameCar.Model + " now with " + sameCar.HorsePowers + "hps."); // Load all Cars. If we dwelve into the GetAllDocuments() method we will see that // QueryAllDocuments() gives us a CouchQuery which we can configure. We tell it to IncludeDocuments() // which means that we will get back not only ids but the actual documents too. GetResult() will perform the // HTTP request to CouchDB and return a CouchGenericViewResult which we in turn can ask to produce objects from JSON, // in this case we pick out the actual documents and instantiate them as instances of the class Car. var cars = db.GetAllDocuments<Car>(); Console.WriteLine("Loaded all Cars: " + cars.Count()); // Now try some linq var tempView = db.NewTempView("test", "test", "if (doc.docType && doc.docType == 'car') emit(doc.Hps, doc);"); var linqCars = tempView.LinqQuery<Car>(); var fastCars = from c in linqCars where c.HorsePowers >= 175 select c;//.Make + " " + c.Model; foreach (var fastCar in fastCars) Console.WriteLine(fastCar); var twoCars = from c in linqCars where c.HorsePowers == 175 || c.HorsePowers == 176 select c;//.Make + " " + c.Model; foreach (var twoCar in twoCars) Console.WriteLine(twoCar); var hps = new int[] { 176, 177 }; var twoMoreCars = from c in linqCars where hps.Contains(c.HorsePowers) select c.Make + " " + c.Model + " with " + c.HorsePowers + "HPs"; foreach (var twoCar in twoMoreCars) Console.WriteLine(twoCar); // cleanup for later db.DeleteDocument(tempView.Doc); // Delete some Cars one by one. CouchDB is an MVCC database which means that for every operation that modifies a document // we need to supply not only its document id, but also the revision that we are aware of. This means that we must supply id/rev // for each document we want to delete. foreach (var eachCar in cars.Where(x => x.HorsePowers > 175)) { db.DeleteDocument(eachCar); Console.WriteLine("Deleted car with id " + eachCar.Id); } cars = db.GetAllDocuments<Car>(); // Get all cars again and see how many are left. Console.WriteLine("Cars left: " + cars.Count()); // We can actually also delete in a single bulk call using DeleteDocuments(). db.DeleteDocuments(cars.ToArray()); Console.WriteLine("Deleted the rest of the cars"); // test out the arbitrary conventions Console.WriteLine("Trying arbitrary documents"); var littleCar = new LittleCar() { docType = "car", Make = "Yugo", Model = "Hell if i know" }; littleCar = db.SaveArbitraryDocument(littleCar); Console.Write("\r\nDelete database (y/n)? "); if (Console.ReadLine().Trim().Equals("y", StringComparison.OrdinalIgnoreCase)) { // Delete the db itself db.Delete(); Console.WriteLine("Deleted database."); } Console.WriteLine("\r\nPress enter to close. "); Console.ReadLine(); }
public CouchDB() { this.server = new CouchServer("localhost", 5984); this.db = server.GetDatabase("ticks"); this.m_random = new Random(DateTime.Now.Millisecond); }