/**
  * Creates the table and columns specified with $this->Table() and
  * $this->Column(). If no table or columns have been specified, this method
  * will throw a fatal error.
  *
  * @param boolean $Explicit If TRUE, and the table specified with $this->Table() already exists, this
  * method will remove any columns from the table that were not defined with
  * $this->Column().
  * @param boolean $Drop If TRUE, and the table specified with $this->Table() already exists, this
  * method will drop the table before attempting to re-create it.
  */
 public function Set($Explicit = FALSE, $Drop = FALSE)
 {
     // Make sure that table and columns have been defined
     if ($this->_TableName == '') {
         throw new Exception(Gdn::Translate('You must specify a table before calling DatabaseStructure::Set()'));
     }
     if (count($this->_Columns) == 0) {
         throw new Exception(Gdn::Translate('You must provide at least one column before calling DatabaseStructure::Set()'));
     }
     // Be sure to convert names to lowercase before comparing because
     // different operating systems/databases vary on how case-sensitivity is
     // handled in table names.
     $SQL = $this->Database->SQL();
     $Tables = $SQL->FetchTables();
     if (in_array(strtolower($this->_DatabasePrefix . $this->_TableName), array_map('strtolower', $Tables))) {
         if ($Drop) {
             // Drop the table.
             $this->Drop();
             // And re-create it.
             return $this->_Create();
         }
         // If the table already exists, go into modify mode.
         return $this->_Modify($Explicit, $Drop);
     } else {
         // If it doesn't already exist, go into create mode.
         return $this->_Create();
     }
 }
 /** Gets the column definitions for the columns in the database.
  * @return array
  */
 public function ExistingColumns() {
    if($this->_ExistingColumns === NULL) {
       if($this->TableExists())
          $this->_ExistingColumns = $this->Database->SQL()->FetchTableSchema($this->_TableName);
       else
          $this->_ExistingColumns = array();
    }
    return $this->_ExistingColumns;
 }
 public function Query($Sql, $InputParameters = NULL, $Options = [])
 {
     // log if we want to
     if (\Config::get('forum::package.trace-include-queries', false)) {
         Trace(['sql' => $Sql, 'params' => $InputParameters, 'options' => $Options]);
     }
     // defer
     $rc = parent::Query($Sql, $InputParameters, $Options);
     // exception?
     return $rc;
 }
 /**
  * @todo Put the query debugging logic into the debug plugin.
  * 1. Create a subclass of this object where Query() does the debugging stuff.
  * 2. Install that class to Gdn to override the database.
  */
 public function Query($Sql, $InputParameters = NULL)
 {
     $Trace = debug_backtrace();
     $Method = '';
     foreach ($Trace as $Info) {
         $Class = ArrayValue('class', $Info, '');
         if ($Class === '' || strlen($Class) > 5 && substr_compare($Class, 'Model', -5, 5, FALSE) == 0) {
             $Type = ArrayValue('type', $Info, '');
             $Method = $Class . $Type . $Info['function'] . '(' . self::FormatArgs($Info['args']) . ')';
         }
     }
     // Save the query for debugging
     // echo '<br />adding to queries: '.$Sql;
     $this->_Queries[] = array('Sql' => $Sql, 'Parameters' => $InputParameters, 'Method' => $Method);
     // Start the Query Timer
     $TimeStart = list($sm, $ss) = explode(' ', microtime());
     $Result = parent::Query($Sql, $InputParameters);
     // Aggregate the query times
     $TimeEnd = list($em, $es) = explode(' ', microtime());
     $this->_ExecutionTime += $em + $es - ($sm + $ss);
     $this->_QueryTimes[] = $em + $es - ($sm + $ss);
     return $Result;
 }
 public function Query($Sql, $InputParameters = NULL, $Options = array())
 {
     $Trace = debug_backtrace();
     $Method = '';
     foreach ($Trace as $Info) {
         $Class = GetValue('class', $Info, '');
         if ($Class === '' || StringEndsWith($Class, 'Model', TRUE) || StringEndsWith($Class, 'Plugin', TRUE)) {
             $Type = ArrayValue('type', $Info, '');
             $Method = $Class . $Type . $Info['function'] . '(' . self::FormatArgs($Info['args']) . ')';
             break;
         }
     }
     // Save the query for debugging
     // echo '<br />adding to queries: '.$Sql;
     $Query = array('Sql' => $Sql, 'Parameters' => $InputParameters, 'Method' => $Method);
     $SaveQuery = TRUE;
     if (isset($Options['Cache'])) {
         $CacheKeys = (array) $Options['Cache'];
         $Cache = array();
         $AllSet = TRUE;
         foreach ($CacheKeys as $CacheKey) {
             $Value = Gdn::Cache()->Get($CacheKey);
             $CacheValue = $Value !== Gdn_Cache::CACHEOP_FAILURE;
             $AllSet &= $CacheValue;
             $Cache[$CacheKey] = $CacheValue;
         }
         $SaveQuery = !$AllSet;
         $Query['Cache'] = $Cache;
     }
     // Start the Query Timer
     $TimeStart = Now();
     $Result = parent::Query($Sql, $InputParameters, $Options);
     // Aggregate the query times
     $TimeEnd = Now();
     $this->_ExecutionTime += $TimeEnd - $TimeStart;
     if ($SaveQuery && !StringBeginsWith($Sql, 'set names')) {
         $Query['Time'] = $TimeEnd - $TimeStart;
         $this->_Queries[] = $Query;
     }
     return $Result;
 }