Git Product home page Git Product logo

query_builder's Introduction

Easy SQL generation for Drupal

This is a class that allows for easy SQL generation for Drupal. The base class is preconfigured for the node, node_revisions and node_comment_statistics tables, but can easily be subclassed or reconfigured to support any other tables.

The QueryBuilder::query method returns an array with the generated sql as the first element and the parameters for the query as the second.

Quick examples:

$builder = new QueryBuilder();

Call

list($sql, $params) = $builder->query(
  array('nid', 'title', 'comment_count')
);

Result

$sql = "SELECT n.nid AS nid, n.title AS title, cs.comment_count AS comment_count
FROM {node} AS n
	INNER JOIN {node_comment_statistics} AS cs ON cs.nid=n.nid
ORDER BY n.created DESC";
$params = array();

Call

list($sql, $params) = $builder->query(
  array('nid', 'title', 'teaser'), 
  array('created'=>'1236938171:')
);

Result

$sql = "SELECT n.nid AS nid, n.title AS title, r.teaser AS teaser
FROM {node} AS n
	INNER JOIN {node_revisions} AS r ON r.vid=n.vid
WHERE n.created>=%d
ORDER BY n.created DESC";
$params = array(
  1236938171,
);

Call

list($sql, $params) = $builder->query(
  array('nid', 'title'), 
  array('created'=>'1236938171:1237298981', 'type'=>'event')
);

Result

$sql = "SELECT n.nid AS nid, n.title AS title
FROM {node} AS n
WHERE n.created>=%d
	AND n.created<=%d
	AND n.type='%s'
ORDER BY n.created DESC";
$params = array(
  1236938171,
  1237298981,
  'event',
);

Call

list($sql, $params) = $builder->query(
  array('nid', 'title', 'teaser'), 
  array('created'=>'1236938171:', 'sort_field'=>'comment_count', 'sort_order'=>'DESC')
);

Result

$sql = "SELECT n.nid AS nid, n.title AS title, r.teaser AS teaser
FROM {node} AS n
	INNER JOIN {node_revisions} AS r ON r.vid=n.vid
	INNER JOIN {node_comment_statistics} AS cs ON cs.nid=n.nid
WHERE n.created>=%d
ORDER BY cs.comment_count DESC";
$params = array(
  1236938171,
);

query_builder's People

Contributors

hugowetterberg avatar

Stargazers

Angus H. avatar JT5D avatar  avatar William Nguyen avatar Gaus Surahman avatar Si Nguyen avatar Rimian Perkins avatar  avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

Forkers

dalalsunil1986

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.