Projekt

Obecné

Profil

Stáhnout (3.59 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2
/*
3
 *
4
 * Copyright (c) 1998-2000 NetUSE AG
5
 *                    Boris Erdmann, Kristian Koehntopp
6
 *
7
 * $Id: db_pgsql.inc,v 1.9 2002/08/26 08:27:43 richardarcher Exp $
8
 *
9
 */
10

    
11
class DB_Sql {
12
  var $Host     = "Filek.cz";
13
  var $Database = "aswi-dictionary";
14
  var $User     = "dbowner";
15
  var $Password = "";
16

    
17
  /** @var mysqli_result  */
18
  var $result = null;
19

    
20
  var $row = null;
21

    
22
  /** @var mysqli */
23
  var $connection = null;
24
  var $Debug = 0;
25

    
26
  /* public: constructor */
27
  function __construct($query = "") {
28
      $this->query($query);
29
  }
30

    
31
  function connect() {
32
    $this->connection = new mysqli($this->Host, $this->User, $this->Password, $this->Database);
33
    $this->connection->set_charset("utf8");
34

    
35
    if ($this->connection->connect_error) {
36
      $this->halt("Connection failed: " . $this->connection->connect_error);
37
    }
38
  }
39

    
40
    function escape_string($string)
41
    {
42
        if ($this->connection == null)
43
        {
44
            $this->connection = new mysqli($this->Host, $this->User, $this->Password, $this->Database);
45
            $this->connection->set_charset("utf8");
46
        }
47
        return $this->connection->real_escape_string($string);
48
    }
49

    
50
  function query(string $Query_String) {
51
    /* No empty queries, please, since PHP4 chokes on them. */
52
    if ($Query_String == "")
53
      /* The empty query string is passed on from the constructor,
54
       * when calling the class without a query, e.g. in situations
55
       * like these: '$db = new DB_Sql_Subclass;'
56
       */
57
      return 0;
58

    
59
    $this->connect();
60

    
61
    if ($this->Debug)
62
      printf("<br>Debug: query = %s<br>\n", $Query_String);
63

    
64
    $this->result = $this->connection->query($Query_String);
65

    
66
//    if ($this->result) {
67
//      $this->halt("Invalid SQL: ".$Query_String);
68
//    }
69

    
70
    return $this->result;
71
  }
72

    
73
  function next_record() {
74
    $this->row = mysqli_fetch_row($this->result);
75
    return $this->row;
76
  }
77

    
78
  function metadata($table="") {
79
    $count = 0;
80
    $id    = 0;
81
    $res   = array();
82

    
83
    if (!empty($table)) {
84
       $this->connect();
85
       $id = pg_exec($this->Link_ID, "select * from $table");
86
       if ($id < 0) {
87
          $this->Error = pg_ErrorMessage($id);
88
          $this->Errno = 1;
89
          $this->halt("Metadata query failed.");
90
       }
91
    } else {
92
      $id = $this->Query_ID;
93
      if (!$id) {
94
        $this->halt("No query specified.");
95
      }
96
    }
97

    
98
    $count = pg_NumFields($id);
99

    
100
    for ($i=0; $i<$count; $i++) {
101
      $res[$i]["table"] = $table;
102
      $res[$i]["name"]  = pg_FieldName  ($id, $i);
103
      $res[$i]["type"]  = pg_FieldType  ($id, $i);
104
      $res[$i]["len"]   = pg_FieldSize  ($id, $i);
105
      $res[$i]["flags"] = "";
106
    }
107

    
108
    if ($table) {
109
      pg_FreeResult($id);
110
    }
111

    
112
    return $res;
113
  }
114

    
115
  function affected_rows() {
116
    return $this->connection->affected_rows;
117
  }
118

    
119
  function num_rows() {
120
    return $this->result->num_rows;
121
  }
122

    
123
  function num_fields() {
124
    return $this->result->field_count;
125
  }
126

    
127
  function f($Name) {
128
    return $this->result[$Name];
129
  }
130

    
131
  function halt($msg) {
132
    printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
133
    printf("<b>PostgreSQL Error</b>: %s (%s)<br>\n",
134
      $this->connection->errno,
135
      $this->connection->error);
136
//    die("Session halted."); // potřebuju vidět errory v logu
137
  }
138

    
139
  function table_names() {
140
    $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'");
141
    $i=0;
142
    while ($this->next_record())
143
     {
144
      $return[$i]["table_name"]= $this->f(0);
145
      $return[$i]["tablespace_name"]=$this->Database;
146
      $return[$i]["database"]=$this->Database;
147
      $i++;
148
     }
149
    return $return;
150
  }
151
}
152
?>
    (1-1/1)