Example #1
0
//构建 update 请求,即 对应下面填的参数 7 ,插入数据
$ttc_update = new tphp_ttc_request($ttc_srv, 7);
$ret = $ttc_update->set_key($key);
printf("update set key.ret:%d\n", $ret);
//set函数是用来指定insert sql 语句中的set 字段名=value 的这样一个操作
$ttc_update->set_str("name", "9qwer");
$ttc_update->set_str("city", "fuzhou");
$ttc_update->set_str("descr", "huanying");
$ttc_update->set("age", 21);
$ttc_result = new tphp_ttc_result();
$ret = $ttc_update->execute($ttc_result);
printf("update execute end.ret:%d\n", $ret);
if ($ret == 0) {
    //清空result对象
    $ttc_result->reset();
    //创建get 请求request对象, 4表示get操作
    $ttc_get = new tphp_ttc_request($ttc_srv, 4);
    $ttc_get->set_key($key);
    //指定需要查询的字段
    $ttc_get->need("name");
    $ttc_get->need("city");
    $ttc_get->need("age");
    $ret = $ttc_get->execute($ttc_result);
    printf("get execute end.ret:%d\n", $ret);
    $ret = $ttc_result->fetch_row();
    printf("get fetch_row ret:%d\n", $ret);
    printf("uid:%d\n", $ttc_result->int_value("uid"));
    printf("name:%s\n", $ttc_result->string_value("name"));
    printf("city:%s\n", $ttc_result->string_value("city"));
    printf("age:%d\n", $ttc_result->int_value("age"));
}
Example #2
0
<?php

$ttc_srv =& new tphp_ttc_server();
//只要server不析构,后台会保持长连接
$ttc_srv->int_key();
$ttc_srv->set_tablename("t_dtc_example");
$ttc_srv->set_address("127.0.0.1", "10101");
$ttc_srv->set_accesskey("0000010184d9cfc2f395ce883a41d7ffc1bbcf4e");
$ttc_srv->set_timeout(5);
$ttc_request = new tphp_ttc_request($ttc_srv, 4);
$ttc_request->set_key(100001);
$ttc_request->need("data");
$ttc_request->need("len");
$ttc_result = new tphp_ttc_result();
$ttc_request->execute($ttc_result);
printf("num:%d\n", $ttc_result->affected_rows());
printf("fetch_row retcode:%d\n", $ttc_result->fetch_row());
printf("set success, result code: %d\n", $ttc_result->result_code());
printf("data:%s\n", $ttc_result->binary_value("data"));
printf("len:%d\n", $ttc_result->int_value("len"));
Example #3
0
 /**
  * 获得DTC记录
  *
  * @param   string  $key, 数据库的主键
  * @param   array   $multikey, 可选参数, 多字段key时必选, 形如array('field2' => 1, 'field3' => 2)
  * @param   array    $need, 可选参数, 默认返回所有列,也可以指明需要返回的列
  * @param   int    $start, 可选参数, 默认从第一条符合条件的记录开始返回,也可指定返回记录的起始偏移量
  * * @param   int    $need, 可选参数, 默认返回所有符合条件的记录,也可指明需要的条数
  */
 public function get($key, $multikey = array(), $need = array(), $itemLimit = 0, $start = 0)
 {
     $this->clearERR();
     if ($this->server == false && $this->init() == false) {
         return false;
     }
     if ($this->checkKey($key) == false) {
         return false;
     }
     $req = new tphp_ttc_request($this->server, TPHP_DTC_OP_GET);
     if ($this->key_type == 1) {
         $errCode = $req->set_key($key);
     } elseif ($this->key_type == 2) {
         $errCode = $req->set_key_str($key);
     } else {
         $this->errCode = 20020;
         $this->errMsg = 'request failed';
         return false;
     }
     if ($errCode != 0) {
         $this->errCode = $errCode;
         $this->errMsg = "dtc config:{$this->dtcKey} get:{$key}: set_key error";
         return false;
     }
     /*当前版本的dtc暂不支持多key的需求
     		if ( !empty($multikey) && is_array($multikey) ) {
     			// 处理多字段key情况
     			foreach ( $multikey as $mk => $mv ) {
     				$chk = $this->checkMultikey($mk, $mv);
     				if ( !$chk ) {
     					return $chk;
     				}
     				$eq = $this->eqv($req, $mk, $mv);
     				if ( $eq != 0 ) {
     					$this->errCode = 20028;
     					$this->errMsg  = "dtc config:{$this->dtcKey} get:{$key}: set multikey error";
     
     					return false;
     				}
     			}
     		}*/
     $fields = $this->config['FIELDS'];
     var_dump($fields);
     $keyfield = $this->config['KEY'];
     if (empty($need)) {
         foreach ($fields as $k => $v) {
             if ($keyfield == $k) {
                 continue;
             }
             $errCode = $req->need($k);
             if ($errCode != 0) {
                 $this->errCode = $errCode;
                 $this->errMsg = "dtc config:{$this->dtcKey} need({$k}) error,error code:{$errCode}";
                 return false;
             }
         }
     } else {
         foreach ($need as $k) {
             if ($keyfield == $k) {
                 continue;
             }
             $errCode = $req->need($k);
             if ($errCode != 0) {
                 $this->errCode = $errCode;
                 $this->errMsg = "dtc config:{$this->dtcKey} need({$k}) error,error code:{$errCode}";
                 return false;
             }
         }
     }
     if (0 != $itemLimit) {
         $errCode = $req->limit($start, $itemLimit);
         if ($errCode != 0) {
             $this->dtcCode = $errCode;
             $this->errCode = $errCode;
             $this->errMsg = "dtc config:{$this->dtcKey} get:{$key} error,error code:{$errCode}";
             return false;
         }
     }
     $result = new tphp_ttc_result();
     $req->execute($result);
     $this->result = $result;
     $errCode = $result->result_code();
     if ($errCode != 0) {
         $this->dtcCode = $errCode;
         $this->errCode = $errCode;
         //$this->errMsg  = "dtc config:{$this->dtcKey} get:{$key} error,error code:{$errCode}";
         $this->errMsg = $result->error_message();
         $this->errFrom = $result->error_from();
         return false;
     }
     $rowCount = $result->num_rows();
     $multiRows = array();
     for ($i = 0; $i < $rowCount; ++$i) {
         $ret = $result->fetch_row();
         if ($ret < 0) {
             $this->errCode = 20026;
             $this->errMsg = "dtc config:{$this->dtcKey} get:{$key} error,fetch_row error";
             return false;
         }
         $rData = array();
         if (empty($need)) {
             foreach ($fields as $k => $v) {
                 if ($k == $keyfield) {
                     continue;
                 }
                 switch ($v['type']) {
                     case DTC::INT:
                         $rData[$k] = $result->int_value($k);
                         break;
                     case DTC::STRING:
                         $rData[$k] = $result->string_value($k);
                         break;
                     case DTC::BINARY:
                         $rData[$k] = $result->binary_value($k);
                         break;
                     case DTC::FLOAT:
                         $rData[$k] = $result->float_value($k);
                         break;
                     default:
                         $this->errCode = 20027;
                         $this->errMsg = "dtc config:{$this->dtcKey} field:{$k} data type:{$v['type']} error";
                         return false;
                 }
             }
             $rData[$keyfield] = $key;
             $multiRows[] = $rData;
         } else {
             foreach ($need as $k) {
                 if ($k == $keyfield) {
                     continue;
                 }
                 switch ($fields[$k]['type']) {
                     case DTC::INT:
                         $rData[$k] = $result->int_value($k);
                         break;
                     case DTC::STRING:
                         $rData[$k] = $result->string_value($k);
                         break;
                     case DTC::BINARY:
                         $rData[$k] = $result->binary_value($k);
                         break;
                     case DTC::FLOAT:
                         $rData[$k] = $result->float_value($k);
                         break;
                     default:
                         $this->errCode = 20027;
                         $this->errMsg = "dtc config:{$this->dtcKey} field:{$k} data type:{$v['type']} error";
                         return false;
                 }
             }
             $rData[$keyfield] = $key;
             $multiRows[] = $rData;
         }
     }
     return $multiRows;
 }
Example #4
0
<?php

$ttc_srv =& new tphp_ttc_server();
//只要server不析构,后台会保持长连接
$ttc_srv->int_key();
$ttc_srv->set_tablename("t_dtc_example");
$ttc_srv->set_address("192.168.214.62", "10009");
$ttc_srv->set_accesskey("0000010284d9cfc2f395ce883a41d7ffc1bbcf4e");
$ttc_srv->set_timeout(5);
$ttc_request = new tphp_ttc_request($ttc_srv, 4);
$ttc_request->set_key(100008);
$ttc_request->need("name");
$ttc_request->need("age");
$ttc_result = new tphp_ttc_result();
$ttc_request->execute($ttc_result);
printf("num:%d\n", $ttc_result->affected_rows());
printf("fetch_row retcode:%d\n", $ttc_result->fetch_row());
printf("set success, result code: %d\n", $ttc_result->result_code());
printf("name:%s\n", $ttc_result->string_value("name"));
printf("age:%d\n", $ttc_result->int_value("age"));