new approach to logging database access and upgraded adodb
[openemr.git] / library / adodb / drivers / adodb-sqlanywhere.inc.php
blobd7cadf96d57fa020b0b9db36d10c273c18a2d1f7
1 <?php
2 /*
3 version V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). 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.
10 Latest version is available at http://adodb.sourceforge.net
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.
17 2) Added blob support. Usage:
18 a) create blob variable on db server:
20 $dbconn->create_blobvar($blobVarName);
22 b) load blob var from file. $filename must be complete path
24 $dbcon->load_blobvar_from_file($blobVarName, $filename);
26 c) Use the $blobVarName in SQL insert or update statement in the values
27 clause:
29 $recordSet = $dbconn->Execute('INSERT INTO tabname (idcol, blobcol) '
31 'VALUES (\'test\', ' . $blobVarName . ')');
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);
37 d) drop blob variable on db server to free up resources:
38 $dbconn->drop_blobvar($blobVarName);
40 Sybase_SQLAnywhere data driver. Requires ODBC.
44 // security - hide paths
45 if (!defined('ADODB_DIR')) die();
47 if (!defined('_ADODB_ODBC_LAYER')) {
48 include(ADODB_DIR."/drivers/adodb-odbc.inc.php");
51 if (!defined('ADODB_SYBASE_SQLANYWHERE')){
53 define('ADODB_SYBASE_SQLANYWHERE',1);
55 class ADODB_sqlanywhere extends ADODB_odbc {
56 var $databaseType = "sqlanywhere";
57 var $hasInsertID = true;
59 function ADODB_sqlanywhere()
61 $this->ADODB_odbc();
64 function _insertid() {
65 return $this->GetOne('select @@identity');
68 function create_blobvar($blobVarName) {
69 $this->Execute("create variable $blobVarName long binary");
70 return;
73 function drop_blobvar($blobVarName) {
74 $this->Execute("drop variable $blobVarName");
75 return;
78 function load_blobvar_from_file($blobVarName, $filename) {
79 $chunk_size = 1000;
81 $fd = fopen ($filename, "rb");
83 $integer_chunks = (integer)filesize($filename) / $chunk_size;
84 $modulus = filesize($filename) % $chunk_size;
85 if ($modulus != 0){
86 $integer_chunks += 1;
89 for($loop=1;$loop<=$integer_chunks;$loop++){
90 $contents = fread ($fd, $chunk_size);
91 $contents = bin2hex($contents);
93 $hexstring = '';
95 for($loop2=0;$loop2<strlen($contents);$loop2+=2){
96 $hexstring .= '\x' . substr($contents,$loop2,2);
99 $hexstring = $this->qstr($hexstring);
101 $this->Execute("set $blobVarName = $blobVarName || " . $hexstring);
104 fclose ($fd);
105 return;
108 function load_blobvar_from_var($blobVarName, &$varName) {
109 $chunk_size = 1000;
111 $integer_chunks = (integer)strlen($varName) / $chunk_size;
112 $modulus = strlen($varName) % $chunk_size;
113 if ($modulus != 0){
114 $integer_chunks += 1;
117 for($loop=1;$loop<=$integer_chunks;$loop++){
118 $contents = substr ($varName, (($loop - 1) * $chunk_size), $chunk_size);
119 $contents = bin2hex($contents);
121 $hexstring = '';
123 for($loop2=0;$loop2<strlen($contents);$loop2+=2){
124 $hexstring .= '\x' . substr($contents,$loop2,2);
127 $hexstring = $this->qstr($hexstring);
129 $this->Execute("set $blobVarName = $blobVarName || " . $hexstring);
132 return;
136 Insert a null into the blob field of the table first.
137 Then use UpdateBlob to store the blob.
139 Usage:
141 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
142 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
144 function UpdateBlob($table,$column,&$val,$where,$blobtype='BLOB')
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;
153 }; //class
155 class ADORecordSet_sqlanywhere extends ADORecordSet_odbc {
157 var $databaseType = "sqlanywhere";
159 function ADORecordSet_sqlanywhere($id,$mode=false)
161 $this->ADORecordSet_odbc($id,$mode);
165 }; //class
168 } //define