~ubuntu-branches/ubuntu/hoary/moodle/hoary

« back to all changes in this revision

Viewing changes to lib/adodb/drivers/adodb-sqlanywhere.inc.php

  • Committer: Bazaar Package Importer
  • Author(s): Isaac Clerencia
  • Date: 2004-12-29 00:49:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041229004952-gliyqzpj2w3e7clx
Tags: 1.4.3-1
* Urgency high as upstream release fixes several security bugs
* New upstream release
* Write database creation errors and warn the user about it, 
closes: #285842, #285842

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/* 
3
 
version V4.20 22 Feb 2004 (c) 2000-2004  John Lim (jlim@natsoft.com.my). All rights
4
 
reserved.
5
 
  Released under both BSD license and Lesser GPL library license. 
6
 
  Whenever there is any discrepancy between the two licenses, 
7
 
  the BSD license will take precedence. 
8
 
Set tabs to 4 for best viewing.
9
 
 
10
 
  Latest version is available at http://php.weblogs.com/
11
 
 
12
 
  21.02.2002 - Wade Johnson wade@wadejohnson.de
13
 
                           Extended ODBC class for Sybase SQLAnywhere.
14
 
   1) Added support to retrieve the last row insert ID on tables with
15
 
          primary key column using autoincrement function.
16
 
 
17
 
   2) Added blob support.  Usage:
18
 
                 a) create blob variable on db server:
19
 
 
20
 
                $dbconn->create_blobvar($blobVarName);
21
 
 
22
 
          b) load blob var from file.  $filename must be complete path
23
 
 
24
 
          $dbcon->load_blobvar_from_file($blobVarName, $filename);
25
 
 
26
 
          c) Use the $blobVarName in SQL insert or update statement in the values
27
 
          clause:
28
 
 
29
 
                $recordSet = $dbconn->Execute('INSERT INTO tabname (idcol, blobcol) '
30
 
                .
31
 
           'VALUES (\'test\', ' . $blobVarName . ')');
32
 
 
33
 
         instead of loading blob from a file, you can also load from 
34
 
          an unformatted (raw) blob variable:
35
 
          $dbcon->load_blobvar_from_var($blobVarName, $varName);
36
 
 
37
 
          d) drop blob variable on db server to free up resources:
38
 
          $dbconn->drop_blobvar($blobVarName);
39
 
 
40
 
  Sybase_SQLAnywhere data driver. Requires ODBC.
41
 
 
42
 
*/
43
 
 
44
 
if (!defined('_ADODB_ODBC_LAYER')) {
45
 
 include(ADODB_DIR."/drivers/adodb-odbc.inc.php");
46
 
}
47
 
 
48
 
if (!defined('ADODB_SYBASE_SQLANYWHERE')){
49
 
 
50
 
 define('ADODB_SYBASE_SQLANYWHERE',1);
51
 
 
52
 
 class ADODB_sqlanywhere extends ADODB_odbc {
53
 
        var $databaseType = "sqlanywhere";      
54
 
        var $hasInsertID = true;
55
 
        
56
 
        function ADODB_sqlanywhere()
57
 
        {
58
 
                $this->ADODB_odbc();
59
 
        }
60
 
 
61
 
         function _insertid() {
62
 
           return $this->GetOne('select @@identity');
63
 
         }
64
 
 
65
 
  function create_blobvar($blobVarName) {
66
 
   $this->Execute("create variable $blobVarName long binary");
67
 
   return;
68
 
  }
69
 
 
70
 
  function drop_blobvar($blobVarName) {
71
 
   $this->Execute("drop variable $blobVarName");
72
 
   return;
73
 
  }
74
 
 
75
 
  function load_blobvar_from_file($blobVarName, $filename) {
76
 
   $chunk_size = 1000;
77
 
 
78
 
   $fd = fopen ($filename, "rb");
79
 
 
80
 
   $integer_chunks = (integer)filesize($filename) / $chunk_size;
81
 
   $modulus = filesize($filename) % $chunk_size;
82
 
   if ($modulus != 0){
83
 
        $integer_chunks += 1;
84
 
   }
85
 
 
86
 
   for($loop=1;$loop<=$integer_chunks;$loop++){
87
 
        $contents = fread ($fd, $chunk_size);
88
 
        $contents = bin2hex($contents);
89
 
 
90
 
        $hexstring = '';
91
 
 
92
 
        for($loop2=0;$loop2<strlen($contents);$loop2+=2){
93
 
         $hexstring .= '\x' . substr($contents,$loop2,2);
94
 
         }
95
 
 
96
 
        $hexstring = $this->qstr($hexstring);
97
 
 
98
 
        $this->Execute("set $blobVarName = $blobVarName || " . $hexstring);
99
 
   }
100
 
 
101
 
   fclose ($fd);
102
 
   return;
103
 
  }
104
 
 
105
 
  function load_blobvar_from_var($blobVarName, &$varName) {
106
 
   $chunk_size = 1000;
107
 
 
108
 
   $integer_chunks = (integer)strlen($varName) / $chunk_size;
109
 
   $modulus = strlen($varName) % $chunk_size;
110
 
   if ($modulus != 0){
111
 
        $integer_chunks += 1;
112
 
   }
113
 
 
114
 
   for($loop=1;$loop<=$integer_chunks;$loop++){
115
 
        $contents = substr ($varName, (($loop - 1) * $chunk_size), $chunk_size);
116
 
        $contents = bin2hex($contents);
117
 
 
118
 
        $hexstring = '';
119
 
 
120
 
        for($loop2=0;$loop2<strlen($contents);$loop2+=2){
121
 
         $hexstring .= '\x' . substr($contents,$loop2,2);
122
 
         }
123
 
 
124
 
        $hexstring = $this->qstr($hexstring);
125
 
 
126
 
        $this->Execute("set $blobVarName = $blobVarName || " . $hexstring);
127
 
   }
128
 
 
129
 
   return;
130
 
  }
131
 
 
132
 
 /*
133
 
  Insert a null into the blob field of the table first.
134
 
  Then use UpdateBlob to store the blob.
135
 
 
136
 
  Usage:
137
 
 
138
 
  $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
139
 
  $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
140
 
 */
141
 
  function UpdateBlob($table,$column,&$val,$where,$blobtype='BLOB')
142
 
  {
143
 
   $blobVarName = 'hold_blob';
144
 
   $this->create_blobvar($blobVarName);
145
 
   $this->load_blobvar_from_var($blobVarName, $val);
146
 
   $this->Execute("UPDATE $table SET $column=$blobVarName WHERE $where");
147
 
   $this->drop_blobvar($blobVarName);
148
 
   return true;
149
 
  }
150
 
 }; //class
151
 
 
152
 
 class  ADORecordSet_sqlanywhere extends ADORecordSet_odbc {    
153
 
 
154
 
  var $databaseType = "sqlanywhere";            
155
 
 
156
 
 function ADORecordSet_sqlanywhere($id,$mode=false)
157
 
 {
158
 
  $this->ADORecordSet_odbc($id,$mode);
159
 
 }
160
 
 
161
 
 
162
 
 }; //class
163
 
 
164
 
 
165
 
} //define
166
 
?>
 
1
<?php
 
2
/* 
 
3
version V4.50 6 July 2004 (c) 2000-2004  John Lim (jlim@natsoft.com.my). All rights
 
4
reserved.
 
5
  Released under both BSD license and Lesser GPL library license. 
 
6
  Whenever there is any discrepancy between the two licenses, 
 
7
  the BSD license will take precedence. 
 
8
Set tabs to 4 for best viewing.
 
9
 
 
10
  Latest version is available at http://adodb.sourceforge.net
 
11
 
 
12
  21.02.2002 - Wade Johnson wade@wadejohnson.de
 
13
                           Extended ODBC class for Sybase SQLAnywhere.
 
14
   1) Added support to retrieve the last row insert ID on tables with
 
15
          primary key column using autoincrement function.
 
16
 
 
17
   2) Added blob support.  Usage:
 
18
                 a) create blob variable on db server:
 
19
 
 
20
                $dbconn->create_blobvar($blobVarName);
 
21
 
 
22
          b) load blob var from file.  $filename must be complete path
 
23
 
 
24
          $dbcon->load_blobvar_from_file($blobVarName, $filename);
 
25
 
 
26
          c) Use the $blobVarName in SQL insert or update statement in the values
 
27
          clause:
 
28
 
 
29
                $recordSet = $dbconn->Execute('INSERT INTO tabname (idcol, blobcol) '
 
30
                .
 
31
           'VALUES (\'test\', ' . $blobVarName . ')');
 
32
 
 
33
         instead of loading blob from a file, you can also load from 
 
34
          an unformatted (raw) blob variable:
 
35
          $dbcon->load_blobvar_from_var($blobVarName, $varName);
 
36
 
 
37
          d) drop blob variable on db server to free up resources:
 
38
          $dbconn->drop_blobvar($blobVarName);
 
39
 
 
40
  Sybase_SQLAnywhere data driver. Requires ODBC.
 
41
 
 
42
*/
 
43
 
 
44
// security - hide paths
 
45
if (!defined('ADODB_DIR')) die();
 
46
 
 
47
if (!defined('_ADODB_ODBC_LAYER')) {
 
48
 include(ADODB_DIR."/drivers/adodb-odbc.inc.php");
 
49
}
 
50
 
 
51
if (!defined('ADODB_SYBASE_SQLANYWHERE')){
 
52
 
 
53
 define('ADODB_SYBASE_SQLANYWHERE',1);
 
54
 
 
55
 class ADODB_sqlanywhere extends ADODB_odbc {
 
56
        var $databaseType = "sqlanywhere";      
 
57
        var $hasInsertID = true;
 
58
        
 
59
        function ADODB_sqlanywhere()
 
60
        {
 
61
                $this->ADODB_odbc();
 
62
        }
 
63
 
 
64
         function _insertid() {
 
65
           return $this->GetOne('select @@identity');
 
66
         }
 
67
 
 
68
  function create_blobvar($blobVarName) {
 
69
   $this->Execute("create variable $blobVarName long binary");
 
70
   return;
 
71
  }
 
72
 
 
73
  function drop_blobvar($blobVarName) {
 
74
   $this->Execute("drop variable $blobVarName");
 
75
   return;
 
76
  }
 
77
 
 
78
  function load_blobvar_from_file($blobVarName, $filename) {
 
79
   $chunk_size = 1000;
 
80
 
 
81
   $fd = fopen ($filename, "rb");
 
82
 
 
83
   $integer_chunks = (integer)filesize($filename) / $chunk_size;
 
84
   $modulus = filesize($filename) % $chunk_size;
 
85
   if ($modulus != 0){
 
86
        $integer_chunks += 1;
 
87
   }
 
88
 
 
89
   for($loop=1;$loop<=$integer_chunks;$loop++){
 
90
        $contents = fread ($fd, $chunk_size);
 
91
        $contents = bin2hex($contents);
 
92
 
 
93
        $hexstring = '';
 
94
 
 
95
        for($loop2=0;$loop2<strlen($contents);$loop2+=2){
 
96
         $hexstring .= '\x' . substr($contents,$loop2,2);
 
97
         }
 
98
 
 
99
        $hexstring = $this->qstr($hexstring);
 
100
 
 
101
        $this->Execute("set $blobVarName = $blobVarName || " . $hexstring);
 
102
   }
 
103
 
 
104
   fclose ($fd);
 
105
   return;
 
106
  }
 
107
 
 
108
  function load_blobvar_from_var($blobVarName, &$varName) {
 
109
   $chunk_size = 1000;
 
110
 
 
111
   $integer_chunks = (integer)strlen($varName) / $chunk_size;
 
112
   $modulus = strlen($varName) % $chunk_size;
 
113
   if ($modulus != 0){
 
114
        $integer_chunks += 1;
 
115
   }
 
116
 
 
117
   for($loop=1;$loop<=$integer_chunks;$loop++){
 
118
        $contents = substr ($varName, (($loop - 1) * $chunk_size), $chunk_size);
 
119
        $contents = bin2hex($contents);
 
120
 
 
121
        $hexstring = '';
 
122
 
 
123
        for($loop2=0;$loop2<strlen($contents);$loop2+=2){
 
124
         $hexstring .= '\x' . substr($contents,$loop2,2);
 
125
         }
 
126
 
 
127
        $hexstring = $this->qstr($hexstring);
 
128
 
 
129
        $this->Execute("set $blobVarName = $blobVarName || " . $hexstring);
 
130
   }
 
131
 
 
132
   return;
 
133
  }
 
134
 
 
135
 /*
 
136
  Insert a null into the blob field of the table first.
 
137
  Then use UpdateBlob to store the blob.
 
138
 
 
139
  Usage:
 
140
 
 
141
  $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
 
142
  $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
 
143
 */
 
144
  function UpdateBlob($table,$column,&$val,$where,$blobtype='BLOB')
 
145
  {
 
146
   $blobVarName = 'hold_blob';
 
147
   $this->create_blobvar($blobVarName);
 
148
   $this->load_blobvar_from_var($blobVarName, $val);
 
149
   $this->Execute("UPDATE $table SET $column=$blobVarName WHERE $where");
 
150
   $this->drop_blobvar($blobVarName);
 
151
   return true;
 
152
  }
 
153
 }; //class
 
154
 
 
155
 class  ADORecordSet_sqlanywhere extends ADORecordSet_odbc {    
 
156
 
 
157
  var $databaseType = "sqlanywhere";            
 
158
 
 
159
 function ADORecordSet_sqlanywhere($id,$mode=false)
 
160
 {
 
161
  $this->ADORecordSet_odbc($id,$mode);
 
162
 }
 
163
 
 
164
 
 
165
 }; //class
 
166
 
 
167
 
 
168
} //define
 
169
?>