//--------------------------------------------------------------------------------------------------------------------
        public override CResultAErreur ExecuterOperation(IDatabaseConnexion connection, IIndicateurProgression indicateur)
        {
            CResultAErreur   result   = CResultAErreur.True;
            IDataBaseCreator createur = connection.GetDataBaseCreator();
            string           strNomTableInContexte = CContexteDonnee.GetNomTableForType(m_type);
            string           strNomTableInDb       = CContexteDonnee.GetNomTableInDbForNomTable(strNomTableInContexte);

            createur.CreationOuUpdateTableFromType(m_type);
            string strChampIdObjet = m_strChampIdObjet;

            if (m_strChampIdObjet.StartsWith("#SQL#"))
            {
                strChampIdObjet = m_strChampIdObjet.Substring("#SQL#".Length);
            }
            CObjetServeur.ClearCacheSchemas();
            if (createur.ChampExists(strNomTableInDb, strChampIdObjet) &&
                createur.ChampExists(strNomTableInDb, m_strChampDbKey))
            {
                using (CContexteDonnee ctx = new CContexteDonnee(connection.IdSession, true, false))
                {
                    C2iRequeteAvancee requete = new C2iRequeteAvancee();
                    requete.TableInterrogee = strNomTableInDb;
                    CStructureTable structure = CStructureTable.GetStructure(m_type);

                    /*
                     * requete.ListeChamps.Add ( new C2iChampDeRequete (
                     *  "ID",
                     *  new CSourceDeChampDeRequete(structure.ChampsId[0].NomChamp),
                     *  typeof(int),
                     *  OperationsAgregation.None,
                     *  true ));*/
                    requete.ListeChamps.Add(new C2iChampDeRequete(
                                                "IDOBJET",
                                                new CSourceDeChampDeRequete(m_strChampIdObjet),
                                                typeof(int),
                                                OperationsAgregation.None,
                                                true));
                    if (m_typeObjetFixe == null)
                    {
                        requete.ListeChamps.Add(new C2iChampDeRequete(
                                                    "TYPEOBJET",
                                                    new CSourceDeChampDeRequete(m_strChampTypeObjet),
                                                    typeof(string),
                                                    OperationsAgregation.None,
                                                    true));
                    }
                    result = requete.ExecuteRequete(connection.IdSession);
                    if (!result)
                    {
                        return(result);
                    }
                    DataTable table = result.Data as DataTable;
                    Dictionary <int, int?> dicIdToIdObjet = new Dictionary <int, int?>();
                    string strFieldIdObjetInTable         = m_strChampIdObjet.Replace("#SQL#", "");
                    foreach (DataRow row in table.Rows)
                    {
                        object val    = row["IDOBJET"];
                        int?   nValId = val == DBNull.Value?null:(int?)val;
                        if (nValId != null && nValId >= 0)
                        {
                            CDbKey key = null;
                            Type   tp  = m_typeObjetFixe;
                            if (tp == null)//Type non fixe
                            {
                                string strType = (string)row["TYPEOBJET"];
                                tp = CActivatorSurChaine.GetType(strType);
                            }
                            if (tp != null)
                            {
                                CObjetDonneeAIdNumerique objPointe = (CObjetDonneeAIdNumerique)Activator.CreateInstance(tp, ctx);
                                if (objPointe.ReadIfExists(nValId.Value))
                                {
                                    key = objPointe.DbKey;
                                }
                            }
                            if (key != null)
                            {
                                string strRequete = "Update " + strNomTableInDb + " set " +
                                                    m_strChampDbKey + "=" + connection.GetStringForRequete(key.StringValue) + " where " +
                                                    strFieldIdObjetInTable + "=" + nValId.Value;
                                if (m_typeObjetFixe == null)
                                {
                                    strRequete += " and " + m_strChampTypeObjet + "='" +
                                                  row["TYPEOBJET"].ToString() + "'";
                                }
                                connection.RunStatement(strRequete);
                            }
                        }
                    }
                }
                //Supprime le champ
                createur.DeleteChamp(strNomTableInDb, strChampIdObjet);
            }
            return(result);
        }
Exemple #2
0
        //---------------------------------------------------------------------------
        public CResultAErreur UpdateStructureBase(IIndicateurProgression indicateurProgress)
        {
            CResultAErreur result = CResultAErreur.True;

            m_indicateurProgress = indicateurProgress;

            int nVersionDB     = (int)new CDatabaseRegistre(Connection).GetValeurLong(c_strCleVersionBDD, -1);
            int nVersionFinale = m_structureDB.GetLastVersion();

            int nOldVersion = nVersionDB;

            if (nVersionDB < 0)
            {
                result = InitialisationBase();
            }

            if (m_bForceMAJ)
            {
                nVersionDB = nVersionDB > 0 ? nVersionDB - 1 : 0;
            }

            if (nVersionDB < 0)
            {
                nVersionDB = 0;
            }

            if (m_indicateurProgress != null)
            {
                m_indicateurProgress.PushLibelle(I.T("Database Update|30005"));
                m_indicateurProgress.SetBornesSegment(nVersionDB, nVersionFinale);
                //m_indicateurProgress.PushSegment(nVersionDB, nVersionFinale);
            }

            result = m_connexion.GetDataBaseCreator().UpdateStructureTableRegistre();

            while (nVersionDB < nVersionFinale && result)
            {
                nVersionDB++;

                result = m_connexion.BeginTrans();

                if (!result)
                {
                    return(result);
                }

                if (m_indicateurProgress != null)
                {
                    m_indicateurProgress.SetInfo("Version " + nVersionDB.ToString());
                    m_indicateurProgress.SetValue(nVersionDB - 1);
                    m_indicateurProgress.PushLibelle("DataBase v" + nVersionDB.ToString());
                    m_indicateurProgress.PushSegment(nVersionDB - 1, nVersionDB);
                }

                result = UpdateToVersion(nVersionDB);

                //Validation ou annulation des modifications si erreur
                if (!result)
                {
                    m_connexion.RollbackTrans();
                    result.EmpileErreur(I.T("Database Update Error|30006"));
                    throw new CExceptionErreur(result.Erreur);
                }
                else
                {
                    result = m_connexion.CommitTrans();
                }


                if (m_indicateurProgress != null)
                {
                    m_indicateurProgress.PopSegment();
                    m_indicateurProgress.PopLibelle();
                }
            }

            /*if (m_indicateurProgress != null)
             *      m_indicateurProgress.PopSegment();*/

            //rafraichit les caches de schéma
            CObjetServeur.ClearCacheSchemas();
            C2iOracleDataAdapter.ClearCacheSchemas();
            CStructureTable.ClearCache();

            return(result);
        }