Package: pysqlquery
PySQLQueryPySQLQueryis a simple Python package for generating SQL code. Made to help programmers without much knowledge of SQL, it offers a simple way to generate SQL code using Pythonic techniques.Table of contentsInstallationDocsExamplesCreating a simple tableCreating a more complex TableSaving all tables that you have been createdContributingCreditsLicenseInstallationUsing pip:pipinstallpysqlqueryDocsSee ourDocsfor comprehensive and detailed documentation on PySQLQuery. In the documentation, you will find in-depth explanations, usage examples, and additional resources to help you maximize your experience with PySQLQuery.ExamplesCreating a simple tablefrompysqlqueryimport(Table,Column,Integer,String,Float,ForeignKey)classTbEmployees(Table):id=Column(Integer,primary_key=True,auto_increment='mysql')name=Column(String(50))email=Column(String(255),unique=True)wage=Column(Float(7,2),default=1250.38)id_department=Column(ForeignKey('t_department','id'))tb_emplooyes=TbEmployees()print(tb_emplooyes)The printed string will be:CREATETABLETBEMPLOYEES(idINTEGERAUTO_INCREMENTNOTNULL,nameVARCHAR(50)NOTNULL,wageFLOAT(7,2)NOTNULLDEFAULT1250.38,PRIMARYKEY(id),FOREIGNKEY(id_department)REFERENCEST_DEPARTMENT(id));The provided example demonstrates the process of creating a single table using PySQLQuery, showcasing the simplicity and expressiveness of the package. Let's break down the key elements of the example:Importing Required Classesfrompysqlqueryimport(Table,Column,Integer,String,Float,ForeignKey)The necessary classes are imported from the pysqlquery package. The Column, Table and a data type classes are essential for defining the structure of the SQL table, the constraint ones are optional like ForeignKey.Defining the TableclassTbEmployees(Table):id=Column(Integer,primary_key=True,auto_increment='mysql')name=Column(String(50))email=Column(String(255),unique=True)wage=Column(Float(7,2),default=1250.38)id_department=Column(ForeignKey('t_department','id'))A new table class, TbEmployees, is defined by inheriting from the Table class. Columns such as id, name, and wage are specified using the Column class, each with its respective data type and constraints.Creating an Instance of the Tabletb_emplooyes=TbEmployees()An instance of the table, tb_employees, is created.Printing the SQL Codeprint(tb_emplooyes)The instance of the table is printed, resulting in the corresponding SQL code being generated.Creating a more complex Tablefrompysqlqueryimport(Table,Column,Integer,String,Float)frompysqlquery.constraintsimport(ForeignKeyConstraint,PrimaryKeyConstraint,UniqueConstraint,)classTbEmployees(Table):__tablename__='T_EMPLOYEES'id=Column(Integer,auto_increment='mysql')name=Column(String(50))email=Column(String(255))wage=Column(Float(7,2),default=1250.38)id_department=Column(Integer)__constraints__=[PrimaryKeyConstraint('pk_t_emplooyes','id'),ForeignKeyConstraint('fk_t_emplooyes_t_department','id_department','t_department','id'),UniqueConstraint('un_t_emplooyes','email')]tb_emplooyes=TbEmployees(create_if_not_exists=True)print(tb_emplooyes)The printed string will be:CREATETABLEIFNOTEXISTST_EMPLOYEES(idINTEGERAUTO_INCREMENTNOTNULL,nameVARCHAR(50)NOTNULL,emailVARCHAR(255)NOTNULL,wageFLOAT(7,2)NOTNULLDEFAULT1250.38id_departmentINTEGERNOTNULL);ALTERTABLET_EMPLOOYESADDCONSTRAINTpk_t_emplooyesPRIMARYKEY(id);ALTERTABLET_EMPLOOYESADDCONSTRAINTfk_t_emplooyes_t_departmentFOREIGNKEY(id_department)REFERENCEST_DEPARTMENT(id);ALTERTABLET_EMPLOOYESADDCONSTRAINTun_t_emplooyesUNIQUE(email);In this extended example, a more complex table is created using PySQLQuery. Let's delve into the key elements specific to this example, without repeating the details covered in the previous one:Importing Required Classes and Constraintsfrompysqlqueryimport(Table,Column,Integer,String,Float)frompysqlquery.constraintsimport(ForeignKeyConstraint,PrimaryKeyConstraint,UniqueConstraint,)In addition to the basic classes, this example introduces named constraint classes from the pysqlquery.constraints module, including PrimaryKeyConstraint, ForeignKeyConstraint, and UniqueConstraint.Defining the TableclassTbEmployees(Table):__tablename__='T_EMPLOYEES'id=Column(Integer,auto_increment='mysql')name=Column(String(50))email=Column(String(255))wage=Column(Float(7,2),default=1250.38)id_department=Column(Integer)__constraints__=[PrimaryKeyConstraint('pk_t_emplooyes','id'),ForeignKeyConstraint('fk_t_emplooyes_t_department','id_department','t_department','id'),UniqueConstraint('un_t_emplooyes','email')]This more complex table includes additional specifications such as a table name (__tablename__) and named constraint list (__constraints__).Saving all tables that you have been createdTheTableclass provides thesave_all_tablesmethod that will save all tables that you have been created in a file.Table.save_all_tables('./my_tables.sql')ContributingYou may contribute in several ways like creating new features, fixing bugs, improving documentation and examples or translating any document here to your language. Find more information inCONTRIBUTING.md.CreditsPySQLQuery is heavily inspired inSQLAlchemy(for table class structure) andArrayMixer(for README).LicenseMIT- Alberto Frigatto, 2023