gads
Version:
An unofficial JS client library for the SOAP-based DFP Ads API
126 lines • 4.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// A statement object for PQL and get*ByStatement queries.
// The StatementBuilder object allows for user control of limit/offset. It
// automatically limits queries to the suggested page limit if not explicitly
// set.
class StatementBuilder {
constructor(where = '', values, limit, offset) {
this._limit = 0;
this._offset = 0;
this.where(where).limit(limit).offset(offset);
this.values = Object.assign({}, values);
}
// Convert values map to array
_buildValues() {
const arr = [];
const map = this.values;
for (const key in map) {
if (map.hasOwnProperty(key)) {
arr.push({
$attr: { 'xsi:type': 'String_ValueMapEntry' },
key,
value: map[key]
});
}
}
return arr;
}
_buildQuery() {
let statement = '';
if (this._select) {
statement = statement + `SELECT ${this._select} `;
}
if (this._from) {
statement = statement + `FROM ${this._from} `;
}
if (this._where) {
statement = statement + `WHERE ${this._where} `;
}
if (this._orderBy) {
statement = statement + `ORDER BY ${this._orderBy} `;
}
if (this._limit) {
statement = statement + `LIMIT ${this._limit} `;
}
if (this._offset) {
statement = statement + `OFFSET ${this._offset} `;
}
return statement.trim();
}
// Sets the statement FROM clause in the form of "table".
// Only necessary for statements being sent to the
// {@code PublisherQueryLanguageService}. The "FROM " keyword will be
// ignored.
// @param string $table the statement from clause without "FROM"
// @return StatementBuilder a reference to this object
from(table) {
this._from = StatementBuilder._removeKeyword(table, 'FROM');
return this;
}
getLimit() {
return this._limit;
}
getOffset() {
return this._offset;
}
getValue(key) {
return this.values[key];
}
increaseOffset(delta) {
return this._offset = (this._offset) ? this._offset + delta : delta;
}
limit(_limit) {
this._limit = _limit || 0;
return this;
}
offset(_offset) {
this._offset = _offset || 0;
return this;
}
// Sets the statement ORDER BY clause in the form of
// "a [ASC|DESC],b[ASC|DESC]". The "ORDER BY " keyword will be ignored.
// @param string $orderBy the statement order by clause without "ORDER BY"
// @return StatementBuilder a reference to this object
orderBy(orderBy) {
this._orderBy = StatementBuilder._removeKeyword(orderBy, 'ORDER BY');
return this;
}
// Removes the {@code keyword} from the {@code clause} if present.
// @param string $clause the clause to remove from
// @param string $keyword the keyword to remove
// @return string a new string with the keyword + " " removed
static _removeKeyword(clause, keyword) {
keyword = keyword + ' ';
return (clause.substr(0, keyword.length) === keyword) ?
clause.substr(keyword.length) : clause;
}
// Sets the statement SELECT clause in the form of "a,b" or "*".
// Only necessary for statements being sent to the
// {@code PublisherQueryLanguageService}. The "SELECT " keyword will be
// ignored.
// @param string $columns the statement select clause without "SELECT"
// @return StatementBuilder a reference to this object
select(columns) {
this._select = StatementBuilder._removeKeyword(columns, 'SELECT');
return this;
}
// Returns this statement object in the format DFP requires
toStatement() {
return {
$attr: { 'xsi:type': 'Statement' },
query: this._buildQuery(),
values: this._buildValues()
};
}
value(key, value) {
this.values[key] = value;
return this;
}
where(_where) {
this._where = StatementBuilder._removeKeyword(_where, 'WHERE');
return this;
}
}
exports.StatementBuilder = StatementBuilder;
//# sourceMappingURL=statementBuilder.js.map