Exemple #1
0
        public string GetFilterText()
        {
            string toReturn = "";

            string filterCase = cbxFilterType.SelectedItem.ToString();

            if (filterCase == "By Value:")
            {
                ComboBox cbxValueFilterType = filterPanel.Children.OfType <ComboBox>().Where(cbx => cbx.Name == "cbxValueFilterType").First();
                string   colRef;
                // If a specific column is selected
                if (currentColumn is not null)
                {
                    colRef = $"[{currentColumn.TableName}].[{currentColumn.Name}]";
                    string   type   = currentColumn.Constraints.Type;
                    string[] values = new string[textBoxes.Length];
                    for (int i = 0; i < textBoxes.Length; i++)
                    {
                        string str = textBoxes[i].Text;
                        values[i] = type switch
                        {
                            "int" => $"{int.Parse(str)}",
                            "varchar" => $"'{str}'",
                            "date" => $"'{DateTime.Parse(str):yyyy-MM-dd}'",
                            "time" => $"'{TimeSpan.Parse(str):hh\\:mm}'",
                            "decimal" => $"{double.Parse(str)}",
                            "bit" => $"{(bool.Parse(str) ? "1" : "0")}",
                            _ => throw new NotImplementedException(),
                        };
                    }

                    toReturn = (cbxValueFilterType.SelectedItem.ToString()) switch
                    {
                        "Contains" => $"Lower({colRef}) LIKE Lower('%{textBoxes[0].Text}%')",
                        "Equal To" => $"{colRef} = {values[0]}",
                        "Between" => $"{colRef} BETWEEN {values[0]} AND {values[1]}",
                        "Less Than" => $"{colRef} < {values[0]}",
                        "More Than" => $"{colRef} > {values[0]}",
                        _ => throw new NotImplementedException(),
                    };
                }
                else
                {
                    if (textBoxes[0].Text == "")
                    {
                        return("");
                    }
                    toReturn = "(";

                    int countAdded = 0;

                    for (int i = 0; i < columns.Length; i++)
                    {
                        string toAdd = "";

                        string type = columns[i].Constraints.Type;

                        string text = textBoxes[0].Text;

                        colRef = $"[{columns[i].TableName}].[{columns[i].Name}]";
                        switch (cbxValueFilterType.SelectedItem.ToString())
                        {
                        case "Contains":
                            switch (type)
                            {
                            case "varchar": toAdd = $"Lower({colRef}) LIKE Lower('%{text}%')"; break;

                            case "int":
                                if (int.TryParse(text, out int intRes))
                                {
                                    toAdd = $"{colRef} = {intRes}";
                                }
                                break;

                            case "decimal":
                                if (double.TryParse(text, out double doubleRes))
                                {
                                    toAdd = $"{colRef} = {doubleRes}";
                                }
                                break;

                            case "date":
                                if (DateTime.TryParse(text, out DateTime dRes))
                                {
                                    toAdd = $"{colRef} = '{dRes.Date:yyyy-MM-yy}'";
                                }
                                break;

                            case "time":
                                if (TimeSpan.TryParse(text, out TimeSpan timeSpan))
                                {
                                    toAdd = $"{colRef} = '{timeSpan:hh\\:mm}'";
                                }
                                break;

                            case "bit":
                                if (text.ToLower() == "true" || text.ToLower() == "false")
                                {
                                    toAdd = $"{colRef} = {bool.Parse(text)}";
                                }
                                break;

                            default: throw new NotImplementedException();
                            }
                            break;

                        case "Equal To":
                            switch (type)
                            {
                            case "varchar": toAdd = $"{colRef} = '{text}'"; break;

                            case "int":
                                if (int.TryParse(text, out int intRes))
                                {
                                    toAdd = $"{colRef} = {intRes}";
                                }
                                break;

                            case "decimal":
                                if (double.TryParse(text, out double doubleRes))
                                {
                                    toAdd = $"{colRef} = {doubleRes}";
                                }
                                break;

                            case "date":
                                if (DateTime.TryParse(text, out DateTime dRes))
                                {
                                    toAdd = $"{colRef} = '{dRes.Date:yyyy-MM-yy}'";
                                }
                                break;

                            case "time":
                                if (TimeSpan.TryParse(text, out TimeSpan timeSpan))
                                {
                                    toAdd = $"{colRef} = '{timeSpan:hh\\:mm}'";
                                }
                                break;

                            case "bit":
                                if (text.ToLower() == "true" || text.ToLower() == "false")
                                {
                                    toAdd = $"{colRef} = {bool.Parse(text)}";
                                }
                                break;

                            default: throw new NotImplementedException();
                            }
                            break;

                        default: throw new NotImplementedException();
                        }

                        if (toAdd != "")
                        {
                            if (countAdded != 0)
                            {
                                toReturn += " OR ";
                            }
                            toReturn += toAdd;
                            countAdded++;
                        }

                        if (i == columns.Length - 1)
                        {
                            toReturn += ")";
                        }
                    }
                }
            }
            else if (filterCase == "By Reference:")
            {
                FilterManager filterManager = filterPanel.Children.OfType <FilterManager>().FirstOrDefault();
                toReturn = filterManager.GetFilterText();
            }
            if (toReturn == "()")
            {
                return("");
            }
            return(toReturn);
        }