/** * @param $sql * @param array|null $arBinds * @param $offset * @param $limit * @param \Bitrix\Main\Diag\SqlTrackerQuery|null $trackerQuery * @return resource * @throws SqlException|\Bitrix\Main\ArgumentException */ protected function queryInternal($sql, array $arBinds = null, $offset = 0, $limit = 0, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null) { $this->connectInternal(); $offset = intval($offset); $limit = intval($limit); if ($offset > 0 && $limit <= 0) { throw new \Bitrix\Main\ArgumentException("Limit should be set if offset is set"); } if ($limit > 0) { if (preg_match("#\\slimit\\s+\\d#i", $sql)) { throw new \Bitrix\Main\ArgumentException("Duplicate limit settings"); } $sql .= "\nLIMIT " . intval($offset) . ", " . intval($limit) . "\n"; } if ($trackerQuery != null) { $trackerQuery->startQuery($sql, $arBinds); } $result = mysql_query($sql, $this->resource); if ($trackerQuery != null) { $trackerQuery->finishQuery(); } $this->lastQueryResult = $result; if (!$result) { throw new SqlException('Mysql query error', mysql_error($this->resource)); } return $result; }
/** * Executes a query against connected database. * Rises SqlQueryException on any database error. * <p> * When object $trackerQuery passed then calls its startQuery and finishQuery * methods before and after query execution. * * @param string $sql Sql query. * @param array $binds Array of binds. * @param \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery Debug collector object. * * @return resource * @throws \Bitrix\Main\Db\SqlQueryException */ protected function queryInternal($sql, array $binds = null, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null) { $this->connectInternal(); if ($trackerQuery != null) { $trackerQuery->startQuery($sql, $binds); } $result = mysql_query($sql, $this->resource); if ($trackerQuery != null) { $trackerQuery->finishQuery(); } $this->lastQueryResult = $result; if (!$result) { throw new SqlQueryException('Mysql query error', mysql_error($this->resource), $sql); } return $result; }
public function fetch(\Bitrix\Main\Text\Converter $converter = null) { if ($this->trackerQuery != null) { $this->trackerQuery->restartQuery(); } $resultFields = $this->getResultFields(); $dataTmp = $this->fetchRowInternal(); if ($this->trackerQuery != null) { $this->trackerQuery->refinishQuery(); } if (!$dataTmp) { return false; } $data = array(); foreach ($dataTmp as $key => $value) { $data[$resultFields[$key]["name"]] = $this->convertDataFromDb($value, $resultFields[$key]["type"]); } if (!empty($this->arSerializedFields)) { foreach ($this->arSerializedFields as $field) { if (array_key_exists($field, $data)) { $data[$field] = unserialize($data[$field]); } } } if (!empty($this->arReplacedAliases)) { foreach ($this->arReplacedAliases as $tech => $human) { if (array_key_exists($tech, $data)) { $data[$human] = $data[$tech]; unset($data[$tech]); } } } if ($this->fetchDataModifier != null) { $c = $this->fetchDataModifier; $data = $c($data); } if ($converter != null) { $arKeys = array_keys($data); foreach ($arKeys as $key) { $data[$key] = $converter->encode($data[$key], array_key_exists($key . "_TYPE", $data) ? $data[$key . "_TYPE"] : \Bitrix\Main\Text\Converter::TEXT); } } return $data; }
/** * @param $sql * @param array|null $arBinds * @param $offset * @param $limit * @param \Bitrix\Main\Diag\SqlTrackerQuery|null $trackerQuery * @return resource * @throws SqlException|\Bitrix\Main\ArgumentException */ protected function queryInternal($sql, array $arBinds = null, $offset = 0, $limit = 0, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null) { $this->connectInternal(); if ($limit > 0) { $sql = $this->getSqlHelper()->getTopSql($sql, $limit, $offset); } if ($trackerQuery != null) { $trackerQuery->startQuery($sql, $arBinds); } $result = mysql_query($sql, $this->resource); if ($trackerQuery != null) { $trackerQuery->finishQuery(); } $this->lastQueryResult = $result; if (!$result) { throw new SqlQueryException('Mysql query error', mysql_error($this->resource), $sql); } return $result; }
/** * @param $sql * @param array|null $arBinds * @param $offset * @param $limit * @param \Bitrix\Main\Diag\SqlTrackerQuery|null $trackerQuery * @return mixed * @throws SqlException|\Bitrix\Main\ArgumentException */ protected function queryInternal($sql, array $arBinds = null, $offset = 0, $limit = 0, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null) { $this->connectInternal(); if ($limit > 0) { $sql = $this->getSqlHelper()->getTopSql($sql, $limit, $offset); } if ($trackerQuery != null) { $trackerQuery->startQuery($sql, $arBinds); } $result = sqlsrv_query($this->resource, $sql, array(), array("Scrollable" => 'forward')); if ($trackerQuery != null) { $trackerQuery->finishQuery(); } $this->lastQueryResult = $result; if (!$result) { throw new SqlQueryException('MS Sql query error', $this->getErrorMessage(), $sql); } return $result; }
/** * @param $sql * @param array|null $arBinds * @param $offset * @param $limit * @param \Bitrix\Main\Diag\SqlTrackerQuery|null $trackerQuery * @return mixed * @throws SqlException|\Bitrix\Main\ArgumentException */ protected function queryInternal($sql, array $arBinds = null, $offset = 0, $limit = 0, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null) { $this->connectInternal(); $offset = intval($offset); $limit = intval($limit); if ($offset > 0 && $limit <= 0) { throw new \Bitrix\Main\ArgumentException("Limit should be set if offset is set"); } if ($limit > 0) { if (preg_match("#\\sROW_NUMBER\\s*\\(\\s*\\)#i", $sql) || preg_match("#SELECT\\s+TOP\\s+\\d#i", $sql)) { throw new \Bitrix\Main\ArgumentException("Duplicate limit settings"); } if ($offset <= 0) { $sql = preg_replace("/^\\s*SELECT/i", "SELECT TOP " . $limit, $sql); } else { if (preg_match("#(\\s+order\\s+by(\\s+[a-z0-9_.]+(\\s+(asc|desc))?\\s*,)*(\\s+[a-z0-9_.]+(\\s+(asc|desc))?)\\s*)\$#i", $sql, $matches)) { $orderBy = $matches[1]; $sqlTmp = substr($sql, 0, -strlen($orderBy)); } else { $orderBy = "ORDER BY (SELECT 1)"; $sqlTmp = $sql; } $sqlTmp = preg_replace("/^\\s*SELECT/i", "SELECT ROW_NUMBER() OVER (" . $orderBy . ") AS ROW_NUMBER_ALIAS,", $sqlTmp); $sql = "WITH ROW_NUMBER_QUERY_ALIAS AS (" . $sqlTmp . ") " . "SELECT * " . "FROM ROW_NUMBER_QUERY_ALIAS " . "WHERE ROW_NUMBER_ALIAS BETWEEN " . $offset . " AND " . ($offset + $limit - 1) . ""; } } if ($trackerQuery != null) { $trackerQuery->startQuery($sql, $arBinds); } $result = sqlsrv_query($this->connectionResource, $sql, array(), array("Scrollable" => 'forward')); if ($trackerQuery != null) { $trackerQuery->finishQuery(); } $this->lastQueryResult = $result; if (!$result) { throw new SqlException('MS Sql query error', $this->getErrorMessage()); } return $result; }
/** * Fetches one row of the query result and returns it in the associative array of raw DB data or false on empty data. * * @return array|false */ public function fetchRaw() { if ($this->trackerQuery != null) { $this->trackerQuery->restartQuery(); } $data = $this->fetchRowInternal(); if ($this->trackerQuery != null) { $this->trackerQuery->refinishQuery(); } if (!$data) { return false; } return $data; }
/** * Fetches one row of the query result and returns it in the associative array or false on empty data. * * @param \Bitrix\Main\Text\Converter $converter Optional converter to encode data on fetching. * * @return array|false */ public function fetch(\Bitrix\Main\Text\Converter $converter = null) { if ($this->trackerQuery != null) { $this->trackerQuery->restartQuery(); } $data = $this->fetchRowInternal(); if ($this->trackerQuery != null) { $this->trackerQuery->refinishQuery(); } if (!$data) { return false; } if ($this->converters) { foreach ($this->converters as $field => $convertDataModifier) { $data[$field] = call_user_func_array($convertDataModifier, array($data[$field])); } } if ($this->serializedFields) { foreach ($this->serializedFields as $field) { if (isset($data[$field])) { $data[$field] = unserialize($data[$field]); } } } if ($this->replacedAliases) { foreach ($this->replacedAliases as $tech => $human) { $data[$human] = $data[$tech]; unset($data[$tech]); } } if ($this->fetchDataModifiers) { foreach ($this->fetchDataModifiers as $fetchDataModifier) { $result = call_user_func_array($fetchDataModifier, array(&$data)); if (is_array($result)) { $data = $result; } } } if ($converter != null) { foreach ($data as $key => $val) { $data[$key] = $converter->encode($val, isset($data[$key . "_TYPE"]) ? $data[$key . "_TYPE"] : \Bitrix\Main\Text\Converter::TEXT); } } return $data; }
/** * @param $sql * @param array|null $arBinds * @param $offset * @param $limit * @param \Bitrix\Main\Diag\SqlTrackerQuery|null $trackerQuery * @return \mysqli_result * @throws SqlException|\Bitrix\Main\ArgumentException */ protected function queryInternal($sql, array $arBinds = null, $offset = 0, $limit = 0, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null) { $this->connectInternal(); if ($limit > 0) { $sql = $this->getSqlHelper()->getTopSql($sql, $limit, $offset); } if ($trackerQuery != null) { $trackerQuery->startQuery($sql, $arBinds); } /** @var $con \mysqli */ $con = $this->resource; $result = $con->query($sql, MYSQLI_STORE_RESULT); if ($trackerQuery != null) { $trackerQuery->finishQuery(); } $this->lastQueryResult = $result; if (!$result) { throw new SqlQueryException('Mysql query error', $this->getErrorMessage(), $sql); } return $result; }
/** * Executes a query against connected database. * Rises SqlQueryException on any database error. * <p> * When object $trackerQuery passed then calls its startQuery and finishQuery * methods before and after query execution. * * @param string $sql Sql query. * @param array $binds Array of binds. * @param \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery Debug collector object. * * @return resource * @throws \Bitrix\Main\Db\SqlQueryException */ protected function queryInternal($sql, array $binds = null, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null) { $this->connectInternal(); if ($trackerQuery != null) { $trackerQuery->startQuery($sql, $binds); } $result = oci_parse($this->resource, $sql); if (!$result) { if ($trackerQuery != null) { $trackerQuery->finishQuery(); } throw new SqlQueryException("", $this->getErrorMessage($this->resource), $sql); } $executionMode = $this->transaction; /** @var \OCI_Lob[] $clob */ $clob = array(); if (!empty($binds)) { $executionMode = OCI_DEFAULT; foreach ($binds as $key => $val) { $clob[$key] = oci_new_descriptor($this->resource, OCI_DTYPE_LOB); oci_bind_by_name($result, ":" . $key, $clob[$key], -1, OCI_B_CLOB); } } if (!oci_execute($result, $executionMode)) { if ($trackerQuery != null) { $trackerQuery->finishQuery(); } throw new SqlQueryException("", $this->getErrorMessage($result), $sql); } if (!empty($binds)) { if (oci_num_rows($result) > 0) { foreach ($binds as $key => $val) { if ($clob[$key]) { $clob[$key]->save($binds[$key]); } } } if ($this->transaction == OCI_COMMIT_ON_SUCCESS) { oci_commit($this->resource); } foreach ($binds as $key => $val) { if ($clob[$key]) { $clob[$key]->free(); } } } if ($trackerQuery != null) { $trackerQuery->finishQuery(); } $this->lastQueryResult = $result; return $result; }
/** * @param $sql * @param array|null $arBinds * @param $offset * @param $limit * @param \Bitrix\Main\Diag\SqlTrackerQuery|null $trackerQuery * @return resource * @throws SqlException|\Bitrix\Main\ArgumentException */ protected function queryInternal($sql, array $arBinds = null, $offset = 0, $limit = 0, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null) { $this->connectInternal(); if ($limit > 0) { $sql = $this->getSqlHelper()->getTopSql($sql, $limit, $offset); } $bindsKeys = array(); if (!empty($arBinds)) { $binds1 = $binds2 = ""; foreach ($arBinds as $key => $value) { if (strlen($value) > 0) { if ($binds1 != "") { $binds1 .= ","; $binds2 .= ","; } $bindsKeys[] = $key; $binds1 .= $key; $binds2 .= ":" . $key; } } if ($binds1 != "") { $sql .= " RETURNING " . $binds1 . " INTO " . $binds2; } } if ($trackerQuery != null) { $trackerQuery->startQuery($sql, $arBinds); } $result = oci_parse($this->resource, $sql); if (!$result) { if ($trackerQuery != null) { $trackerQuery->finishQuery(); } throw new SqlQueryException("", $this->getErrorMessage(), $sql); } $executionMode = $this->transaction; $clob = array(); if (!empty($arBinds)) { $executionMode = OCI_DEFAULT; foreach ($bindsKeys as $key) { $clob[$key] = oci_new_descriptor($this->resource, OCI_D_LOB); oci_bind_by_name($result, ":" . $key, $clob[$key], -1, OCI_B_CLOB); } } if (!oci_execute($result, $executionMode)) { if ($trackerQuery != null) { $trackerQuery->finishQuery(); } throw new SqlQueryException("", $this->getErrorMessage(), $sql); } if (!empty($arBinds)) { if (oci_num_rows($result) > 0) { foreach ($bindsKeys as $key) { $clob[$key]->save($arBinds[$key]); } } if ($this->transaction == OCI_COMMIT_ON_SUCCESS) { oci_commit($this->resource); } foreach ($bindsKeys as $key) { $clob[$key]->free(); } } if ($trackerQuery != null) { $trackerQuery->finishQuery(); } $this->lastQueryResult = $result; return $result; }
/** * @param $sql * @param array|null $arBinds * @param $offset * @param $limit * @param \Bitrix\Main\Diag\SqlTrackerQuery|null $trackerQuery * @return resource * @throws SqlException|\Bitrix\Main\ArgumentException */ protected function queryInternal($sql, array $arBinds = null, $offset = 0, $limit = 0, \Bitrix\Main\Diag\SqlTrackerQuery $trackerQuery = null) { $this->connectInternal(); $offset = intval($offset); $limit = intval($limit); if ($offset > 0 && $limit <= 0) { throw new \Bitrix\Main\ArgumentException("Limit should be set if offset is set"); } $bindsKeys = array(); if (!empty($arBinds)) { $binds1 = $binds2 = ""; foreach ($arBinds as $key => $value) { if (strlen($value) > 0) { if ($binds1 != "") { $binds1 .= ","; $binds2 .= ","; } $bindsKeys[] = $key; $binds1 .= $key; $binds2 .= ":" . $key; } } if ($binds1 != "") { $sql .= " RETURNING " . $binds1 . " INTO " . $binds2; } } if ($limit > 0) { if (preg_match("#\\sROWNUM\\W#i", $sql)) { throw new \Bitrix\Main\ArgumentException("Duplicate limit settings"); } if ($offset <= 0) { $sql = "SELECT * " . "FROM (" . $sql . ") " . "WHERE ROWNUM <= " . $limit . ""; } else { $sql = "SELECT * " . "FROM (" . " SELECT rownum_query_alias.*, ROWNUM rownum_alias " . " FROM (" . $sql . ") rownum_query_alias " . " WHERE ROWNUM <= " . ($offset + $limit - 1) . " " . ") " . "WHERE rownum_alias >= " . $offset . ""; } } if ($trackerQuery != null) { $trackerQuery->startQuery($sql, $arBinds); } $result = oci_parse($this->connectionResource, $sql); if (!$result) { if ($trackerQuery != null) { $trackerQuery->finishQuery(); } throw new SqlException("", $this->getErrorMessage()); } $executionMode = $this->transaction; $clob = array(); if (!empty($arBinds)) { $executionMode = OCI_DEFAULT; foreach ($bindsKeys as $key) { $clob[$key] = oci_new_descriptor($this->connectionResource, OCI_D_LOB); oci_bind_by_name($result, ":" . $key, $clob[$key], -1, OCI_B_CLOB); } } if (!oci_execute($result, $executionMode)) { if ($trackerQuery != null) { $trackerQuery->finishQuery(); } throw new SqlException("", $this->getErrorMessage()); } if (!empty($arBinds)) { if (oci_num_rows($result) > 0) { foreach ($bindsKeys as $key) { $clob[$key]->save($arBinds[$key]); } } if ($this->transaction == OCI_COMMIT_ON_SUCCESS) { oci_commit($this->connectionResource); } foreach ($bindsKeys as $key) { $clob[$key]->free(); } } if ($trackerQuery != null) { $trackerQuery->finishQuery(); } $this->lastQueryResult = $result; return $result; }