Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/app/models/connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def execute_query(query, limit: 50)
query = query.chomp(";")

# Check if the query already has a LIMIT clause
has_limit = query.match?(/LIMIT \s*\d+\s*$/i)
has_limit = query.match?(/\bLIMIT\s+\d+/i)
# Append LIMIT only if not already present
final_query = has_limit ? query : "#{query} LIMIT #{limit}"
client.send(:query, db, final_query)
Expand Down
43 changes: 43 additions & 0 deletions server/spec/models/connector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,49 @@
expect(result).to eq(query_result)
end
end
<<<<<<< HEAD
=======

context "when query is for Postgresql connector" do
before do
allow(postgres_connector).to receive(:connector_client).and_return(postgres_client_class)

allow(postgres_client_class).to receive(:new).and_return(postgres_client_instance)

allow(postgres_client_instance).to receive(:create_connection)
.with(postgres_connector.configuration.with_indifferent_access)
.and_return(postgres_db_connection)

allow(postgres_client_instance).to receive(:query)
.with(postgres_db_connection, "SET search_path TO \"public\", \"public\"; #{postgres_query} LIMIT 50")
.and_return(postgres_result)
end

it "appends a LIMIT clause and executes the query" do
expect(postgres_client_instance)
.to receive(:query)
.with(
postgres_db_connection,
"SET search_path TO \"public\", \"public\"; #{postgres_query} LIMIT 50"
).and_return(postgres_result)
result = postgres_connector.execute_query(postgres_query)
expect(result).to eq(postgres_result)
end
end

context "when query has a LIMIT and OFFSET clause" do
let(:query_with_limit_and_offset) { "#{query} LIMIT 50 OFFSET 10" }

it "executes the query without modifying it" do
expect(client_double)
.to receive(:query)
.with(db_connection, query_with_limit_and_offset)
.and_return(query_result)
result = connector.execute_query(query_with_limit_and_offset)
expect(result).to eq(query_result)
end
end
>>>>>>> e9fcf2dbe (chore(CE): update connector model to handle query pagination safely (#1790))
end

describe "#execute_search" do
Expand Down
Loading