Skip to content

Commit

Permalink
final updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Apr 20, 2016
1 parent 16ccbd1 commit 4a6442e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 78 deletions.
130 changes: 65 additions & 65 deletions lib/prepared.js
Expand Up @@ -146,53 +146,6 @@ function PreparedStatement(name, text, values) {
}
});

/**
* @method PreparedStatement.get
* @description
* Returns a new Prepared Statement object `{name, text, values}`, based on the object's state.
*
* When a {@link PreparedStatement} object is passed into a query method, {@link PreparedStatement.get get} is called automatically.
*
* @returns {Object}
* New object `{name, text, values}`, with `values` set when it is not `null`/`undefined`.
*/
this.get = function () {
var obj = {
name: this.name,
text: this.text
};
if (!$utils.isNull(this.values)) {
obj.values = this.values;
}
return obj;
};

/**
* @method PreparedStatement.create
* @description
* Returns a new Prepared Statement object `{name, text, values}`, based on the object's state,
* but with the new `values`.
*
* @param {Array} [values]
* Optional property `values` to be used in the new object: `Array`/`null`/`undefined`.
*
* @returns {Object}
* New object `{name, text, values}`, with `values` set when it is not `null`/`undefined`.
*/
this.create = function (values) {
if (!$utils.isNull(values) && !Array.isArray(values)) {
throw new TypeError("'values' must be an array or null/undefined.");
}
var obj = {
name: this.name,
text: this.text
};
if (!$utils.isNull(values)) {
obj.values = values;
}
return obj;
};

if (name && typeof name === 'object') {
this.values = name.values;
this.text = name.text;
Expand All @@ -203,27 +156,74 @@ function PreparedStatement(name, text, values) {
this.values = values;
}

/**
* @method PreparedStatement.format
* @description
* Uses `pg-promise` formatting engine (method {@link formatting.format as.format}) to format the query according to its current state.
*
* This method is primarily for logging and diagnostics, as Prepared Statements are to be formatted by the server.
*
* ATTENTION: This method can never guarantee the same query as formatted by the server.
*
* @param {Object} [options]
* Formatting options, as used by method {@link formatting.format as.format}.
*
* @returns {string}
*/
this.format = function (options) {
return $fm.as.format(this.text, this.values, options);
};

Object.freeze(this);
}

/**
* @method PreparedStatement.get
* @description
* Returns a new Prepared Statement object `{name, text, values}`, based on the object's state.
*
* When a {@link PreparedStatement} object is passed into a query method, {@link PreparedStatement.get get} is called automatically.
*
* @returns {Object}
* New object `{name, text, values}`, with `values` set when it is not `null`/`undefined`.
*/
PreparedStatement.prototype.get = function () {
var obj = {
name: this.name,
text: this.text
};
if (!$utils.isNull(this.values)) {
obj.values = this.values;
}
return obj;
};

/**
* @method PreparedStatement.create
* @description
* Returns a new Prepared Statement object `{name, text, values}`, based on the object's state,
* but with the new `values`.
*
* @param {Array} [values]
* Optional property `values` to be used in the new object: `Array`/`null`/`undefined`.
*
* @returns {Object}
* New object `{name, text, values}`, with `values` set when it is not `null`/`undefined`.
*/
PreparedStatement.prototype.create = function (values) {
if (!$utils.isNull(values) && !Array.isArray(values)) {
throw new TypeError("'values' must be an array or null/undefined.");
}
var obj = {
name: this.name,
text: this.text
};
if (!$utils.isNull(values)) {
obj.values = values;
}
return obj;
};

/**
* @method PreparedStatement.format
* @description
* Uses `pg-promise` formatting engine (method {@link formatting.format as.format}) to format the query according to its current state.
*
* This method is primarily for logging and diagnostics, as Prepared Statements are to be formatted by the server.
*
* ATTENTION: This method can never guarantee the same query as formatted by the server.
*
* @param {Object} [options]
* Formatting options, as used by method {@link formatting.format as.format}.
*
* @returns {string}
*/
PreparedStatement.prototype.format = function (options) {
return $fm.as.format(this.text, this.values, options);
};

/**
* @method PreparedStatement.toString
* @description
Expand Down
1 change: 1 addition & 0 deletions test/typescript/build.bat
Expand Up @@ -10,3 +10,4 @@ call tsc formatting %PARAMS%
call tsc extensions %PARAMS%
call tsc minify %PARAMS%
call tsc pg %PARAMS%
call tsc prepared %PARAMS%
11 changes: 0 additions & 11 deletions test/typescript/init.ts
Expand Up @@ -18,17 +18,6 @@ db.one('');

db.one(new pgPromise.QueryFile(''));

var ps1 = new pgp.PreparedStatement({name:'', text:''});
var ps2 = new pgp.PreparedStatement(ps1);

db.one(ps1.create(123));

db.one({
name:'',
text:''
});


var txMode = new pgPromise.txMode.TransactionMode();

function myTransaction(t) {
Expand Down
23 changes: 23 additions & 0 deletions test/typescript/prepared.ts
@@ -0,0 +1,23 @@
/// <reference path='../../typescript/pg-promise' />

import * as pgPromise from 'pg-promise';

var pgp = pgPromise();
var db = pgp('connection');

var ps1 = new pgp.PreparedStatement('', '');
var ps2 = new pgp.PreparedStatement({name: '', text: ''});
var ps3 = new pgp.PreparedStatement(ps1);

db.one(ps1.get());
db.one(ps1);

db.one(ps1.create(undefined));
db.one(ps1.create(null));
db.one(ps1.create());
db.one(ps1.create([123]));

db.one({
name: '',
text: ''
});
4 changes: 2 additions & 2 deletions typescript/pg-promise.d.ts
Expand Up @@ -243,8 +243,8 @@ declare module 'pg-promise' {
values:Array<any>;

get():TPrepared;

create(values?:any):TPrepared;
create(values?:Array<any>):TPrepared;

format(options?:TFormattingOptions):string;

Expand Down

0 comments on commit 4a6442e

Please sign in to comment.