Beispiel #1
0
        ////////////////////////////////////////////////////////
        public string GetStringExpression(IExpression expression, CFiltreData filtre)
        {
            string strRetour = "";

            if (expression is CComposantFiltre)
            {
                expression = ((CComposantFiltre)expression).GetComposantFiltreFinal(filtre);
            }

            if (expression is CComposantFiltreChamp)
            {
                CComposantFiltreChamp champ = (CComposantFiltreChamp)expression;
                if (champ.Alias != "")
                {
                    strRetour += champ.Alias + ".";
                }
                strRetour += champ.NomChamp;
            }


            else if (expression is CComposantFiltreVariable)
            {
                strRetour = ((CComposantFiltreVariable)expression).GetString();
            }



            else if (expression is CComposantFiltreConstante)
            {
                CComposantFiltreConstante constante = (CComposantFiltreConstante)expression;
                strRetour = CConvertisseurObjetToSqlServeur.ConvertToSql(constante.Valeur);
            }



            else if (expression is CComposantFiltreHasNo)
            {
                strRetour += GetStringExpression((IExpression)expression.Parametres[0], filtre);
                strRetour += " is null";
            }

            else if (expression is CComposantFiltreHas)
            {
                strRetour += GetStringExpression((IExpression)expression.Parametres[0], filtre);
                strRetour += " is not null";
            }



            else if (expression is CComposantFiltreListe)
            {
                CComposantFiltreListe liste = (CComposantFiltreListe)expression;
                strRetour = "";
                foreach (IExpression expInListe in liste.Liste)
                {
                    strRetour += GetStringExpression(expInListe, filtre) + ",";
                }
                if (strRetour.Length > 0)
                {
                    strRetour = strRetour.Substring(0, strRetour.Length - 1);
                }
                strRetour = "(" + strRetour + ")";
            }

            else if (expression is CComposantFiltreSousFiltre && m_connexion != null)
            {
                CComposantFiltreSousFiltre compo  = (CComposantFiltreSousFiltre)expression;
                CResultAErreur             result = compo.InterpreteParametres();
                if (result)
                {
                    string strWhere      = "";
                    string strJoin       = "";
                    string strPrefixFrom = "";
                    bool   bDistinct     = false;
                    compo.Filtre.Parametres.Clear();
                    compo.Filtre.AddChampAAjouterAArbreTable(compo.ChampSousFiltre);
                    foreach (object parametre in filtre.Parametres)
                    {
                        compo.Filtre.Parametres.Add(parametre);
                    }
                    CComposantFiltreOperateur compoEt = new CComposantFiltreOperateur(CComposantFiltreOperateur.c_IdOperateurEt);
                    compoEt.Parametres.Add(compo.Filtre.ComposantPrincipal);
                    CComposantFiltreHas compoNotNull = new CComposantFiltreHas();
                    compoNotNull.Parametres.Add(compo.ChampSousFiltre);
                    compoEt.Parametres.Add(compoNotNull);
                    compo.Filtre.ComposantPrincipal = compoEt;
                    IEnumerable <C2iDbDatabaseConnexion.CParametreRequeteDatabase> parametres = null;
                    result = m_connexion.PrepareRequeteFromFiltre(
                        compo.Filtre,
                        ref strWhere,
                        ref strJoin,
                        ref bDistinct,
                        ref strPrefixFrom,
                        ref parametres);
                    if (result)
                    {
                        strRetour += " ";
                        if (compo.ChampTeste.Alias != "")
                        {
                            strRetour += compo.ChampTeste.Alias + ".";
                        }
                        strRetour += compo.ChampTeste.NomChamp;

                        if (compo is CComposantFiltreInSousFiltre)
                        {
                            strRetour += " in (";
                        }
                        else
                        {
                            strRetour += " not in (";
                        }
                        string strSelect = "select ";
                        if (compo.ChampSousFiltre.Alias != "")
                        {
                            strSelect += compo.ChampSousFiltre.Alias + ".";
                        }
                        strSelect += compo.ChampSousFiltre.NomChamp;
                        strSelect += " from " + CContexteDonnee.GetNomTableInDbForNomTable(compo.TableSousFiltre);
                        strRetour += m_connexion.GetSql(strSelect, strPrefixFrom, strJoin, strWhere);
                        strRetour += ")";
                    }
                }
            }



            else if (expression is CComposantFiltreOperateur)
            {
                COperateurAnalysable operateur = ((CComposantFiltreOperateur)expression).Operateur;
                string strTexteOperateur       = operateur.Texte;
                if (operateur.Texte.Length > 3 && operateur.Texte.Substring(0, 3).ToUpper() == "NOT")
                {
                    strTexteOperateur = operateur.Texte.Substring(0, 3) + " " + operateur.Texte.Substring(3);
                }
                if (operateur.Id == CComposantFiltreOperateur.c_IdOperateurContains)
                {
                    strTexteOperateur = "like";
                }
                if (operateur.Id == CComposantFiltreOperateur.c_IdOperateurWithout)
                {
                    strTexteOperateur = "not like";
                }
                if (operateur.Niveau > 0 && operateur.Niveau != 4)
                {
                    string str1, str2, str3;
                    str1 = GetStringExpression((IExpression)expression.Parametres[0], filtre);
                    str2 = strTexteOperateur;
                    str3 = GetStringExpression((IExpression)expression.Parametres[1], filtre);
                    string[] valeurs = new string[] { str3 };
                    if (operateur.Id == CComposantFiltreOperateur.c_IdOperateurContains ||
                        operateur.Id == CComposantFiltreOperateur.c_IdOperateurWithout)
                    {
                        str3 = "'%'+" + str3 + "+'%'";
                    }
                    if (operateur.Niveau >= 4)
                    {
                        str1 = "(" + str1 + ")";
                        str3 = "(" + str3 + ")";
                    }
                    bool bChercheVide = false;
                    if (strTexteOperateur.ToUpper() == "LIKE")
                    {
                        bChercheVide = str3 == "%%";
                        if (!bChercheVide && str3.Contains("@"))
                        {
                            try
                            {
                                int nParam = Int32.Parse(str3.Substring("@PARAM".Length));
                                if (filtre.Parametres[nParam - 1].ToString() == "%%")
                                {
                                    bChercheVide = true;
                                }
                            }
                            catch
                            {
                            }
                        }
                    }

                    if (bChercheVide)
                    {
                        strRetour += str3 + "=" + str3;
                    }
                    else
                    {
                        strRetour = str1 + " " + str2 + " " + str3;

                        if (operateur.Id == CComposantFiltreOperateur.c_IdOperateurLike ||
                            operateur.Id == CComposantFiltreOperateur.c_IdOperateurNotLike)
                        {
                            strRetour += " ESCAPE '\\'";
                        }
                    }
                }
                else
                {
                    strRetour = strTexteOperateur + "(";
                    foreach (IExpression exp in expression.Parametres)
                    {
                        strRetour += GetStringExpression(exp, filtre) + ",";
                    }
                    if (expression.Parametres.Count > 0)
                    {
                        strRetour = strRetour.Substring(0, strRetour.Length - 1);
                    }
                    strRetour += ")";
                }
            }
            return(strRetour);
        }