Example #1
0
 /// <summary>
 /// overloaded version without sequence, but with ASelectedOperations
 /// </summary>
 public static void SubmitChanges(
     TTypedDataTable ATable,
     TDBTransaction ATransaction,
     eSubmitChangesOperations ASelectedOperations,
     string AUserId)
 {
     SubmitChanges(ATable, ATransaction, ASelectedOperations, AUserId, "", "");
 }
Example #2
0
        /// <summary>
        /// submit those rows in the table that have been modified or created or deleted
        /// </summary>
        /// <param name="ATable"></param>
        /// <param name="ATransaction"></param>
        /// <param name="ASelectedOperations"></param>
        /// <param name="AUserId">the current user, for auditing</param>
        /// <param name="ASequenceName"></param>
        /// <param name="ASequenceField"></param>
        /// <returns></returns>
        public static void SubmitChanges(
            TTypedDataTable ATable,
            TDBTransaction ATransaction,
            eSubmitChangesOperations ASelectedOperations,
            string AUserId,
            string ASequenceName, string ASequenceField)
        {
            // allow this method to be called with null values, eg. when saving complex TypedDataSets with some removed empty tables
            if (ATable == null)
            {
                return;
            }

            short TableId = Convert.ToInt16(ATable.GetType().GetField("TableId").GetValue(null));

            if (!ATable.ThrowAwayAfterSubmitChanges && !ATable.DontThrowAwayAfterSubmitChanges && (ATable.Rows.Count > 1000))
            {
                TLogging.Log(
                    "Warning to the developer: Saving " + ATable.Rows.Count.ToString() + " records to table " + ATable.TableName +
                    " without ThrowAwayAfterSubmitChanges is quite slow");
                TLogging.Log(
                    "It is recommended to use either ThrowAwayAfterSubmitChanges and Reload all data at once, or GetChanges to have less queries for updated modification timestamp");
                TLogging.LogStackTrace(TLoggingType.ToLogfile);
            }

            List <OdbcParameter>InsertParameters = new List <OdbcParameter>();
            StringBuilder InsertStatement = new StringBuilder();

            for (RowCount = 0; (RowCount != ATable.Rows.Count); RowCount++)
            {
                DataRow TheRow = ATable.Rows[RowCount];

                if ((TheRow.RowState == DataRowState.Added) && ((ASelectedOperations & eSubmitChangesOperations.eInsert) != 0))
                {
                    bool TreatRowAsAdded = false;

                    if (ASequenceField.Length > 0)
                    {
                        // only insert next sequence value if the field has negative value.
                        // this is needed when creating location 0 for a new site/ledger
                        if (Convert.ToInt64(TheRow[ASequenceField]) < 0)
                        {
                            // accept changes for the row, so that we can update the dataset on the client and still know the negative temp sequence number
                            TheRow.AcceptChanges();
                            TheRow[ASequenceField] = (System.Object)DBAccess.GDBAccessObj.GetNextSequenceValue(ASequenceName, ATransaction);
                            TreatRowAsAdded = true;   // setting this variable to 'true' is *vital* for the retrieval of the s_modification_id_t for that record once it is saved!
                        }
                    }

                    if (ATable.ThrowAwayAfterSubmitChanges)
                    {
                        TTypedDataAccess.InsertRow(TableId, ref TheRow, AUserId, InsertStatement, InsertParameters);
                    }
                    else
                    {
                        TTypedDataAccess.InsertRow(TableId, ref TheRow, ATransaction, AUserId, TreatRowAsAdded);
                    }
                }
                else
                {
                    bool hasPrimaryKey = TTypedDataTable.GetPrimaryKeyColumnOrdList(TableId).Length > 0;

                    if ((TheRow.RowState == DataRowState.Modified) && ((ASelectedOperations & eSubmitChangesOperations.eUpdate) != 0))
                    {
                        if (!hasPrimaryKey)
                        {
                            throw new EDBSubmitException(
                                "[TTypedDataAccess.SubmitChanges] NO PRIMARY KEY --- Cannot update record because table " +
                                TTypedDataTable.GetTableName(TableId) + " has no primary key.",
                                eSubmitChangesOperations.eUpdate);
                        }
                        else
                        {
                            TTypedDataAccess.UpdateRow(TableId, ATable.ThrowAwayAfterSubmitChanges, ref TheRow, ATransaction, AUserId);
                        }
                    }

                    if ((TheRow.RowState == DataRowState.Deleted) && ((ASelectedOperations & eSubmitChangesOperations.eDelete) != 0))
                    {
                        if (!hasPrimaryKey)
                        {
                            throw new EDBSubmitException(
                                "[TTypedDataAccess.SubmitChanges] NO PRIMARY KEY --- Cannot delete record because table " +
                                TTypedDataTable.GetTableName(TableId) + " has no primary key.",
                                eSubmitChangesOperations.eDelete);
                        }
                        else
                        {
                            TTypedDataAccess.DeleteRow(TableId, TheRow, ATransaction);
                        }
                    }
                }

                if (InsertParameters.Count > MAX_SQL_PARAMETERS)
                {
                    // Inserts in one query
                    if (0 == DBAccess.GDBAccessObj.ExecuteNonQuery(InsertStatement.ToString(), ATransaction, InsertParameters.ToArray()))
                    {
                        throw new EDBSubmitException("[TTypedDataAccess.SubmitChanges] Problems INSERTing a row [#1]",
                            eSubmitChangesOperations.eInsert);
                    }

                    InsertStatement = new StringBuilder();
                    InsertParameters = new List <OdbcParameter>();
                }
            }

            if (InsertStatement.Length > 0)
            {
                // Inserts in one query
                if (0 == DBAccess.GDBAccessObj.ExecuteNonQuery(InsertStatement.ToString(), ATransaction, InsertParameters.ToArray()))
                {
                    throw new EDBSubmitException("[TTypedDataAccess.SubmitChanges] Problems INSERTing a row [#2]", eSubmitChangesOperations.eInsert);
                }
            }
        }