13 require_once dirname(__FILE__).
'/DAL.inc';
37 public $_lobStack = Array();
46 private $_queryid =
'';
54 private $_totalBindings = 0;
63 private $_arrayRepresentation = array();
74 private $_sqlString =
'';
82 private $_bindings = array();
92 private $_keywords = array();
102 private $_execMethod = NULL;
110 private $_bindMap = array();
124 public function __construct($queryid=
'', array $arrayRepresentation=array(), $sqlString=
'')
126 $this->_queryid = $queryid;
128 if (isset($arrayRepresentation[
'query']) === FALSE) {
132 $this->_arrayRepresentation = $arrayRepresentation;
133 $this->_sqlString = $sqlString;
146 return $this->_arrayRepresentation[
'query_type'];
159 return $this->_arrayRepresentation;
175 $subQuery = clone $query;
180 if ($subQuery->getQueryType() ===
'with') {
181 $afterPart = $subQueryArray[
'WITH'][
'AFTER'];
187 $this->_arrayRepresentation[
'query'] = $subQueryArray;
188 $this->_findAndReplacePlaceHolder($bindName, $afterPart);
189 $this->_arrayRepresentation[
'query_type'] =
'with';
190 $this->_addBindingsFromQuery($subQuery);
192 $this->_addBindingsFromQuery($subQuery);
193 $this->_findAndReplacePlaceHolder($bindName, $subQueryArray);
209 private function _addBindingsFromQuery(
Query $subQuery)
212 foreach ($bindings as $binding) {
213 $newName = $this->_getNextValidBindName();
214 $subQuery->_findAndReplacePlaceHolder($binding[
'name'], $newName);
215 $this->_addBindVar($newName, $binding[
'value'], $binding[
'type'], $binding[
'name']);
227 private function _getNextValidBindName()
229 $this->_totalBindings++;
230 $bindName =
':bind_'.$this->_totalBindings;
247 private function _addBindVar($name, $value, $type, $realName)
249 $this->_bindings[] = array(
255 $this->_bindMap[] = array(
270 private function _removeLastBindVar($count=1)
272 for ($i = 0; $i < $count; $i++) {
273 array_pop($this->_bindings);
274 array_pop($this->_bindMap);
275 $this->_totalBindings--;
292 $this->_sqlString = str_replace($oldName, $newName, $this->_sqlString);
305 return $this->_queryid;
318 return $this->_bindings;
331 return $this->_arrayRepresentation[
'query'];
348 if ($convert === TRUE || $this->_sqlString ===
'') {
350 require_once SQ_LIB_PATH.
'/DAL/DALBaker.inc';
352 $this->_sqlString = $converter->convertToSql($this->_arrayRepresentation[
'query']);
355 return $this->_sqlString;
368 $sql = $this->
getSql(TRUE);
371 foreach ($this->_keywords as $keyword => $value) {
372 $sql = str_replace(
'['.$keyword.
']', $value, $sql);
376 $this->_sqlString = $sql;
383 $query = oci_parse($dbh, $sql);
385 foreach ($this->_bindings as $binding) {
387 if ($binding[
'type'] === PDO::PARAM_LOB) {
388 oci_bind_by_name($query, $binding[
'name'], $binding[
'value'], -1, SQLT_CLOB);
391 $this->_lobStack[] = $binding[
'value'];
393 oci_bind_by_name($query, $binding[
'name'], $binding[
'value'], -1);
399 $query = $dbh->prepare($sql);
401 foreach ($this->_bindings as $binding) {
403 $query->bindValue($binding[
'name'], $binding[
'value']);
425 $this->_execMethod = $method;
440 return $this->_execMethod;
458 public function bind($name, $value, $dataType=NULL)
461 if (($value instanceof
Query) === TRUE) {
462 $this->
merge($name, $value);
463 }
else if (is_array($value) === TRUE) {
464 $bindNames = array();
465 foreach ($value as $val) {
466 $newBindName = $this->_getNextValidBindName();
467 $this->_addBindVar($newBindName, $val, $dataType, $name);
468 $bindNames[] = $newBindName;
471 if ($this->_findAndReplacePlaceHolder($name, implode(
',', $bindNames)) === FALSE) {
472 $this->_removeLastBindVar(count($bindNames));
475 $newBindName = $this->_getNextValidBindName();
476 $this->_addBindVar($newBindName, $value, $dataType, $name);
479 if ($this->_findAndReplacePlaceHolder($name, $newBindName) === FALSE) {
480 $this->_removeLastBindVar();
499 private function _findAndReplacePlaceHolder($placeHolder, $value)
502 $arrays[] =& $this->_arrayRepresentation[
'query'];
505 for ($i = 0; $i < count($arrays); $i++) {
506 $this_array =& $arrays[$i];
508 foreach(array_keys($this_array) as $key) {
509 if ($this_array[$key] === $placeHolder) {
510 $this_array[$key] = $value;
517 if (is_array($this_array[$key])) {
518 $arrays[] =& $this_array[$key];
539 private function _findAndReplaceCallBack(&$item, $key, array $data)
541 if ($item === $data[0]) {
544 throw new Exception(
'done');
561 foreach ($queries as $query) {
562 if (($query instanceof
Query) === TRUE) {
564 $this->
merge(
'HOOKID:'.$subQueryid, $query);
566 $this->
bind(
'HOOKID:'.$subQueryid, $query, PDO::PARAM_STR);
585 $this->_keywords = array_merge($this->_keywords, $keywords);
599 if (DAL::getDBType() ===
'oci') {
600 foreach($this->_lobStack as $key => $lob) {
602 oci_free_descriptor($lob);
603 unset($this->_lobStack[$key]);