File: be_sql.inc

package info (click to toggle)
phplib 1%3A7.3dev-3.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,752 kB
  • ctags: 247
  • sloc: php: 6,659; perl: 323; pascal: 157; makefile: 102; sh: 7
file content (130 lines) | stat: -rw-r--r-- 3,692 bytes parent folder | download
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
<?php

##
## Copyright (c) 1999 Internet Images srl
##                    Massimiliano Masserelli <negro@interim.it>
##
## $Id: be_sql.inc,v 1.1 1999/07/22 13:44:27 negro Exp $
##
## PHPLIB Blob Engine using plain sql
## depends on db_sql
## 
## This is a "reference" class to be used only as an example. This should
## not be used in applications.
## 
## It's also a good skeleton for writing a new Blob Engine
##
## The table used by this class must contain three fields:
##  be_bid	16 chars
##  be_seq	6 chars
##  be_data     4096 chars


class BE_Sql {
    ##
    ## Define following parameters by overwriting or by deriving 
    ## your own class (recommended)
    ##
    var $database_class = "DB_Sql";     ## access table via this class
    var $database_table = "be_table";   ## write data into this table
    var $split_length   = 4096;         ## maximum amount of data for each row
                                        ## this value should be safe enough
    ## end of configuration

    var $db;  ## Internal, object used for db connection
    
    function start() {
        $name = $this->database_class;
        $this->db = new $name;
    }
    
    function blob_create() {
        if (! is_object($this->db)) {
            $this->start();
        }
        $bn = false;
        $count = 0;
        while (! $bn && ($count++ < 10)) {
            $bn = uniqid("");
            $this->db->query(sprintf("INSERT INTO %s".
                        " (be_bid, be_seq, be_data) ".
                        "   VALUES ".
                        " ('%s', '%06d', '')", $this->database_table, $bn, 0));
            if ($this->db->affected_rows() != 1) {
                $bn = "";
            }
        }
        return $bn;
    }

    function blob_open($id) {
        if (! is_object($this->db)) {
            $this->start();
        }
        $this->db->query("SELECT count(*) FROM ". $this->database_table. 
                    " WHERE be_bid = '$id'");
        if ($this->db->next_record()) {
          return ($this->db->f(0) > 0);
        } else {
          return false;
        }
    }

    function blob_close($id) {
        return true;
    }

    function blob_delete($id) {
        if (! is_object($this->db)) {
            $this->start();
        }
        $this->db->query("DELETE FROM ". $this->database_table.
                    " WHERE be_bid = '$id'");
        return true;
    }

    function blob_read($id) {
        if (! is_object($this->db)) {
            $this->start();
        }
        $str = "";
        $this->db->query("SELECT be_data, be_seq FROM ". $this->database_table.
                    " WHERE be_bid = '$id' ORDER BY be_seq");
        while ($this->db->next_record()) {
          $str .= $this->db->f(0);
        }
        return base64_decode($str);
    }

    function blob_write($id, $data) {
        if (! is_object($this->db)) {
            $this->start();
        }
        $this->db->query("BEGIN TRANSACTION");
        $this->db->query("DELETE FROM ". $this->database_table.
                    " WHERE be_bid = '$id'");
        $str = base64_encode($data);
        $seq = 0;
        while ($part = substr($str, 0, $this->split_length)) {
          $this->db->query(sprintf("INSERT INTO %s ".
                "(be_bid, be_seq, be_data) ".
                " VALUES ".
                "('%s', '%06d', '%s')",
                $this->database_table,
                $id,
                $seq++,
                $part));
          $str = substr($str, $this->split_length);
        }
        $this->db->query("END TRANSACTION");
        return true;
    }


##    function halt($s) {
##        echo "<b>$s</b>";
##        exit;
##    }

}
?>