# -*- coding: utf-8 -*-
"""只操作本程序创建的临时对象；全部数据为合成样本。"""
import os, sys, json, sqlite3, tempfile, pathlib, subprocess, time, socket, stat
from pathlib import Path
def emit(**kw):
    print(json.dumps(kw, ensure_ascii=False, sort_keys=True))
data=[('A','blue'),('A','blue'),('A','green')]
def run(partial):
    c=sqlite3.connect(':memory:'); c.execute('CREATE TABLE channels(tenant,name,UNIQUE(tenant,name))'); c.execute('BEGIN'); errors=[]
    for i,row in enumerate(data):
        if partial: c.execute('SAVEPOINT row_import')
        try: c.execute('INSERT INTO channels VALUES(?,?)',row)
        except sqlite3.IntegrityError:
            errors.append(i+1)
            if not partial: c.rollback(); break
            c.execute('ROLLBACK TO row_import')
        finally:
            if partial: c.execute('RELEASE row_import')
    c.commit(); return c.execute('SELECT * FROM channels ORDER BY name').fetchall(),errors
p,e=run(True); a,f=run(False); assert len(p)==2 and e==[2] and a==[] and f==[2]
emit(partial_rows=p,partial_rejected=e,atomic_rows=a,atomic_rejected=f)
