写一个简单的ORM

最近在研究SQLAlchemy源码,自己造个轮子先(当然,SQLAlchemy远比这里复杂)

import logging

logging.basicConfig(level=logging.DEBUG)


class Column:
    def __init__(self, type, default=None):
        self.col_name = None
        self.col_type = type
        self.col_default = default

    def __eq__(self, value):
        logging.debug("__eq__ of {} && {}".format(self, value))
        return "{} = {}".format(self.col_name, value)


class Base(type):
    def __new__(cls, *args, **kwargs):
        logging.debug("creating class {}, args {}, kwargs {}".format(
            cls, args, kwargs
        ))

        if len(args) > 2:
            for k, v in args[2].items():
                if k.startswith("_"):
                    continue

                v.col_name = k

        return type.__new__(cls, *args, **kwargs)


class User(metaclass=Base):
    __tablename__ = "user"

    id = Column(int)
    name = Column(str)
    passwd = Column(str)


class Queryable:
    def __init__(self, table):
        self.table = table

    def filter(self, text):
        return "select * from {} where {}".format(
            self.table.__tablename__,
            text,
        )


class Session:
    def query(self, table):
        logging.debug("returning Queryable({})".format(table))
        return Queryable(table)


session = Session()
print(session.query(User).filter(User.id == 1))

运行结果:

jiajun@debian test: python3 orm.py
DEBUG:root:creating class <class '__main__.Base'>, args ('User', (), {'__qualname__': 'User', '__tablename__': 'user', 'id': <__main__.Column object at 0x7f8174b107b8>, 'passwd': <__main__.Column object at 0x7f8174b10940>, 'name': <__main__.Column object at 0x7f8174b10828>, '__module__': '__main__'}), kwargs {}
DEBUG:root:returning Queryable(<class '__main__.User'>)
DEBUG:root:__eq__ of <__main__.Column object at 0x7f8174b107b8> && 1
select * from user where id = 1

更多文章
  • Haskell简明教程(四):Monoid, Applicative, Monad
  • HTTPS 的详细流程
  • OAuth2 为什么需要 Authorization Code?
  • 任务队列怎么写?python rq源码阅读与分析
  • XMonad 配置教程
  • Haskell简明教程(三):Haskell语法
  • Haskell简明教程(二):从命令式语言进行抽象
  • Haskell简明教程(一):从递归说起
  • 2017年必装的VIM插件推荐
  • TCP/IP简明教程 - 从零构建TCP/IP协议(二)连接,断开与拥塞控制
  • TCP/IP简明教程 - 从零构建TCP/IP协议(这次叫PCT协议)
  • Lua Manual 阅读笔记
  • Golang Map 源码阅读与分析
  • MySQL 零碎知识 - MySQL必知必会
  • Golang slice 源码阅读与分析