@guardian/google-admanager-api
Version:
Google Ad Manager API Client Library for NodeJs
183 lines • 5.68 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatementBuilder = void 0;
const exceptions_handler_1 = require("../handlers/exceptions.handler");
class StatementBuilder {
constructor() {
this.SELECT = "SELECT";
this.FROM = "FROM";
this.WHERE = "WHERE";
this.LIMIT = "LIMIT";
this.OFFSET = "OFFSET";
this.ORDER_BY = "ORDER BY";
this.valueEntries = [];
}
removeKeyword(clause, keyword) {
const formattedKeyword = keyword.trim() + " ";
return clause.toUpperCase().startsWith(formattedKeyword)
? clause.substring(formattedKeyword.length)
: clause;
}
select(columns) {
this._select = this.removeKeyword(columns, this.SELECT);
return this;
}
from(table) {
this._from = this.removeKeyword(table, this.FROM);
return this;
}
where(table) {
this._where = this.removeKeyword(table, this.WHERE);
return this;
}
limit(count) {
this._limit = count;
return this;
}
offset(count) {
this._offset = count;
return this;
}
increaseOffsetBy(amount) {
if (this._offset === undefined)
this._offset = 0;
this._offset += amount;
return this;
}
getOffset() {
return this._offset;
}
removeLimitAndOffset() {
this._offset = undefined;
this._limit = undefined;
return this;
}
orderBy(orderBy) {
this._orderBy = this.removeKeyword(orderBy, this.ORDER_BY);
return this;
}
addValue(key, value) {
this.valueEntries.push({
key,
value: this.getValue(value),
});
return this;
}
getValue(value) {
switch (typeof value) {
case "string":
return {
value,
attributes: {
"xsi:type": "TextValue",
},
};
case "number":
return {
value: value,
attributes: {
"xsi:type": "NumberValue",
},
};
case "boolean":
return {
value: value,
attributes: {
"xsi:type": "BooleanValue",
},
};
case "object":
if (Array.isArray(value)) {
return {
values: value.map((v) => this.getValue(v)),
attributes: {
"xsi:type": "SetValue",
},
};
}
if (this.isDateTime(value)) {
return {
value: value,
attributes: {
"xsi:type": "DateTimeValue",
},
};
}
if (this.isDate(value)) {
return {
value: value,
attributes: {
"xsi:type": "DateValue",
},
};
}
return {
value: value,
attributes: {
"xsi:type": "ObjectValue",
},
};
default:
throw new Error(typeof value);
}
}
isDateTime(arg) {
arg = arg;
if (!arg.date || !this.isDate(arg.date))
return false;
if (!arg.hour || arg.hour === null)
return false;
if (!arg.minute || arg.minute === null)
return false;
if (!arg.second || arg.second === null)
return false;
return true;
}
isDate(arg) {
arg = arg;
if (!arg.year || arg.year === null)
return false;
if (!arg.month || arg.month === null)
return false;
if (!arg.day || arg.day === null)
return false;
return true;
}
validateQuery() {
if (!this._limit && this._offset) {
throw new exceptions_handler_1.InvalidOperationException("OFFSET cannot be set if LIMIT is not set.");
}
}
buildQuery() {
const stringBuilder = [];
this.validateQuery();
if (typeof this.select !== "undefined" && this._select) {
stringBuilder.push(`${this.SELECT} ${this._select}`);
}
if (typeof this.from !== "undefined" && this._from) {
stringBuilder.push(`${this.FROM} ${this._from}`);
}
if (typeof this.where !== "undefined" && this._where) {
stringBuilder.push(`${this.WHERE} ${this._where}`);
}
if (typeof this.orderBy !== "undefined" && this._orderBy) {
stringBuilder.push(`${this.ORDER_BY} ${this._orderBy}`);
}
if (typeof this.limit !== "undefined" && this._limit) {
stringBuilder.push(`${this.LIMIT} ${this._limit}`);
}
if (typeof this.offset !== "undefined" && this._offset) {
stringBuilder.push(`${this.OFFSET} ${this._offset}`);
}
return stringBuilder.join(" ");
}
toStatement() {
return {
query: this.buildQuery(),
values: this.valueEntries,
};
}
}
exports.StatementBuilder = StatementBuilder;
StatementBuilder.SUGGESTED_PAGE_LIMIT = 500;
//# sourceMappingURL=statementBuilder.util.js.map