Ejemplo n.º 1
0
        // /////////////////////////////////////////////////////////////////////////////
        public CResultAErreur DoDelete(DataRow row)
        {
            string strRequete = "Delete from " + m_structure.NomTableInDb + " where ";
            string strWhere   = "";

            foreach (CInfoChampTable champ in m_structure.Champs)
            {
                strWhere += champ.NomChamp + "=" + GetStringForRequete(row[champ.NomChamp, DataRowVersion.Original]) + " and ";
            }
            if (strWhere.Length > 0)
            {
                //Supprime le and final
                strWhere = strWhere.Substring(0, strWhere.Length - 5);
            }
            strRequete += strWhere;
            CResultAErreur result = m_connexion.RunStatement(strRequete);

            if (result && result.Data is int)
            {
                if ((int)result.Data == 0)                  //Pas de lignes mises à jour
                {
                    throw new Exception(CObjetDonnee.GetMessageAccesConccurentiel(row));
                }
                //throw new Exception(I.T("Another program has modified the data.|103"));
            }
            return(result);
        }
Ejemplo n.º 2
0
 public int Update(DataSet dataSet)
 {
     try
     {
         return(m_adapter.Update(dataSet));
     }
     catch (DBConcurrencyException e)
     {
         throw new Exception(CObjetDonnee.GetMessageAccesConccurentiel(e.Row));
     }
 }
Ejemplo n.º 3
0
        // /////////////////////////////////////////////////////////////////////////////
        public virtual CResultAErreur DoUpdate(DataRow row)
        {
            string strRequete = "update " + m_structure.NomTableInDb + " set ";
            string strSet     = "";
            string strWhere   = "";

            foreach (CInfoChampTable champ in m_structure.Champs)
            {
                if (!champ.IsId && !IsExclu(champ.NomChamp))
                {
                    strSet += champ.NomChamp + "=" + GetStringForRequete(row[champ.NomChamp, System.Data.DataRowVersion.Current]) + ",";
                }

                if (!IsExclu(champ.NomChamp))
                {
                    strWhere += champ.NomChamp + "=" + GetStringForRequete(row[champ.NomChamp, DataRowVersion.Original]) + " and ";
                }
            }
            if (strSet.Length > 0)
            {
                //Supprime la virgule finale
                strSet = strSet.Substring(0, strSet.Length - 1);
                //Supprime le and final
                strWhere = strWhere.Substring(0, strWhere.Length - 5);
            }
            strRequete += strSet + " where " + strWhere;
            CResultAErreur result = m_connexion.RunStatement(strRequete);

            if (result && result.Data is int)
            {
                if ((int)result.Data == 0)//Pas de lignes mises à jour
                {
                    throw new Exception(CObjetDonnee.GetMessageAccesConccurentiel(row));
                }
                //throw new Exception(I.T("Another program has modified the data.|103"));
            }
            return(result);
        }
Ejemplo n.º 4
0
        private int ExecuteCommande(DataRow dr)
        {
            int nbLigneAffectes = 0;

            try
            {
                switch (dr.RowState)
                {
                case DataRowState.Added:
                    if (!m_adapter.InsertCommand.CommandText.ToUpper().Contains("INSERT"))    //Pas de commande
                    {
                        nbLigneAffectes++;
                    }
                    else
                    {
                        nbLigneAffectes += ExecuteCommande(m_adapter.InsertCommand, dr);
                    }
                    break;

                case DataRowState.Deleted:
                    if (!m_adapter.DeleteCommand.CommandText.ToUpper().Contains("DELETE"))    //Pas de commande
                    {
                        nbLigneAffectes++;
                    }
                    else
                    {
                        nbLigneAffectes += ExecuteCommande(m_adapter.DeleteCommand, dr);
                    }
                    break;

                case DataRowState.Modified:
                    if (!m_adapter.UpdateCommand.CommandText.ToUpper().Contains("UPDATE"))    //Pas de commande
                    {
                        nbLigneAffectes++;
                    }
                    else
                    {
                        nbLigneAffectes += ExecuteCommande(m_adapter.UpdateCommand, dr);
                    }
                    break;

                case DataRowState.Detached:
                case DataRowState.Unchanged:
                default:
                    break;
                }
            }
            catch (Exception e)
            {
                StringBuilder  bl      = new StringBuilder();
                DataRowVersion version = DataRowVersion.Current;
                if (dr.RowState == DataRowState.Deleted)
                {
                    version = DataRowVersion.Original;
                }
                foreach (DataColumn col in dr.Table.Columns)
                {
                    try
                    {
                        bl.Append(col.ColumnName);
                        bl.Append('=');
                        object val = dr[col, version];
                        if (val != null)
                        {
                            bl.Append(val.ToString());
                        }
                        else
                        {
                            bl.Append("null");
                        }
                        bl.Append('\t');
                    }
                    catch
                    {
                    }
                }
                string    strMes   = "Error on row \r\n" + bl.ToString() + "\r\n" + e.ToString();
                Exception exDetail = new Exception(strMes, e);
                throw (exDetail);
            }
            if (nbLigneAffectes != 1 && dr.Table.TableName != sc2i.data.CVersionDonnees.c_nomTable)
            {
                throw new Exception(CObjetDonnee.GetMessageAccesConccurentiel(dr));
            }
            //   throw new Exception(I.T("Another program has modified the data.|103"));
            return(nbLigneAffectes);
        }
        /// /////////////////////////////////////////////////////////////////
        public int Update(DataSet ds)
        {
            DataTable table = ds.Tables[CContexteDonnee.GetNomTableForType(m_typeDonnees)];
            ArrayList listeRelationsAutoReference = new ArrayList();

            foreach (DataRelation relation in table.ParentRelations)
            {
                if (relation.ParentTable == table &&
                    relation.ChildTable == table)
                {
                    listeRelationsAutoReference.Add(relation);
                    if (relation.ParentColumns.Length != 1 ||
                        relation.ChildColumns.Length != 1)
                    {
                        throw new Exception(I.T("Auto referred tables on several keys aren't allowed|102"));
                    }
                }
            }
            if (listeRelationsAutoReference.Count == 0)
            {
                return(m_adapter.Update(ds));
            }
            if (table == null)
            {
                return(0);
            }
            int nNb = 0;

            //Trie les éléments pour modifs et ajouts
            //Il faut modifier en premier les lignes qui sont dépendantes des autres

            /*Pour trier : On regarde  les fils de chaque ligne à modifier. On place
             * Chaque ligne après ses fils dans la liste. A la fin, on inverse la liste
             * */
            ArrayList        listeToAddOrUpdate = new ArrayList();
            CArbreDependance arbreTotal         = new CArbreDependance(null);
            Hashtable        tableRowToArbre    = new Hashtable();

            foreach (DataRow row in table.Rows)
            {
                if (row.RowState == DataRowState.Added || row.RowState == DataRowState.Modified)
                {
                    foreach (DataRelation relation in listeRelationsAutoReference)
                    {
                        CArbreDependance arbre = tableRowToArbre[row] as CArbreDependance;
                        if (arbre == null)
                        {
                            arbre = new CArbreDependance(row);
                        }
                        tableRowToArbre[row] = arbre;

                        //Cherche l'arbreParent;
                        DataRow          rowParente  = row.GetParentRow(relation);
                        CArbreDependance arbreParent = arbreTotal;
                        if (rowParente != null)
                        {
                            arbreParent = (CArbreDependance)tableRowToArbre[rowParente];
                            if (arbreParent == null)
                            {
                                arbreParent = arbreTotal;
                            }
                        }
                        arbre.ArbreParent = arbreParent;
                        foreach (DataRow rowFille in row.GetChildRows(relation))
                        {
                            CArbreDependance arbreFille = (CArbreDependance)tableRowToArbre[rowFille];
                            if (arbreFille != null)
                            {
                                arbreFille.ArbreParent = arbre;
                            }
                        }
                    }
                }
            }
            arbreTotal.InsertFillesIntoArrayList(listeToAddOrUpdate);

            foreach (DataRow row in listeToAddOrUpdate)
            {
                string     strLastId = row[0].ToString();
                IDbCommand command   = null;
                if (row.RowState == DataRowState.Added)
                {
                    command = InsertCommand;
                }
                if (row.RowState == DataRowState.Modified)
                {
                    command = UpdateCommand;
                }
                if (command != null)
                {
                    bool bForceOpen = false;
                    if (command.Connection.State != ConnectionState.Open)
                    {
                        bForceOpen = true;
                        command.Connection.Open();
                    }
                    int nUpdateOrAdd = 0;
                    if (row.RowState == DataRowState.Added)
                    {
                        if (command.CommandText.ToUpper().Contains("INSERT"))
                        {
                            nUpdateOrAdd = ExecuteAdd(command, row);
                        }
                        else
                        {
                            nUpdateOrAdd++;
                        }
                    }
                    else
                    {
                        if (command.CommandText.ToUpper().Contains("UPDATE"))
                        {
                            nUpdateOrAdd = ExecuteUpdate(command, row);
                        }
                        else
                        {
                            nUpdateOrAdd++;
                        }
                    }

                    if (nUpdateOrAdd != 1)
                    {
                        StringBuilder bl = new StringBuilder();
                        bl.Append(CObjetDonnee.GetMessageAccesConccurentiel(row));
                        //I.T("Another program has modified the data.|103"));
                        bl.Append("\r\n");
                        if (row.HasVersion(DataRowVersion.Original))
                        {
                            foreach (DataColumn col in row.Table.Columns)
                            {
                                try
                                {
                                    bl.Append(row[col, DataRowVersion.Original].ToString());
                                    bl.Append(" / ");
                                    bl.Append(row[col].ToString());
                                    bl.Append("\r\n");
                                }
                                catch
                                {
                                }
                            }
                        }
                        throw new Exception(bl.ToString());
                    }
                    nNb += nUpdateOrAdd;
                    if (bForceOpen)
                    {
                        command.Connection.Close();
                    }
                }
            }

            //traite les suppression
            //Il faut supprimer en premier les lignes qui ne sont pas dépendantes des autres

            /*Pour trier : On regarde pour les fils de chaque ligne à supprimer. On place
             * Chaque ligne après ses fils dans la liste
             * */
            /*Stef 25/03 : changement de l'algo : utilisation d'un arbre
             * */
            ArrayList listeToDelete = new ArrayList();

            arbreTotal      = new CArbreDependance(null);
            tableRowToArbre = new Hashtable();
            foreach (DataRow row in table.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    foreach (DataRelation relation in listeRelationsAutoReference)
                    {
                        CArbreDependance arbre = tableRowToArbre[row] as CArbreDependance;
                        if (arbre == null)
                        {
                            arbre = new CArbreDependance(row);
                        }
                        tableRowToArbre[row] = arbre;

                        //Cherche l'arbreParent;
                        DataRow          rowParente  = row.GetParentRow(relation, DataRowVersion.Original);
                        CArbreDependance arbreParent = arbreTotal;
                        if (rowParente != null)
                        {
                            arbreParent = (CArbreDependance)tableRowToArbre[rowParente];
                            if (arbreParent == null)
                            {
                                arbreParent = arbreTotal;
                            }
                        }
                        arbre.ArbreParent = arbreParent;
                        foreach (DataRow rowFille in row.GetChildRows(relation, DataRowVersion.Original))
                        {
                            CArbreDependance arbreFille = (CArbreDependance)tableRowToArbre[rowFille];
                            if (arbreFille != null)
                            {
                                arbreFille.ArbreParent = arbre;
                            }
                        }
                    }
                }
            }
            arbreTotal.InsertFillesIntoArrayList(listeToDelete);
            listeToDelete.Reverse();

            /*foreach ( DataRow row in table.Rows )
             * {
             *      if ( row.RowState == DataRowState.Deleted )
             *      {
             *              Hashtable tableFilles = new Hashtable();
             *              foreach ( DataRelation relation in listeRelationsAutoReference )
             *              {
             *                      DataRow[] childs = table.Select ( relation.ChildColumns[0]+"="+
             *                              row[relation.ParentColumns[0], DataRowVersion.Original], null, DataViewRowState.Deleted );
             *                      foreach ( DataRow rowFille in childs )
             *                              tableFilles[rowFille] = true;
             *              }
             *              int nPosInsert = 0;
             *              for ( int nLook = 0; nLook < listeToDelete.Count && tableFilles.Count > 0; nLook++ )
             *              {
             *                      if ( tableFilles[listeToDelete[nLook]] != null )
             *                      {
             *                              nPosInsert = nLook+1;
             *                              tableFilles.Remove(listeToDelete[nLook]);
             *                      }
             *              }
             *              listeToDelete.Insert ( nPosInsert, row );
             *      }
             * }*/
            IDbCommand deleteCommand = DeleteCommand;

            if (deleteCommand.CommandText.ToUpper().Contains("DELETE"))
            {
                foreach (DataRow row in listeToDelete)
                {
                    if (row.RowState == DataRowState.Deleted)
                    {
                        nNb += ExecuteDelete(deleteCommand, row);
                    }
                }
            }
            else
            {
                nNb += listeToDelete.Count;
            }
            return(nNb);
        }