在array_position和array_positions中, 每個數組元素與使用IS NOT DISTINCT FROM語法的搜索值進行比較。
在array_position中,如果沒有找到該值,則返回NULL。
在array_positions中,如果數組是NULL,則返回NULL; 如果數組中沒有找到該值,相反返回空數組。
在string_to_array中,如果定界符參數爲 NULL,輸入字符串中的每一個字符將變成結果數組中的一個獨立元素。如果定界符是一個空串,則整個輸入字符串被作爲一個單一元素的數組返回。否則輸入字符串會被在每一個出現定界符字符串的位置分裂。
在string_to_array中,如果空值串參數被忽略或者爲 NULL,輸入中的子串不會被替換成 NULL。在array_to_string中,如果空值串參數被忽略或者爲 NULL,該數組中的任何空值元素會被簡單地跳過並且不會在輸出串中被表示。
沒有找到這樣表示
array_position(days,:day) is null
完整的代碼
function signinAppend($param,&$arr,$token,$ui){
$dbh = new PDO(CRDB, CRDB_U, CRDB_P, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_PERSISTENT => true
));
try{
$keys = ["year","month","day"];
if(checkParam($keys,$param)){
$year = $param["year"];
$month = $param["month"];
$day = (int)$param["day"];
}else{
$year = Date("Y");
$month = Date("m");
$day = (int)Date("d");
}
try {
$dbh->beginTransaction();
$dbh->exec("SAVEPOINT cockroach_restart_signin");// This savepoint allows us to retry our transaction.
} catch (Exception $e) {
$arr["msg"] = "signinAppend transation開始失敗";
return;
}
$n = 0;
$maxretry = 3;
while (true) {
try {
if($n++ > $maxretry){
throw new Exception("transation重試次數超標");
}
$stmt = $dbh->prepare('select id from signin WHERE userid=:userid and year=:year and month=:month');
$stmt->bindValue(':userid', $ui["id"], PDO::PARAM_INT);
$stmt->bindValue(':year', $year, PDO::PARAM_INT);
$stmt->bindValue(':month', $month, PDO::PARAM_INT);
$stmt->execute();
if ($stmt->rowCount() <= 0) {//沒有該月份數據,插入。
$arr["msg"] = "簽到成功";
$stmt = $dbh->prepare(
'insert into signin(userid,year,month,days) values(:userid,:year,:month,:day)'
);
$stmt->bindValue(':userid', $ui["id"], PDO::PARAM_INT);
$stmt->bindValue(':year', $year, PDO::PARAM_INT);
$stmt->bindValue(':month', $month, PDO::PARAM_INT);
$stmt->bindValue(':day', '{'.$day.'}', PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() <=0){
throw new Exception("測試異常2");
}
}else{
$arr["msg"] = "簽到成功";
$stmt = $dbh->prepare(
'update signin set days=array_append(days,:day) where userid=:userid and year=:year and month=:month and array_position(days,:day) is null'
);
$stmt->bindValue(':day', $day, PDO::PARAM_STR);
$stmt->bindValue(':userid', $ui["id"], PDO::PARAM_INT);
$stmt->bindValue(':year', $year, PDO::PARAM_INT);
$stmt->bindValue(':month', $month, PDO::PARAM_INT);
$stmt->execute();
if ($stmt->rowCount() <= 0) {//沒有該月份數據,插入。
$arr["msg"] = "重複簽到";
}
}
$dbh->exec('RELEASE SAVEPOINT cockroach_restart_signin');// Attempt to release the savepoint (which is really the commit).
$dbh->commit();
$arr["status"] = 1;
break;
}catch(Exception $e0){
$dbh->rollBack();
$arr["msg"] = $e0->getMessage();
return;
}catch (PDOException $e) {
if ($e->getCode() != '40001') {// Non-recoverable error. Rollback and bubble error up the chain.
$dbh->rollBack();
$arr["msg"] = "userRegister error 2";
return;
} else {// Cockroach transaction retry code. Rollback to the savepoint and restart.
$dbh->exec('ROLLBACK TO SAVEPOINT cockroach_restart_signin');
}
}
}
}catch(Exception $e){
$arr["msg"] = $e->getMessage();
}
}