public void TestPredictionInputOutput() { var echoModel = new EchoModel(); echoModel.RegisterModel(); using (var doc = new MutableDocument()) { doc.SetString("name", "Daniel"); doc.SetInt("number", 2); doc.SetDouble("max", Double.MaxValue); Db.Save(doc); } var date = DateTimeOffset.Now; var power = Function.Power(Expression.Property("number"), Expression.Int(2)); var map = new Dictionary <string, object> { ["null"] = null, ["number1"] = 10, ["number2"] = 10.1, ["int_min"] = Int32.MinValue, ["int_max"] = Int32.MaxValue, ["int64_min"] = Int64.MinValue, ["int64_max"] = Int64.MaxValue, ["float_min"] = Single.MinValue, ["float_max"] = Single.MaxValue, // NOTE: Double limits are not guaranteed ["boolean_true"] = true, ["boolean_false"] = false, ["string"] = "hello", ["date"] = date, ["expr_property"] = Expression.Property("name"), ["expr_value_number1"] = Expression.Value(20), ["expr_value_number2"] = Expression.Value(20.1), ["expr_value_boolean"] = Expression.Value(true), ["expr_value_string"] = Expression.Value("hi"), ["expr_value_date"] = Expression.Value(date), ["expr_value_null"] = Expression.Value(null), ["expr_power"] = power }; var submap = new Dictionary <string, object> { ["foo"] = "bar" }; map["dict"] = submap; var subList = new[] { "1", "2", "3" }; map["array"] = subList; var subExprMap = new Dictionary <string, object> { ["ping"] = "pong" }; map["expr_value_dict"] = Expression.Value(subExprMap); var subExprList = new[] { "4", "5", "6" }; map["expr_value_array"] = Expression.Value(subExprList); var input = Expression.Value(map); var model = nameof(EchoModel); var prediction = Function.Prediction(model, input); using (var q = QueryBuilder.Select(SelectResult.Expression(prediction)) .From(DataSource.Database(Db))) { var rows = VerifyQuery(q, (n, result) => { var pred = result.GetDictionary(0); pred.Count.Should().Be(map.Count, "because all properties should be serialized and recovered correctly"); pred.GetInt("number1").Should().Be(10); pred.GetDouble("number2").Should().Be(10.1); pred.GetInt("int_min").Should().Be(Int32.MinValue); pred.GetInt("int_max").Should().Be(Int32.MaxValue); pred.GetLong("int64_min").Should().Be(Int64.MinValue); pred.GetLong("int64_max").Should().Be(Int64.MaxValue); pred.GetFloat("float_min").Should().Be(Single.MinValue); pred.GetFloat("float_max").Should().Be(Single.MaxValue); pred.GetBoolean("boolean_true").Should().BeTrue(); pred.GetBoolean("boolean_false").Should().BeFalse(); pred.GetString("string").Should().Be("hello"); pred.GetDate("date").Should().Be(date); pred.GetString("null").Should().BeNull(); pred.GetDictionary("dict").Should().Contain(submap); pred.GetArray("array").Should().ContainInOrder(subList); pred.GetString("expr_property").Should().Be("Daniel"); pred.GetInt("expr_value_number1").Should().Be(20); pred.GetDouble("expr_value_number2").Should().Be(20.1); pred.GetBoolean("expr_value_boolean").Should().BeTrue(); pred.GetString("expr_value_string").Should().Be("hi"); pred.GetDate("expr_value_date").Should().Be(date); pred.GetString("expr_value_null").Should().BeNull(); pred.GetDictionary("expr_value_dict").Should().Contain(subExprMap); pred.GetArray("expr_value_array").Should().ContainInOrder(subExprList); pred.GetInt("expr_power").Should().Be(4); }); rows.Should().Be(1); } }
static void Main(string[] args) { // This only needs to be done once for whatever platform the executable is running // (UWP, iOS, Android, or desktop) Couchbase.Lite.Support.NetDesktop.Activate(); // create database var config = new DatabaseConfiguration(); config.ConflictResolver = new ExampleConflictResolver(); var database = new Database("my-database", config); // create document var newTask = new MutableDocument(); newTask.SetString("type", "task"); newTask.SetString("owner", "todo"); newTask.SetDate("createdAt", DateTimeOffset.UtcNow); newTask = database.Save(newTask).ToMutable(); // mutate document newTask.SetString("name", "Apples"); newTask = database.Save(newTask).ToMutable(); // typed accessors newTask.SetDate("createdAt", DateTimeOffset.UtcNow); var date = newTask.GetDate("createdAt"); // database transaction database.InBatch(() => { for (int i = 0; i < 10; i++) { using (var doc = new MutableDocument()) { doc.SetString("type", "user"); doc.SetString("name", $"user {i}"); using (var saved = database.Save(doc)) { Console.WriteLine($"saved user document {saved.GetString("name")}"); } } } }); // blob var bytes = File.ReadAllBytes("avatar.jpg"); var blob = new Blob("image/jpg", bytes); newTask.SetBlob("avatar", blob); newTask = database.Save(newTask).ToMutable(); var taskBlob = newTask.GetBlob("avatar"); var data = taskBlob.Content; newTask.Dispose(); // query var query = QueryBuilder.Select(SelectResult.Expression(Meta.ID)) .From(DataSource.Database(database)) .Where(Expression.Property("type").EqualTo(Expression.String("user")) .And(Expression.Property("admin").EqualTo(Expression.Boolean(false)))); var rows = query.Execute(); foreach (var row in rows) { Console.WriteLine($"doc ID :: ${row.GetString(0)}"); } // live query query.AddChangeListener((sender, e) => { Console.WriteLine($"Number of rows :: {e.Results.Count()}"); }); using (var newDoc = new MutableDocument()) { newDoc.SetString("type", "user"); newDoc.SetBoolean("admin", false); database.Save(newDoc); } // fts example // insert documents var tasks = new[] { "buy groceries", "play chess", "book travels", "buy museum tickets" }; foreach (string task in tasks) { using (var doc = new MutableDocument()) { doc.SetString("type", "task").SetString("name", task); // Chaining is possible database.Save(doc); } } // create Index var index = IndexBuilder.FullTextIndex(FullTextIndexItem.Property("name")); database.CreateIndex("byName", index); using (var ftsQuery = QueryBuilder.Select(SelectResult.Expression(Meta.ID).As("id")) .From(DataSource.Database(database)) .Where(FullTextExpression.Index("byName").Match("'buy'"))) { var ftsRows = ftsQuery.Execute(); foreach (var row in ftsRows) { var doc = database.GetDocument(row.GetString("id")); // Use alias instead of index Console.WriteLine( $"document properties {JsonConvert.SerializeObject(doc.ToDictionary(), Formatting.Indented)}"); } } // create conflict /* * 1. Create a document twice with the same ID (the document will have two conflicting revisions). * 2. Upon saving the second revision, the ExampleConflictResolver's resolve method is called. * The `theirs` ReadOnlyDocument in the conflict resolver represents the current rev and `mine` is what's being saved. * 3. Read the document after the second save operation and verify its property is as expected. * The conflict resolver will have deleted the obsolete revision. */ using (var theirs = new MutableDocument("buzz")) using (var mine = new MutableDocument("buzz")) { theirs.SetString("status", "theirs"); mine.SetString("status", "mine"); database.Save(theirs); database.Save(mine); } var conflictResolverResult = database.GetDocument("buzz"); Console.WriteLine($"conflictResolverResult doc.status ::: {conflictResolverResult.GetString("status")}"); // replication (Note: Linux / Mac requires .NET Core 2.0+ due to // https://github.com/dotnet/corefx/issues/8768) /* * Tested with SG 1.5 https://www.couchbase.com/downloads * Config file: * { * "databases": { * "db": { * "server":"walrus:", * "users": { * "GUEST": {"disabled": false, "admin_channels": ["*"]} * }, * "unsupported": { * "replicator_2":true * } * } * } * } */ var url = new Uri("ws://localhost:4984/db"); var replConfig = new ReplicatorConfiguration(database, new URLEndpoint(url)); var replication = new Replicator(replConfig); replication.Start(); // replication change listener replication.AddChangeListener((sender, e) => { if (e.Status.Activity == ReplicatorActivityLevel.Stopped) { Console.WriteLine("Replication has completed."); } }); Console.ReadLine(); // This is important to do because otherwise the native connection // won't be released until the next garbage collection query.Dispose(); database.Dispose(); }
public List <Information> Gets() { List <Information> informations = new List <Information>(); IQuery query = QueryBuilder.Select(SelectResult.Expression(Expression.Property("payCode"))) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("pays"))); var rows = query.Execute(); query = QueryBuilder.Select(SelectResult.Expression(Expression.Property("finCode"))) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("typefinancement"))); var rowsFin = query.Execute(); query = QueryBuilder.Select(SelectResult.Expression(Expression.Property("sfaCode"))) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("situationfamille"))); var rowsSit = query.Execute(); query = QueryBuilder.Select(SelectResult.Expression(Expression.Property("Num"))) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("commune"))); var rowsCom = query.Execute(); query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("futuracquereur"))); var rowsAq = query.Execute(); query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("programme"))); var rowsProg = query.Execute(); query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("lot"))); var rowsLot = query.Execute(); query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("vendeur"))); var rowsVdr = query.Execute(); query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("constructeur"))); var rowsCtr = query.Execute(); query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("civilite"))); var rowsCiv = query.Execute(); query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("reservation"))); var rowsRes = query.Execute(); query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(_dataBaseGetter.Get())) .Where(Expression.Property("table").EqualTo(Expression.String("option"))); var rowsOpt = query.Execute(); informations.Add(new Information() { Info = "Civilités", Quantite = rowsCiv.Count() }); informations.Add(new Information() { Info = "Communes", Quantite = rowsCom.Count() }); informations.Add(new Information() { Info = "Constructeurs", Quantite = rowsCtr.Count() }); informations.Add(new Information() { Info = "Futurs Acquéreurs", Quantite = rowsAq.Count() }); informations.Add(new Information() { Info = "Lots", Quantite = rowsLot.Count() }); informations.Add(new Information() { Info = "Options", Quantite = rowsOpt.Count() }); informations.Add(new Information() { Info = "Pays", Quantite = rows.Count() }); informations.Add(new Information() { Info = "Programmes", Quantite = rowsProg.Count() }); informations.Add(new Information() { Info = "Réservations", Quantite = rowsRes.Count() }); informations.Add(new Information() { Info = "Situations Familiales", Quantite = rowsSit.Count() }); informations.Add(new Information() { Info = "Types de financements", Quantite = rowsFin.Count() }); informations.Add(new Information() { Info = "Vendeurs", Quantite = rowsVdr.Count() }); return(informations); }
static void Main(string[] args) { // Get the database (and create it if it doesn't exist) var database = new Database("mydb"); // Create a new document (i.e. a record) in the database string id = null; using (var mutableDoc = new MutableDocument()) { mutableDoc.SetFloat("version", 2.0f) .SetString("type", "SDK"); // Save it to the database database.Save(mutableDoc); id = mutableDoc.Id; } // Update a document using (var doc = database.GetDocument(id)) using (var mutableDoc = doc.ToMutable()) { mutableDoc.SetString("language", "python"); database.Save(mutableDoc); using (var docAgain = database.GetDocument(id)) { Console.WriteLine($"Document ID :: {docAgain.Id}"); Console.WriteLine($"Learning {docAgain.GetString("language")}"); } } // Create a query to fetch documents of type SDK // i.e. SELECT * FROM database WHERE type = "SDK" using (var query = QueryBuilder.Select(SelectResult.All()) .From(DataSource.Database(database)) .Where(Expression.Property("type").EqualTo(Expression.String("SDK")))) { // Run the query var result = query.Execute(); Console.WriteLine($"Number of rows :: {result.Count()}"); } using (var query = QueryBuilder.Select( SelectResult.Expression(Meta.ID), SelectResult.Property("language")) .From(DataSource.Database(database))) { foreach (var result in query.Execute()) { Console.WriteLine($"Document Name :: {result.GetString("language")}"); } } // Create replicator to push and pull changes to and from the cloud var targetEndpoint = new URLEndpoint(new Uri("ws://localhost:4984/getting-started-db")); var replConfig = new ReplicatorConfiguration(database, targetEndpoint); // Add authentication replConfig.Authenticator = new BasicAuthenticator("john", "pass"); // Create replicator (make sure to add an instance or static variable // named _Replicator) var _Replicator = new Replicator(replConfig); _Replicator.AddChangeListener((sender, args) => { if (args.Status.Error != null) { Console.WriteLine($"Error :: {args.Status.Error}"); } }); //Path.Combine(AppContext.BaseDirectory, "CouchbaseLite"); _Replicator.Start(); }