Skip to content

Commit

Permalink
RavenDB-20921 Pass queryCommand props as object, and add projection b…
Browse files Browse the repository at this point in the history
…ehavior
  • Loading branch information
kalczur authored and ppekrol committed Nov 7, 2023
1 parent 5eab5ef commit 54cda9f
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 31 deletions.
47 changes: 30 additions & 17 deletions src/Raven.Studio/typescript/commands/database/query/queryCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ import endpoints = require("endpoints");
import queryCriteria = require("models/database/query/queryCriteria");
import queryUtil = require("common/queryUtil");

interface QueryCommandProps {
db: database;
skip: number;
take: number;
criteria: queryCriteria;
disableCache?: boolean;
disableAutoIndexCreation?: boolean;
queryId?: string;
projectionBehavior?: Raven.Client.Documents.Queries.ProjectionBehavior;
}

class queryCommand extends commandBase {

private static readonly missingEndOfQuery = "Expected end of query";
private readonly props: QueryCommandProps;

constructor(private db: database, private skip: number, private take: number, private criteria: queryCriteria,
private disableCache?: boolean, private disableAutoIndexCreation?: boolean, private queryId?: string) {
constructor(props: QueryCommandProps) {
super();
this.props = props;
}

execute(): JQueryPromise<pagedResultExtended<document>> {
Expand All @@ -28,7 +40,7 @@ class queryCommand extends commandBase {
includesRevisions: results.RevisionIncludes
});

return this.post<pagedResultExtended<document>>(this.getUrl(), this.getPayload(), this.db)
return this.post<pagedResultExtended<document>>(this.getUrl(), this.getPayload(), this.props.db)
.then((results) => selector(results))
.fail((response: JQueryXHR) => {
if (response.status === 404) {
Expand All @@ -47,14 +59,14 @@ class queryCommand extends commandBase {
}

private getQueryText() {
const queryText = this.criteria.queryText();
const queryText = this.props.criteria.queryText();
if (!queryText) {
return [undefined, undefined];
}

const [parameters, rql] = queryCommand.extractQueryParameters(queryText);

if (this.criteria.showFields()) {
if (this.props.criteria.showFields()) {
return [parameters, queryUtil.replaceSelectAndIncludeWithFetchAllStoredFields(rql)];
}

Expand Down Expand Up @@ -118,28 +130,29 @@ f();
const [parameters, rql] = this.getQueryText();
const payload = {
Query: rql,
Start: this.skip,
PageSize: this.take,
DisableCaching: this.disableCache ? Date.now() : undefined,
QueryParameters: parameters
Start: this.props.skip,
PageSize: this.props.take,
DisableCaching: this.props.disableCache ? Date.now() : undefined,
QueryParameters: parameters,
ProjectionBehavior: this.props.projectionBehavior
}

return JSON.stringify(payload);
}

getUrl(method: "POST"|"GET" = "POST") {
const criteria = this.criteria;
const criteria = this.props.criteria;
const url = endpoints.databases.queries.queries;

const argsForPOST = {
diagnostics: this.criteria.diagnostics() ? "true" : undefined,
diagnostics: this.props.criteria.diagnostics() ? "true" : undefined,
debug: criteria.indexEntries() ? "entries" : undefined,
addTimeSeriesNames: true,
addSpatialProperties: true,
metadataOnly: typeof(criteria.metadataOnly()) !== 'undefined' ? criteria.metadataOnly() : undefined,
ignoreLimit: this.criteria.ignoreIndexQueryLimit(),
disableAutoIndexCreation: this.disableAutoIndexCreation,
clientQueryId: this.queryId
ignoreLimit: this.props.criteria.ignoreIndexQueryLimit(),
disableAutoIndexCreation: this.props.disableAutoIndexCreation,
clientQueryId: this.props.queryId
};

let urlArgs = this.urlEncodeArgs(argsForPOST);
Expand All @@ -151,9 +164,9 @@ f();
...argsForPOST,
query: rql,
parameters: JSON.stringify(parameters),
start: this.skip,
pageSize: this.take,
disableCache: this.disableCache ? Date.now() : undefined
start: this.props.skip,
pageSize: this.props.take,
disableCache: this.props.disableCache ? Date.now() : undefined
};

urlArgs = this.urlEncodeArgs(argsForGET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ class docsIdsBasedOnQueryFetcher {
criteria.queryText(`${wherelessQueryText} where startsWith(id(), '${this.escape(documentIdPrefix)}')`);
criteria.metadataOnly(true);

return new queryCommand(this.database(), 0, 10, criteria)
return new queryCommand({
db: this.database(),
skip: 0,
take: 10,
criteria
})
.execute()
.then(result => result.items.map(x => x.getId()));
}
Expand Down
13 changes: 9 additions & 4 deletions src/Raven.Studio/typescript/viewmodels/database/patch/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,15 @@ class patch extends viewModelBase {
const matchingDocs = $.Deferred<number>();

if (patchScriptParts.length === 2) {
const query = queryCriteria.empty();
query.queryText(patchScriptParts[0]);

new queryCommand(this.activeDatabase(), 0, 0, query)
const criteria = queryCriteria.empty();
criteria.queryText(patchScriptParts[0]);

new queryCommand({
db: this.activeDatabase(),
skip: 0,
take: 0,
criteria
})
.execute()
.done((queryResults: pagedResultExtended<document>) => matchingDocs.resolve(queryResults.totalResultCount))
.fail(() => matchingDocs.resolve(-1))
Expand Down
45 changes: 37 additions & 8 deletions src/Raven.Studio/typescript/viewmodels/database/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class query extends viewModelBase {

cacheEnabled = ko.observable<boolean>(true);
disableAutoIndexCreation = ko.observable<boolean>(true);
projectionBehavior = ko.observable<Raven.Client.Documents.Queries.ProjectionBehavior>("Default");

private indexEntriesStateWasTrue = false; // Used to save current query settings when switching to a 'dynamic' index

Expand Down Expand Up @@ -994,6 +995,7 @@ class query extends viewModelBase {
const criteriaDto = criteria.toStorageDto();
const disableCache = !this.cacheEnabled();
const disableAutoIndexCreation = this.disableAutoIndexCreation();
const projectionBehavior = this.projectionBehavior();

if (criteria.queryText()) {
this.spinners.isLoading(true);
Expand All @@ -1002,7 +1004,15 @@ class query extends viewModelBase {

//TODO: this.currentColumnsParams().enabled(this.showFields() === false && this.indexEntries() === false);

const queryCmd = new queryCommand(database, 0, 25, criteria, disableCache, disableAutoIndexCreation);
const queryCmd = new queryCommand({
db: database,
skip: 0,
take: 25,
criteria,
disableCache,
disableAutoIndexCreation,
projectionBehavior
});

// we declare this variable here, if any result returns skippedResults <> 0 we enter infinite scroll mode
let totalSkippedResults = 0;
Expand All @@ -1022,7 +1032,17 @@ class query extends viewModelBase {
const resultsFetcher = (skip: number, take: number) => {
const criteriaForFetcher = this.lastCriteriaExecuted;

const command = new queryCommand(database, skip + totalSkippedResults, take + 1, criteriaForFetcher, disableCache, disableAutoIndexCreation, query.clientQueryId);
const command = new queryCommand({
db: database,
skip: skip + totalSkippedResults,
take: take + 1,
criteria: criteriaForFetcher,
disableCache,
disableAutoIndexCreation,
queryId: query.clientQueryId,
projectionBehavior
});

this.onQueryRun();

const resultsTask = $.Deferred<pagedResultExtended<document>>();
Expand Down Expand Up @@ -1668,12 +1688,15 @@ class query extends viewModelBase {
this.spinners.isLoadingSpatialResults(true);
this.failedToGetResultsForSpatial(false);

const command = new queryCommand(this.activeDatabase(),
this.allSpatialResultsItems().length,
query.maxSpatialResultsToFetch + 1,
this.criteria().clone(),
!this.cacheEnabled(),
this.disableAutoIndexCreation());
const command = new queryCommand({
db: this.activeDatabase(),
skip: this.allSpatialResultsItems().length,
take: query.maxSpatialResultsToFetch + 1,
criteria: this.criteria().clone(),
disableCache: !this.cacheEnabled(),
disableAutoIndexCreation: this.disableAutoIndexCreation()
});

command.execute()
.done((queryResults: pagedResultExtended<document>) => {
const spatialProperties = queryResults.additionalResultInfo.SpatialProperties;
Expand Down Expand Up @@ -1826,6 +1849,12 @@ class query extends viewModelBase {
this.$downloadForm.attr("action", url);
this.$downloadForm.submit();
}

setProjectionBehavior(projectionBehavior: Raven.Client.Documents.Queries.ProjectionBehavior) {
this.projectionBehavior(projectionBehavior);
// Force dropdown menu to close. (for nested dropdowns, the menu stays open)
$(".projection-behavior-dropdown").removeClass("open");
}
}

export = query;
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ class customSorters extends viewModelBase {

const queryTask = $.Deferred<pagedResult<documentObject>>();

new queryCommand(this.activeDatabase(), 0, 128, criteria)
new queryCommand({
db: this.activeDatabase(),
skip: 0,
take: 128,
criteria
})
.execute()
.done(results => {
this.resultsCount(results.items.length);
Expand Down
12 changes: 12 additions & 0 deletions src/Raven.Studio/wwwroot/App/views/database/query/query.html
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,18 @@ <h2 class="on-base-background">
<label for="ignoreLimit">Ignore the index query size limit</label>
</div>
</div>
<div class="margin-left">
<div class="control-label">Projection Behavior</div>
<div class="btn-group projection-behavior-dropdown">
<button class="btn btn-default dropdown-toggle" data-toggle="dropdown" data-toggle="dropdown">
<span data-bind="text: projectionBehavior()"></span>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" data-bind="foreach: ['Default', 'FromDocument', 'FromDocumentOrThrow', 'FromIndex', 'FromIndexOrThrow']" style="position: initial;">
<li><a href="#" data-bind="text: $data, click: $parent.setProjectionBehavior.bind($parent, $data)"></a></li>
</ul>
</div>
</div>
</div>
</script>

Expand Down

0 comments on commit 54cda9f

Please sign in to comment.