Package: fastapi-efficient-sql
fastapi-efficient-sqlInstalled as package bypip install fastapi-efficient-sqlInstall developing requirements bypyenv local 3.7.9,poetry env use 3.7.9,poetry shellandpip install -r requirements-dev.txtRun demo service bypython -m examples.serviceRun unittest bypytest -svSome preparations before using efficient sqlfromfastapi_esqlimportAppMetaclass,BaseManager,BaseModelclassDemoMetaclass(AppMetaclass):defget_ro_conn(self):returnTortoise.get_connection("demo_ro")defget_rw_conn(self):returnTortoise.get_connection("demo_rw")classAccount(BaseModel):id=fields.IntField(pk=True)active=fields.BooleanField(null=False,default=True)gender=fields.IntEnumField(GenderEnum,null=False,default=GenderEnum.unknown)name=fields.CharField(max_length=32,null=False,default="")locale=fields.CharEnumField(LocaleEnum,max_length=5,null=False)classAccountMgr(BaseManager,metaclass=DemoMetaclass):model=AccountSome supported efficient sqlselect_custom_fieldsbasic exampleaids=[1,2,3]awaitAccountMgr.select_custom_fields(fields=["id","extend ->> '$.last_login.ipv4' ipv4","extend ->> '$.last_login.start_datetime' start_datetime","CAST(extend ->> '$.last_login.online_sec' AS SIGNED) online_sec"],wheres=f"id IN ({','.join(map(str,aids))}) AND gender=1",# These 4 types of `wheres` are equal# wheres=Q(Q(id__in=aids), Q(gender=1), join_type="AND"),# wheres={"id__in": aids, "gender": 1},# wheres=[Q(id__in=aids), Q(gender=1)],index="PRIMARY",)Generate sql and executeSELECTid,extend->>'$.last_login.ipv4'ipv4,extend->>'$.last_login.start_datetime'start_datetime,CAST(extend->>'$.last_login.online_sec'ASSIGNED)online_secFROM`account`FORCEINDEX(`PRIMARY`)WHEREidIN(1,2,3)ANDgender=1complex exampleawaitAccountMgr.select_custom_fields(fields=["locale","gender","COUNT(1) cnt"],wheres=Q(id__range=[1,12]),groups=["locale","gender"],having="cnt > 0",orders=["locale","-gender"],offset=0,limit=10,)Generate sql and executeSELECTlocale,gender,COUNT(1)cntFROM`account`WHERE`id`BETWEEN1AND12GROUPBYlocale,genderHAVINGcnt>0ORDERBYlocaleASC,genderDESCLIMIT0,10update_json_fieldawaitAccountMgr.update_json_field(json_field="extend",wheres=Q(id=8),merge_dict={"updated_at":"2022-10-30 21:34:15","info":{"online_sec":636,}},path_value_dict={"$.last_login":{"ipv4":"209.182.101.161",},"$.uuid":"fd04f7f2-24fc-4a73-a1d7-b6e99a464c5f",},remove_paths=["$.deprecated"],json_type=dict,)Generate sql and executeUPDATE`account`SETextend=JSON_MERGE_PATCH(JSON_SET(JSON_REMOVE(COALESCE(extend,'{}'),'$.deprecated'),'$.last_login',CAST('{"ipv4": "209.182.101.161"}'ASJSON),'$.uuid','fd04f7f2-24fc-4a73-a1d7-b6e99a464c5f'),'{"updated_at": "2022-10-30 21:34:15", "info": {"online_sec": 636}}')WHERE`id`=8upsert_on_duplicateawaitAccountMgr.upsert_on_duplicate([{"id":10,"gender":1,"name":"田中 知実","locale":"ja_JP","extend":{"rdm":1}},{"id":11,"gender":2,"name":"Tara Chadha","locale":"en_IN","extend":{"rdm":10}},{"id":12,"gender":2,"name":"吴磊","locale":"zh_CN","extend":{"rdm":9}},],insert_fields=["id","gender","name","locale","extend"],upsert_fields=["gender","name"],merge_fields=["extend"],)Generate sql and executeINSERTINTO`account`(id,gender,name,locale,extend)VALUES(10,1,'田中 知実','ja_JP','{"rdm": 1}'),(11,2,'Tara Chadha','en_IN','{"rdm": 10}'),(12,2,'吴磊','zh_CN','{"rdm": 9}')AS`new_account`ONDUPLICATEKEYUPDATEgender=`new_account`.gender,name=`new_account`.name,extend=JSON_MERGE_PATCH(COALESCE(`account`.extend,'{}'),`new_account`.extend)insert_into_selectawaitAccountMgr.insert_into_select(wheres=Q(id__in=[4,5,6]),remain_fields=["gender"],assign_field_dict={"locale":Cases("id",{3:LocaleEnum.zh_CN,4:LocaleEnum.en_US,5:LocaleEnum.fr_FR},default=""),"active":False,"name":RawSQL("CONCAT(LEFT(name, 26), ' [NEW]')"),"extend":{},},to_table="account_bak",)Generate sql and executeINSERTINTO`account_bak`(gender,locale,active,name,extend)SELECTgender,CASEidWHEN3THEN'zh_CN'WHEN4THEN'en_US'WHEN5THEN'fr_FR'ELSE''ENDlocale,Falseactive,CONCAT(LEFT(name,26),' [NEW]')name,'{}'extendFROM`account`WHERE`id`IN(4,5,6)bulk_update_from_dictsawaitAccountMgr.bulk_update_from_dicts([{"id":7,"active":False,"deleted":False,"gender":GenderEnum.male,"extend":{"test":1,"debug":0}},{"id":15,"active":True,"deleted":False,"gender":GenderEnum.unknown,"extend":{"test":1,"debug":0}}],join_fields=["id","deleted"],update_fields=["active","gender"],merge_fields=["extend"],)Generate sql and executeUPDATE`account`JOIN(SELECT*FROM(VALUESROW(7,False,False,1,'{"test": 1, "debug": 0}'),ROW(15,False,True,0,'{"test": 1, "debug": 0}'))ASfly_table(id,deleted,active,gender,extend))tmpON`account`.id=tmp.idAND`account`.deleted=tmp.deletedSET`account`.active=tmp.active,`account`.gender=tmp.gender,`account`.extend=JSON_MERGE_PATCH(COALESCE(`account`.extend,'{}'),tmp.extend)