Ignore:
Timestamp:
Nov 20, 2020, 12:08:12 AM (3 years ago)
Author:
chronos
Message:
  • Added: Static types added to almost all classes, methods and function. Supported by PHP 7.4.
  • Fixed: Various found code issues.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/Common/Database.php

    r874 r887  
    22
    33// Extended database class
    4 // Date: 2016-01-11
     4// Date: 2020-11-10
    55
    66function microtime_float()
     
    1313{
    1414  var $PDOStatement;
    15   var $num_rows = 0;
     15  public int $num_rows = 0;
    1616
    1717  function fetch_assoc()
     
    3333class Database
    3434{
    35   var $Prefix;
    36   var $Functions;
    37   var $Type;
    38   var $PDO;
    39   var $Error;
    40   var $insert_id;
    41   var $LastQuery;
    42   var $ShowSQLError;
    43   var $ShowSQLQuery;
    44   var $LogSQLQuery;
    45   var $LogFile;
     35  public string $Prefix;
     36  public array $Functions;
     37  public string $Type;
     38  public PDO $PDO;
     39  public string $Error;
     40  public string $insert_id;
     41  public string $LastQuery;
     42  public bool $ShowSQLError;
     43  public bool $ShowSQLQuery;
     44  public bool $LogSQLQuery;
     45  public string $LogFile;
    4646
    4747  function __construct()
     
    5757    $this->LogFile = dirname(__FILE__).'/../../Query.log';
    5858  }
    59  
    60 
    61   function Connect($Host, $User, $Password, $Database)
     59
     60  function Connect(string $Host, string $User, string $Password, string $Database)
    6261  {
    6362    if ($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
     
    7978  }
    8079
    81   function Connected()
     80  function Connected(): bool
    8281  {
    8382    return isset($this->PDO);
    8483  }
    8584
    86   function select_db($Database)
     85  function select_db(string $Database)
    8786  {
    8887    $this->query('USE `'.$Database.'`');
    8988  }
    9089
    91   function query($Query)
     90  function query($Query): DatabaseResult
    9291  {
    9392    if (!$this->Connected()) throw new Exception(T('Not connected to database'));
     
    9998      $Time = round(microtime_float() - $QueryStartTime, 4);
    10099      $Duration = ' ; '.$Time. ' s';
    101     } 
     100    }
    102101    if (($this->LogSQLQuery == true) and ($Time != 0))
    103102      file_put_contents($this->LogFile, $Query.$Duration."\n", FILE_APPEND);
     
    112111      $this->insert_id = $this->PDO->lastInsertId();
    113112    } else
    114     {     
    115       $this->Error = $this->PDO->errorInfo();
    116       $this->Error = $this->Error[2];
     113    {
     114      $Error = $this->PDO->errorInfo();
     115      $this->Error = $Error[2];
    117116      if (($this->Error != '') and ($this->ShowSQLError == true))
    118117        echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>');
     
    122121  }
    123122
    124   function select($Table, $What = '*', $Condition = 1)
     123  function select(string $Table, string $What = '*', string $Condition = '1'): DatabaseResult
    125124  {
    126125    return $this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
    127126  }
    128127
    129   function delete($Table, $Condition)
     128  function delete(string $Table, string $Condition)
    130129  {
    131130    $this->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
    132131  }
    133132
    134   function insert($Table, $Data)
     133  function insert(string $Table, array $Data)
    135134  {
    136135    $this->query($this->GetInsert($Table, $Data));
    137136    $this->insert_id = $this->PDO->lastInsertId();
    138137  }
    139  
    140   function GetInsert($Table, $Data)
     138
     139  function GetInsert(string $Table, array $Data): string
    141140  {
    142141    $Name = '';
     
    157156  }
    158157
    159   function update($Table, $Condition, $Data)
     158  function update(string $Table, string $Condition, array $Data)
    160159  {
    161160    $this->query($this->GetUpdate($Table, $Condition, $Data));
    162161  }
    163  
    164   function GetUpdate($Table, $Condition, $Data)
     162
     163  function GetUpdate(string $Table, string $Condition, array $Data): string
    165164  {
    166165    $Values = '';
     
    178177  }
    179178
    180   function replace($Table, $Data)
     179  function replace(string $Table, array $Data)
    181180  {
    182181    $Name = '';
     
    199198  }
    200199
    201   function charset($Charset)
     200  function charset(string $Charset)
    202201  {
    203202    $this->query('SET NAMES "'.$Charset.'"');
    204203  }
    205204
    206   function real_escape_string($Text)
     205  function real_escape_string(string $Text): string
    207206  {
    208207    return addslashes($Text);
    209208  }
    210209
    211   function quote($Text)
     210  function quote(string $Text): string
    212211  {
    213212    return $this->PDO->quote($Text);
     
    222221  {
    223222  }
    224  
    225   public function Transaction($Queries)
     223
     224  public function Transaction(array $Queries)
    226225  {
    227226    //echo('|'."\n");
Note: See TracChangeset for help on using the changeset viewer.