Files
ventas_php/classes/db.class.php

280 lines
6.2 KiB
PHP
Executable File

<?php
require_once __DIR__ . '/system-config.class.php';
require_once __DIR__ . '/database-manager.class.php';
class DB
{
public $query = NULL;
private $sqlResult = NULL;
public $connection = null;
private $dbManager = null;
private $empresaId = null;
private $isMaster = false;
private $projectStatus = "test";
public function setSqlHost($value)
{
// Mantener para compatibilidad, pero ya no se usa internamente
}
public function getSqlHost()
{
$config = $this->isMaster ?
SystemConfig::getMasterDatabaseConfig() :
SystemConfig::getEmpresaDatabaseConfig($this->empresaId);
return $config['host'];
}
public function setSqlDatabase($value)
{
// Mantener para compatibilidad, pero ya no se usa internamente
}
public function getSqlDatabase()
{
$config = $this->isMaster ?
SystemConfig::getMasterDatabaseConfig() :
SystemConfig::getEmpresaDatabaseConfig($this->empresaId);
return $config['database'];
}
public function setSqlUser($value)
{
// Mantener para compatibilidad, pero ya no se usa internamente
}
public function getSqlUser()
{
$config = $this->isMaster ?
SystemConfig::getMasterDatabaseConfig() :
SystemConfig::getEmpresaDatabaseConfig($this->empresaId);
return $config['user'];
}
public function setSqlPassword($value)
{
// Mantener para compatibilidad, pero ya no se usa internamente
}
public function getSqlPassword()
{
$config = $this->isMaster ?
SystemConfig::getMasterDatabaseConfig() :
SystemConfig::getEmpresaDatabaseConfig($this->empresaId);
return $config['password'];
}
public function setQuery($value)
{
$this->query = $value;
}
public function getQuery()
{
return $this->query;
}
public function setProjectStatus($value)
{
$this->projectStatus = $value;
}
public function getProjectStatus()
{
return $this->projectStatus;
}
function __construct($useMaster = false, $empresaId = null)
{
$this->dbManager = DatabaseManager::getInstance();
$this->isMaster = $useMaster;
if ($useMaster) {
$this->connection = $this->dbManager->getMasterConnection();
} else {
if ($empresaId !== null) {
$this->empresaId = $empresaId;
$this->connection = $this->dbManager->getEmpresaConnection($empresaId);
} elseif (isset($_SESSION['empresaId'])) {
$this->empresaId = $_SESSION['empresaId'];
try {
$this->connection = $this->dbManager->getEmpresaConnection($_SESSION['empresaId']);
} catch (Exception $e) {
// Fallback a master si la BD de empresa no existe
$this->isMaster = true;
$this->connection = $this->dbManager->getMasterConnection();
}
} else {
// Fallback directo a master
$this->isMaster = true;
$this->connection = $this->dbManager->getMasterConnection();
}
}
}
public function DatabaseConnect()
{
// Ya no se necesita, la conexión se maneja mediante DatabaseManager
return true;
}
public function ExecuteQuery()
{
//TODO we might want to add some security in the queries here, but that can be done later, this is the place
if($this->projectStatus == "test")
{
//echo "<br><br>".$this->query."<br><br>";
// print_r(debug_backtrace());
if ($this->connection instanceof MockDatabase) {
$this->sqlResult = $this->connection->query($this->query);
} else {
$this->sqlResult = mysqli_query($this->connection, $this->query) or die (trigger_error($this->query . " " . mysqli_error($this->connection)));
}
}
else
{
$this->sqlResult = @mysqli_query($this->connection, $this->query);
}
}
function GetResult()
{
$retArray = array();
$this->ExecuteQuery();
while($rs = $this->connection instanceof MockDatabase ? $this->sqlResult->fetch_assoc() : mysqli_fetch_assoc($this->sqlResult))
{
$retArray[] = $rs;
}
$this->CleanQuery();
return $retArray;
}
function GetResultById($id = NULL)
{
$retArray = array();
$this->ExecuteQuery();
while($rs=mysqli_fetch_assoc($this->sqlResult))
{
$retArray[$rs[$id]] = $rs;
}
$this->CleanQuery();
return $retArray;
}
function GetTotalRows()
{
$this->ExecuteQuery();
if ($this->connection instanceof MockDatabase) {
return $this->sqlResult->num_rows;
} else {
return mysqli_num_rows($this->sqlResult);
}
}
function GetRow()
{
$this->ExecuteQuery();
if ($this->connection instanceof MockDatabase) {
$rs = $this->sqlResult->fetch_assoc();
} else {
$rs = mysqli_fetch_assoc($this->sqlResult);
}
$this->CleanQuery();
return $rs;
}
function GetSingle()
{
$this->ExecuteQuery();
if ($this->connection instanceof MockDatabase) {
$row = $this->sqlResult->fetch_array();
} else {
$row = mysqli_fetch_array($this->sqlResult);
}
$rs = $row[0];
if(!$rs)
$rs = 0;
$this->CleanQuery();
return $rs;
}
function InsertData()
{
$this->ExecuteQuery();
if ($this->connection instanceof MockDatabase) {
$last_id = $this->connection->insert_id();
} else {
$last_id = mysqli_insert_id($this->connection);
}
$this->CleanQuery();
return $last_id;
}
function UpdateData()
{
$this->ExecuteQuery();
if ($this->connection instanceof MockDatabase) {
$return = $this->connection->affected_rows();
} else {
$return = mysqli_affected_rows($this->connection);
}
$this->CleanQuery();
return $return;
}
function DeleteData()
{
return $this->UpdateData();
}
function CleanQuery()
{
if ($this->connection instanceof MockDatabase) {
$this->sqlResult->free();
} else {
// Only free if it's actually a result object, not a boolean
if ($this->sqlResult instanceof mysqli_result) {
@mysqli_free_result($this->sqlResult);
}
}
//$this->query = "";
}
function EnumSelect( $table , $field )
{
$this->query = "SHOW COLUMNS FROM `$table` LIKE '$field' ";
$this->ExecuteQuery();
$row = mysqli_fetch_array( $this->sqlResult , MYSQLI_NUM );
$regex = "/'(.*?)'/";
preg_match_all( $regex , $row[1], $enum_array );
$enum_fields = $enum_array[1];
return( $enum_fields );
}
}
?>