Exemple #1
0
        public static PgQuery CreateInstance(string queryText, int[] parametersDataTypes, DocumentDatabase documentDatabase)
        {
            queryText = queryText.Trim();

            try
            {
                if (RqlQuery.TryParse(queryText, parametersDataTypes, documentDatabase, out var rqlQuery))
                {
                    return(rqlQuery);
                }

                if (PowerBIQuery.TryParse(queryText, parametersDataTypes, documentDatabase, out var powerBiQuery))
                {
                    if (documentDatabase.ServerStore.LicenseManager.CanUsePowerBi(withNotification: true, out var licenseLimitException) == false)
                    {
                        throw licenseLimitException;
                    }

                    return(powerBiQuery);
                }

                if (HardcodedQuery.TryParse(queryText, parametersDataTypes, out var hardcodedQuery))
                {
                    return(hardcodedQuery);
                }

                throw new PgErrorException(
                          PgErrorCodes.StatementTooComplex,
                          "Unhandled query (Are you using ; in your query? " +
                          $"That is likely causing the postgres client to split the query and results in partial queries): {Environment.NewLine}" +
                          $"{queryText}");
            }
            catch (Exception e)
            {
                if (_log.IsInfoEnabled)
                {
                    _log.Info($"Failed to create instance of {nameof(PgQuery)}:{Environment.NewLine}{queryText}", e);
                }

                throw;
            }
        }
Exemple #2
0
        public static bool TryParse(string queryText, int[] parametersDataTypes, out HardcodedQuery hardcodedQuery)
        {
            // TODO: The hardcoded queries in NpgsqlConfig might look a bit different for every user because they are generated using a function. Add support to more than just the current queries by not matching the entire string but ignoring parts of it.
            // TODO: For more accurate implementation, use the `resultsFormat` and send an appropriate _result table (Binary or Text). So for example return PowerBIConfig.TableScheamResponseBinary when the foramt is binary, and PowerBIConfig.TableScheamResponseText otherwise.
            // var resultsFormat = GetDefaultResultsFormat();

            var     normalizedQuery = queryText.NormalizeLineEndings();
            PgTable result          = null;

            if (normalizedQuery.StartsWith(PowerBIConfig.TableSchemaQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = PowerBIConfig.TableSchemaResponse;
            }

            else if (normalizedQuery.StartsWith(PowerBIConfig.TableSchemaSecondaryQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = PowerBIConfig.TableSchemaSecondaryResponse;
            }

            else if (normalizedQuery.StartsWith(PowerBIConfig.ConstraintsQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = PowerBIConfig.ConstraintsResponse;
            }

            else if (normalizedQuery.Equals(PowerBIConfig.CharacterSetsQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = PowerBIConfig.CharacterSetsResponse;
            }

            // Npgsql
            else if (normalizedQuery.Equals(NpgsqlConfig.TypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.TypesResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.CompositeTypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.CompositeTypesResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.EnumTypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.EnumTypesResponse;
            }

            else if (normalizedQuery.Replace("\n", "").Equals(NpgsqlConfig.VersionQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.VersionResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.Npgsql5TypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.Npgsql5TypesResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.Npgsql5CompositeTypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.Npgsql5CompositeTypesResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.Npgsql5EnumTypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.Npgsql5EnumTypesResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.Npgsql4TypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.Npgsql4TypesResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.Npgsql4_1_2TypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.Npgsql4_1_2TypesResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.Npgsql4_0_3TypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.Npgsql4_0_3TypesResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.Npgsql4_0_0TypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.Npgsql4_0_0TypesResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.Npgsql4_0_0CompositeTypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.Npgsql4_0_0CompositeTypesResponse;
            }

            else if (normalizedQuery.Equals(NpgsqlConfig.Npgsql3TypesQuery, StringComparison.OrdinalIgnoreCase))
            {
                result = NpgsqlConfig.Npgsql3TypesResponse;
            }

            else if (normalizedQuery.StartsWith("DISCARD ALL", StringComparison.OrdinalIgnoreCase))
            {
                result = new PgTable();
            }

            else if (normalizedQuery.StartsWith("ROLLBACK", StringComparison.OrdinalIgnoreCase))
            {
                result = new PgTable();
            }

            if (result != null)
            {
                hardcodedQuery = new HardcodedQuery(queryText, parametersDataTypes, result);
                return(true);
            }

            hardcodedQuery = null;
            return(false);
        }