SQLAlchemy Introduction [LINK]
The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes with database tables, and instances of those classes (objects) with rows in their corresponding tables. it has des a system that transparently synchronizes all changes in state between objects and their related rows, called a unit of work, as well as a system for expressing database queries in terms of the user defined classes and their defined relationships between each other.
In my understanding, SQLAlchemy is a data operation method for Object/link ing. An object is a class written by Python, each entity of this class, it corresponds to each row of data in the database ). The operations on the entities of this class are directly mapped (affected) to the corresponding tuples in the database. This method is called a work unit (operation unit, which is somewhat similar to an atomic operation)
Example of Self-written Flavor Query
sqlalchemy.orm sqlalchemy sqlalchemy.ext.declarative sqlalchemy sql_connection = engine = create_engine(sql_connection, echo= Session = sessionmaker(bind= session = BASE = = id = Column(Integer, primary_key= name = Column(String(255 memory_mb = vcpus = root_gb = ephemeral_gb = flavorid = Column(String(255 swap = Column(Integer, nullable=False, default= rxtx_factor = Column(Float, nullable=False, default=1 vcpu_weight = Column(Integer, nullable= disabled = Column(Boolean, default= is_public = Column(Boolean, default= flavors = flavor flavor.id, flavor.name
Code output
2013-05-15 18:41:10,928 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()2013-05-15 18:41:10,929 INFO sqlalchemy.engine.base.Engine ()2013-05-15 18:41:10,933 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'character_set%%'2013-05-15 18:41:10,934 INFO sqlalchemy.engine.base.Engine ()2013-05-15 18:41:10,935 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'2013-05-15 18:41:10,936 INFO sqlalchemy.engine.base.Engine ()2013-05-15 18:41:10,937 INFO sqlalchemy.engine.base.Engine SHOW COLLATION2013-05-15 18:41:10,937 INFO sqlalchemy.engine.base.Engine ()2013-05-15 18:41:10,946 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'2013-05-15 18:41:10,946 INFO sqlalchemy.engine.base.Engine ()2013-05-15 18:41:10,948 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)2013-05-15 18:41:10,950 INFO sqlalchemy.engine.base.Engine SELECT instance_types.id AS instance_types_id, instance_types.name AS instance_types_name, instance_types.memory_mb AS instance_types_memory_mb, instance_types.vcpus AS instance_types_vcpus, instance_types.root_gb AS instance_types_root_gb, instance_types.ephemeral_gb AS instance_types_ephemeral_gb, instance_types.flavorid AS instance_types_flavorid, instance_types.swap AS instance_types_swap, instance_types.rxtx_factor AS instance_types_rxtx_factor, instance_types.vcpu_weight AS instance_types_vcpu_weight, instance_types.disabled AS instance_types_disabled, instance_types.is_public AS instance_types_is_public FROM instance_types2013-05-15 18:41:10,950 INFO sqlalchemy.engine.base.Engine ()1 m1.medium2 m1.tiny3 m1.large4 m1.xlarge5 m1.small6 m1.nano7 m1.micro
Code structure in Nova
BASE = declarative_base () # Same as the method in the previous sub-class. class NovaBase (): # The difference is that models is the module pass defined in 1.
Class InstanceTypes (BASE, NovaBase): = "instance_types" # database table name id = (Integer, primary_key = True) # Column is used to define the "instance_types" Field of the data table, its parameter is the specific type of this field name = Column (String (255) memory_mb = Column (Integer) vcpus = Column (Integer) root_gb = Column (Integer) ephemeral_gb = Column (Integer) flavorid = Column (String (255) swap = Column (Integer, nullable = False, default = 0) rxtx_factor = Column (Float, nullable = False, default = 1) vcpu_weight = Column (Integer, nullable = True) disabled = Column (Boolean, default = False) is_public = Column (Boolean, default = True)