]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/drivers/adodb-sqlanywhere.inc.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / drivers / adodb-sqlanywhere.inc.php
1 <?php
2 /*
3 version V4.22 15 Apr 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     {
54         var $databaseType = "sqlanywhere";
55         var $hasInsertID = true;
56
57         function ADODB_sqlanywhere()
58         {
59             $this->ADODB_odbc();
60         }
61
62         function _insertid()
63         {
64             return $this->GetOne('select @@identity');
65         }
66
67         function create_blobvar($blobVarName)
68         {
69             $this->Execute("create variable $blobVarName long binary");
70             return;
71         }
72
73         function drop_blobvar($blobVarName)
74         {
75             $this->Execute("drop variable $blobVarName");
76             return;
77         }
78
79         function load_blobvar_from_file($blobVarName, $filename)
80         {
81             $chunk_size = 1000;
82
83             $fd = fopen($filename, "rb");
84
85             $integer_chunks = (integer)filesize($filename) / $chunk_size;
86             $modulus = filesize($filename) % $chunk_size;
87             if ($modulus != 0) {
88                 $integer_chunks += 1;
89             }
90
91             for ($loop = 1; $loop <= $integer_chunks; $loop++) {
92                 $contents = fread($fd, $chunk_size);
93                 $contents = bin2hex($contents);
94
95                 $hexstring = '';
96
97                 for ($loop2 = 0; $loop2 < strlen($contents); $loop2 += 2) {
98                     $hexstring .= '\x' . substr($contents, $loop2, 2);
99                 }
100
101                 $hexstring = $this->qstr($hexstring);
102
103                 $this->Execute("set $blobVarName = $blobVarName || " . $hexstring);
104             }
105
106             fclose($fd);
107             return;
108         }
109
110         function load_blobvar_from_var($blobVarName, &$varName)
111         {
112             $chunk_size = 1000;
113
114             $integer_chunks = (integer)strlen($varName) / $chunk_size;
115             $modulus = strlen($varName) % $chunk_size;
116             if ($modulus != 0) {
117                 $integer_chunks += 1;
118             }
119
120             for ($loop = 1; $loop <= $integer_chunks; $loop++) {
121                 $contents = substr($varName, (($loop - 1) * $chunk_size), $chunk_size);
122                 $contents = bin2hex($contents);
123
124                 $hexstring = '';
125
126                 for ($loop2 = 0; $loop2 < strlen($contents); $loop2 += 2) {
127                     $hexstring .= '\x' . substr($contents, $loop2, 2);
128                 }
129
130                 $hexstring = $this->qstr($hexstring);
131
132                 $this->Execute("set $blobVarName = $blobVarName || " . $hexstring);
133             }
134
135             return;
136         }
137
138         /*
139          Insert a null into the blob field of the table first.
140          Then use UpdateBlob to store the blob.
141
142          Usage:
143
144          $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
145          $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
146         */
147         function UpdateBlob($table, $column, &$val, $where, $blobtype = 'BLOB')
148         {
149             $blobVarName = 'hold_blob';
150             $this->create_blobvar($blobVarName);
151             $this->load_blobvar_from_var($blobVarName, $val);
152             $this->Execute("UPDATE $table SET $column=$blobVarName WHERE $where");
153             $this->drop_blobvar($blobVarName);
154             return true;
155         }
156     }
157
158     ; //class
159
160     class  ADORecordSet_sqlanywhere extends ADORecordSet_odbc
161     {
162
163         var $databaseType = "sqlanywhere";
164
165         function ADORecordSet_sqlanywhere($id, $mode = false)
166         {
167             $this->ADORecordSet_odbc($id, $mode);
168         }
169
170     }
171
172     ; //class
173
174 } //define