RFC: Simple-Storage

As you might know, days ago I released a new (still in beta) project named Simple-ORM, which is a very basic (but efficient) database abstraction. It’s major feature are:

  1. Easy iteration and data update inspired by ActiveRecords
  2. Built on top of PDO
  3. Implicit database connection, connecting only when it’s necessary
  4. Native query cache support.
  5. Support single-level transactions (limited by PDO)
  6. Data filtering with callback functions per column

Right now, it helps developer with some very basic standard SQL (insert, update, delete and a very limited select) and it is really simple to extend just with “raw” SQL. Another pretty cool feature is that it supports cache natively. Just visit the examples dir (I’ll add more samples ASAP).

It’s main disadvantage is that it is not a ORM properly, you still need to know SQL in order to make it usable for real life situations, I did it in that way because perform normal tasks (query based on objects, relationships or anything else that a proper ORM offers) at run-time is not efficient, it requires a lot of RAM, and is not that scalable. As much easier for the developer, harder for the server (me, right now)

In order to fix that (for me it’s okay, right I’m composing by hand all my SQL) I’m creating a project called Simple-Storage that will generate code, using Simple-ORM as base-code, to manipulate data. Additionally it would generate tables schemes, indexes (when it’s necessary), validation, relations, data-migration and several other features (when I would need something else I’ll add). Simple-storage would be a compiler (finally something useful where I can apply my knowledge from University) that will generate PHP (or any other if someone wants to extend it) code to manipulate the tables. By the fact that it wouldn’t perform any sort of checking/validation/generate-SQL/etc at run time and it has cache, it will be very scalable, and useful (IMHO).

Right now I’m sharing (expecting a feedback) about its possible syntax (I’d to have something clear, easy to learn/read and useful):

/**
 *  Simple table definition, we create a table called
 *  "posts".
 *
 */
(set default length 100)
table posts
    title:    string
    uri:      string
    abstract: text
    content:  text
    lang:     langs

/**
 *  Tag table.
 *
 */
table tags
    tag: string(50)
    lang: langs

/**
 *  This is the simplest table, it just contains
 *  a language list.
 *
 */
table langs:
    lang string(20)

/**
 *  Many-to-many relations ships, a post might
 *  has several tags, and a tag might has several posts.
 */
posts has many tags
tags  has many posts

/**
 *  we create a "hook" on table "posts" in order to
 *  recreate cache when a give post is updated.
 */
onUpdate: posts
    delete cache getPostByUri uri

/**
 *  Queries
 */

/**
 *  Create a method into the "posts" model that will retrieve three columns
 *  (title, lang.lang and uri) from posts that has some tag or set of tags.
 */
getPostByTag: posts
    columns:
        title
        lang.lang
        uri
    filters:
       posts in tags
    cache: 1h

/**
 *  Create a method that retrieves the entire post from based on its
 *  "uri", this query is cached for 1 hour
 *
 */
getPostByUri: posts
    uri equalto $1
    cache: 1h

/**
 *  Another query
 */
getPostByLang: posts
    lang equalto langs

This is a sample script that will create three tables (posts, tags and lang), relationships and some method to read data from the table. I’d like to get your thoughts about the language and idea in general.

2 Comments

  1. Jaume says:

    That looks a lot like YAML to me. Use YAML instead.

Leave a Reply