cockroachDB postgreSQL的array_pos

2020-03-28  本文已影响0人  毛毛v5

在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();
    }
}

上一篇下一篇

猜你喜欢

热点阅读