XML
|
|
|
|
<?php /*
This code is application server that allows to display and edit database table
contents with native Borland Delphi components using SOAP.
1. Your must have XML-RPC and DOMXML extesions installed on your Web server
2. Grab latest ADODB library from http://php.weblogs.com and install it.
3. Edit database connection properties with your values
4. Open Borland Delphi 7 IDE and create new application
5. Place TSOAPConnection, TClientDataset, TDatasource, TDBGrid components on to the main form
6. Set TSOAPConnection "URL" property to URL to this file on your server,
set TSOAPConnection "UseSOAPAdapter" property to false and "Connected" property to true
7. Set TClientDataset "RemoteProvider" property to name of TSOAPConnection component instanse name,
"ProviderName" to any value, "Active" property to true
8. Set TDataSource "Dataset" property to name of TClientDataset instanse,
"Active" propery to true
9. Set TDBGrid "DataSource" property to name of TDataSource instance name.
Your should see now contents of the database table in grid.
10. Compile and run application.
To apply updates to server your must call ApplyUpdates method of the TClientDataset
This code is ALPHA release.
Any suggestions is wellcome.
This code is under GPL
Author: Denis O.Philippov <denis at atlant dot ru>
*/
include "adodb.inc.php"; $ado2midas = array(
C => "string",
X => "bin.hex",
N => "r8",
I => "i",
R => "+",
B => "bin.hex" );
$ADODB_FETCH_MODE = 2; $conn = &AdonewConnection("mysql");
$conn -> pconnect("host", "user", "pass", "databasename"); $tablename = "tablename"; $HTTP_RAW_POST_DATA = str_replace("base64Binary", "string", $HTTP_RAW_POST_DATA);
$s = xmlrpc_server_create(); xmlrpc_server_register_method($s,'AS_GetRecords', 'AS_GetRecords'); xmlrpc_server_register_method($s,'AS_ApplyUpdates', 'AS_ApplyUpdates');
$out=xmlrpc_server_call_method($s, $HTTP_RAW_POST_DATA, $response, $output_options = array(
"output_type" => "xml",
"verbosity" => "no_white_space",
"escaping" => array("markup", "non-ascii", "non-print"),
"version" => "soap 1.1",
"encoding" => "utf-8"
)
);
echo str_replace(array("<item>","</item>"), "", $out);
function getdatapacket($sql){
global $conn, $ado2midas;
if($rs = ($conn ->execute($sql))){
$doc = domxml_open_mem('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><DATAPACKET Version="2.0"></DATAPACKET>');
$datapacket = $doc->document_element();
$metadata = $doc -> create_element("METADATA");
$metadata = $datapacket -> append_child($metadata);
$fields = $doc -> create_element("FIELDS");
$fields = $metadata -> append_child($fields);
$fieldcount = $rs -> FieldCount( );
for($i=0; $i < $fieldcount; $i++) {
$_fld = $rs -> Fetchfield($i);
$type = $rs -> metatype($_fld ->type);
$types[$i] = $_fld;
$metatype[$i] = $type;
$field = $doc -> create_element("FIELD");
$field = $fields -> append_child($field);
$field -> set_attribute("attrname", $_fld -> name);
$field -> set_attribute("fieldtype", $ado2midas[$type]);
if($type == "C" || $type == "X" || $type == "B"){
$field -> set_attribute("WIDTH", $_fld -> max_length);
}
if($type == "X"){
$field -> set_attribute("SUBTYPE", "Text");
}elseif($type == "B"){
$field -> set_attribute("SUBTYPE", "Graphics");
}
}
$params = $doc -> create_element("PARAMS");
$params = $metadata -> append_child($params);
$rowdata = $doc -> create_element("ROWDATA");
$rowdata = $datapacket -> append_child($rowdata);
while($rw = $rs -> fetchrow()){
$row = $doc -> create_element("ROW");
$row = $rowdata -> append_child($row);
for($i=0; $i< $fieldcount; $i++){
if($metatype[$i] == "X "|| $metatype[$i] == "B"){
$row -> set_attribute($types[$i] -> name, base64_encode($rw[$types[$i] -> name]));
}else{
$row -> set_attribute($types[$i] -> name, iconv("windows-1251", "UTF-8", $rw[$types[$i] -> name]));
}
}
}
$ret->RecsOut = $rs -> RecordCount( );
$ret->return = $doc->dump_mem(false, "UTF-8");
return $ret;
}else{
return false;
}
}
function AS_GetRecords($method, $params){
global $conn, $tablename;
$providername = $params[ProviderName];
$count = $params["Count"];
$options = $params[Option];
$commandtext = $params[CommandText];
$params = $params[Params];
$ownerdata = $params[OwnerData];
$_ret = getdatapacket("select * from $tablename");
xmlrpc_set_type(&$_ret->return, "base64");
return array( "Params" => $params, "OwnerData" => $ownerdata,
"RecsOut" => $_ret -> RecsOut, "return" => $_ret->return);
}
function AS_ApplyUpdates($method, $params){
global $conn, $ado2midas, $tablename;
$ProviderName = &$params[ProviderName];
$Delta = &$params[Delta];
$MaxErrors = &$params[MaxErrors];
$OwnerData = &$params[OwnerData];
$errorcount = 0;
$Delta = base64_decode($Delta);
$Delta = str_replace('<?xml version="1.0" standalone="yes"?>',
'<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>',
$Delta);
$doc = domxml_open_mem($Delta);
$meta = $doc -> get_elements_by_tagname("FIELD");
for($i=0; $i< sizeof($meta); $i++){
$defs = (array)$meta[$i] -> attributes();
reset($defs);
while(list($k, $v) = each($defs)){
$_defs[$v -> name]=$v -> value;
}
$fields[$_defs["attrname"]]["type"]=$_defs["fieldtype"];
}
$rows = $doc -> get_elements_by_tagname("ROW");
for($i=0; $i< sizeof($rows); $i++){
$__attr = $rows[$i] -> attributes();
unset($attr);
while(list($k, $v) = each($__attr)){
$attr[$v -> name]=$v -> value;
}
unset($__attr);
if($attr["RowState"] == 2){ //deleted
reset($attr);
unset($where);
while(list($k, $a) = each($attr)){
if($k != "RowState" && strlen($a)>0){
if($fields[$k]["type"] == "bin.hex"){
$where[]="$k = '".base64_decode($a)."'";
}else{
$where[]="$k = '$a'";
}
}
}
if(sizeof($where) > 0){
$sql = "delete from $tablename where ".join(" and ", $where);
if(!$conn -> execute($sql)){
$errorcount ++;
}
}
}elseif($attr["RowState"] == 4){ //inserted record
reset($attr);
unset($line);
while(list($k, $a) = each($attr)){
if(strlen($a) >0){
if($k != "RowState"){
if($fields[$k]["type"] == "bin.hex"){
$line[]="'".base64_decode($a)."'";
}else{
$line[]="'$a'";
}
$_fields[]=$k;
}
}
}
if(sizeof($line) > 0){
$sql = "insert into $tablename(".join(", ", $_fields).") values (".join(", ", $line).")";
if(!$conn -> execute($sql)){
$errorcount ++;
}
}
}elseif($attr["RowState"] == 1){ //updated record
$_attr1 = (array)$rows[$i+1] -> attributes();
unset($_attr);
while(list($k, $v) = each($_attr1)){
$_attr[$v -> name]=$v ->value;
}
unset($_attr1);
reset($attr);
unset($where);
while(list($k, $a) = each($attr)){
if(strlen($a) >0){
if(!($k == "RowState" || $fields[$k]["type"] == "bin.hex" ||
$fields[$k]["type"] == "r8")){
$where[]="$k = '".utf8_decode($a)."'";
}
}
}
reset($_attr);
unset($new);
while(list($k, $a) = each($_attr)){
if($k != "RowState"){
if($fields[$k]["type"] == "bin.hex"){
$new[]="$k = '".base64_decode($a)."'";
}else{
$new[]="$k = '".utf8_decode($a)."'";
}
}
}
if(sizeof($where) > 0 && sizeof($new) > 0){
$sql = "update $tablename set ".join(", ", $new)." where ".join(" and ", $where);
if(!$conn -> execute($sql)){
$errorcount ++;
}
}
}
}
$ret="";
xmlrpc_set_type($ret, "base64");
return array(ErrorCount => $errorcount, ownerdata => $ownerdata, "return" => $ret);
}
?>
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|