Package: saiorm
Saiorm is a simple library for accessing database.
It will take you have a very easy way to use SQL database.Method:Methodinsert, select, update, delete, execute, executemany, increase, decreaseshould be executedfinally,they will take effect immediately.Methodlast_sqlreturn the latest executed sql.Methodget_fields_nameget a list of all fields name, cache them by default.Methodwherecould be dict or str type.INrequire a string or a sequence with str type.Methodselectandgetreturn data only.Methodupdate,delete,executereturn a dict,including lastrowid, rowcount, rownumber, sql.Various method join,should use string param for method join and method where.ATTENTION1. Saiorm does not convert value type in condition(limit,order_by,group_by,
various join etc.),method where not convert value type in native functions and IN.
If you want to use the values passed from user,you must check them,
because it’s easily to triggering injection vulnerability.Saiorm require python3 and pymysql.3. Support MySQL only,you can inherit from saiorm.base.BaseDB to support other types
of database with the same API,like siaorm.MySQL.ChainDB.You can add “`” as a prefix to set the field to native function in method select and update.InitializationimportsaiormDB=saiorm.init()# with no table name prefix# DB = saiorm.init(table_name_prefix="abc_") # with table name prefixDB.connect({"host":"","port":3306,"database":"","user":"","password":""})table=DB.table("xxx")Usage for calling mysql function onlyDB.select("`NOW()")DB.select("`SUM(1+2)")will transform to SQLSELECTNOW();SELECTSUM(1+2);Usage for select and getselect will return all data2. get will modify _limit attribute automatically,then return the latest line only.If you call get method, limit method will be overwritedselect and get receive a fields param.# select all fieldstable.select()# get the latest linetable.order_by("id DESC").get()# kinds of params in wheretable.where({"a":1,"b":("BETWEEN","1","2"),"c":("`ABS(?)","2"),"d":("!=",0),"e":("IN","1,2,3"),"f":"`NOW()",}).select("e,f")will transform to SQLSELECT*FROMxxx;SELECT*FROMxxxORDERBYidDESCLIMIT1;SELECTe,fFROMxxxWHEREa=1ANDbBETWEEN'1'AND'2'ANDc=ABS(2)ANDd!=0ANDeIN(1,2,3)ANDf=NOW();Usage for updateIf you want use native function,you can pass a tuple.table.where({"a":("IN",["1","2","3"]),"b":("`ABS(?)","2"),}).update({"c":"`ABS(2)","d":("`ABS(?)",3),"e":"2",})will transform to SQLUPDATExxxSETc=ABS(2),d=ABS(3),e='2'WHEREaIN(1,2,3)ANDb=ABS(2);Usage for insertinsert function support two kinds of data# use natural dicttable.insert({"a":"1","b":"2",})# use split dicttable.insert({"fields":["a","b"],"values":["1","2"],})# use natural dict in list, SQL will in one linetable.insert_many([{"a":"1","b":"2",},{"a":"3","b":"4",},{"a":"5","b":"6",}])# use split dict in list, SQL will in one linetable.insert_many({"fields":["a","b"],"values":[["1","2"],["3","4"],["5","6"]]})will transform to SQLINSERTINTOxxx(a,b)VALUES('1','2');INSERTINTOxxx(a,b)VALUES('1','2');INSERTINTOxxx(a,b)VALUES('1','2'),('3','4'),('5','6');INSERTINTOxxx(a,b)VALUES('1','2'),('3','4'),('5','6');If use split dict,key fields is not necessary,it will insert by the order of table struct.Usage for deleteBy default,deletemust havewherecondition,or you can pass strict=False when initialization.table.where({"a":"1","b":"2","c":("`ABS(?)","2"),}).delete()table.delete()# will not be executed, or set strict=False when initializationwill transform to SQLDELETEFROMxxxWHEREa='1'ANDb='2'ANDc=ABS(2);DELETEFROMxxx;Usage for increaseNumerical field increasetable.increase("a",1)will transform to SQLUPDATExxxSETa=a+1Usage for decreaseNumerical field decreasetable.decrease("a",1)will transform to SQLUPDATExxxSETa=a-1where conditiontable.where({"a":1,"b":("BETWEEN","1","2"),"c":("ABS(?)","2"),"d":("!=",0),"e":("IN","1,2,3"),"f":"NOW()",}).select("e,f")must check param to prevent injection vulnerabilities.when calling native mysql function the param placeholder could be ?.condition will be equals to value,or pass a tuple or list, and set the first item to change it.use IN or BETWEEN should pass a tuple or list.pass string type is allowed,you should join param into this string.Method Shorthandt equals to tablew equals to whereob equals to order_byl equals to limitgb equals to group_byj equals to joinij equals to inner_joinlj equals to left_joinrj equals to right_joins equals to selecti equals to insertim equals to insert_manyu equals to updated equals to deleteinc equals to increasedec equals to decrease