Newer
Older
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
* @param array specifies the types of the fields
*
* @return mixed MDB2_OK on success, a MDB2 error on failure
*
* @access public
* @see bindParam()
*/
function bindValueArray($values, $types = null)
{
$types = is_array($types) ? array_values($types) : array_fill(0, count($values), null);
$parameters = array_keys($values);
foreach ($parameters as $key => $parameter) {
$this->db->pushErrorHandling(PEAR_ERROR_RETURN);
$this->db->expectError(MDB2_ERROR_NOT_FOUND);
$err = $this->bindValue($parameter, $values[$parameter], $types[$key]);
$this->db->popExpect();
$this->db->popErrorHandling();
if (PEAR::isError($err)) {
if ($err->getCode() == MDB2_ERROR_NOT_FOUND) {
//ignore (extra value for missing placeholder)
continue;
}
return $err;
}
}
return MDB2_OK;
}
// }}}
// {{{ function bindParam($parameter, &$value, $type = null)
/**
* Bind a variable to a parameter of a prepared query.
*
* @param int the order number of the parameter in the query
* statement. The order number of the first parameter is 1.
* @param mixed variable that is meant to be bound to specified
* parameter. The type of the value depends on the $type argument.
* @param string specifies the type of the field
*
* @return mixed MDB2_OK on success, a MDB2 error on failure
*
* @access public
*/
function bindParam($parameter, &$value, $type = null)
{
if (!is_numeric($parameter)) {
$parameter = preg_replace('/^:(.*)$/', '\\1', $parameter);
}
if (!in_array($parameter, $this->positions)) {
return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
}
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
if (!is_null($type)) {
$this->types[$parameter] = $type;
}
return MDB2_OK;
}
// }}}
// {{{ function bindParamArray(&$values, $types = null)
/**
* Bind the variables of multiple a parameter of a prepared query in bulk.
*
* @param array specifies all necessary information
* for bindParam() the array elements must use keys corresponding to
* the number of the position of the parameter.
* @param array specifies the types of the fields
*
* @return mixed MDB2_OK on success, a MDB2 error on failure
*
* @access public
* @see bindParam()
*/
function bindParamArray(&$values, $types = null)
{
$types = is_array($types) ? array_values($types) : array_fill(0, count($values), null);
$parameters = array_keys($values);
foreach ($parameters as $key => $parameter) {
$err = $this->bindParam($parameter, $values[$parameter], $types[$key]);
if (PEAR::isError($err)) {
return $err;
}
}
return MDB2_OK;
}
// }}}
// {{{ function &execute($values = null, $result_class = true, $result_wrap_class = false)
/**
* Execute a prepared query statement.
*
* @param array specifies all necessary information
* for bindParam() the array elements must use keys corresponding
* to the number of the position of the parameter.
* @param mixed specifies which result class to use
* @param mixed specifies which class to wrap results in
*
* @return mixed MDB2_Result or integer (affected rows) on success,
* a MDB2 error on failure
* @access public
*/
function &execute($values = null, $result_class = true, $result_wrap_class = false)
{
if (is_null($this->positions)) {
return $this->db->raiseError(MDB2_ERROR, null, null,
'Prepared statement has already been freed', __FUNCTION__);
}
$values = (array)$values;
if (!empty($values)) {
$err = $this->bindValueArray($values);
if (PEAR::isError($err)) {
return $this->db->raiseError(MDB2_ERROR, null, null,
'Binding Values failed with message: ' . $err->getMessage(), __FUNCTION__);
}
}
$result =$this->_execute($result_class, $result_wrap_class);
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
return $result;
}
// }}}
// {{{ function &_execute($result_class = true, $result_wrap_class = false)
/**
* Execute a prepared query statement helper method.
*
* @param mixed specifies which result class to use
* @param mixed specifies which class to wrap results in
*
* @return mixed MDB2_Result or integer (affected rows) on success,
* a MDB2 error on failure
* @access private
*/
function &_execute($result_class = true, $result_wrap_class = false)
{
$this->last_query = $this->query;
$query = '';
$last_position = 0;
foreach ($this->positions as $current_position => $parameter) {
if (!array_key_exists($parameter, $this->values)) {
return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
}
$value = $this->values[$parameter];
$query.= substr($this->query, $last_position, $current_position - $last_position);
if (!isset($value)) {
$value_quoted = 'NULL';
} else {
$type = !empty($this->types[$parameter]) ? $this->types[$parameter] : null;
$value_quoted = $this->db->quote($value, $type);
if (PEAR::isError($value_quoted)) {
return $value_quoted;
}
}
$query.= $value_quoted;
$last_position = $current_position + 1;
}
$query.= substr($this->query, $last_position);
$this->db->offset = $this->offset;
$this->db->limit = $this->limit;
if ($this->is_manip) {
$result = $this->db->exec($query);
} else {
$result =$this->db->query($query, $this->result_types, $result_class, $result_wrap_class);
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
}
return $result;
}
// }}}
// {{{ function free()
/**
* Release resources allocated for the specified prepared query.
*
* @return mixed MDB2_OK on success, a MDB2 error on failure
*
* @access public
*/
function free()
{
if (is_null($this->positions)) {
return $this->db->raiseError(MDB2_ERROR, null, null,
'Prepared statement has already been freed', __FUNCTION__);
}
$this->statement = null;
$this->positions = null;
$this->query = null;
$this->types = null;
$this->result_types = null;
$this->limit = null;
$this->is_manip = null;
$this->offset = null;
$this->values = null;
return MDB2_OK;
}
// }}}
}
// }}}
// {{{ class MDB2_Module_Common
/**
* The common modules class for MDB2 module objects
*
* @package MDB2
* @category Database
* @author Lukas Smith <smith@pooteeweet.org>
*/
class MDB2_Module_Common
{
// {{{ Variables (Properties)
/**
* contains the key to the global MDB2 instance array of the associated
* MDB2 instance
*
* @var int
* @access protected
*/
var $db_index;
// }}}
// {{{ constructor: function __construct($db_index)
/**
* Constructor
*/
function __construct($db_index)
{
$this->db_index = $db_index;
}
// }}}
// {{{ function &getDBInstance()
/**
* Get the instance of MDB2 associated with the module instance
*
* @return object MDB2 instance or a MDB2 error on failure
*
* @access public
*/
{
if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
$result =$GLOBALS['_MDB2_databases'][$this->db_index];
$result =$this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
'could not find MDB2 instance');
}
return $result;
}
// }}}
}
// }}}
// {{{ function MDB2_closeOpenTransactions()
/**
* Close any open transactions form persistent connections
*
* @return void
*
* @access public
*/
function MDB2_closeOpenTransactions()
{
reset($GLOBALS['_MDB2_databases']);
while (next($GLOBALS['_MDB2_databases'])) {
$key = key($GLOBALS['_MDB2_databases']);
if ($GLOBALS['_MDB2_databases'][$key]->opened_persistent
&& $GLOBALS['_MDB2_databases'][$key]->in_transaction
) {
$GLOBALS['_MDB2_databases'][$key]->rollback();
}
}
}
// }}}
// {{{ function MDB2_defaultDebugOutput(&$db, $scope, $message, $is_manip = null)
/**
* default debug output handler
*
* @param object reference to an MDB2 database object
* @param string usually the method name that triggered the debug call:
* for example 'query', 'prepare', 'execute', 'parameters',
* 'beginTransaction', 'commit', 'rollback'
* @param string message that should be appended to the debug variable
* @param array contains context information about the debug() call
* common keys are: is_manip, time, result etc.
*
* @return void|string optionally return a modified message, this allows
* rewriting a query before being issued or prepared
*
* @access public
*/
function MDB2_defaultDebugOutput(&$db, $scope, $message, $context = array())
{
$db->debug_output.= $scope.'('.$db->db_index.'): ';
$db->debug_output.= $message.$db->getOption('log_line_break');
return $message;
}
// }}}