/// <summary>
            /// Checks whether the row version for a sales transaction is valid or not.
            /// </summary>
            /// <param name="context">The database context.</param>
            /// <param name="transactionId">The sales transaction identifier.</param>
            /// <param name="rowversion">The row version value.</param>
            /// <remarks>An exception is thrown for a invalid row version.</remarks>
            /// <returns>Transaction's created date time.</returns>
            private static DateTime RetrieveTransactionCreatedDateOrThrowOnInvalidRowVersion(SqliteDatabaseContext context, string transactionId, byte[] rowversion)
            {
                const string GetRowVersionQueryString =
                    "SELECT {0}, {1} FROM [crt].SALESTRANSACTION WHERE TRANSACTIONID = @TRANSACTIONID;";

                var query = new SqlQuery(GetRowVersionQueryString, RowversionColumnName, CreatedDateTimeColumName);

                query.Parameters["@TRANSACTIONID"] = transactionId;

                byte[] savedRowverion = null;

                // default created time as utc now
                DateTime createdDateTime = DateTime.UtcNow;

                SalesTransactionData transactionData = context.ReadEntity <SalesTransactionData>(query).FirstOrDefault();

                if (transactionData != null)
                {
                    savedRowverion = transactionData.Version;

                    // update created date time value in case it exists in the db
                    createdDateTime = transactionData.CreatedDateTime.DateTime;
                }

                // null is fine since the record does not exist yet, otherwise compare to see if they match
                if (savedRowverion != null && !RowVersionHelper.AreEquals(rowversion, savedRowverion))
                {
                    throw new StorageException(StorageErrors.Microsoft_Dynamics_Commerce_Runtime_ObjectVersionMismatchError, "Version mismatch while saving transaction.");
                }

                return(createdDateTime);
            }
            /// <summary>
            /// Creates and adds a data row based on the sales transaction data contents.
            /// </summary>
            /// <param name="table">The data table to be populated.</param>
            /// <param name="salesTransactionData">The sales transaction data.</param>
            /// <param name="createdDateTime">The transaction's created date time.</param>
            private static void AddSalesTransactionDataRow(DataTable table, SalesTransactionData salesTransactionData, DateTime createdDateTime)
            {
                // no out of the box rowversion support in sqlite, increment value here
                salesTransactionData.Version = RowVersionHelper.Increment(salesTransactionData.Version);

                DataRow row = salesTransactionData.CreateDataRow(table);

                // all fields must be set otherwise the insert or update query will clear them out
                row[CreatedDateTimeColumName]   = createdDateTime;
                row[ModifiedDateTimeColumnName] = DateTime.UtcNow;

                table.Rows.Add(row);
            }
Ejemplo n.º 3
0
            private static NullResponse SaveCart(SaveCartDataRequest request)
            {
                ThrowIf.Null(request, "request");
                ThrowIf.Null(request.SalesTransactions, "request.SalesTransactions");

                var salesTransactionDataCollection = SalesTransactionConverter.ConvertToData(request.SalesTransactions, serializeData: true);

                using (DataTable table = new DataTable(SalesTransactionTableTypeName))
                {
                    SalesTransactionData.FillSchemaForSave(table);
                    foreach (SalesTransactionData salesTransactionData in salesTransactionDataCollection)
                    {
                        table.Rows.Add(salesTransactionData.CreateDataRow(table));
                    }

                    ParameterSet parameters = new ParameterSet();
                    parameters[SalesTransactionTableTypeVariableName] = table;
                    parameters["@b_IgnoreRowVersionCheck"]            = request.IgnoreRowVersionCheck;

                    try
                    {
                        int errorCode;
                        using (var databaseContext = new SqlServerDatabaseContext(request))
                        {
                            errorCode = databaseContext.ExecuteStoredProcedureNonQuery(SaveSalesTransactionSprocName, parameters);
                        }

                        if (errorCode == (int)DatabaseErrorCodes.VersionMismatchError)
                        {
                            throw new StorageException(StorageErrors.Microsoft_Dynamics_Commerce_Runtime_ObjectVersionMismatchError, errorCode, "Version mismatch while saving transaction.");
                        }

                        if (errorCode != (int)DatabaseErrorCodes.Success)
                        {
                            throw new StorageException(StorageErrors.Microsoft_Dynamics_Commerce_Runtime_CriticalStorageError, errorCode, "Unable to save transactions.");
                        }
                    }
                    catch (DatabaseException databaseException)
                    {
                        throw new StorageException(
                                  StorageErrors.Microsoft_Dynamics_Commerce_Runtime_CriticalStorageError,
                                  (int)databaseException.ErrorCode,
                                  databaseException,
                                  "Unable to save transactions.");
                    }
                }

                return(new NullResponse());
            }
            /// <summary>
            /// Creates a data table for sales transaction.
            /// </summary>
            /// <returns>The data table for the sales transaction table.</returns>
            private static DataTable CreateSalesTransactionDataTable()
            {
                DataTable table = new DataTable(SalesTransactionTableName);

                SalesTransactionData.FillSchemaForSave(table);

                // add fields not present in the default schema
                DataColumn createdDateTimeColumn = new DataColumn(CreatedDateTimeColumName, typeof(DateTime))
                {
                    DefaultValue = DateTime.UtcNow
                };

                table.Columns.Add(createdDateTimeColumn);
                table.Columns.Add(ModifiedDateTimeColumnName, typeof(DateTime));

                return(table);
            }