Commits

Anonymous committed 0624371

Updated: Added button to reprocess all records in selected domain/collection to update to latest document schema version.

Comments (0)

Files changed (2)

BsonSql.Console/frmConsole2.Designer.cs

             this.tpItem = new System.Windows.Forms.TabPage();
             this.gbOptions = new System.Windows.Forms.GroupBox();
             this.txtResult = new BsonSql.Console.SelectableTextBox();
+            this.tpTools = new System.Windows.Forms.TabPage();
+            this.gbReprocess = new System.Windows.Forms.GroupBox();
+            this.btnUpdateSchemaVersion = new System.Windows.Forms.Button();
             this.panel1.SuspendLayout();
             this.tabs.SuspendLayout();
             this.tpCollection.SuspendLayout();
             ((System.ComponentModel.ISupportInitialize)(this.dgvCollection)).BeginInit();
             this.tpItem.SuspendLayout();
             this.gbOptions.SuspendLayout();
+            this.tpTools.SuspendLayout();
+            this.gbReprocess.SuspendLayout();
             this.SuspendLayout();
             // 
             // txtServer
             | System.Windows.Forms.AnchorStyles.Right)));
             this.tabs.Controls.Add(this.tpCollection);
             this.tabs.Controls.Add(this.tpItem);
+            this.tabs.Controls.Add(this.tpTools);
             this.tabs.Location = new System.Drawing.Point(12, 100);
             this.tabs.Name = "tabs";
             this.tabs.SelectedIndex = 0;
             this.txtResult.Size = new System.Drawing.Size(481, 264);
             this.txtResult.TabIndex = 1;
             // 
+            // tpTools
+            // 
+            this.tpTools.Controls.Add(this.gbReprocess);
+            this.tpTools.Location = new System.Drawing.Point(4, 22);
+            this.tpTools.Name = "tpTools";
+            this.tpTools.Size = new System.Drawing.Size(496, 494);
+            this.tpTools.TabIndex = 2;
+            this.tpTools.Text = "Tools";
+            this.tpTools.UseVisualStyleBackColor = true;
+            // 
+            // gbReprocess
+            // 
+            this.gbReprocess.Controls.Add(this.btnUpdateSchemaVersion);
+            this.gbReprocess.Location = new System.Drawing.Point(6, 3);
+            this.gbReprocess.Name = "gbReprocess";
+            this.gbReprocess.Size = new System.Drawing.Size(487, 100);
+            this.gbReprocess.TabIndex = 0;
+            this.gbReprocess.TabStop = false;
+            this.gbReprocess.Text = "Manage Collection";
+            // 
+            // btnUpdateSchemaVersion
+            // 
+            this.btnUpdateSchemaVersion.Location = new System.Drawing.Point(21, 29);
+            this.btnUpdateSchemaVersion.Name = "btnUpdateSchemaVersion";
+            this.btnUpdateSchemaVersion.Size = new System.Drawing.Size(139, 23);
+            this.btnUpdateSchemaVersion.TabIndex = 0;
+            this.btnUpdateSchemaVersion.Text = "Update Schema Version";
+            this.btnUpdateSchemaVersion.UseVisualStyleBackColor = true;
+            this.btnUpdateSchemaVersion.Click += new System.EventHandler(this.btnUpdateSchemaVersion_Click);
+            // 
             // frmConsole2
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
             this.tpItem.PerformLayout();
             this.gbOptions.ResumeLayout(false);
             this.gbOptions.PerformLayout();
+            this.tpTools.ResumeLayout(false);
+            this.gbReprocess.ResumeLayout(false);
             this.ResumeLayout(false);
             this.PerformLayout();
 
         private SelectableTextBox txtResult;
         private System.Windows.Forms.TabPage tpCollection;
         private System.Windows.Forms.DataGridView dgvCollection;
+        private System.Windows.Forms.TabPage tpTools;
+        private System.Windows.Forms.GroupBox gbReprocess;
+        private System.Windows.Forms.Button btnUpdateSchemaVersion;
 	}
 }

BsonSql.Console/frmConsole2.cs

 using MongoDB.Bson;
 using MongoDB.Bson.Serialization;
 using MongoDB.Bson.IO;
+using System.Data.SqlClient;
 
 namespace BsonSql.Console
 {
 
         void cbCollection_SelectedIndexChanged(object sender, EventArgs e)
         {
-            //setup datagridview 
-            dgvCollection.ReadOnly = true;
-            //dgvCollection.Rows.Clear();
-            //dgvCollection.AutoGenerateColumns = false;
-            //dgvCollection.Columns.Clear();
-            dgvCollection.AllowUserToDeleteRows = false;
-            dgvCollection.AllowUserToAddRows = false;
-            dgvCollection.AllowDrop = false;
-            dgvCollection.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
+            ////setup datagridview 
+            //dgvCollection.ReadOnly = true;
+            //dgvCollection.AllowUserToDeleteRows = false;
+            //dgvCollection.AllowUserToAddRows = false;
+            //dgvCollection.AllowDrop = false;
+            //dgvCollection.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
 
-            BsonCollection collection = cbCollection.SelectedItem as BsonCollection;
+            //BsonCollection collection = cbCollection.SelectedItem as BsonCollection;
 
-            if (cbCollection.SelectedItem != null)
-            {
-                BsonSqlCollection bsqlCollection = GetBsonSqlCollection(collection);
+            //if (cbCollection.SelectedItem != null)
+            //{
+            //    BsonSqlCollection bsqlCollection = GetBsonSqlCollection(collection);
 
-                var query = Driver.Builders.QueryBuilder.Where(
-                Driver.Builders.WhereClauseBuilder.CreateBound("SchemaVersion", Driver.Builders.Enums.Comparison.GreaterOrEquals));
+            //    var query = Driver.Builders.QueryBuilder.Where(
+            //    Driver.Builders.WhereClauseBuilder.CreateBound("SchemaVersion", Driver.Builders.Enums.Comparison.GreaterOrEquals));
 
-                var bsqlCursor = bsqlCollection.FindAs<BsonDocument>(
-                    query.Bind(new BsonDocument() 
-                        { 
-                            { "SchemaVersion", 0 }
-                        }));
+            //    var bsqlCursor = bsqlCollection.FindAs<BsonDocument>(
+            //        query.Bind(new BsonDocument() 
+            //            { 
+            //                { "SchemaVersion", 0 }
+            //            }));
 
-                DataSet ds = new DataSet(collection.Domain.Name);
-                DataTable dt = new DataTable(collection.Name);
-                foreach (string fieldName in collection.IndexedFields)
-                {
-                    DataColumn dc = new DataColumn(fieldName);
-                    dt.Columns.Add(dc);
+            //    DataSet ds = new DataSet(collection.Domain.Name);
+            //    DataTable dt = new DataTable(collection.Name);
+            //    foreach (string fieldName in collection.IndexedFields)
+            //    {
+            //        DataColumn dc = new DataColumn(fieldName);
+            //        try { dt.Columns.Add(dc); }
+            //        catch { }
 
-                }
+            //    }
 
-                foreach (BsonDocument d in bsqlCursor)
-                {
-                    DataRow row = dt.NewRow();
+            //    foreach (BsonDocument d in bsqlCursor)
+            //    {
+            //        DataRow row = dt.NewRow();
 
-                    foreach (string fieldName in collection.IndexedFields)
-                    {
-                        if (d.Contains(fieldName))
-                        {
-                            row[fieldName] = (fieldName == "_id") ? d[fieldName].AsGuid.ToString() : d[fieldName].ToString();
-                        }
-                        else
-                        {
-                            row[fieldName] = String.Empty;
-                        }
-                    }
+            //        foreach (string fieldName in collection.IndexedFields)
+            //        {
+            //            if (d.Contains(fieldName))
+            //            {
+            //                row[fieldName] = (fieldName == "_id") ? d[fieldName].AsGuid.ToString() : d[fieldName].ToString();
+            //            }
+            //            else
+            //            {
+            //                row[fieldName] = String.Empty;
+            //            }
+            //        }
 
-                    dt.Rows.Add(row);
-                }
+            //        dt.Rows.Add(row);
+            //    }
 
-                ds.Tables.Add(dt);
+            //    ds.Tables.Add(dt);
 
-                dgvCollection.DataSource = dt;
-                //dgvCollection.DataMember = ;
-            }
+            //    dgvCollection.DataSource = dt;
+            //    //dgvCollection.DataMember = ;
+            //}
         }
 
         private BsonSqlCollection GetBsonSqlCollection()
         {
-            BsonSqlCollection bsqlCollection = null;
             BsonCollection collection = cbCollection.SelectedItem as BsonCollection;
-            if (cbCollection.SelectedItem != null)
-            {
-                bsqlCollection = GetBsonSqlCollection(collection);
-            }
-
-            return bsqlCollection;
+            return GetBsonSqlCollection(collection);
         }
 
         private BsonSqlCollection GetBsonSqlCollection(BsonCollection collection)
             tabs.SelectedTab = tpItem;
             cbAction.SelectedItem = "lookup";
             txtId.Text = id.ToString();
+            ItemExecute();
+        }
 
+        private void btnUpdateSchemaVersion_Click(object sender, EventArgs e)
+        {
+            UpdateRecordsToLatestSchemaVersion();
+        }
+
+        private void UpdateRecordsToLatestSchemaVersion()
+        {
+            //select top record in table
+            BsonCollection collection = cbCollection.SelectedItem as BsonCollection;
+            var bsqlCollection = GetBsonSqlCollection(collection);
+            //string sqlConnectionString = bsqlCollection.GetConnectionStringFromValues(new Dictionary<string, string> { { "MessageId", Guid.NewGuid().ToString() } });
+            SqlConnection sqlConnection = null;
+
+            try
+            {
+                //Get first document
+                var query = Driver.Builders.QueryBuilder.Where(
+                Driver.Builders.WhereClauseBuilder.CreateBound("SchemaVersion", Driver.Builders.Enums.Comparison.GreaterOrEquals));
+
+                //get one record
+                BsonDocument bsonDocument = bsqlCollection.FindOneAs<BsonDocument>(
+                    query.Bind(new BsonDocument() 
+                        { 
+                            { "SchemaVersion", 0 }
+                        }));
+
+                //save it, updating schema version
+                bsqlCollection.Save<BsonDocument>(bsonDocument);
+
+                Guid sourceId = bsonDocument["_id"].AsGuid;
+
+                string sqlConnectionString = bsqlCollection.GetConnectionStringFromConnectionId("server1");
+                sqlConnection = new SqlConnection(sqlConnectionString);
+
+
+                //connect to sql, get updated schema version 
+                SqlCommand cmd = new SqlCommand(
+                    String.Format("SELECT TOP 1 SchemaVersion FROM {0}_I WHERE BsonDocumentID = @docId", collection.Name), sqlConnection);
+                cmd.Parameters.AddWithValue("@docId", sourceId);
+                sqlConnection.Open();
+                int schemaVersion = Convert.ToInt32(cmd.ExecuteScalar());
+
+                //use schema version as starting point to get records that need to be updated to latest schema
+                SqlCommand cmdUpdateId = new SqlCommand(
+                    String.Format("SELECT TOP 1 BsonDocumentID FROM {0}_I WHERE SchemaVersion < @schemaVersion", collection.Name), sqlConnection);
+                cmdUpdateId.Parameters.AddWithValue("@schemaVersion", schemaVersion);
+
+                bool hasRecords = true;
+
+                while (hasRecords)
+                {
+                    object updateId = cmdUpdateId.ExecuteScalar();
+                    if (updateId == null)
+                    {
+                        hasRecords = false;
+                    }
+                    else
+                    {
+                        Guid recordId = (Guid)updateId;
+                        var record = bsqlCollection.FindOneByIdAs<BsonDocument>(recordId);
+                        bsqlCollection.Save<BsonDocument>(record);
+                    }
+                }
+
+                MessageBox.Show(String.Format("Updated to SchemaVersion: {0}", schemaVersion), "Update Schema Versions");
+            }
+            catch (Exception ex)
+            {
+                MessageBox.Show(ex.ToString(), "Update Schema Versions");
+            }
+            finally
+            {
+                //close Sql Connection
+                if (sqlConnection != null && sqlConnection.State == ConnectionState.Open) { sqlConnection.Close(); }
+            }
         }
     }
 }