php - How to Late bind with T-SQL and the Statement Object? -


i looking long term solution accommodate t-sql queries need use late binding. late binding syntax ms-sql pain , unique.

when comes t-sql late binding need declare , set,

declare @id int  set @id = 1 select * users user_id = @id; 

now zend framework 2 builds queries like

$select = new sql\select(); $select->from(['users']); $select->columns(['*']); $select->where->equalto('user_id', '@id'); // create statement object $stmt = $sql->preparestatementforsqlobject(); $stmt->prepare(); $stmt->execute(['@id' => 1]); 

now in zf2's statement object's prepare() method can prefix declare , set existing $sql, so:

 public function prepare($sql = null, array $options = []) {     if ($this->isprepared) {         throw new exception\runtimeexception('already prepared');     }     $sql = ($sql) ?: $this->sql;     $options = ($options) ?: $this->prepareoptions;      $pref = &$this->parameterreferences;     // change substr_count($sql, '?') <-> |-> substr_count($sql, '@')     ($position = 0, $count = substr_count($sql, '?'); $position < $count; $position++) {         if (!isset($this->prepareparams[$position])) {             $pref[$position] = ['', sqlsrv_param_in, null, null];         } else {             $pref[$position] = &$this->prepareparams[$position];         }     }      // <!-- beg of custom stuff. can grab $sql string, built here, $this->sql, , prefix t-sql required declare , set. -->     // can @id , 1 through execute()'s call , parameter container.     $prependsql = "declare @id int";     $prependsql .= "set @id = 1";     $prependsql .= $this->sql;     $this->sql = $prependsql;     $sql = $this->sql;     // <!-- end of custom stuff -->     $this->resource = sqlsrv_prepare($this->sqlsrv, $sql, $pref, $options);      $this->isprepared = true;      return $this; } 

is how should doing this? feel undermining zf2's db abstraction layer. direction or input great on how suppose this.


Comments