summaryrefslogtreecommitdiff
path: root/includes/database.pear.inc
blob: 3446e3d69e9e39cc20e8bafa85c6b9f0827df0d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
// $Id$

require_once 'DB.php';

function db_connect($url) {
  global $db_handle;

  $db_handle = DB::connect($url);

  if (DB::isError($db_handle)) {
    die("Database problem: ". $db_handle->getMessage());
  }

  $db_handle->setFetchMode(DB_FETCHMODE_ASSOC);
}

/**
 * Runs a query in the database.
 *
 * @param $query sql query
 * @param $type  module type of this item
 * @return sql result resource
 */
function db_query($query) {
  $args = func_get_args();
  if (count($args) > 1) {
    $args = array_map("check_query", $args);
    $args[0] = $query;
    return _db_query(call_user_func_array("sprintf", $args));
  }
  else {
    return _db_query($query);
  }
}

// debug version
function db_queryd($query) {
  $args = func_get_args();
  if (count($args) > 1) {
    $args = array_map("check_query", $args);
    $args[0] = $query;
    return _db_query(call_user_func_array("sprintf", $args), 1);
  }
  else {
    return _db_query($query, 1);
  }
}

// private
function _db_query($query, $debug = 0) {
  global $db_handle, $queries;

  if (variable_get("dev_query", 0)) {
    $queries[] = $query;
  }

  $result = $db_handle->query($query);

  if ($debug) {
    print "<p>query: $query<br />";
  }

  if (DB::isError($result)) {
    watchdog("error", "database: ". $result->getMessage() ."\nquery: ". htmlspecialchars($query));
  }
  else {
    return $result;
  }
}

function db_fetch_object($result) {
  if ($result) {
    return $result->fetchRow(DB_FETCHMODE_OBJECT);
  }
}

function db_fetch_array($result) {
  if ($result) {
    return $result->fetchRow(DB_FETCHMODE_ASSOC);
  }
}

function db_num_rows($result) {
  if ($result) {
    return $result->numRows($result);
  }
}

function db_result($result, $row = 0) {
  if ($result && $result->numRows($result) > $row) {
    $tmp = $result->fetchRow(DB_FETCHMODE_ORDERED);
    return $tmp[$row];
  }
}

function db_error() {
  global $db_handle;

  return DB::isError($db_handle);
}

function db_next_id($name) {
  global $db_handle;

  return $db_handle->nextID($name);
}

function db_affected_rows() {
  global $db_handle;

  return $db_handle->affectedRows();
}

/**
 * Generates a limited query
 *
 * @param string  $query query
 * @param integer $from  the row to start to fetching
 * @param integer $count the numbers of rows to fetch
 *
 * @return mixed a DB_Result object or a DB_Error
 *
 * @access public
 */

function db_query_range($query, $from, $count) {
  global $db_handle;

  return $db_handle->limitQuery($query, $from, $count);
}

?>