/** * Ejecutar una consulta SQL * * Ejecuta una consulta SQL a la base de datos y retorna una resultado (objeto) asociado a los registros obtenidos o retorna false en caso de que no haya exito. * <code> * * Ejemplo 1: * <?php * * // Obtener varios resultados * * $myAct = new OPF_myActiveRecord(); * * if ($res = $myAct->query('SELECT field1,... FROM table')){ * * foreach ($res as $row) * * echo $row->field1,'<br>'; * * }else * * echo $myAct->getErrorLog(); * * ?> * * </code> * @param string $sql Consulta SQL * @param boolean $saveInLog Guardar en el log * @return object */ public function query($sql, $saveInLog = true) { $eError = array(); if ($saveInLog) { $GLOBALS['OF_SQL_LOG'] .= @vsprintf(str_ireplace('?', "'%s'", $sql), $this->arrayPrepare[$this->myact_table]) . ' ' . "\n"; } $isrW = false; foreach ($this->arrayCrud as $rW) { $pos = stripos($sql, $rW); if ($pos === false) { } else { if (stripos($sql, 'select') === false) { $isrW = true; break; } } } if ($isrW) { # Establecer nombre de secuencia automaticamente en Pgsql if (!isset($this->tableIdSeq[$this->myact_table]) && strcmp($this->myact_table, 'OPF_myActiveRecord')) { if ($this->getEngine() == 'pgsql') { $this->tableIdSeq[$this->myact_table] = $this->myact_table . '_' . $this->tablePk[$this->myact_table] . $this->posFijSeq; } } $sth = $this->myact_dbh->prepare($sql); $this->bindParamValues($sth); $sth->execute(); $this->num_rows = $sth->rowCount(); $eError = $sth->errorInfo(); if (isset($eError[2])) { $GLOBALS['OF_SQL_LOG_ERROR'] .= $eError[2] . "\n"; } return $this->num_rows; } else { # Select / No se afectan en transacciones, no afectan las busquedas $objReturn; $array = array(); $sth = $this->myact_dbh->prepare($sql); $this->bindParamValues($sth); $sth->execute(); $resQuery = $sth->fetchAll(); $eError = $sth->errorInfo(); if (isset($eError[2])) { $GLOBALS['OF_SQL_LOG_ERROR'] .= $eError[2] . "\n"; } else { $this->num_cols = $sth->columnCount(); $this->num_rows = 0; foreach ($resQuery as $row) { $array[] = $this->buildRes($row); ++$this->num_rows; } } return $array; } $this->arrayPrepare[$this->myact_table] = array(); }
/** * Bulid the sql based on $readFromXML, $SelectCommand and $table variables * The logic is: first we look if readFromXML is set to true, then we look for * SelectCommand and at end if none of these we use the table varable * Return string or false if the sql found * @return mixed */ protected function _setSQL() { $sqlId = false; if ($this->readFromXML == true && strlen($this->SelectCommand) > 0) { $sqlId = $this->getSqlElement($this->SelectCommand); } else { if ($this->SelectCommand && strlen($this->SelectCommand) > 0) { $sqlId = $this->SelectCommand; } else { if ($this->table && strlen($this->table) > 0) { if ($this->dbtype == 'mongodb') { $sqlId = $this->table; } else { $sqlId = "SELECT * FROM " . (string) $this->table; } } } } if ($this->dbtype == 'mongodb') { $sqlId = $this->pdo->selectCollection($sqlId); } return $sqlId; }
public static function Instance($modx) { if (self::$_instance == NULL) { self::$_instance = new self($modx); } return self::$_instance; }