nobrin / macaron

A simple O/R mapper for SQLite
http://nobrin.github.com/macaron/
MIT License
31 stars 8 forks source link

SQLite view support #27

Open FrViPofm opened 10 years ago

FrViPofm commented 10 years ago

Sometimes, it is very convenient to use a view instead of a table.

Let us imagine a page with a side block showing the 10 last twits. In your template you need the author's id and name. It is very easy to prepare it in a view, and shurely faster to read it than invoquing twit.author.name.

Is it possible to think a "read_only" class for working with views ?

FrViPofm commented 9 years ago

I follow my idea with an example.

-- create Article
CREATE TABLE article (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    "title" TEXT,
    "pub_date" TEXT,
    "author" TEXT,
    "content" TEXT,
    "creation_date" TIMESTAMP,
    "keywords" TEXT DEFAULT ('NULL')
-- create Available
CREATE VIEW "available" AS SELECT * FROM article
    WHERE pub_date IS NOT NULL
    AND pub_date < date('now')

and in macaron models

class Article(macaron.Model):
    title           = macaron.CharField(max_length=64)
    author          = macaron.CharField(max_length=64)
    content         = macaron.CharField()
    pub_date        = macaron.DateField(null=True)
    creation_date   = macaron.TimestampAtCreate()
    keywords        = macaron.CharField(null=True)

class Available(macaron.View, Article):
    """" read only view"""
    id              = macaron.ViewField(Article.pk)
    title           = macaron.ViewField(Article.title)
    author          = macaron.ViewField(Article.author)
    content         = macaron.ViewField(Article.content)
    pub_date        = macaron.ViewField(Article.pub_date)
    creation_date   = macaron.ViewField(Article.creation_date)
    keywords        = macaron.ViewField(Article.keywords)

Just a suggestion...

nobrin commented 9 years ago

Hi, Your suggestion seems nice. And, in case of such as fetching author.name, the strategy may contribute its performance. I prepare the next release of Macaon, after that, I'll make a study on the suggestion.

FrViPofm commented 3 years ago

Hi, It could be a parent class of the Field class, kind of read-only field.