Example #1
0
        public override void PerformDBAction()
        {
            ValueExpression VE = new ValueExpression(Db.ProjEnvironment, Db.BusinessFlow, Db.DSList);

            VE.Value = Act.SQL;
            string SQLCalculated  = VE.ValueCalculated;
            string collectionName = "";

            if (Action == Actions.ActDBValidation.eDBValidationType.SimpleSQLOneValue)
            {
                collectionName = Act.Table;
            }
            else
            {
                collectionName = GetCollectionName(SQLCalculated);
            }
            var DB         = mMongoClient.GetDatabase(DbName);
            var collection = DB.GetCollection <BsonDocument>(collectionName);

            try
            {
                switch (Action)
                {
                case Actions.ActDBValidation.eDBValidationType.FreeSQL:

                    if (SQLCalculated.Contains("insertMany"))
                    {
                        string queryParam = GetUpdateQueryParams(SQLCalculated).ToString();
                        Newtonsoft.Json.Linq.JArray jsonArray = Newtonsoft.Json.Linq.JArray.Parse(queryParam);
                        List <BsonDocument>         documents = new List <BsonDocument>();
                        foreach (Newtonsoft.Json.Linq.JObject obj in jsonArray.Children <Newtonsoft.Json.Linq.JObject>())
                        {
                            BsonDocument document = BsonDocument.Parse(obj.ToString());
                            documents.Add(document);
                        }
                        collection.InsertMany(documents);
                    }
                    else if (SQLCalculated.Contains("insertOne") || SQLCalculated.Contains("insert"))
                    {
                        BsonDocument insertDocumnet = BsonDocument.Parse(GetUpdateQueryParams(SQLCalculated));
                        collection.InsertOne(insertDocumnet);
                    }
                    else
                    {
                        var result = collection.Find(GetQueryParamater(SQLCalculated, "find")).
                                     Project(Builders <BsonDocument> .Projection.Exclude("_id").Exclude(GetQueryParamater(SQLCalculated, "projection"))).
                                     Sort(BsonDocument.Parse(GetQueryParamater(SQLCalculated, "sort"))).
                                     Limit(Convert.ToInt32(GetQueryParamater(SQLCalculated, "limit"))).
                                     ToList();

                        var obj = result.ToJson();
                        Act.ParseJSONToOutputValues(obj.ToString(), 1);
                    }
                    break;

                case Actions.ActDBValidation.eDBValidationType.RecordCount:
                    var count = collection.Count(new BsonDocument());
                    Act.AddOrUpdateReturnParamActual("Record Count", count.ToString());
                    break;

                case Actions.ActDBValidation.eDBValidationType.UpdateDB:

                    //do commit
                    if (Act.CommitDB_Value == true)
                    {
                        var session = mMongoClient.StartSession();
                        session.StartTransaction();
                        UpdateCollection(SQLCalculated, collection);
                        session.CommitTransaction();
                    }
                    else
                    {
                        UpdateCollection(SQLCalculated, collection);
                    }
                    break;

                case Actions.ActDBValidation.eDBValidationType.SimpleSQLOneValue:
                    string col = Act.Column;
                    string where = Act.Where;
                    string filter    = "";
                    var    isNumeric = double.TryParse(where, out double n);

                    //Simply matches on specific column type int
                    //For ex where contains any int value
                    if (isNumeric)
                    {
                        filter = "{" + col + ":" + where + "}";
                    }
                    else
                    {
                        //Equality matches on the whole embedded document require an exact match of the specified <value> document, including the field order
                        //For ex where contains value = {field1:_value1,field2:_value2,field3:"_value3",...}
                        if (where.Contains(","))
                        {
                            filter = "{" + col + ":" + where + "}";
                        }
                        //Simply matches on specific column
                        //For ex where contains any string value
                        else
                        {
                            filter = "{" + col + ":\"" + where + "\"}";
                        }
                    }
                    var resultSimpleSQLOne = collection.Find(filter).Project(Builders <BsonDocument> .Projection.Exclude("_id")).ToList().ToJson();
                    Act.ParseJSONToOutputValues(resultSimpleSQLOne.ToString(), 1);
                    break;

                default:
                    Act.Error += "Operation Type " + Action + " is not yes supported for Mongo DB";
                    break;
                }
            }
            catch (Exception e)
            {
                Act.Error = "Failed to execute. Error :" + e.Message;
                Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {e.Message}", e);
            }
            if (!Db.KeepConnectionOpen)
            {
                Disconnect();
            }
        }
Example #2
0
        public override void PerformDBAction()
        {
            Connect();
            string          SQL      = Act.SQL;
            string          keyspace = Act.Keyspace;
            ValueExpression VE       = new ValueExpression(Db.ProjEnvironment, Db.BusinessFlow, Db.DSList);

            VE.Value = SQL;
            string SQLCalculated = VE.ValueCalculated;

            try
            {
                switch (Action)
                {
                case Actions.ActDBValidation.eDBValidationType.FreeSQL:
                    //To Use JSON Appraoch only if Cassandra version is 2.2 or onwards
                    try
                    {
                        if (IfUseJSON() && (!SQLCalculated.ToLower().Contains("select json ")))
                        {
                            if (SQLCalculated.ToLower().Contains("select"))
                            {
                                SQLCalculated = Regex.Replace(SQLCalculated, "select", "select json ", RegexOptions.IgnoreCase);
                            }
                            RowSet RS = session.Execute(SQLCalculated);
                            UpdateOutValues(RS);
                        }
                        else
                        {
                            RowSet RS = session.Execute(SQLCalculated);
                            UpdateRowSetWithUDT(RS, SQLCalculated);
                        }
                    }
                    catch (Exception e)
                    {
                        Act.Error = "Please check the version of the Database and update on the Environment(by default it will take 2.2)" + e;
                        Reporter.ToLog(eLogLevel.ERROR, e.Message);
                    }
                    break;

                case Actions.ActDBValidation.eDBValidationType.RecordCount:

                    string SQLRecord = keyspace + "." + SQLCalculated;
                    int    count     = GetRecordCount(SQLRecord);
                    Act.AddOrUpdateReturnParamActual("Record Count", count.ToString());
                    break;

                case Actions.ActDBValidation.eDBValidationType.SimpleSQLOneValue:
                    string where = Act.Where;
                    string table = Act.Table;
                    string col   = Act.Column;
                    string sql   = null;
                    try
                    {
                        if (IfUseJSON())
                        {
                            sql = "Select json " + col + " from " + keyspace + "." + table + " where " + where + ";";
                            RowSet R = session.Execute(sql);

                            UpdateOutValues(R);
                        }
                        else
                        {
                            sql = "Select " + col + " from " + keyspace + "." + table + " where " + where + ";";
                            RowSet R = session.Execute(sql);

                            UpdateRowSetWithUDT(R, sql);
                        }
                    }
                    catch (Exception e)
                    {
                        Act.Error = "Please check the version of the Database and update on the Environment(by default it will take 2.2)";
                        Reporter.ToLog(eLogLevel.ERROR, e.Message);
                    }
                    break;

                case Actions.ActDBValidation.eDBValidationType.UpdateDB:
                    RowSet RS1 = session.Execute(SQLCalculated);
                    break;

                default:
                    throw new Exception("Action Not SUpported");
                }
            }
            catch (Exception e)
            {
                Act.Error = "Failed to execute";
                Reporter.ToLog(eLogLevel.ERROR, $"Method - {MethodBase.GetCurrentMethod().Name}, Error - {e.Message}");
            }
            if (!Db.KeepConnectionOpen)
            {
                Disconnect();
            }
        }