Package: mageia
mageiaThis is a simple database tools for MySQLBased on SQLAlchemyTransaction-SafeFriendly interface for developerUse:paramsyntax bind paramRead/Write SplittingLoad BalanceDocumentationpip install mageiaUsage (DB)frommageia.dbimportDB,create_engine,Dicturl="mysql+pymysql://root:password@localhost/info?charset=utf8"# create a engine instance, and DB instanceengine=create_engine(url)db=DB(engine)Query# return [dict-like,dict-like,dict-like,] or Noneuser_list=db.query("select * from user where id=:id",id=1)user_list=db.query("select * from user where id in :id",id=[1,2,3])user=user_list[0]print(user.id)print(user.name)Query One# return dict-like or Nonedb.query_one("select * from user where id=:id",id=1)Query Page# return [dict-like,dict-like,dict-like,] or Noneuser=db.query_page("select * from user",page=1,limit=10)InsertJack={"name":"Jack","age":18,"sex":1}table="user"# Insert done, don't need commitdb.add(table,Jack)Insert ManyJack={"name":"Jack","age":18,"sex":1}Tom={"name":"Tom","age":18,"sex":1}table="user"db.add_many(table,[Tom,Jack])Deletedata=db.query_one("select * from user where id = :id",id=1)# Delete done, don't need commit, data must contain a primary key ,etc id .db.delete("user",data)Mergedata=db.query_one("select * from user where id = :id",id=1)# merge done, don't need commit, data must contain a primary key ,etc id .data.name="stefanlei"db.merge("user",data)Merge manyuser_list=db.query("select * from user where id in :ids",ids=[1,2,3,4,5,6,7,8])foruserinuser_list:user.name="stefanlei"db.merge_many("user",user_list)Execute# return affected rows totaldb.execute("delete from user where id = 1")db.execute("update user set age = 18 where age = :age",age=16)Transaction-SafeYou can also usesessionto keepTransaction-Safe# Use session to keep transaction-safewithdb.session()ass:db_user=s.query_one("select * from user")s.delete("user",db_user)user=Dict()user.name="stefan"user.age=18s.add("user",user)Usage (ProxyDB)frommageia.proxyimportProxyDBsettings={"master":"mysql+pymysql://user:password@192.168.1.1/mageia?charset=utf8","slave":[{"url":"mysql+pymysql://user:password@192.168.1.2/mageia?charset=utf8","optional":{"weight":10}},{"url":"mysql+pymysql://user:password@192.168.1.3/mageia?charset=utf8","optional":{"weight":20}}],}db=ProxyDB(settings)Write (master)# Use session to keep transaction-safe ,and execute in masterwithdb.session()ass:sql="select * from student"user=s.query_one(sql)user.age=18s.merge("student",user)jack={"name":"Jack","age":23}s.add("student",jack)s.delete("student",user)read (slave)# Use session to keep transaction-safe ,and execute in slavewithdb.session_slave()ass:sql="select * from student"user=s.query_one(sql)user_list=s.query(sql)user_page=s.query_page(sql,page=1,limit=10)Autodb=ProxyDB(settings)# auto select master or slavesql="select * from student"user=db.query_one(sql)Jack={"name":"Jack","age":18}db.add("student",Jack)db.delete("student",user)Load Balancefrommageia.proxyimportProxyDBfrommageia.loadbalanceimportWeightRandomsettings={"master":"mysql+pymysql://user:password@192.168.1.1/mageia?charset=utf8","slave":[{"url":"mysql+pymysql://user:password@192.168.1.2/mageia?charset=utf8","optional":{"weight":10}},{"url":"mysql+pymysql://user:password@192.168.1.3/mageia?charset=utf8","optional":{"weight":20}}],}# Load Balance Policydb=ProxyDB(settings,balance_class=WeightRandom)