File: ct_split_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 (170 lines) | stat: -rw-r--r-- 5,129 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
##
## Copyright (c) 1999 Internet Images srl
##                    Massimiliano Masserelli
##
## $Id: ct_split_sql.inc,v 1.4 1999/11/05 11:18:18 kir Exp $
##
## PHPLIB Data Storage Container using a SQL database and multiple
## rows for each element
##
## Every session-name pair will end up in one OR MORE table rows, thus
## allowing serialization of huge quantities of data.
##

class CT_Split_Sql {
    ##
    ## Define these parameters by overwriting or by
    ## deriving your own class from it (recommened)
    ##
    
    var $database_table = "active_sessions_split";
    var $database_class = "";
    var $database_lock_semaphore = "";
    var $split_length = 4096;  ## Split data every xxx bytes

    var $enc_methods = array(
      "base64"  => array("enc" => "base64_encode", "dec" => "base64_decode"),
      "slashes" => array("enc" => "addslashes",    "dec" => "strval")
    );
    var $encoding_mode = "base64";
    ## end of configuration

    var $db;

    function ac_start() {
        $name = $this->database_class;
        $this->db = new $name;
    }

    function ac_get_lock() {
        if ( "" != $this->database_lock_semaphore ) {
            while ( ! $this->db->query("SELECT get_lock('%s')", 
                        $this->database_lock_semaphore) ) {
                $t = 1 + time(); while ( $t > time() ) { ; }
            }
        }
    }

    function ac_release_lock() {
        if ( "" != $this->database_lock_semaphore ) {
            $this->db->query("SELECT release_lock('%s')", 
                    $this->database_lock_semaphore);
        }
    }

    function ac_gc($gc_time, $name) {
        $timeout = time();
        $sqldate = date("YmdHis", $timeout - ($gc_time * 60));
        $this->db->query(sprintf("DELETE FROM %s ".
                    "WHERE ct_changed < '%s' AND ct_name = '%s'",
                    $this->database_table, 
                    $sqldate,
                    addslashes($name)));
    }

    function ac_store($id, $name, $str) {
        $ret = true;
        
        $cmd  = $this->enc_methods[$this->encoding_mode]["enc"];
        $str  = sprintf("%s:%s", $this->encoding_mode, $cmd($str));
        
        $now = date("YmdHis", time());
        
        if ( $this->db->type == "oracle" )
        {
            Ora_CommitOff($this->db->Link_ID);
        } else {
            $this->db->query("BEGIN TRANSACTION");
        }
        $this->ac_delete($id, $name);
        
        $count = 0;
        while ($part = substr($str, 0, $this->split_length)) {
            $this->db->query(sprintf("INSERT INTO %s ".
                " (ct_sid, ct_name, ct_pos, ct_val, ct_changed) ".
                "  VALUES ".
                " ('%s','%s','%06d','%s','%s')",
                $this->database_table,
                $id,
                addslashes($name),
                $count++,
                $part,
                $now
            ));
            $str = substr($str, $this->split_length);
        }
        if ( $this->db->type == "oracle" )
        {
            // Commit transaction
            if(!@Ora_Commit($this->db->Link_ID))
            {
                $ret = false;
            }
            // Enable autocommit again            
            Ora_CommitOn($this->db->Link_ID);
        } else {
            $this->db->query("END TRANSACTION");
        }
        return $ret;
    }

    function ac_delete($id, $name) {
        $this->db->query(sprintf("DELETE FROM %s ".
                    "WHERE ct_name = '%s' AND ct_sid = '%s'",
                    $this->database_table,
                    addslashes($name),
                    $id));
    }

    function ac_get_value($id, $name) {
        $this->db->query(sprintf("SELECT ct_val, ct_pos FROM %s ".
                        "WHERE ct_sid  = '%s' AND ct_name = '%s' ".
                        "ORDER BY ct_pos",
                    $this->database_table,
                    $id,
                    addslashes($name)));
        $str="";
        while ($this->db->next_record()) {
          $str .= $this->db->f("ct_val");
        }

        # get encoding method from (method:value) pair
        $colon_pos = strpos($str,':');
        $str_method = substr ( $str , 0 , $colon_pos );
        if( empty($str_method) ) { 
            // For compatibility with old sessions
            $str_method = "base64";
            $str_value  = $str;
        } else {
            // New format case
            $str_value  = substr ( $str , $colon_pos + 1 );
        }
        $cmd  = $this->enc_methods[$str_method]["dec"];

        $str  = $cmd($str_value);
        
## DEB        echo $str;
        return $str;
    }

    function ac_newid($str, $name) {
        return $str;
    }

    function ac_halt($s) {
        $this->db->halt($s);
    }
   
    ## provide your own encoding/decoding method(s)
    function set_enc_meth($name, $enc_func, $dec_func) {
      if (!isset($this->enc_methods[$name])) {
        $this->enc_methods[$name] = array ('enc'=> $enc_func, 'dec'=> $dec_func) ;
      }
    }

    function get_enc_meth($name) {
      return $this->enc_methods[$name];
    }
}
?>