public function execute($sql, array $data = null) { $this->connect(); if (!is_array($data)) { $data = []; } if (is_string($sql)) { return parent::execute($sql, $data); } $data = array_values($data); if ($sql->paramCount()) { if (count($data) < $sql->paramCount()) { throw new DatabaseException('Prepared execute - not enough parameters.'); } foreach ($data as $i => $v) { switch (gettype($v)) { case 'boolean': case 'integer': $sql->bindValue(':i' . $i, (int) $v, SQLITE3_INTEGER); break; case 'double': $sql->bindValue(':i' . $i, $v, SQLITE3_FLOAT); break; case 'array': $sql->bindValue(':i' . $i, implode(',', $v), SQLITE3_TEXT); break; case 'object': $sql->bindValue(':i' . $i, serialize($v), SQLITE3_TEXT); break; case 'resource': if (is_resource($v) && get_resource_type($v) === 'stream') { $sql->bindValue(':i' . $i, stream_get_contents($v), SQLITE3_TEXT); } else { $sql->bindValue(':i' . $i, serialize($v), SQLITE3_TEXT); } break; case 'NULL': $sql->bindValue(':i' . $i, null, SQLITE3_NULL); break; default: $sql->bindValue(':i' . $i, (string) $v, SQLITE3_TEXT); break; } } } $rtrn = $sql->execute(); if (!$rtrn) { throw new DatabaseException('Prepared execute error : ' . $this->lnk->lastErrorMsg()); } $this->iid = $this->lnk->lastInsertRowID(); $this->aff = $this->lnk->changes(); return $rtrn; }
public function execute($sql, array $data = null) { $this->connect(); if (!is_array($data)) { $data = array(); } if (is_string($sql)) { return parent::execute($sql, $data); } $data = array_values($data); if ($sql->param_count) { if (count($data) < $sql->param_count) { throw new DatabaseException('Prepared execute - not enough parameters.'); } $lds = 32 * 1024; $ref = array(''); $lng = array(); $nul = null; foreach ($data as $i => $v) { switch (gettype($v)) { case 'boolean': case 'integer': $data[$i] = (int) $v; $ref[0] .= 'i'; $ref[$i + 1] =& $data[$i]; break; case 'NULL': $ref[0] .= 's'; $ref[$i + 1] =& $data[$i]; break; case 'double': $ref[0] .= 'd'; $ref[$i + 1] =& $data[$i]; break; default: if (is_resource($data[$i]) && get_resource_type($data[$i]) === 'stream') { $ref[0] .= 'b'; $ref[$i + 1] =& $nul; $lng[] = $i; continue; } if (!is_string($data[$i])) { $data[$i] = serialize($data[$i]); } if (strlen($data[$i]) > $lds) { $ref[0] .= 'b'; $ref[$i + 1] =& $nul; $lng[] = $i; } else { $ref[0] .= 's'; $ref[$i + 1] =& $data[$i]; } break; } } call_user_func_array(array($sql, 'bind_param'), $ref); foreach ($lng as $index) { if (is_resource($data[$index]) && get_resource_type($data[$index]) === 'stream') { while (!feof($data[$index])) { $sql->send_long_data($index, fread($data[$index], $lds)); } } else { $data[$index] = str_split($data[$index], $lds); foreach ($data[$index] as $chunk) { $sql->send_long_data($index, $chunk); } } } } $rtrn = $sql->execute(); if (!$this->mnd) { $sql->store_result(); } if (!$rtrn) { throw new DatabaseException('Prepared execute error : ' . $this->lnk->error); } $this->iid = $this->lnk->insert_id; $this->aff = $this->lnk->affected_rows; if (!$this->mnd) { return $sql->field_count ? $sql : $rtrn; } return $sql->field_count ? $sql->get_result() : $rtrn; }