| 
									
										
										
										
											2013-09-13 16:58:38 -04:00
										 |  |  | // Raw
 | 
					
						
							|  |  |  | // -------
 | 
					
						
							| 
									
										
										
										
											2014-04-15 13:10:32 -04:00
										 |  |  | var _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2014-04-27 19:35:36 -04:00
										 |  |  | var inherits = require('inherits'); | 
					
						
							|  |  |  | var EventEmitter = require('events').EventEmitter; | 
					
						
							| 
									
										
										
										
											2013-09-03 22:25:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-14 16:56:55 -04:00
										 |  |  | function Raw(sql, bindings) { | 
					
						
							| 
									
										
										
										
											2014-04-15 13:10:32 -04:00
										 |  |  |   if (sql.toSQL) { | 
					
						
							|  |  |  |     return this._processQuery(sql); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  |   this.sql = sql; | 
					
						
							| 
									
										
										
										
											2014-04-15 13:10:32 -04:00
										 |  |  |   this.bindings = _.isArray(bindings) ? bindings : | 
					
						
							|  |  |  |     bindings ? [bindings] : []; | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  |   this._debug = void 0; | 
					
						
							|  |  |  |   this._transacting = void 0; | 
					
						
							| 
									
										
										
										
											2014-03-14 16:56:55 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-04-27 19:35:36 -04:00
										 |  |  | inherits(Raw, EventEmitter); | 
					
						
							| 
									
										
										
										
											2013-09-03 22:01:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  | // Wraps the current sql with `before` and `after`.
 | 
					
						
							|  |  |  | Raw.prototype.wrap = function(before, after) { | 
					
						
							|  |  |  |   this.sql = before + this.sql + after; | 
					
						
							|  |  |  |   return this; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-03-14 16:56:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  | // Calls `toString` on the Knex object.
 | 
					
						
							|  |  |  | Raw.prototype.toString = function() { | 
					
						
							|  |  |  |   return this.toQuery(); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-03-14 16:56:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  | // Returns the raw sql for the query.
 | 
					
						
							| 
									
										
										
										
											2014-04-09 10:11:41 -04:00
										 |  |  | Raw.prototype.toSQL = function() { | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  |   return { | 
					
						
							|  |  |  |     sql: this.sql, | 
					
						
							| 
									
										
										
										
											2014-04-09 10:11:41 -04:00
										 |  |  |     method: 'raw', | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  |     bindings: this.bindings | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-03-14 16:56:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 13:10:32 -04:00
										 |  |  | // Convert the query toSQL.
 | 
					
						
							|  |  |  | Raw.prototype._processQuery = function(sql) { | 
					
						
							|  |  |  |   var processed = sql.toSQL(); | 
					
						
							|  |  |  |   return new this.constructor(processed.sql, processed.bindings); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-14 16:56:55 -04:00
										 |  |  | // Allow the `Raw` object to be utilized with full access to the relevant
 | 
					
						
							|  |  |  | // promise API.
 | 
					
						
							| 
									
										
										
										
											2014-04-08 16:25:57 -04:00
										 |  |  | require('./interface')(Raw); | 
					
						
							| 
									
										
										
										
											2014-03-14 16:56:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = Raw; |