Executes a string of SQL. Returns a @@DataSet object.

@param string $Sql A string of SQL to be executed.
@param array $InputParameters An array of values with as many elements as there are bound parameters in the SQL statement being executed.
490490
public function Query($Sql, $InputParameters = NULL, $Options = array()) {
      if ($Sql == '')
         trigger_error(ErrorMessage('Database was queried with an empty string.', $this->ClassName, 'Query'), E_USER_ERROR);

      // Get the return type.
      if (isset($Options['ReturnType']))
         $ReturnType = $Options['ReturnType'];
      elseif (preg_match('/^\s*"?(insert)\s+/i', $Sql))
         $ReturnType = 'ID';
      elseif (!preg_match('/^\s*"?(update|delete|replace|create|drop|load data|copy|alter|grant|revoke|lock|unlock)\s+/i', $Sql))
         $ReturnType = 'DataSet';
      else
         $ReturnType = NULL;

		if (isset($Options['Cache'])) {
         // Check to see if the query is cached.
         $CacheKeys = (array)GetValue('Cache',$Options,NULL);
         $CacheOperation = GetValue('CacheOperation',$Options,NULL);
         if (is_null($CacheOperation)) {
            switch ($ReturnType) {
               case 'DataSet':
                  $CacheOperation = 'get';
                  break;
               case 'ID':
               case NULL:
                  $CacheOperation = 'remove';
                  break;
            }
         }
         
         switch ($CacheOperation) {
            case 'get':
               foreach ($CacheKeys as $CacheKey) {
                  $Data = Gdn::Cache()->Get($CacheKey);
               }

               // Cache hit. Return.
               if ($Data !== Gdn_Cache::CACHEOP_FAILURE)
                  return new Gdn_DataSet($Data);
               
               // Cache miss. Save later.
               $StoreCacheKey = $CacheKey;
               break;
            
            case 'increment':
            case 'decrement':
               $CacheMethod = ucfirst($CacheOperation);
               foreach ($CacheKeys as $CacheKey) {
                  $CacheResult = Gdn::Cache()->$CacheMethod($CacheKey);
               }
               break;
            
            case 'remove':
               foreach ($CacheKeys as $CacheKey) {
                  $Res = Gdn::Cache()->Remove($CacheKey);
               }
               break;
         }
		}

      // Run the Query
      if (!is_null($InputParameters) && count($InputParameters) > 0) {
         // Make sure other unbufferred queries are not open
         if (is_object($this->_CurrentResultSet)) {
            $this->_CurrentResultSet->Result();
            $this->_CurrentResultSet->FreePDOStatement(FALSE);
         }

         $PDOStatement = $this->Connection()->prepare($Sql);

         if (!is_object($PDOStatement)) {
            trigger_error(ErrorMessage('PDO Statement failed to prepare', $this->ClassName, 'Query', $this->GetPDOErrorMessage($this->Connection()->errorInfo())), E_USER_ERROR);
         } else if ($PDOStatement->execute($InputParameters) === FALSE) {
            trigger_error(ErrorMessage($this->GetPDOErrorMessage($PDOStatement->errorInfo()), $this->ClassName, 'Query', $Sql), E_USER_ERROR);
         }
      } else {
         $PDOStatement = $this->Connection()->query($Sql);
      }

      if ($PDOStatement === FALSE) {
         trigger_error(ErrorMessage($this->GetPDOErrorMessage($this->Connection()->errorInfo()), $this->ClassName, 'Query', $Sql), E_USER_ERROR);
      }
      
      // Did this query modify data in any way?
      if ($ReturnType == 'ID') {
         $this->_CurrentResultSet = $this->Connection()->lastInsertId();
      } else {
         if ($ReturnType == 'DataSet') {
            // Create a DataSet to manage the resultset
            $this->_CurrentResultSet = new Gdn_DataSet();
            $this->_CurrentResultSet->Connection = $this->Connection();
            $this->_CurrentResultSet->PDOStatement($PDOStatement);
         }
      }
      
      if (isset($StoreCacheKey)) {
         if ($CacheOperation == 'get')
            Gdn::Cache()->Store(
               $StoreCacheKey, 
               (($this->_CurrentResultSet instanceof Gdn_DataSet) ? $this->_CurrentResultSet->ResultArray() : $this->_CurrentResultSet),
               GetValue('CacheOptions', $Options, array())
               );
      }
      
      return $this->_CurrentResultSet;
   }
189939
public function execute(string $sql, $bind = []): Statement
    {
        $this->operation = 'execute';
        $this->sql = $sql;
        $this->params = $bind;
        return $this;
    }
422532
public static function insertBySql($sql)
    {
        if (is_array($sql)) {
            $bind = $sql;
            unset($bind[0]);
            $sql = $sql[0];
        } else {
            $bind = [];
        }

        $model = new static;

        $table = $model->getSource($bind);
        return $model->getConnection($bind)->insertBySql('INSERT' . " INTO [$table] " . $sql, $bind);
    }
289509
public function execute($input_parameters = null)
    {
        $debug = $this->pdo->getDebugSnapshot();
        $return = parent::execute($input_parameters);

        $sql = $this->getExecutedQueryString($input_parameters?$input_parameters:array());
        $this->pdo->log($sql, null, __METHOD__, $debug);
        $this->pdo->setLastExecutedQuery($sql);
        $this->pdo->incrementQueryCount();

        return $return;
    }
748041
public function execute($bound_input_params = Array())
    {
        $log = "Executing: " . $this->queryString;

        if (count($bound_input_params) > 0) {

            foreach ($bound_input_params as $key => $val) {
                $log .= "\nBinding Values: " . $key . ' = ' . $val;
            }
        }

        $this->connection->writeToLog($log);
        return parent::execute($bound_input_params);
    }
752911
public function createCommand($sql = null, $params = [])
    {
        $command = new Command([
            'db' => $this,
            'sql' => $sql,
        ]);

        return $command->bindValues($params);
    }
1362010
function _query($sql,$inputarr=false)
	{
	GLOBAL $php_errormsg;
		if (isset($php_errormsg)) $php_errormsg = '';
		$this->_error = '';

		if ($inputarr) {
			if (is_array($sql)) {
				$stmtid = $sql[1];
			} else {
				$stmtid = db2_prepare($this->_connectionID,$sql);

				if ($stmtid == false) {
					$this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
					return false;
				}
			}

			if (! db2_execute($stmtid,$inputarr)) {
				if ($this->_haserrorfunctions) {
					$this->_errorMsg = db2_stmt_errormsg();
					$this->_errorCode = db2_stmt_error();
				}
				return false;
			}

		} else if (is_array($sql)) {
			$stmtid = $sql[1];
			if (!db2_execute($stmtid)) {
				if ($this->_haserrorfunctions) {
					$this->_errorMsg = db2_stmt_errormsg();
					$this->_errorCode = db2_stmt_error();
				}
				return false;
			}
		} else
			$stmtid = @db2_exec($this->_connectionID,$sql);

		$this->_lastAffectedRows = 0;
		if ($stmtid) {
			if (@db2_num_fields($stmtid) == 0) {
				$this->_lastAffectedRows = db2_num_rows($stmtid);
				$stmtid = true;
			} else {
				$this->_lastAffectedRows = 0;
			}

			if ($this->_haserrorfunctions) {
				$this->_errorMsg = '';
				$this->_errorCode = 0;
			} else
				$this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
		} else {
			if ($this->_haserrorfunctions) {
				$this->_errorMsg = db2_stmt_errormsg();
				$this->_errorCode = db2_stmt_error();
			} else
				$this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';

		}
		return $stmtid;
	}
734758
protected function executeQuery($sql, array $parameters = [])
    {
        $this->connect();

        $dbParameters = $this->prepareParameters($parameters);
        try {
            $pdoStatement = $this->pdo->prepare($sql);
            $pdoStatement->execute($dbParameters);

            return $pdoStatement;
        } catch (PDOException $e) {
            throw new QueryException($e, $sql, $dbParameters);
        }
    }
343621
public static function getSqlWithParams(string $sql, array $params = []): string
    {
        if (! empty($params)) {
            $indexed = ($params === array_values($params));
            foreach ($params as $k => $v) {
                if (is_string($v)) {
                    $v = "'${v}'";
                }
                if (is_array($v)) {
                    $v = "'" . implode("','", $v) . "'";
                }
                if ($indexed) {
                    $sql = preg_replace('/\?/', $v, $sql, 1);
                } else {
                    $sql = str_replace(":${k}", $v, $sql);
                }
            }
        }

        return str_replace(PHP_EOL, '', $sql);
    }
739340
public function Query($Sql, $Parameters = NULL) {
		$Db = Gdn::Database();

		// Replace db prefixes.
		$Sql = str_replace(array(':_z', ':_'), array($Db->DatabasePrefix.self::TABLE_PREFIX, $Db->DatabasePrefix), $Sql);

      // Figure out the type of the type of the query.
      if (StringBeginsWith($Sql, 'select'))
         $Type = 'select';
      elseif (StringBeginsWith($Sql, 'truncate'))
         $Type = 'truncate';
      elseif (StringBeginsWith($Sql, 'insert'))
         $Type = 'insert';
      elseif (StringBeginsWith($Sql, 'update'))
         $Type = 'update';
      elseif (StringBeginsWith($Sql, 'delete'))
         $Type = 'delete';
      else
         $Type = 'select';

		// Execute the query.
      if (is_array($Parameters))
         $this->SQL->NamedParameters($Parameters);

		$Result = $this->SQL->Query($Sql, $Type);

		//$this->Timer->Split('Sql: '. str_replace("\n", "\n     ", $Sql));

		return $Result;
	}
5576
public function execute($inputParams = [])
    {
        $bingID = $this->pdo->getBingId();
        $pool = $this->pdo->pool;
        if(!empty($inputParams)){
            $this->_boundColumns = $inputParams;
        }
        if(!empty($this->_boundColumns)){
            $this->prepareParamName();
            $this->_resultData = $pool->prepareAndExecute($this->statement,$this->_boundColumns,$bingID);
            $this->pdo->setLastInsertId($this->_resultData->insert_id);
        }else{
            $this->_resultData = $pool->doQuery($this->statement,$bingID);
        }
        if($this->_resultData->result === false){
            throw new PDOException($this->_resultData->error);
        }
        return true;
    }
1273079
public function execute($sql, $params = null)
    {
        $stmt = $this->_pdo->prepare($sql);

        if (is_array($params)) {
            foreach ($params as $token => $value) {
                $stmt->bindValue($token, $value);
            }
        }

        try {
            $stmt->execute();
        } catch (PDOException $exception) {
            $this->_database->error('execute', $exception);
        }

        return $stmt;
    }
252981
public function createCommand($sql = null, $params = [])
    {
        /** @var Command $command */
        $command = new $this->commandClass([
            'db' => $this,
            'sql' => $sql,
        ]);

        return $command->bindValues($params);
    }
965246
protected function bindParams(&$sql, array $bind = [])
    {
        foreach ($bind as $key => $value) {
            if (is_array($value)) {
                $name = $this->bind($value[0], $value[1], isset($value[2]) ? $value[2] : null);
            } else {
                $name = $this->bind($value);
            }

            if (is_numeric($key)) {
                $sql = substr_replace($sql, ':' . $name, strpos($sql, '?'), 1);
            } else {
                $sql = str_replace(':' . $key, ':' . $name, $sql);
            }
        }
    }
252129
public function query(string $sql, array $params = []): Statement
    {
        $this->usedPrepare = false;
        $stmt = $this->prepare($sql, $params);
        $this->usedPrepare = true;
        $stmt->execute();
        return $stmt;
    }
909186
protected function _modifyParams(&$sql, &$params) {
        if (is_array($sql)) {
            if (count($sql) < 2) {
                throw new \InvalidArgumentException('If $sql is array, it needs to have at least 2 elements: (0) sql query, string and (1) parameters (single value or array).');
            }
            if ((count($params) > 0)) {
                throw new \InvalidArgumentException('$sql is array, $params should be empty in this case.');
            }
            $params = $sql[1];
            $sql = $sql[0];
        }
        if (!is_array($params)) {
            $params = array ($params);
        }
    }
143445
public function sql(string $sql, array $params = []) {
        $r = $this->model->db->query($sql, $params);
        if (!is_array($r) || 0 === count($r)) {
            return [];
        }
        return $this->model->setup($r);
    }