示例#1
0
        public void Commit()
        {
            Collection coll = CreateCollection("test");
            var        docs = new[]
            {
                new { _id = 1, title = "Book 1", pages = 20 },
                new { _id = 2, title = "Book 2", pages = 30 },
                new { _id = 3, title = "Book 3", pages = 40 },
                new { _id = 4, title = "Book 4", pages = 50 },
            };

            // start the transaction
            coll.Session.StartTransaction();

            Result r = ExecuteAddStatement(coll.Add(docs));

            Assert.AreEqual(4, r.AffectedItemsCount);

            // now roll it back
            coll.Session.Commit();

            DocResult foundDocs = ExecuteFindStatement(coll.Find());

            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Next());
            Assert.False(foundDocs.Next());
        }
示例#2
0
        public void Commit()
        {
            Collection coll = CreateCollection("test");
            var        docs = new[]
            {
                new { _id = 1, title = "Book 1", pages = 20 },
                new { _id = 2, title = "Book 2", pages = 30 },
                new { _id = 3, title = "Book 3", pages = 40 },
                new { _id = 4, title = "Book 4", pages = 50 },
            };

            // start the transaction
            coll.Session.StartTransaction();

            Result r = coll.Add(docs).Execute();

            Assert.Equal <ulong>(4, r.RecordsAffected);

            // now roll it back
            coll.Session.Commit();

            DocResult foundDocs = coll.Find().Execute();

            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Next());
            Assert.False(foundDocs.Next());
        }
示例#3
0
        public void BindDbDoc()
        {
            Collection coll = CreateCollection("test");
            var        docs = new[]
            {
                new { _id = 1, title = "Book 1", pages = 20 },
                new { _id = 2, title = "Book 2", pages = 30 },
                new { _id = 3, title = "Book 3", pages = 40 },
                new { _id = 4, title = "Book 4", pages = 50 },
            };
            Result r = coll.Add(docs).Execute();

            Assert.Equal <ulong>(4, r.RecordsAffected);

            //var s = MySql.Data.ResourcesX.TestingResources;

            DbDoc     docParams = new DbDoc(new { pages1 = 30, pages2 = 40 });
            DocResult foundDocs = coll.Find("pages = :Pages1 || pages = :Pages2").Bind(docParams).Execute();

            Assert.True(foundDocs.Next());
            Assert.Equal("Book 2", foundDocs.Current["title"]);
            Assert.True(foundDocs.Next());
            Assert.Equal("Book 3", foundDocs.Current["title"]);
            Assert.False(foundDocs.Next());
        }
示例#4
0
        /// <summary>
        /// Asynchronously deletes the record defined by the <paramref name="uniqueId"/> from
        /// the data store further defined by the <paramref name="groupName"/>.
        /// </summary>
        /// <param name="uniqueId">The id of the record in the data store (string)</param>
        /// <param name="groupName">The name of the group the record is stored in (string)</param>
        /// <returns>Task (bool) whether the function executed without exception</returns>
        public async Task <bool> DeleteAsync(string uniqueId, string groupName)
        {
            // Get the session inside a using statement to properly dispose/close.
            using (Session session = MySQLX.GetSession(NOSQLConnection))
            {
                // Get the schema and collection.
                Schema schema     = session.GetSchema(Constants.LogsSchemaName);
                var    collection = schema.GetCollection(Constants.LogsCollectionPrefix + groupName);

                // Asynchronously execute a find on the id field where the value equals the uniqueId.
                DocResult result = await collection.Find($"{Constants.LogsIdField} = :id").Bind("id", uniqueId).ExecuteAsync().ConfigureAwait(false);

                string resultstring = "";

                while (result.Next())
                {
                    resultstring = (string)result.Current[Constants.LogsIdField];
                }

                // If the uniqueId passed to the function was not found in the data store, throw an argument exception.
                if (resultstring.Equals(""))
                {
                    throw new ArgumentException(Constants.LogDeleteDNE);
                }

                // Otherwise asynchronously execute a remove on the id field where the value equals the uniqueId.
                await collection.Remove($"{Constants.LogsIdField} = :id").Bind("id", uniqueId).ExecuteAsync().ConfigureAwait(false);

                return(true);
            }
        }
示例#5
0
        /// <summary>
        /// Method to read all of the snapshots in specific year.
        /// </summary>
        /// <param name="year">Year needed to get all snapshots pertaining to it.</param>
        /// <returns>A list of SnapShotResult objects.</returns>
        public async Task <List <SnapShotResult> > ReadYearlySnapshotAsync(string year)
        {
            using (Session session = MySQLX.GetSession(NOSQLConnection))
            {
                Schema schema = session.GetSchema(Constants.SnapshotSchemaName);

                var collection = schema.GetCollection(Constants.SnapshotCollectionPrefix);

                // Snapshots for specific year.
                DocResult result = await collection.Find($"{Constants.SnapshotMonth} like :_month").Bind("_month", year + "%").ExecuteAsync().ConfigureAwait(false);

                var snapshotList = new List <SnapShotResult>();

                // Making a snapshot object for each result and adding it into a list.
                while (result.Next())
                {
                    var snapshot = new SnapShotResult((string)result.Current[Constants.SnapshotMonth], (string)result.Current[Constants.SnapshotOperationsDict], (string)result.Current[Constants.SnapshotUsersDict], (string)result.Current[Constants.SnapshotTopCityDict],
                                                      (string)result.Current[Constants.SnapshotTopUserUploadedDict], (string)result.Current[Constants.SnapshotTopUploadedIngredientDict], (string)result.Current[Constants.SnapshotTopUploadedStoreDict],
                                                      (string)result.Current[Constants.SnapshotTopSearchedIngredientDict], (string)result.Current[Constants.SnapshotTopSearchedStoreDict], (string)result.Current[Constants.SnapshotTopUpvotedUserDict], (string)result.Current[Constants.SnapshotTopDownvotedUserDict]);
                    snapshotList.Add(snapshot);
                }

                return(snapshotList);
            }
        }
示例#6
0
        public void FindConditional()
        {
            Collection coll = CreateCollection("test");
            var        docs = new[]
            {
                new { _id = 1, title = "Book 1", pages = 20 },
                new { _id = 2, title = "Book 2", pages = 30 },
                new { _id = 3, title = "Book 3", pages = 40 },
                new { _id = 4, title = "Book 4", pages = 50 },
            };
            Result r = coll.Add(docs).Execute();

            Assert.Equal <ulong>(4, r.RecordsAffected);

            DocResult foundDocs = coll.Find("pages = :Pages").Bind("pAges", 40).Execute();

            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Current["title"] == "Book 3");
            Assert.False(foundDocs.Next());
        }
示例#7
0
        public void BindJsonAsString()
        {
            Collection coll = CreateCollection("test");
            var        docs = new[]
            {
                new { _id = 1, title = "Book 1", pages = 20 },
                new { _id = 2, title = "Book 2", pages = 30 },
                new { _id = 3, title = "Book 3", pages = 40 },
                new { _id = 4, title = "Book 4", pages = 50 },
            };
            Result r = coll.Add(docs).Execute();

            Assert.Equal <ulong>(4, r.RecordsAffected);

            var       jsonParams = "{ \"pages1\" : 30, \"pages2\" : 40 }";
            DocResult foundDocs  = coll.Find("pages = :Pages1 || pages = :Pages2").Bind(jsonParams).Execute();

            Assert.True(foundDocs.Next());
            Assert.Equal("Book 2", foundDocs.Current["title"]);
            Assert.True(foundDocs.Next());
            Assert.Equal("Book 3", foundDocs.Current["title"]);
            Assert.False(foundDocs.Next());
        }
示例#8
0
        public void SimpleFindWithLimit()
        {
            Collection coll = CreateCollection("test");
            var        docs = new[]
            {
                new { _id = 1, title = "Book 1", pages = 20 },
                new { _id = 2, title = "Book 2", pages = 30 },
                new { _id = 3, title = "Book 3", pages = 40 },
                new { _id = 4, title = "Book 4", pages = 50 },
            };
            Result r = coll.Add(docs).Execute();

            Assert.Equal <ulong>(4, r.RecordsAffected);

            DocResult foundDocs = coll.Find("pages > 20").Limit(1).Execute();

            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Current["title"] == "Book 2");
            Assert.False(foundDocs.Next());

            // Limit out of range.
            Assert.Throws <ArgumentOutOfRangeException>(() => coll.Find().Limit(0).Execute());
            Assert.Throws <ArgumentOutOfRangeException>(() => coll.Find().Limit(-1).Execute());
        }
示例#9
0
        public void SimpleFindWithSort()
        {
            Collection coll = CreateCollection("test");
            var        docs = new[]
            {
                new { _id = 1, title = "Book 1", pages = 20 },
                new { _id = 2, title = "Book 2", pages = 30 },
                new { _id = 3, title = "Book 3", pages = 40 },
                new { _id = 4, title = "Book 4", pages = 50 },
            };
            Result r = coll.Add(docs).Execute();

            Assert.Equal <ulong>(4, r.RecordsAffected);

            DocResult foundDocs = coll.Find("pages > 20").OrderBy("pages DESC").Execute();

            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Current["title"] == "Book 4");
            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Current["title"] == "Book 3");
            Assert.True(foundDocs.Next());
            Assert.True(foundDocs.Current["title"] == "Book 2");
            Assert.False(foundDocs.Next());
        }
示例#10
0
        /// <summary>
        /// An async method to read all the logs in a specified month.
        /// </summary>
        /// <param name="year">Year needed to get specific month.</param>
        /// <param name="month">Month needed to get specific logs.</param>
        /// <param name="amountOfDays">Amount of days needed for organization.</param>
        /// <returns>A list of list of the LogResult ofbject.</returns>
        public async Task <List <List <LogResult> > > ReadSpecificMonthAsync(string year, string month, int amountOfDays)
        {
            List <List <LogResult> > logsForMonth = new List <List <LogResult> >();

            // Get the session inside a using statement to properly dispose/close.
            using (Session session = MySQLX.GetSession(NOSQLConnection))
            {
                // Get log schema.
                Schema schema = session.GetSchema(Constants.LogsSchemaName);

                // Use the amountOfDays variable to loop to get dates.
                for (int date = 1; date < amountOfDays + 1; date++)
                {
                    // Make group name based on year and month.
                    string groupName = year + month;
                    string day       = "";

                    if (date < 10)
                    {
                        // If the date is less than 10, a "0" has to be added for formatting purposes.
                        day += "0";
                    }
                    // Convert date into a string for use.
                    day += Convert.ToString(date);

                    groupName += day;

                    // Get logs for a specific day.
                    var collection = schema.GetCollection(Constants.LogsCollectionPrefix + groupName);

                    // Need to check if collection exists beforehand.
                    List <LogResult> logsForDay = new List <LogResult>();
                    if (collection.ExistsInDatabase())
                    {
                        DocResult result = await collection.Find().ExecuteAsync().ConfigureAwait(false);

                        while (result.Next())
                        {
                            LogResult logObject = new LogResult((string)result.Current[Constants.LogsTimestampField], (string)result.Current[Constants.LogsOperationField],
                                                                (string)result.Current[Constants.LogsIdentifierField], (string)result.Current[Constants.LogsIPAddressField], (string)result.Current[Constants.LogsErrorTypeField]);
                            logsForDay.Add(logObject);
                        }
                    }
                    logsForMonth.Add(logsForDay);
                }
            }
            return(logsForMonth);
        }
示例#11
0
        /// <summary>
        /// Find the id of the record that was inserted into the data store.
        /// </summary>
        /// <param name="record">The record to find (INOSQLRecord)</param>
        /// <param name="groupName">The name of the group where the record is located (string)</param>
        /// <returns>Task (string), the id of the record</returns>
        public async Task <string> FindIdFieldAsync(INOSQLRecord record, string groupName)
        {
            // Try converting the INOSQLRecord to a LogRecord, if it failed throw an argument exception.
            try
            {
                LogRecord temp = (LogRecord)record;
            }
            catch
            {
                throw new ArgumentException(Constants.LogFindInvalidArgument);
            }

            LogRecord logRecord = (LogRecord)record;

            // Get the session inside a using statement to properly dispose/close.
            using (Session session = MySQLX.GetSession(NOSQLConnection))
            {
                // Get the schema and collection.
                Schema schema     = session.GetSchema(Constants.LogsSchemaName);
                var    collection = schema.GetCollection(Constants.LogsCollectionPrefix + groupName);

                // The items of the log record which all need to be found.
                var documentParams = new DbDoc(new { timestamp = logRecord.Timestamp, operation = logRecord.Operation, identifier = logRecord.Identifier, ip = logRecord.IPAddress });

                // Asynchronously execute a find on the fields that correspond to the parameter values above.
                DocResult result = await collection.Find($"{Constants.LogsTimestampField} = :timestamp && {Constants.LogsOperationField} = :operation && {Constants.LogsIdentifierField} = :identifier && {Constants.LogsIPAddressField} = :ip").Bind(documentParams).ExecuteAsync().ConfigureAwait(false);

                // Prepare string to be returned
                string resultstring = "";

                while (result.Next())
                {
                    resultstring = (string)result.Current[Constants.LogsIdField];
                }

                if (resultstring.Equals(""))
                {
                    throw new ArgumentException(Constants.LogFindDNE);
                }

                return(resultstring);
            }
        }
示例#12
0
        /// <summary>
        /// Method to get all the specific months of existing snapshots.
        /// </summary>
        /// <returns></returns>
        public async Task <List <string> > GetYearAndMonthAsync()
        {
            var yearMonthList = new List <string>();

            using (Session session = MySQLX.GetSession(NOSQLConnection))
            {
                Schema schema = session.GetSchema(Constants.SnapshotSchemaName);

                var collection = schema.GetCollection(Constants.SnapshotCollectionPrefix);

                // Get all the snapshots in the collection.
                DocResult result = await collection.Find().ExecuteAsync().ConfigureAwait(false);

                while (result.Next())
                {
                    var stringYearMonth = (string)result.Current[Constants.SnapshotMonth];
                    yearMonthList.Add(stringYearMonth);
                }
            }
            return(yearMonthList);
        }
示例#13
0
        /// <summary>
        /// Method to read a snapshot pertaining to a specific month.
        /// </summary>
        /// <param name="year">Year needed to get specific snapshot.</param>
        /// <param name="month">Month needed to get specific snapshot.</param>
        /// <returns>A SnapShotResult object that has all the information.</returns>
        public async Task <SnapShotResult> ReadMonthlySnapshotAsync(string year, string month)
        {
            using (Session session = MySQLX.GetSession(NOSQLConnection))
            {
                Schema schema = session.GetSchema(Constants.SnapshotSchemaName);

                string specificMonth = year + month;

                var collection = schema.GetCollection(Constants.SnapshotCollectionPrefix);
                // Snapshot for specific month.
                DocResult result = await collection.Find($"{Constants.SnapshotMonth} = :_month").Bind("_month", specificMonth).ExecuteAsync().ConfigureAwait(false);

                // Making a snapshot object with the result.
                result.Next();

                var snapshot = new SnapShotResult((string)result.Current[Constants.SnapshotMonth], (string)result.Current[Constants.SnapshotOperationsDict], (string)result.Current[Constants.SnapshotUsersDict], (string)result.Current[Constants.SnapshotTopCityDict],
                                                  (string)result.Current[Constants.SnapshotTopUserUploadedDict], (string)result.Current[Constants.SnapshotTopUploadedIngredientDict], (string)result.Current[Constants.SnapshotTopUploadedStoreDict],
                                                  (string)result.Current[Constants.SnapshotTopSearchedIngredientDict], (string)result.Current[Constants.SnapshotTopSearchedStoreDict], (string)result.Current[Constants.SnapshotTopUpvotedUserDict], (string)result.Current[Constants.SnapshotTopDownvotedUserDict]);

                return(snapshot);
            }
        }