Skip to main content

EnORM—EnDATA Object Relational Mapper.

Project description

EnORM

EnORM—EnDATA Object Relational Mapper.

Example Usage

from EnORM import CASCADE, Column, DBEngine, DBSession, Float, ForeignKey, Integer, Model, Serial, String


class Company(Model):
    __table__ = "companies"

    id = Column(Serial, primary_key=True)
    name = Column(String, nullable=False)
    country = Column(String, 40)


class Employee(Model):
    id = Column(Serial, primary_key=True)
    full_name = Column(String, 100, nullable=False)
    salary = Column(Float)
    age = Column(Integer)
    role = Column(String, 20, default="entry")
    company_id = Column(Serial, None, ForeignKey(Company, reverse_name="employees", on_delete=CASCADE))


eng = DBEngine("postgresql://user:secret@localhost:5432/my_db")

with DBSession(eng) as session:
    the_company = session.query(Company).filter(Company.country == "United Kingdom").first()

    new_employee = Employee(full_name="Nima Bavari Goudarzi", salary=64320.00, role="engineer", company_id=the_company.id)
    session.add(new_employee)
    
    sharks = session.query(Employee, Employee.full_name, Employee.company_id).filter(Employee.salary > 90000.00).all()

Documentation

API Reference

Reference for the EnORM public API.

const .fkey.CASCADE [source]

String literal "cascade".

class .column.Column(type_, length=None, rel=None, *, primary_key=False, default=None, nullable=True) [source]

Abstraction of a real table column in a database.

Parameters

type_ : typing.Type

The type of the column as represented in SQL.

length : int | None (default None)

Max length of the value. Provided only when the type of the column is .types.String.

rel : .fkey.ForeignKey | None (default None)

Marker of a relationship. Provided only when a foreign relation exists.

primary_key : bool (default False)

Whether or not the column is a primary key. Keyword-only.

default : typing.Any (default None)

Default value for cells of the column to take. Keyword-only.

nullable : bool (default True)

Whether or not the cells of the column are nullable. Keyword-only.

Attributes

model : typing.Type

Relational model that the column belongs to.

variable_name : str

Name with which the column is defined.

view_name : str

Name of the SQL table that the column belongs to.

class .types.Date [source]

ORM representation of datetime.date objects.

class .types.DateTime [source]

ORM representation of datetime.datetime objects.

class .db_engine.DBEngine(conn_str) [source]

Connection adapter for the .db_session.DBSession object.

Parameters

conn_str : str

Database resource location, along with auth params.

Attributes

conn : pyodbc.Connection

DB API connection object.

cursor : pyodbc.Cursor

DB API cursor object.

class .db_session.DBSession(engine) [source]

Singleton DB session to access the database across all uses.

Parameters

engine : .db_engine.AbstractEngine

DB engine that the session uses.

Attributes

engine : .db_engine.AbstractEngine

DB engine that the session uses.

queue : list

List of newly created instances of DB objects not yet persisted.

accumulator : list

List of ORM queries not yet executed.

Methods

query(*fields) -> .query.Query

Starts a query and returns the query object.

add(obj) -> None

Adds a new record to the DB session.

save() -> None

Persists all started queries.

commit_adds() -> None

Persists all added records on the DB session.

class .fkey.ForeignKey(foreign_model, *, reverse_name, on_delete, on_update) [source]

Representer of foreign key relations.

Parameters

foreign_model : typing.Type

MappedClass that represents a foreign model.

reverse_name : str

Relation name on the related class tied to this class. Keyword-only.

on_delete : str | None (default None)

Whether to delete cascade style or not. Keyword-only.

on_update : str | None (default None)

Whether to update cascade style or not. Keyword-only.

class .model.Model(**attrs) [source]

Abstract representer of an ORM database model in Python.

Parameters

attrs : typing.Any

Initialization attributes.

Attributes

sql : str

SQL statement for new object creation.

Methods

label(alias) -> .column.Label

Returns the ORM representation for SQL table aliasing.

class .types.Float [source]

ORM representation of float objects.

class .types.Integer [source]

ORM representation of int objects.

class .types.Serial [source]

ORM representation of serial types in SQL variants.

Parameters

val : int

Underlying integer value.

Attributes

val : int

Underlying integer value.

class .types.String [source]

ORM representation of str objects.

class .types.Time [source]

ORM representation of datetime.time objects.

func agg(field, name_in_sql) -> .column.BaseColumn [source]

Describes the basis for all aggregate functions.

Parameters

field : .column.BaseColumn

Argument.

name_in_sql : str

Name of the function in SQL.

func count(field) -> .column.BaseColumn [source]

Describes the SQL aggregate function COUNT.

func sum_(field) -> .column.BaseColumn [source]

Describes the SQL aggregate function SUM.

func avg(field) -> .column.BaseColumn [source]

Describes the SQL aggregate function AVG.

func min_(field) -> .column.BaseColumn [source]

Describes the SQL aggregate function MIN.

func max_(field) -> .column.BaseColumn [source]

Describes the SQL aggregate function MAX.

func char_length(c) -> .column.Scalar [source]

Describes the SQL function CHAR_LENGTH.

Parameters

c : .column.BaseColumn | str

Generic column or its string value.

func concat(*parts) -> str [source]

Concatenates multiple strings into one.

Parameters

parts : list of str

Separate parts.

func current_date() -> .column.Scalar [source]

Describes the SQL function CURRENT_DATE.

func current_time(precision=None) -> .column.Scalar [source]

Describes the SQL function CURRENT_TIME.

Parameters

precision : int | None (default None)

Number of significant decimal digits.

func current_timestamp(precision=None) -> .column.Scalar [source]

Describes the SQL function CURRENT_TIMESTAMP.

Parameters

precision : int | None (default None)

Number of significant decimal digits.

func current_user() -> .column.Scalar [source]

Describes the SQL function CURRENT_USER.

func local_time(precision=None) -> .column.Scalar [source]

Describes the SQL function LOCALTIME.

Parameters

precision : int | None (default None)

Number of significant decimal digits.

func local_timestamp() -> .column.Scalar [source]

Describes the SQL function LOCALTIMESTAMP.

func now() -> .column.Scalar [source]

Describes the SQL function NOW.

func random() -> .column.Scalar [source]

Describes the SQL function RANDOM.

func session_user() -> .column.Scalar [source]

Describes the SQL function SESSION_USER.

func user() -> .column.Scalar [source]

Describes the SQL function USER.

Tutorial

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

EnORM-1.0.1.tar.gz (34.0 kB view hashes)

Uploaded Source

Built Distribution

EnORM-1.0.1-py3-none-any.whl (30.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page