/** * Performs a query, then returns a resultset * * @param string $action[optional] The crud action performed (select, insert, update, delete, create, alter) * * @return B2DBResultset */ public function performQuery($action = '') { try { $values = $this->getCriteria() instanceof B2DBCriteria ? $this->getCriteria()->getValues() : array(); TBGLogging::log('executing PDO query (' . B2DB::getSQLCount() . ')', 'B2DB'); $time = explode(' ', microtime()); $pretime = $time[1] + $time[0]; $res = $this->statement->execute($values); if (!$res) { $error = $this->statement->errorInfo(); if (B2DB::isDebugMode()) { $time = explode(' ', microtime()); $posttime = $time[1] + $time[0]; B2DB::sqlHit($this->printSQL(), implode(', ', $values), $posttime - $pretime); } throw new B2DBException($error[2], $this->printSQL()); } if (B2DB::isDebugMode()) { TBGLogging::log('done', 'B2DB'); } if ($this->getCriteria() instanceof B2DBCriteria && $this->getCriteria()->action == 'insert') { if (B2DB::getDBtype() == 'mysql') { $this->insert_id = B2DB::getDBLink()->lastInsertId(); } elseif (B2DB::getDBtype() == 'pgsql') { TBGLogging::log('sequence: ' . B2DB::getTablePrefix() . $this->getCriteria()->getTable()->getB2DBName() . '_id_seq', 'b2db'); $this->insert_id = B2DB::getDBLink()->lastInsertId(B2DB::getTablePrefix() . $this->getCriteria()->getTable()->getB2DBName() . '_id_seq'); TBGLogging::log('id is: ' . $this->insert_id, 'b2db'); } } $action = $this->getCriteria() instanceof B2DBCriteria ? $this->getCriteria()->action : ''; $retval = new B2DBResultset($this); if (B2DB::isDebugMode()) { $time = explode(' ', microtime()); $posttime = $time[1] + $time[0]; B2DB::sqlHit($this->printSQL(), implode(', ', $values), $posttime - $pretime); } if (!$this->getCriteria() || $this->getCriteria()->action != 'select') { $this->statement->closeCursor(); } return $retval; } catch (Exception $e) { throw $e; } }
protected function _createToSQL() { $sql = ''; $qc = $this->getQC(); $sql .= "CREATE TABLE " . $this->_getTableNameSQL() . " (\n"; $field_sql = array(); foreach ($this->_columns as $column) { $field_sql[] = $this->_getColumnDefinitionSQL($column); } $sql .= join(",\n", $field_sql); $sql .= ", PRIMARY KEY ({$qc}" . $this->_getRealColumnFieldName($this->id_column) . "{$qc}) "; $sql .= ') '; if (B2DB::getDBtype() != 'pgsql') { $sql .= 'AUTO_INCREMENT=' . $this->_autoincrement_start_at . ' '; } if (B2DB::getDBtype() != 'pgsql') { $sql .= 'CHARACTER SET ' . $this->_charset; } return $sql; }
/** * Runs the action for the second step of the installation * where you enter database information * * @param TBGRequest $request The request object * * @return null */ public function runInstallStep2(TBGRequest $request) { $this->preloaded = false; $this->selected_connection_detail = 'custom'; if (!$this->error) { try { B2DB::initialize(THEBUGGENIE_CORE_PATH . 'b2db_bootstrap.inc.php'); } catch (Exception $e) { } if (B2DB::isInitialized()) { $this->preloaded = true; $this->username = B2DB::getUname(); $this->password = B2DB::getPasswd(); $this->dsn = B2DB::getDSN(); $this->hostname = B2DB::getHost(); $this->port = B2DB::getPort(); $this->b2db_dbtype = B2DB::getDBtype(); $this->db_name = B2DB::getDBname(); } } }
/** * Generate the "select" part of the query * * @return string */ protected function _generateSelectSQL() { $sql = 'SELECT '; $first_sel = true; if ($this->distinct) { $sql .= ' DISTINCT '; } if ($this->customsel) { if ($this->distinct && B2DB::getDBtype() == 'pgsql') { foreach ($this->sort_orders as $a_sort) { $this->addSelectionColumn($a_sort['column']); } } foreach ($this->selections as $column => $a_sel) { if ($a_sel['special'] != '') { $sql .= !$first_sel ? ', ' : ''; if ($a_sel['variable'] != '') { $sql .= ' @' . $a_sel['variable'] . ':='; } $sql .= strtoupper($a_sel['special']) . '(' . $a_sel['column'] . ')'; $sql .= $a_sel['additional'] != '' ? ' ' . $a_sel['additional'] . ' ' : ''; if (strlen(stristr($a_sel['special'], '(')) > 0) { $sql .= ')'; } if ($a_sel['alias'] != '') { $sql .= ' AS ' . $a_sel['alias']; } else { $sql .= ' AS ' . $this->getSelectionAlias($column); } } else { $sql .= !$first_sel ? ', ' : ''; if (isset($a_sel['variable']) && $a_sel['variable'] != '') { $sql .= ' @' . $a_sel['variable'] . ':='; } $sql .= $a_sel['column']; if ($a_sel['alias'] != '') { $sql .= ' AS ' . $a_sel['alias']; } else { $sql .= ' AS ' . $this->getSelectionAlias($column); } } if ($a_sel['alias'] == '') { $a_sel['alias'] = $this->getSelectionAlias($column); } $first_sel = false; } } else { $this->_addAllSelectColumns(); $sql .= $this->_generateSelectAllSQL(); } return $sql; }