Index: inc/core/class.dc.auth.php
===================================================================
--- inc/core/class.dc.auth.php	(revision 3731)
+++ inc/core/class.dc.auth.php	(revision 3761)
@@ -93,13 +93,14 @@
     {
         # Check user and password
-        $strReq = 'SELECT user_id, user_super, user_pwd, user_change_pwd, ' .
-        'user_name, user_firstname, user_displayname, user_email, ' .
-        'user_url, user_default_blog, user_options, ' .
-        'user_lang, user_tz, user_post_status, user_creadt, user_upddt ' .
-        'FROM ' . $this->con->escapeSystem($this->user_table) . ' ' .
-        "WHERE user_id = '" . $this->con->escape($user_id) . "' ";
+        $sql = new dcSelectStatement($this->core, 'coreAuthCheckUser');
+        $sql
+            ->columns(array('user_id', 'user_super', 'user_pwd', 'user_change_pwd', 'user_name', 'user_firstname',
+                'user_displayname', 'user_email', 'user_url', 'user_default_blog', 'user_options', 'user_lang',
+                'user_tz', 'user_post_status', 'user_creadt', 'user_upddt'))
+            ->from($this->user_table)
+            ->where('user_id = ' . $sql->quote($user_id));
 
         try {
-            $rs = $this->con->select($strReq);
+            $rs = $this->con->select($sql->statement());
         } catch (Exception $e) {
             $err = $e->getMessage();
@@ -146,5 +147,8 @@
                 $cur           = $this->con->openCursor($this->user_table);
                 $cur->user_pwd = (string) $rs->user_pwd;
-                $cur->update("WHERE user_id = '" . $rs->user_id . "'");
+
+                $sql = new dcUpdateStatement($this->core, 'coreAuthCheckUser');
+                $sql->where('user_id = ' . $sql->quote($rs->user_id));
+                $cur->update($sql->whereStatement());
             }
         } elseif ($user_key != '') {
@@ -618,7 +622,9 @@
     public function recoverUserPassword($recover_key)
     {
-        $strReq = 'SELECT user_id, user_email ' .
-        'FROM ' . $this->user_table . ' ' .
-        "WHERE user_recover_key = '" . $this->con->escape($recover_key) . "' ";
+        $sql = new dcSelectStatement($this->core, 'coreAuthRecoverUserPwd');
+        $sql
+            ->columns(array('user_id', 'user_email'))
+            ->from($this->user_table)
+            ->where('user_recover_key = ' . $sql->quote($recover_key));
 
         $rs = $this->con->select($strReq);
@@ -635,5 +641,7 @@
         $cur->user_change_pwd  = 1; // User will have to change this temporary password at next login
 
-        $cur->update("WHERE user_recover_key = '" . $this->con->escape($recover_key) . "'");
+        $sql = new dcUpdateStatement($this->core, 'coreAuthRecoverUserPwd');
+        $sql->where('user_recover_key = ' . $sql->quote($recover_key));
+        $cur->update($sql->whereStatement());
 
         return array('user_email' => $rs->user_email, 'user_id' => $rs->user_id, 'new_pass' => $new_pass);
Index: inc/core/class.dc.blog.php
===================================================================
--- inc/core/class.dc.blog.php	(revision 3731)
+++ inc/core/class.dc.blog.php	(revision 3761)
@@ -209,5 +209,9 @@
         $cur->blog_upddt = date('Y-m-d H:i:s');
 
-        $cur->update("WHERE blog_id = '" . $this->con->escape($this->id) . "' ");
+        $sql = new dcUpdateStatement($this->core, 'coreTriggerBlog');
+        $sql
+            ->where('blog_id = ' . $sql->quote($this->id));
+
+        $cur->update($sql->whereStatement());
 
         # --BEHAVIOR-- coreBlogAfterTriggerBlog
@@ -241,11 +245,12 @@
         # Get posts affected by comments edition
         if (empty($affected_posts)) {
-            $strReq =
-            'SELECT post_id ' .
-            'FROM ' . $this->prefix . 'comment ' .
-            'WHERE comment_id' . $this->con->in($comments_ids) .
-                'GROUP BY post_id';
-
-            $rs = $this->con->select($strReq);
+            $sql = new dcSelectStatement($this->core, 'coreTriggerCommentsScope');
+            $sql
+                ->columns('post_id')
+                ->from($this->prefix . 'comment')
+                ->where('comment_id' . $sql->in($comments_ids))
+                ->group('post_id');
+
+            $rs = $this->con->select($sql->statement());
 
             $affected_posts = array();
@@ -253,4 +258,5 @@
                 $affected_posts[] = (integer) $rs->post_id;
             }
+            unset($sql);
         }
 
@@ -260,12 +266,15 @@
 
         # Count number of comments if exists for affected posts
-        $strReq =
-        'SELECT post_id, COUNT(post_id) AS nb_comment, comment_trackback ' .
-        'FROM ' . $this->prefix . 'comment ' .
-        'WHERE comment_status = 1 ' .
-        'AND post_id' . $this->con->in($affected_posts) .
-            'GROUP BY post_id,comment_trackback';
-
-        $rs = $this->con->select($strReq);
+        $sql = new dcSelectStatement($this->core, 'coreTriggerCommentsCount');
+        $sql
+            ->columns(array('post_id', 'COUNT(post_id) AS nb_comment', 'comment_trackback'))
+            ->from($this->prefix . 'comment')
+            ->where(array(
+                'comment_status = 1',
+                'post_id' . $sql->in($affected_posts)
+            ))
+            ->group(array('post_id', 'comment_trackback'));
+
+        $rs = $this->con->select($sql->statement());
 
         $posts = array();
@@ -461,21 +470,23 @@
     private function getCategoriesCounter($params = array())
     {
-        $strReq =
-        'SELECT  C.cat_id, COUNT(P.post_id) AS nb_post ' .
-        'FROM ' . $this->prefix . 'category AS C ' .
-        'JOIN ' . $this->prefix . "post P ON (C.cat_id = P.cat_id AND P.blog_id = '" . $this->con->escape($this->id) . "' ) " .
-        "WHERE C.blog_id = '" . $this->con->escape($this->id) . "' ";
+        $sql = new dcSelectStatement($this->core, 'coreGetCategoriesCounter');
+        $sql
+            ->columns(array('C.cat_id', 'COUNT(P.post_id) AS nb_post'))
+            ->from($this->prefix . 'category AS C')
+            ->join('JOIN ' . $this->prefix . 'post P ' .
+                'ON (C.cat_id = P.cat_id AND P.blog_id = ' . $sql->quote($this->id) . ')')
+            ->where('C.blog_id = ' . $sql->quote($this->id));
 
         if (!$this->core->auth->userID()) {
-            $strReq .= 'AND P.post_status = 1 ';
+            $sql->where('P.post_status = 1');
         }
 
         if (!empty($params['post_type'])) {
-            $strReq .= 'AND P.post_type ' . $this->con->in($params['post_type']);
-        }
-
-        $strReq .= 'GROUP BY C.cat_id ';
-
-        $rs       = $this->con->select($strReq);
+            $sql->where('P.post_type ' . $sql->in($params['post_type']));
+        }
+
+        $sql->group('C.cat_id');
+
+        $rs       = $this->con->select($sql->statement());
         $counters = array();
         while ($rs->fetch()) {
@@ -568,7 +579,11 @@
         $this->core->callBehavior('coreBeforeCategoryUpdate', $this, $cur);
 
-        $cur->update(
-            'WHERE cat_id = ' . (integer) $id . ' ' .
-            "AND blog_id = '" . $this->con->escape($this->id) . "' ");
+        $sql = new dcUpdateStatement($this->core, 'coreCategoryUpdate');
+        $sql
+            ->where(array(
+                'cat_id = ' . (integer) $id,
+                'blog_id = ' . $sql->quote($this->id)
+            ));
+        $cur->update($sql->whereStatement());
 
         # --BEHAVIOR-- coreAfterCategoryUpdate
@@ -639,10 +654,13 @@
         }
 
-        $strReq = 'SELECT COUNT(post_id) AS nb_post ' .
-        'FROM ' . $this->prefix . 'post ' .
-        'WHERE cat_id = ' . (integer) $id . ' ' .
-        "AND blog_id = '" . $this->con->escape($this->id) . "' ";
-
-        $rs = $this->con->select($strReq);
+        $sql = new dcSelectStatement($this->core, 'coreCategoryPostCount');
+        $sql
+            ->columns('COUNT(post_id) AS nb_post')
+            ->from($this->prefix . 'post')
+            ->where(array(
+                'cat_id = ' . (integer) $id,
+                'blog_id = ' . $sql->quote($this->id)
+            ));
+        $rs = $this->con->select($sql->statement());
 
         if ($rs->nb_post > 0) {
@@ -669,30 +687,23 @@
     private function checkCategory($title, $url, $id = null)
     {
-        # Let's check if URL is taken...
-        $strReq =
-        'SELECT cat_url FROM ' . $this->prefix . 'category ' .
-        "WHERE cat_url = '" . $this->con->escape($url) . "' " .
-        ($id ? 'AND cat_id <> ' . (integer) $id . ' ' : '') .
-        "AND blog_id = '" . $this->con->escape($this->id) . "' " .
-            'ORDER BY cat_url DESC';
-
-        $rs = $this->con->select($strReq);
+        // Let's check if URL is taken...
+        $sql = new dcSelectStatement($this->core, 'coreCheckCategoryURL');
+        $sql
+            ->columns('cat_url')
+            ->from($this->prefix . 'category')
+            ->where('blog_id = ' . $sql->quote($this->id))
+            ->order('cat_url DESC');
+        if ($id) {
+            $sql->where('AND cat_id <> ' . (integer) $i);
+        }
+        $sql->cond('AND cat_url = ' . $sql->quote($url));
+
+        $rs = $this->con->select($sql->statement());
 
         if (!$rs->isEmpty()) {
-            if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli' || $this->con->driver() == 'mysqlimb4') {
-                $clause = "REGEXP '^" . $this->con->escape($url) . "[0-9]+$'";
-            } elseif ($this->con->driver() == 'pgsql') {
-                $clause = "~ '^" . $this->con->escape($url) . "[0-9]+$'";
-            } else {
-                $clause = "LIKE '" . $this->con->escape($url) . "%'";
-            }
-            $strReq =
-            'SELECT cat_url FROM ' . $this->prefix . 'category ' .
-            "WHERE cat_url " . $clause . ' ' .
-            ($id ? 'AND cat_id <> ' . (integer) $id . ' ' : '') .
-            "AND blog_id = '" . $this->con->escape($this->id) . "' " .
-                'ORDER BY cat_url DESC ';
-
-            $rs = $this->con->select($strReq);
+            // Replace condition on cat_url
+            $sql->cond('AND cat_url ' . $sql->regexp($url), true);
+
+            $rs = $this->con->select($sql->statement());
             $a  = array();
             while ($rs->fetch()) {
@@ -791,58 +802,51 @@
         $this->core->callBehavior('coreBlogBeforeGetPosts', $params);
 
+        $sql = new dcSelectStatement($this->core, 'coreGetPosts');
+
         if ($count_only) {
-            $strReq = 'SELECT count(DISTINCT P.post_id) ';
+            $sql->columns('count(DISTINCT P.post_id)');
         } elseif (!empty($params['sql_only'])) {
-            $strReq = 'SELECT P.post_id ';
+            $sql->columns('P.post_id');
         } else {
-            if (!empty($params['no_content'])) {
-                $content_req = '';
-            } else {
-                $content_req =
-                    'post_excerpt, post_excerpt_xhtml, ' .
-                    'post_content, post_content_xhtml, post_notes, ';
+            if (empty($params['no_content'])) {
+                $sql->columns(array('post_excerpt', 'post_excerpt_xhtml', 'post_content', 'post_content_xhtml', 'post_notes'));
             }
 
             if (!empty($params['columns']) && is_array($params['columns'])) {
-                $content_req .= implode(', ', $params['columns']) . ', ';
-            }
-
-            $strReq =
-                'SELECT P.post_id, P.blog_id, P.user_id, P.cat_id, post_dt, ' .
-                'post_tz, post_creadt, post_upddt, post_format, post_password, ' .
-                'post_url, post_lang, post_title, ' . $content_req .
-                'post_type, post_meta, ' .
-                'post_status, post_firstpub, post_selected, post_position, ' .
-                'post_open_comment, post_open_tb, nb_comment, nb_trackback, ' .
-                'U.user_name, U.user_firstname, U.user_displayname, U.user_email, ' .
-                'U.user_url, ' .
-                'C.cat_title, C.cat_url, C.cat_desc ';
-        }
-
-        $strReq .=
-        'FROM ' . $this->prefix . 'post P ' .
-        'INNER JOIN ' . $this->prefix . 'user U ON U.user_id = P.user_id ' .
-        'LEFT OUTER JOIN ' . $this->prefix . 'category C ON P.cat_id = C.cat_id ';
+                $sql->columns($params['columns']);
+            }
+
+            $sql->columns(array('P.post_id', 'P.blog_id', 'P.user_id', 'P.cat_id', 'post_dt', 'post_tz', 'post_creadt',
+                'post_upddt', 'post_format', 'post_password', 'post_url', 'post_lang', 'post_title', 'post_type',
+                'post_meta', 'post_status', 'post_firstpub', 'post_selected', 'post_position', 'post_open_comment',
+                'post_open_tb', 'nb_comment', 'nb_trackback', 'U.user_name', 'U.user_firstname', 'U.user_displayname',
+                'U.user_email', 'U.user_url', 'C.cat_title', 'C.cat_url', 'C.cat_desc'));
+        }
+
+        $sql->from($this->prefix . 'post P')
+            ->join('INNER JOIN ' . $this->prefix . 'user U ON U.user_id = P.user_id')
+            ->join('LEFT OUTER JOIN ' . $this->prefix . 'category C ON P.cat_id = C.cat_id');
+
+        if (!empty($params['join'])) {
+            $sql->join($params['join']);
+        }
 
         if (!empty($params['from'])) {
-            $strReq .= $params['from'] . ' ';
-        }
-
-        $strReq .=
-        "WHERE P.blog_id = '" . $this->con->escape($this->id) . "' ";
+            $sql->from($params['from']);
+        }
+
+        $sql->where('P.blog_id = ' . $sql->quote($this->id));
 
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .= 'AND ((post_status = 1 ';
-
+            $cond = '((post_status = 1';
             if ($this->without_password) {
-                $strReq .= 'AND post_password IS NULL ';
-            }
-            $strReq .= ') ';
-
+                $cond .= ' AND post_password IS NULL';
+            }
+            $cond .= ')';
             if ($this->core->auth->userID()) {
-                $strReq .= "OR P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "')";
-            } else {
-                $strReq .= ') ';
-            }
+                $cond .= 'OR P.user_id = ' . $sql->quote($this->core->auth->userID());
+            }
+            $cond .= ')';
+            $sql->where($cond);
         }
 
@@ -850,8 +854,8 @@
         if (isset($params['post_type'])) {
             if (is_array($params['post_type']) || $params['post_type'] != '') {
-                $strReq .= 'AND post_type ' . $this->con->in($params['post_type']);
+                $sql->where('post_type ' . $sql->in($params['post_type']));
             }
         } else {
-            $strReq .= "AND post_type = 'post' ";
+            $sql->where("post_type = 'post' ");
         }
 
@@ -862,5 +866,5 @@
                 $params['post_id'] = array((integer) $params['post_id']);
             }
-            $strReq .= 'AND P.post_id ' . $this->con->in($params['post_id']);
+            $sql->where('P.post_id ' . $sql->in($params['post_id']));
         }
 
@@ -871,13 +875,13 @@
                 $params['exclude_post_id'] = array((integer) $params['exclude_post_id']);
             }
-            $strReq .= 'AND P.post_id NOT ' . $this->con->in($params['exclude_post_id']);
+            $sql->where('P.post_id NOT ' . $sql->in($params['exclude_post_id']));
         }
 
         if (isset($params['post_url']) && $params['post_url'] !== '') {
-            $strReq .= "AND post_url = '" . $this->con->escape($params['post_url']) . "' ";
+            $sql->where('post_url = ' . $sql->quote($params['post_url']));
         }
 
         if (!empty($params['user_id'])) {
-            $strReq .= "AND U.user_id = '" . $this->con->escape($params['user_id']) . "' ";
+            $sql->where('U.user_id = ' . $sql->quote($params['user_id']));
         }
 
@@ -889,5 +893,5 @@
                 array_walk($params['cat_id'], function (&$v, $k) {$v = $v . " ?not";});
             }
-            $strReq .= 'AND ' . $this->getPostsCategoryFilter($params['cat_id'], 'cat_id') . ' ';
+            $sql->where($this->getPostsCategoryFilter($params['cat_id'], 'cat_id'));
         } elseif (isset($params['cat_url']) && $params['cat_url'] !== '') {
             if (!is_array($params['cat_url'])) {
@@ -897,37 +901,34 @@
                 array_walk($params['cat_url'], function (&$v, $k) {$v = $v . " ?not";});
             }
-            $strReq .= 'AND ' . $this->getPostsCategoryFilter($params['cat_url'], 'cat_url') . ' ';
+            $sql->where($this->getPostsCategoryFilter($params['cat_url'], 'cat_url'));
         }
 
         /* Other filters */
         if (isset($params['post_status'])) {
-            $strReq .= 'AND post_status = ' . (integer) $params['post_status'] . ' ';
+            $sql->where('post_status = ' . (integer) $params['post_status']);
         }
 
         if (isset($params['post_firstpub'])) {
-            $strReq .= 'AND post_firstpub = ' . (integer) $params['post_firstpub'] . ' ';
+            $sql->where('post_firstpub = ' . (integer) $params['post_firstpub']);
         }
 
         if (isset($params['post_selected'])) {
-            $strReq .= 'AND post_selected = ' . (integer) $params['post_selected'] . ' ';
+            $sql->where('post_selected = ' . (integer) $params['post_selected']);
         }
 
         if (!empty($params['post_year'])) {
-            $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%Y') . ' = ' .
-            "'" . sprintf('%04d', $params['post_year']) . "' ";
+            $sql->where($sql->dateFormat('post_dt', '%Y') . ' = ' . "'" . sprintf('%04d', $params['post_year']) . "'");
         }
 
         if (!empty($params['post_month'])) {
-            $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%m') . ' = ' .
-            "'" . sprintf('%02d', $params['post_month']) . "' ";
+            $sql->where($sql->dateFormat('post_dt', '%m') . ' = ' . "'" . sprintf('%02d', $params['post_month']) . "'");
         }
 
         if (!empty($params['post_day'])) {
-            $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%d') . ' = ' .
-            "'" . sprintf('%02d', $params['post_day']) . "' ";
+            $sql->where($sql->dateFormat('post_dt', '%d') . ' = ' . "'" . sprintf('%02d', $params['post_day']) . "'");
         }
 
         if (!empty($params['post_lang'])) {
-            $strReq .= "AND P.post_lang = '" . $this->con->escape($params['post_lang']) . "' ";
+            $sql->where('P.post_lang = ' . $sql->quote($params['post_lang']));
         }
 
@@ -943,7 +944,7 @@
                 if ($words) {
                     foreach ($words as $i => $w) {
-                        $words[$i] = "post_words LIKE '%" . $this->con->escape($w) . "%'";
+                        $words[$i] = "post_words LIKE '%" . $sql->escape($w) . "%'";
                     }
-                    $strReq .= 'AND ' . implode(' AND ', $words) . ' ';
+                    $sql->where(implode(' AND ', $words));
                 }
             }
@@ -951,42 +952,41 @@
 
         if (isset($params['media'])) {
-            if ($params['media'] == '0') {
-                $strReq .= 'AND NOT ';
-            } else {
-                $strReq .= 'AND ';
-            }
-            $strReq .= 'EXISTS (SELECT M.post_id FROM ' . $this->prefix . 'post_media M ' .
-                'WHERE M.post_id = P.post_id ';
+            $sqlEx = new dcSelectStatement($this->core, 'coreGetPostsMedia');
+            $sqlEx
+                ->columns('M.post_id')
+                ->from($this->prefix . 'post_media M')
+                ->where('M.post_id = P.post_id');
             if (isset($params['link_type'])) {
-                $strReq .= " AND M.link_type " . $this->con->in($params['link_type']) . " ";
-            }
-            $strReq .= ")";
+                $sqlEx->where('M.link_type ' . $sql->in($params['link_type']));
+            }
+            $sql->where(($params['media'] == '0' ? 'NOT ' : '') . 'EXISTS (' . $sqlEx->statement() . ')');
         }
 
         if (!empty($params['where'])) {
-            $strReq .= $params['where'] . ' ';
+            $sql->cond($params['where']);
         }
 
         if (!empty($params['sql'])) {
-            $strReq .= $params['sql'] . ' ';
+            $sql->sql($params['sql']);
         }
 
         if (!$count_only) {
             if (!empty($params['order'])) {
-                $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' ';
+                $sql->order($sql->escape($params['order']));
             } else {
-                $strReq .= 'ORDER BY post_dt DESC ';
-            }
-        }
-
-        if (!$count_only && !empty($params['limit'])) {
-            $strReq .= $this->con->limit($params['limit']);
-        }
+                $sql->order('post_dt DESC');
+            }
+            if (!empty($params['limit'])) {
+                $sql->limit($params['limit']);
+            }
+        }
+
+        $query = $sql->statement();
 
         if (!empty($params['sql_only'])) {
-            return $strReq;
-        }
-
-        $rs            = $this->con->select($strReq);
+            return $query;
+        }
+
+        $rs            = $this->con->select($query);
         $rs->core      = $this->core;
         $rs->_nb_media = array();
@@ -1070,38 +1070,42 @@
     public function getLangs($params = array())
     {
-        $strReq = 'SELECT COUNT(post_id) as nb_post, post_lang ' .
-        'FROM ' . $this->prefix . 'post ' .
-        "WHERE blog_id = '" . $this->con->escape($this->id) . "' " .
-            "AND post_lang <> '' " .
-            "AND post_lang IS NOT NULL ";
+        $sql = new dcSelectStatement($this->core, 'coreGetLangs');
+        $sql
+            ->columns(array('COUNT(post_id) as nb_post', 'post_lang'))
+            ->from($this->prefix . 'post')
+            ->where(array(
+                'blog_id = ' . $sql->quote($this->id),
+                "post_lang <> ''",
+                'post_lang IS NOT NULL'
+            ));
 
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .= 'AND ((post_status = 1 ';
+            $cond = '((post_status = 1';
 
             if ($this->without_password) {
-                $strReq .= 'AND post_password IS NULL ';
-            }
-            $strReq .= ') ';
+                $cond .= ' AND post_password IS NULL';
+            }
+            $cond .= ')';
 
             if ($this->core->auth->userID()) {
-                $strReq .= "OR user_id = '" . $this->con->escape($this->core->auth->userID()) . "')";
-            } else {
-                $strReq .= ') ';
-            }
+                $cond .= " OR user_id = " . $sql->quote($this->core->auth->userID());
+            }
+            $cond .= ')';
+            $sql->where($cond);
         }
 
         if (isset($params['post_type'])) {
             if ($params['post_type'] != '') {
-                $strReq .= "AND post_type = '" . $this->con->escape($params['post_type']) . "' ";
+                $sql->where("post_type = " . $sql->quote($params['post_type']));
             }
         } else {
-            $strReq .= "AND post_type = 'post' ";
+            $sql->where("post_type = 'post'");
         }
 
         if (isset($params['lang'])) {
-            $strReq .= "AND post_lang = '" . $this->con->escape($params['lang']) . "' ";
-        }
-
-        $strReq .= 'GROUP BY post_lang ';
+            $sql->where("post_lang = " . $sql->quote($params['lang']));
+        }
+
+        $sql->group('post_lang');
 
         $order = 'desc';
@@ -1109,7 +1113,7 @@
             $order = $params['order'];
         }
-        $strReq .= 'ORDER BY post_lang ' . $order . ' ';
-
-        return $this->con->select($strReq);
+        $sql->order('post_lang ' . $order);
+
+        return $this->con->select($sql->statement());
     }
 
@@ -1134,4 +1138,6 @@
     public function getDates($params = array())
     {
+        $sql = new dcSelectStatement($this->core, 'coreGetDates');
+
         $dt_f  = '%Y-%m-%d';
         $dt_fc = '%Y%m%d';
@@ -1148,56 +1154,59 @@
         $dt_fc .= '000000';
 
-        $cat_field = $catReq = $limit = '';
+        $sql
+            ->distinct()
+            ->columns(array(
+                $sql->dateFormat('post_dt', $dt_f) . ' AS dt',
+                'COUNT(P.post_id) AS nb_post'
+            ))
+            ->from($this->prefix . 'post P')
+            ->join('LEFT JOIN ' . $this->prefix . 'category C ON P.cat_id = C.cat_id')
+            ->group('dt');
 
         if (isset($params['cat_id']) && $params['cat_id'] !== '') {
-            $catReq    = 'AND P.cat_id = ' . (integer) $params['cat_id'] . ' ';
-            $cat_field = ', C.cat_url ';
+            $sql
+                ->columns('C.cat_url')
+                ->where('P.cat_id = ' . (integer) $params['cat_id'])
+                ->group('C.car_url');
         } elseif (isset($params['cat_url']) && $params['cat_url'] !== '') {
-            $catReq    = "AND C.cat_url = '" . $this->con->escape($params['cat_url']) . "' ";
-            $cat_field = ', C.cat_url ';
+            $sql
+                ->columns('C.cat_url')
+                ->where('C.cat_url = ' . $sql->quote($params['cat_url']))
+                ->group('C.car_url');
         }
         if (!empty($params['post_lang'])) {
-            $catReq = 'AND P.post_lang = \'' . $params['post_lang'] . '\' ';
-        }
-
-        $strReq = 'SELECT DISTINCT(' . $this->con->dateFormat('post_dt', $dt_f) . ') AS dt ' .
-        $cat_field .
-        ',COUNT(P.post_id) AS nb_post ' .
-        'FROM ' . $this->prefix . 'post P LEFT JOIN ' . $this->prefix . 'category C ' .
-        'ON P.cat_id = C.cat_id ' .
-        "WHERE P.blog_id = '" . $this->con->escape($this->id) . "' " .
-            $catReq;
+            $sql->where('P.post_lang = ' . $sql->quote($params['post_lang']));
+        }
 
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .= 'AND ((post_status = 1 ';
-
+            $cond = '((post_status = 1';
             if ($this->without_password) {
-                $strReq .= 'AND post_password IS NULL ';
-            }
-            $strReq .= ') ';
+                $cond .= ' AND post_password IS NULL';
+            }
+            $cond .= ')';
 
             if ($this->core->auth->userID()) {
-                $strReq .= "OR P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "')";
-            } else {
-                $strReq .= ') ';
-            }
+                $cond .= ' OR P.user_id = ' . $sql->quote($this->core->auth->userID());
+            }
+            $cond .= ')';
+            $sql->where($cond);
         }
 
         if (!empty($params['post_type'])) {
-            $strReq .= "AND post_type " . $this->con->in($params['post_type']) . " ";
+            $sql->where("post_type " . $sql->in($params['post_type']));
         } else {
-            $strReq .= "AND post_type = 'post' ";
+            $sql->where("post_type = 'post'");
         }
 
         if (!empty($params['year'])) {
-            $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%Y') . " = '" . sprintf('%04d', $params['year']) . "' ";
+            $sql->where($sql->dateFormat('post_dt', '%Y') . " = '" . sprintf('%04d', $params['year']) . "'");
         }
 
         if (!empty($params['month'])) {
-            $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%m') . " = '" . sprintf('%02d', $params['month']) . "' ";
+            $sql->where($sql->dateFormat('post_dt', '%m') . " = '" . sprintf('%02d', $params['month']) . "'");
         }
 
         if (!empty($params['day'])) {
-            $strReq .= 'AND ' . $this->con->dateFormat('post_dt', '%d') . " = '" . sprintf('%02d', $params['day']) . "' ";
+            $sql->where($sql->dateFormat('post_dt', '%d') . " = '" . sprintf('%02d', $params['day']) . "'");
         }
 
@@ -1216,9 +1225,8 @@
             $dt = date('YmdHis', strtotime($dt));
 
-            $strReq .= 'AND ' . $this->con->dateFormat('post_dt', $dt_fc) . $pdir . "'" . $dt . "' ";
-            $limit = $this->con->limit(1);
-        }
-
-        $strReq .= 'GROUP BY dt ' . $cat_field;
+            $sql
+                ->where($sql->dateFormat('post_dt', $dt_fc) . $pdir . "'" . $dt . "'")
+                ->limit(1);
+        }
 
         $order = 'desc';
@@ -1226,10 +1234,7 @@
             $order = $params['order'];
         }
-
-        $strReq .=
-            'ORDER BY dt ' . $order . ' ' .
-            $limit;
-
-        $rs = $this->con->select($strReq);
+        $sql->order('dt ' . $order);
+
+        $rs = $this->con->select($sql->statement());
         $rs->extend('rsExtDates');
         return $rs;
@@ -1253,8 +1258,10 @@
         {
             # Get ID
-            $rs = $this->con->select(
-                'SELECT MAX(post_id) ' .
-                'FROM ' . $this->prefix . 'post '
-            );
+            $sql = new dcSelectStatement($this->core, 'corePostCreateGetID');
+            $sql
+                ->columns('MAX(post_id)')
+                ->from($this->prefix . 'post');
+
+            $rs = $this->con->select($sql->statement());
 
             $cur->post_id     = (integer) $rs->f(0) + 1;
@@ -1328,12 +1335,17 @@
         $cur->post_upddt = date('Y-m-d H:i:s');
 
-        #If user is only "usage", we need to check the post's owner
+        // If user is only "usage", we need to check the post's owner
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq = 'SELECT post_id ' .
-            'FROM ' . $this->prefix . 'post ' .
-            'WHERE post_id = ' . $id . ' ' .
-            "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' ";
-
-            $rs = $this->con->select($strReq);
+            $sql = new dcSelectStatement($this->core, 'corePostUpdateCheckOwner');
+            $sql
+                ->column('post_id')
+                ->from($this->prefix . 'post')
+                ->where(array(
+                    'post_id = ' . $id,
+                    'user_id = ' . $sql->quote($this->core->auth->userID())
+                ));
+
+            $rs = $this->con->select($sql->statement());
+            unset($sql);
 
             if ($rs->isEmpty()) {
@@ -1345,5 +1357,8 @@
         $this->core->callBehavior('coreBeforePostUpdate', $this, $cur);
 
-        $cur->update('WHERE post_id = ' . $id . ' ');
+        $sql = new dcUpdateStatement($this->core, 'corePostUpdate');
+        $sql->where('post_id = ' . $id);
+
+        $cur->update($sql->whereStatement());
 
         # --BEHAVIOR-- coreAfterPostUpdate
@@ -1381,10 +1396,14 @@
         $status    = (integer) $status;
 
-        $strReq = "WHERE blog_id = '" . $this->con->escape($this->id) . "' " .
-        "AND post_id " . $this->con->in($posts_ids);
+        $sql = new dcUpdateStatement($this->core, 'coreUpdPostsStatus');
+        $sql
+            ->where(array(
+                'blog_id = ' . $sql->quote($this->id),
+                'post_id ' . $sql->in($posts_ids)
+            ));
 
         #If user can only publish, we need to check the post's owner
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' ";
+            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID()));
         }
 
@@ -1394,5 +1413,5 @@
         $cur->post_upddt  = date('Y-m-d H:i:s');
 
-        $cur->update($strReq);
+        $cur->update($sql->whereStatement());
         $this->triggerBlog();
 
@@ -1426,10 +1445,14 @@
         $selected  = (boolean) $selected;
 
-        $strReq = "WHERE blog_id = '" . $this->con->escape($this->id) . "' " .
-        "AND post_id " . $this->con->in($posts_ids);
+        $sql = new dcUpdateStatement($this->core, 'coreUpdPostsSelected');
+        $sql
+            ->where(array(
+                'blog_id = ' . $sql->quote($this->id),
+                'post_id ' . $sql->in($posts_ids)
+            ));
 
         # If user is only usage, we need to check the post's owner
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' ";
+            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID()));
         }
 
@@ -1439,5 +1462,5 @@
         $cur->post_upddt    = date('Y-m-d H:i:s');
 
-        $cur->update($strReq);
+        $cur->update($sql->whereStatement());
         $this->triggerBlog();
     }
@@ -1469,10 +1492,14 @@
         $cat_id    = (integer) $cat_id;
 
-        $strReq = "WHERE blog_id = '" . $this->con->escape($this->id) . "' " .
-        "AND post_id " . $this->con->in($posts_ids);
-
-        # If user is only usage, we need to check the post's owner
+        $sql = new dcUpdateStatement($this->core, 'coreUpdPostsCategory');
+        $sql
+            ->where(array(
+                'blog_id = ' . $sql->quote($this->id),
+                'post_id ' . $sql->in($posts_ids)
+            ));
+
+        // If user is only usage, we need to check the post's owner
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' ";
+            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID()));
         }
 
@@ -1482,5 +1509,5 @@
         $cur->post_upddt = date('Y-m-d H:i:s');
 
-        $cur->update($strReq);
+        $cur->update($sql->whereStatement());
         $this->triggerBlog();
     }
@@ -1506,8 +1533,11 @@
         $cur->post_upddt = date('Y-m-d H:i:s');
 
-        $cur->update(
-            'WHERE cat_id = ' . $old_cat_id . ' ' .
-            "AND blog_id = '" . $this->con->escape($this->id) . "' "
-        );
+        $sql = new dcUpdateStatement($this->core, 'coreChangePostsCategory');
+        $sql
+            ->where(array(
+                'cat_id = ' . $old_cat_id,
+                'blog_id = ' . $sql->quote($this->id)
+            ));
+        $cur->update($sql->whereStatement());
         $this->triggerBlog();
     }
@@ -1540,14 +1570,18 @@
         }
 
-        $strReq = 'DELETE FROM ' . $this->prefix . 'post ' .
-        "WHERE blog_id = '" . $this->con->escape($this->id) . "' " .
-        "AND post_id " . $this->con->in($posts_ids);
-
-        #If user can only delete, we need to check the post's owner
+        $sql = new dcDeleteStatement($this->core, 'coreDelPosts');
+        $sql
+            ->from($this->prefix . 'post')
+            ->where(array(
+                'blog_id = ' . $sql->quote($this->id),
+                'post_id ' . $sql->in($posts_ids)
+            ));
+
+        // If user can only delete, we need to check the post's owner
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .= "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' ";
-        }
-
-        $this->con->execute($strReq);
+            $sql->where('user_id = ' . $sql->quote($this->core->auth->userID()));
+        }
+
+        $this->con->execute($sql->statement());
         $this->triggerBlog();
     }
@@ -1558,10 +1592,15 @@
     public function publishScheduledEntries()
     {
-        $strReq = 'SELECT post_id, post_dt, post_tz ' .
-        'FROM ' . $this->prefix . 'post ' .
-        'WHERE post_status = -1 ' .
-        "AND blog_id = '" . $this->con->escape($this->id) . "' ";
-
-        $rs = $this->con->select($strReq);
+        $sql = new dcSelectStatement($this->core, 'coreScheduledEntriesPublish');
+        $sql
+            ->columns(array('post_id', 'post_dt', 'post_tz'))
+            ->from($this->prefix . 'post')
+            ->where(array(
+                'post_status = -1',
+                'blog_id = ' . $sql->quote($this->id)
+            ));
+
+        $rs = $this->con->select($sql->statement());
+        unset($sql);
 
         $now       = dt::toUTC(time());
@@ -1588,10 +1627,14 @@
             $this->core->callBehavior('coreBeforeScheduledEntriesPublish', $this, $to_change);
 
-            $strReq =
-            'UPDATE ' . $this->prefix . 'post SET ' .
-            'post_status = 1 ' .
-            "WHERE blog_id = '" . $this->con->escape($this->id) . "' " .
-            'AND post_id ' . $this->con->in((array) $to_change) . ' ';
-            $this->con->execute($strReq);
+            $sql = new dcUpdateStatement($this->core, 'coreScheduledEntriesPublish');
+            $sql
+                ->reference($this->prefix . 'post')
+                ->set('post_status = 1')
+                ->where(array(
+                    'blog_id = ' . $sql->quote($this->id),
+                    'post_id ' . $sql->in((array) $to_change)
+                ));
+
+            $this->con->execute($sql->statement());
             $this->triggerBlog();
 
@@ -1624,10 +1667,14 @@
         if (count($to_change)) {
 
-            $strReq =
-            'UPDATE ' . $this->prefix . 'post ' .
-            'SET post_firstpub = 1 ' .
-            "WHERE blog_id = '" . $this->con->escape($this->id) . "' " .
-            'AND post_id ' . $this->con->in((array) $to_change) . ' ';
-            $this->con->execute($strReq);
+            $sql = new dcUpdateStatement($this->core, 'coreFirstPublicationEntries');
+            $sql
+                ->reference($this->prefix . 'post')
+                ->set('post_firstpub = 1')
+                ->where(array(
+                    'blog_id = ' . $sql->quote($this->id),
+                    'post_id ' . $sql->in((array) $to_change)
+                ));
+
+            $this->con->execute($sql->statement());
 
             # --BEHAVIOR-- coreFirstPublicationEntries
@@ -1644,17 +1691,20 @@
     public function getPostsUsers($post_type = 'post')
     {
-        $strReq = 'SELECT P.user_id, user_name, user_firstname, ' .
-        'user_displayname, user_email ' .
-        'FROM ' . $this->prefix . 'post P, ' . $this->prefix . 'user U ' .
-        'WHERE P.user_id = U.user_id ' .
-        "AND blog_id = '" . $this->con->escape($this->id) . "' ";
+        $sql = new dcSelectStatement($this->core, 'coreGetPostsUsers');
+        $sql
+            ->columns(array('P.user_id', 'user_name', 'user_firstname', 'user_displayname', 'user_email'))
+            ->from(array($this->prefix . 'post P', $this->prefix . 'user U'))
+            ->where(array(
+                'P.user_id = U.user_id',
+                'blog_id = ' . $sql->quote($this->id)
+            ));
 
         if ($post_type) {
-            $strReq .= "AND post_type = '" . $this->con->escape($post_type) . "' ";
-        }
-
-        $strReq .= 'GROUP BY P.user_id, user_name, user_firstname, user_displayname, user_email ';
-
-        return $this->con->select($strReq);
+            $sql->where("post_type = '" . $sql->escape($post_type) . "'");
+        }
+
+        $sql->group(array('P.user_id', 'user_name', 'user_firstname', 'user_displayname', 'user_email'));
+
+        return $this->con->select($sql->statement());
     }
 
@@ -1687,13 +1737,18 @@
 
         if (!empty($sub)) {
-            $rs = $this->con->select(
-                'SELECT cat_id, cat_url, cat_lft, cat_rgt FROM ' . $this->prefix . 'category ' .
-                "WHERE blog_id = '" . $this->con->escape($this->id) . "' " .
-                'AND ' . $field . ' ' . $this->con->in(array_keys($sub))
-            );
+            $sql = new dcSelectStatement($this->core);
+            $sql
+                ->columns(array('cat_id', 'cat_url', 'cat_lft', 'cat_rgt'))
+                ->from($this->prefix . 'category')
+                ->where(array(
+                    'blog_id = ' . $sql->quote($this->id),
+                    $field . ' ' . $sql->in(array_keys($sub))
+                ));
+            $rs = $this->con->select($sql->statement());
 
             while ($rs->fetch()) {
                 $queries[$rs->f($field)] = '(C.cat_lft BETWEEN ' . $rs->cat_lft . ' AND ' . $rs->cat_rgt . ')';
             }
+            unset($sql);
         }
 
@@ -1881,29 +1936,22 @@
 
         # Let's check if URL is taken...
-        $strReq = 'SELECT post_url FROM ' . $this->prefix . 'post ' .
-        "WHERE post_url = '" . $this->con->escape($url) . "' " .
-        'AND post_id <> ' . (integer) $post_id . ' ' .
-        "AND blog_id = '" . $this->con->escape($this->id) . "' " .
-            'ORDER BY post_url DESC';
-
-        $rs = $this->con->select($strReq);
+        $sql = new dcSelectStatement($this->core, 'coreGetPostURL');
+        $sql
+            ->columns('post_url')
+            ->from($this->prefix . 'post')
+            ->where(array(
+                'post_id <> ' . (integer) $post_id,
+                'blog_id = ' . $sql->quote($this->id)
+            ))
+            ->cond('AND post_url = ' . $sql->quote($url))
+            ->order('post_url DESC');
+
+        $rs = $this->con->select($sql->statement());
 
         if (!$rs->isEmpty()) {
-            if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli' || $this->con->driver() == 'mysqlimb4') {
-                $clause = "REGEXP '^" . $this->con->escape(preg_quote($url)) . "[0-9]+$'";
-            } elseif ($this->con->driver() == 'pgsql') {
-                $clause = "~ '^" . $this->con->escape(preg_quote($url)) . "[0-9]+$'";
-            } else {
-                $clause = "LIKE '" .
-                $this->con->escape(preg_replace(array('%', '_', '!'), array('!%', '!_', '!!'), $url)) .
-                    "%' ESCAPE '!'";
-            }
-            $strReq = 'SELECT post_url FROM ' . $this->prefix . 'post ' .
-            "WHERE post_url " . $clause . ' ' .
-            'AND post_id <> ' . (integer) $post_id . ' ' .
-            "AND blog_id = '" . $this->con->escape($this->id) . "' " .
-                'ORDER BY post_url DESC ';
-
-            $rs = $this->con->select($strReq);
+            // Replace condition on post_url
+            $sql->cond('AND post_url ' . $sql->regexp($url), true);
+
+            $rs = $this->con->select($sql->statement());
             $a  = array();
             while ($rs->fetch()) {
@@ -1963,65 +2011,60 @@
     public function getComments($params = array(), $count_only = false)
     {
+        $sql = new dcSelectStatement($this->core, 'coreGetComments');
+
         if ($count_only) {
-            $strReq = 'SELECT count(comment_id) ';
+            $sql->columns('count(comment_id)');
         } elseif (!empty($params['sql_only'])) {
-            $strReq = 'SELECT P.post_id ';
+            $sql->columns('P.post_id');
         } else {
-            if (!empty($params['no_content'])) {
-                $content_req = '';
-            } else {
-                $content_req = 'comment_content, ';
+            if (empty($params['no_content'])) {
+                $sql->columns('comment_content');
             }
 
             if (!empty($params['columns']) && is_array($params['columns'])) {
-                $content_req .= implode(', ', $params['columns']) . ', ';
-            }
-
-            $strReq =
-                'SELECT C.comment_id, comment_dt, comment_tz, comment_upddt, ' .
-                'comment_author, comment_email, comment_site, ' .
-                $content_req . ' comment_trackback, comment_status, ' .
-                'comment_spam_status, comment_spam_filter, comment_ip, ' .
-                'P.post_title, P.post_url, P.post_id, P.post_password, P.post_type, ' .
-                'P.post_dt, P.user_id, U.user_email, U.user_url ';
-        }
-
-        $strReq .=
-        'FROM ' . $this->prefix . 'comment C ' .
-        'INNER JOIN ' . $this->prefix . 'post P ON C.post_id = P.post_id ' .
-        'INNER JOIN ' . $this->prefix . 'user U ON P.user_id = U.user_id ';
+                $sql->columns($params['columns']);
+            }
+
+            $sql->columns(array('C.comment_id', 'comment_dt', 'comment_tz', 'comment_upddt', 'comment_author', 'comment_email', 'comment_site', 'comment_trackback', 'comment_status', 'comment_spam_status', 'comment_spam_filter', 'comment_ip', 'P.post_title', 'P.post_url', 'P.post_id', 'P.post_password', 'P.post_type', 'P.post_dt', 'P.user_id', 'U.user_email', 'U.user_url'));
+        }
+
+        $sql
+            ->from($this->prefix . 'comment C')
+            ->join(array(
+                'INNER JOIN ' . $this->prefix . 'post P ON C.post_id = P.post_id',
+                'INNER JOIN ' . $this->prefix . 'user U ON P.user_id = U.user_id '
+            ));
 
         if (!empty($params['from'])) {
-            $strReq .= $params['from'] . ' ';
-        }
-
-        $strReq .=
-        "WHERE P.blog_id = '" . $this->con->escape($this->id) . "' ";
+            $sql->from($params['from']);
+        }
+
+        $sql->where('P.blog_id = ' . $sql->quote($this->id));
 
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .= 'AND ((comment_status = 1 AND P.post_status = 1 ';
+            $cond = '((comment_status = 1 AND P.post_status = 1';
 
             if ($this->without_password) {
-                $strReq .= 'AND post_password IS NULL ';
-            }
-            $strReq .= ') ';
+                $cond .= ' AND post_password IS NULL';
+            }
+            $cond .= ')';
 
             if ($this->core->auth->userID()) {
-                $strReq .= "OR P.user_id = '" . $this->con->escape($this->core->auth->userID()) . "')";
-            } else {
-                $strReq .= ') ';
-            }
+                $cond .= ' OR P.user_id = ' . $sql->quote($this->core->auth->userID());
+            }
+            $cond .= ')';
+            $sql->where($cond);
         }
 
         if (!empty($params['post_type'])) {
-            $strReq .= 'AND post_type ' . $this->con->in($params['post_type']);
+            $sql->where('post_type ' . $sql->in($params['post_type']));
         }
 
         if (isset($params['post_id']) && $params['post_id'] !== '') {
-            $strReq .= 'AND P.post_id = ' . (integer) $params['post_id'] . ' ';
+            $sql->where('P.post_id = ' . (integer) $params['post_id']);
         }
 
         if (isset($params['cat_id']) && $params['cat_id'] !== '') {
-            $strReq .= 'AND P.cat_id = ' . (integer) $params['cat_id'] . ' ';
+            $sql->where('P.cat_id = ' . (integer) $params['cat_id']);
         }
 
@@ -2032,37 +2075,37 @@
                 $params['comment_id'] = array((integer) $params['comment_id']);
             }
-            $strReq .= 'AND comment_id ' . $this->con->in($params['comment_id']);
+            $sql->where('comment_id ' . $sql->in($params['comment_id']));
         }
 
         if (isset($params['comment_email'])) {
-            $comment_email = $this->con->escape(str_replace('*', '%', $params['comment_email']));
-            $strReq .= "AND comment_email LIKE '" . $comment_email . "' ";
+            $comment_email = $sql->quote(str_replace('*', '%', $params['comment_email']));
+            $sql->where('comment_email LIKE ' . $comment_email);
         }
 
         if (isset($params['comment_site'])) {
-            $comment_site = $this->con->escape(str_replace('*', '%', $params['comment_site']));
-            $strReq .= "AND comment_site LIKE '" . $comment_site . "' ";
+            $comment_site = $sql->quote(str_replace('*', '%', $params['comment_site']));
+            $sql->where('comment_site LIKE ' . $comment_site);
         }
 
         if (isset($params['comment_status'])) {
-            $strReq .= 'AND comment_status = ' . (integer) $params['comment_status'] . ' ';
+            $sql->where('comment_status = ' . (integer) $params['comment_status']);
         }
 
         if (!empty($params['comment_status_not'])) {
-            $strReq .= 'AND comment_status <> ' . (integer) $params['comment_status_not'] . ' ';
+            $sql->where('comment_status <> ' . (integer) $params['comment_status_not']);
         }
 
         if (isset($params['comment_trackback'])) {
-            $strReq .= 'AND comment_trackback = ' . (integer) (boolean) $params['comment_trackback'] . ' ';
+            $sql->where('comment_trackback = ' . (integer) (boolean) $params['comment_trackback']);
         }
 
         if (isset($params['comment_ip'])) {
-            $comment_ip = $this->con->escape(str_replace('*', '%', $params['comment_ip']));
-            $strReq .= "AND comment_ip LIKE '" . $comment_ip . "' ";
+            $comment_ip = $sql->quote(str_replace('*', '%', $params['comment_ip']));
+            $sql->where('comment_ip LIKE ' . $comment_ip);
         }
 
         if (isset($params['q_author'])) {
-            $q_author = $this->con->escape(str_replace('*', '%', strtolower($params['q_author'])));
-            $strReq .= "AND LOWER(comment_author) LIKE '" . $q_author . "' ";
+            $q_author = $sql->quote(str_replace('*', '%', strtolower($params['q_author'])));
+            $sql->where('LOWER(comment_author) LIKE ' . $q_author);
         }
 
@@ -2078,7 +2121,7 @@
                 if ($words) {
                     foreach ($words as $i => $w) {
-                        $words[$i] = "comment_words LIKE '%" . $this->con->escape($w) . "%'";
+                        $words[$i] = "comment_words LIKE '%" . $sql->escape($w) . "%'";
                     }
-                    $strReq .= 'AND ' . implode(' AND ', $words) . ' ';
+                    $sql->where(implode(' AND ', $words));
                 }
             }
@@ -2086,24 +2129,26 @@
 
         if (!empty($params['sql'])) {
-            $strReq .= $params['sql'] . ' ';
+            $sql->sql($params['sql']);
         }
 
         if (!$count_only) {
             if (!empty($params['order'])) {
-                $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' ';
+                $sql->order($sql->escape($params['order']));
             } else {
-                $strReq .= 'ORDER BY comment_dt DESC ';
+                $sql->order('comment_dt DESC');
             }
         }
 
         if (!$count_only && !empty($params['limit'])) {
-            $strReq .= $this->con->limit($params['limit']);
-        }
+            $sql->limit($params['limit']);
+        }
+
+        $query = $sql->statement();
 
         if (!empty($params['sql_only'])) {
-            return $strReq;
-        }
-
-        $rs       = $this->con->select($strReq);
+            return $query;
+        }
+
+        $rs       = $this->con->select($query);
         $rs->core = $this->core;
         $rs->extend('rsExtComment');
@@ -2128,8 +2173,9 @@
         {
             # Get ID
-            $rs = $this->con->select(
-                'SELECT MAX(comment_id) ' .
-                'FROM ' . $this->prefix . 'comment '
-            );
+            $sql = new dcSelectStatement($this->core, 'coreCommentCreateGetID');
+            $sql
+                ->columns('MAX(comment_id)')
+                ->from($this->prefix . 'comment');
+            $rs = $this->con->select($sql->statement());
 
             $cur->comment_id    = (integer) $rs->f(0) + 1;
@@ -2208,5 +2254,8 @@
         $this->core->callBehavior('coreBeforeCommentUpdate', $this, $cur, $rs);
 
-        $cur->update('WHERE comment_id = ' . $id . ' ');
+        $sql = new dcUpdateStatement($this->core, 'coreCommentUpdate');
+        $sql->where('comment_id = ' . $id);
+
+        $cur->update($sql->whereStatement());
 
         # --BEHAVIOR-- coreAfterCommentUpdate
@@ -2243,18 +2292,23 @@
         $status = (integer) $status;
 
-        $strReq =
-        'UPDATE ' . $this->prefix . 'comment ' .
-            'SET comment_status = ' . $status . ' ';
-        $strReq .=
-        'WHERE comment_id' . $this->con->in($co_ids) .
-        'AND post_id in (SELECT tp.post_id ' .
-        'FROM ' . $this->prefix . 'post tp ' .
-        "WHERE tp.blog_id = '" . $this->con->escape($this->id) . "' ";
+        $sqlIn = new dcSelectStatement($this->core, 'coreUpdCommentsStatusIn');
+        $sqlIn
+            ->columns('tp.post_id')
+            ->from($this->prefix . 'post tp')
+            ->where('tp.blog_id = ' . $sqlIn->quote($this->id));
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .=
-            "AND user_id = '" . $this->con->escape($this->core->auth->userID()) . "' ";
-        }
-        $strReq .= ')';
-        $this->con->execute($strReq);
+            $sqlIn->where('user_id = ' . $sqlIn->quote($this->core->auth->userID()));
+        }
+
+        $sql = new dcUpdateStatement($this->core, 'coreUpdCommentsStatus');
+        $sql
+            ->reference($this->prefix . 'comment')
+            ->set('comment_status = ' . $status)
+            ->where(array(
+                'comment_id ' . $sql->in($co_ids),
+                'post_id in (' . $sqlIn->statement() . ')'
+            ));
+
+        $this->con->execute($sql->statement());
         $this->triggerComments($co_ids);
         $this->triggerBlog();
@@ -2290,11 +2344,14 @@
         # Retrieve posts affected by comments edition
         $affected_posts = array();
-        $strReq         =
-        'SELECT post_id ' .
-        'FROM ' . $this->prefix . 'comment ' .
-        'WHERE comment_id' . $this->con->in($co_ids) .
-            'GROUP BY post_id';
-
-        $rs = $this->con->select($strReq);
+
+        $sql = new dcSelectStatement($this->core, 'coreDelCommentsScope');
+        $sql
+            ->column('post_id')
+            ->from($this->prefix . 'comment')
+            ->where('comment_id' . $sql->in($co_ids))
+            ->group('post_id');
+
+        $rs = $this->con->select($sql->statement());
+        unset($sql);
 
         while ($rs->fetch()) {
@@ -2302,17 +2359,22 @@
         }
 
-        $strReq =
-        'DELETE FROM ' . $this->prefix . 'comment ' .
-        'WHERE comment_id' . $this->con->in($co_ids) . ' ' .
-        'AND post_id in (SELECT tp.post_id ' .
-        'FROM ' . $this->prefix . 'post tp ' .
-        "WHERE tp.blog_id = '" . $this->con->escape($this->id) . "' ";
-        #If user can only delete, we need to check the post's owner
+        $sqlIn = new dcSelectStatement($this->core, 'coreDelCommentsIn');
+        $sqlIn
+            ->columns('tp.post_id')
+            ->from($this->prefix . 'post tp')
+            ->where(array('tp.blog_id = ' . $sqlIn->quote($this->id)));
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .=
-            "AND tp.user_id = '" . $this->con->escape($this->core->auth->userID()) . "' ";
-        }
-        $strReq .= ")";
-        $this->con->execute($strReq);
+            $sqlIn->where('tp.user_id = ' . $sqlIn->quote($this->core->auth->userID()));
+        }
+
+        $sql = new dcDeleteStatement($this->core, 'coreDelComments');
+        $sql
+            ->from($this->prefix . 'comment')
+            ->where(array(
+                'comment_id ' . $sql->in($co_ids),
+                'post_id in (' . $sqlIn->statement() . ')'
+            ));
+
+        $this->con->execute($sql->statement());
         $this->triggerComments($co_ids, true, $affected_posts);
         $this->triggerBlog();
@@ -2325,17 +2387,23 @@
         }
 
-        $strReq =
-        'DELETE FROM ' . $this->prefix . 'comment ' .
-        'WHERE comment_status = -2 ' .
-        'AND post_id in (SELECT tp.post_id ' .
-        'FROM ' . $this->prefix . 'post tp ' .
-        "WHERE tp.blog_id = '" . $this->con->escape($this->id) . "' ";
-        #If user can only delete, we need to check the post's owner
+        $sqlIn = new dcSelectStatement($this->core, 'coreDelJunkCommentsIn');
+        $sqlIn
+            ->columns('tp.post_id')
+            ->from($this->prefix . 'post tp')
+            ->where('tp.blog_id = ' . $sqlIn->quote($this->id));
         if (!$this->core->auth->check('contentadmin', $this->id)) {
-            $strReq .=
-            "AND tp.user_id = '" . $this->con->escape($this->core->auth->userID()) . "' ";
-        }
-        $strReq .= ")";
-        $this->con->execute($strReq);
+            // If user can only delete, we need to check the post's owner
+            $sqlIn->where('tp.user_id = ' . $sqlIn->quote($this->core->auth->userID()));
+        }
+
+        $sql = new dcDeleteStatement($this->core, 'coreDelJunkComments');
+        $sql
+            ->from($this->prefix . 'comment')
+            ->where(array(
+                'comment_status = -2',
+                'post_id in (' . $sqlIn->statement() . ')'
+            ));
+
+        $this->con->execute($sql->statement());
         $this->triggerBlog();
     }
Index: inc/core/class.dc.core.php
===================================================================
--- inc/core/class.dc.core.php	(revision 3731)
+++ inc/core/class.dc.core.php	(revision 3761)
@@ -442,6 +442,10 @@
         # Fetch versions if needed
         if (!is_array($this->versions)) {
-            $strReq = 'SELECT module, version FROM ' . $this->prefix . 'version';
-            $rs     = $this->con->select($strReq);
+            $sql = new dcSelectStatement($this, 'coreGetVersion');
+            $sql
+                ->columns(array('module', 'version'))
+                ->from($this->prefix . 'version');
+
+            $rs     = $this->con->select($sql->statement());
 
             while ($rs->fetch()) {
@@ -474,5 +478,8 @@
             $cur->insert();
         } else {
-            $cur->update("WHERE module='" . $this->con->escape($module) . "'");
+            $sql = new dcUpdateStatement($this, 'coreSetVersion');
+            $sql->where('module=' . $sql->quote($module));
+
+            $cur->update($sql->whereStatement());
         }
 
@@ -487,9 +494,10 @@
     public function delVersion($module)
     {
-        $strReq =
-        'DELETE FROM ' . $this->prefix . 'version ' .
-        "WHERE module = '" . $this->con->escape($module) . "' ";
-
-        $this->con->execute($strReq);
+        $sql = new dcDeleteStatement($this, 'coreDelVersion');
+        $sql
+            ->from($this->prefix . 'version')
+            ->where('module = ' . $sql->quote($module));
+
+        $this->con->execute($sql->statement());
 
         if (is_array($this->versions)) {
@@ -530,37 +538,36 @@
     public function getUsers($params = array(), $count_only = false)
     {
+        $sql = new dcSelectStatement($this, 'coreGetUsers');
+        $sql
+            ->from($this->prefix . 'user U')
+            ->where('NULL IS NULL');
+
         if ($count_only) {
-            $strReq =
-            'SELECT count(U.user_id) ' .
-            'FROM ' . $this->prefix . 'user U ' .
-                'WHERE NULL IS NULL ';
+            $sql->columns('COUNT(U.user_id)');
         } else {
-            $strReq =
-            'SELECT U.user_id,user_super,user_status,user_pwd,user_change_pwd,' .
-            'user_name,user_firstname,user_displayname,user_email,user_url,' .
-            'user_desc, user_lang,user_tz, user_post_status,user_options, ' .
-            'count(P.post_id) AS nb_post ' .
-            'FROM ' . $this->prefix . 'user U ' .
-            'LEFT JOIN ' . $this->prefix . 'post P ON U.user_id = P.user_id ' .
-                'WHERE NULL IS NULL ';
+            $sql
+                ->columns(array('U.user_id', 'user_super', 'user_status', 'user_pwd', 'user_change_pwd', 'user_name',
+                    'user_firstname', 'user_displayname', 'user_email', 'user_url', 'user_desc', 'user_lang', 'user_tz',
+                    'user_post_status', 'user_options', 'COUNT(P.post_id) AS nb_post'))
+                ->join('LEFT JOIN ' . $this->prefix . 'post P ON U.user_id = P.user_id');
         }
 
         if (!empty($params['q'])) {
-            $q = $this->con->escape(str_replace('*', '%', strtolower($params['q'])));
-            $strReq .= 'AND (' .
+            $q = $sql->escape(str_replace('*', '%', strtolower($params['q'])));
+            $sql->where(
+                '(' .
                 "LOWER(U.user_id) LIKE '" . $q . "' " .
                 "OR LOWER(user_name) LIKE '" . $q . "' " .
                 "OR LOWER(user_firstname) LIKE '" . $q . "' " .
-                ') ';
+                ')'
+            );
         }
 
         if (!empty($params['user_id'])) {
-            $strReq .= "AND U.user_id = '" . $this->con->escape($params['user_id']) . "' ";
+            $sql->where('U.user_id = ' . $sql->quote($params['user_id']));
         }
 
         if (!$count_only) {
-            $strReq .= 'GROUP BY U.user_id,user_super,user_status,user_pwd,user_change_pwd,' .
-                'user_name,user_firstname,user_displayname,user_email,user_url,' .
-                'user_desc, user_lang,user_tz,user_post_status,user_options ';
+            $sql->group(array('U.user_id', 'user_super', 'user_status', 'user_pwd', 'user_change_pwd', 'user_name', 'user_firstname', 'user_displayname', 'user_email', 'user_url', 'user_desc', 'user_lang', 'user_tz', 'user_post_status', 'user_options'));
 
             if (!empty($params['order']) && !$count_only) {
@@ -571,17 +578,18 @@
                         $table_prefix = ''; // order = nb_post (asc|desc)
                     }
-                    $strReq .= 'ORDER BY ' . $table_prefix . $this->con->escape($params['order']) . ' ';
+                    $sql->order($table_prefix . $sql->escape($params['order']));
                 } else {
-                    $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' ';
+                    $sql->order($sql->escape($params['order']));
                 }
             } else {
-                $strReq .= 'ORDER BY U.user_id ASC ';
+                $sql->order('U.user_id ASC');
             }
         }
 
         if (!$count_only && !empty($params['limit'])) {
-            $strReq .= $this->con->limit($params['limit']);
-        }
-        $rs = $this->con->select($strReq);
+            $sql->limit($params['limit']);
+        }
+
+        $rs = $this->con->select($sql->statement());
         $rs->extend('rsExtUser');
         return $rs;
@@ -637,5 +645,9 @@
         }
 
-        $cur->update("WHERE user_id = '" . $this->con->escape($id) . "' ");
+        $sql = new dcUpdateStatement($this, 'coreUpdUser');
+        $sql
+            ->where('user_id = ' . $sql->quote($id));
+
+        $cur->update($sql->whereStatement());
 
         $this->auth->afterUpdUser($id, $cur);
@@ -646,8 +658,11 @@
 
         # Updating all user's blogs
-        $rs = $this->con->select(
-            'SELECT DISTINCT(blog_id) FROM ' . $this->prefix . 'post ' .
-            "WHERE user_id = '" . $this->con->escape($id) . "' "
-        );
+        $sql = new dcSelectStatement($this, 'coreUpdUser');
+        $sql
+            ->columns('DISTINCT(blog_id)')
+            ->from($this->prefix . 'post')
+            ->where('user_id = ' . $sql->quote($id));
+
+        $rs = $this->con->select($sql->statement());
 
         while ($rs->fetch()) {
@@ -681,8 +696,10 @@
         }
 
-        $strReq = 'DELETE FROM ' . $this->prefix . 'user ' .
-        "WHERE user_id = '" . $this->con->escape($id) . "' ";
-
-        $this->con->execute($strReq);
+        $sql = new dcDeleteStatement($this, 'coreDelUser');
+        $sql
+            ->from($this->prefix . 'user')
+            ->where('user_id = ' . $sql->quote($id));
+
+        $this->con->execute($sql->statement());
 
         $this->auth->afterDelUser($id);
@@ -697,9 +714,11 @@
     public function userExists($id)
     {
-        $strReq = 'SELECT user_id ' .
-        'FROM ' . $this->prefix . 'user ' .
-        "WHERE user_id = '" . $this->con->escape($id) . "' ";
-
-        $rs = $this->con->select($strReq);
+        $sql = new dcSelectStatement($this, 'coreUserExists');
+        $sql
+            ->columns('user_id')
+            ->from($this->prefix . 'user')
+            ->where('user_id = ' . $sql->quote($id));
+
+        $rs = $this->con->select($sql->statement());
 
         return !$rs->isEmpty();
@@ -721,10 +740,12 @@
     public function getUserPermissions($id)
     {
-        $strReq = 'SELECT B.blog_id, blog_name, blog_url, permissions ' .
-        'FROM ' . $this->prefix . 'permissions P ' .
-        'INNER JOIN ' . $this->prefix . 'blog B ON P.blog_id = B.blog_id ' .
-        "WHERE user_id = '" . $this->con->escape($id) . "' ";
-
-        $rs = $this->con->select($strReq);
+        $sql = new dcSelectStatement($this, 'coreGetUserPermissions');
+        $sql
+            ->columns(array('B.blog_id', 'blog_name', 'blog_url', 'permissions'))
+            ->from($this->prefix . 'permissions P')
+            ->join('INNER JOIN ' . $this->prefix . 'blog B ON P.blog_id = B.blog_id')
+            ->where('user_id = ' . $sql->quote($id));
+
+        $rs = $this->con->select($sql->statement());
 
         $res = array();
@@ -756,8 +777,10 @@
         }
 
-        $strReq = 'DELETE FROM ' . $this->prefix . 'permissions ' .
-        "WHERE user_id = '" . $this->con->escape($id) . "' ";
-
-        $this->con->execute($strReq);
+        $sql = new dcDeleteStatement($this, 'coreSetUserPermissions');
+        $sql
+            ->from($this->prefix . 'permissions')
+            ->where('user_id = ' . $sql->quote($id));
+
+        $this->con->execute($sql->statement());
 
         foreach ($perms as $blog_id => $p) {
@@ -792,9 +815,12 @@
 
         if ($delete_first || $no_perm) {
-            $strReq = 'DELETE FROM ' . $this->prefix . 'permissions ' .
-            "WHERE blog_id = '" . $this->con->escape($blog_id) . "' " .
-            "AND user_id = '" . $this->con->escape($id) . "' ";
-
-            $this->con->execute($strReq);
+            $sql = new dcDeleteStatement($this, 'coreSetUserBlogPermissions');
+            $sql
+                ->from($this->prefix . 'permissions')
+                ->where(array(
+                    'blog_id = ' . $sql->quote($blog_id),
+                    'user_id = ' . $sql->quote($id)
+                ));
+            $this->con->execute($sql->statement());
         }
 
@@ -814,7 +840,10 @@
         $cur = $this->con->openCursor($this->prefix . 'user');
 
+        $sql = new dcUpdateStatement($this, 'coreSetUserDefaultBlog');
+        $sql->where('user_id = ' . $sql->quote($id));
+
         $cur->user_default_blog = (string) $blog_id;
 
-        $cur->update("WHERE user_id = '" . $this->con->escape($id) . "'");
+        $cur->update($sql->whereStatement());
     }
 
@@ -890,21 +919,23 @@
     public function getBlogPermissions($id, $with_super = true)
     {
-        $strReq =
-        'SELECT U.user_id AS user_id, user_super, user_name, user_firstname, ' .
-        'user_displayname, user_email, permissions ' .
-        'FROM ' . $this->prefix . 'user U ' .
-        'JOIN ' . $this->prefix . 'permissions P ON U.user_id = P.user_id ' .
-        "WHERE blog_id = '" . $this->con->escape($id) . "' ";
+        $sql = new dcSelectStatement($this, 'coreGetBlogPermissions');
+        $sql
+            ->columns(array('U.user_id AS user_id', 'user_super', 'user_name', 'user_firstname', 'user_displayname',
+                'user_email', 'permissions'))
+            ->from($this->prefix . 'user U')
+            ->join('JOIN ' . $this->prefix . 'permissions P ON U.user_id = P.user_id')
+            ->where('blog_id = ' . $sql->quote($id));
 
         if ($with_super) {
-            $strReq .=
-            'UNION ' .
-            'SELECT U.user_id AS user_id, user_super, user_name, user_firstname, ' .
-            "user_displayname, user_email, NULL AS permissions " .
-            'FROM ' . $this->prefix . 'user U ' .
-                'WHERE user_super = 1 ';
-        }
-
-        $rs = $this->con->select($strReq);
+            $sqlSuper = new dcSelectStatement($this, 'coreGetBlogPermissionsSuper');
+            $sqlSuper
+                ->columns(array('U.user_id AS user_id', 'user_super', 'user_name', 'user_firstname', 'user_displayname',
+                    'user_email', 'NULL AS permissions'))
+                ->from($this->prefix . 'user U')
+                ->where('user_super = 1');
+            $sql->sql('UNION ' . $sqlSuper->statement());
+        }
+
+        $rs = $this->con->select($sql->statement());
 
         $res = array();
@@ -955,45 +986,34 @@
     public function getBlogs($params = array(), $count_only = false)
     {
-        $join  = ''; // %1$s
-        $where = ''; // %2$s
+        $sql = new dcSelectStatement($this, 'coreGetBlogs');
+        $sql->from($this->prefix . 'blog B');
 
         if ($count_only) {
-            $strReq = 'SELECT count(B.blog_id) ' .
-            'FROM ' . $this->prefix . 'blog B ' .
-                '%1$s ' .
-                'WHERE NULL IS NULL ' .
-                '%2$s ';
+            $sql->columns('COUNT(B.blog_id)');
         } else {
-            $strReq =
-            'SELECT B.blog_id, blog_uid, blog_url, blog_name, blog_desc, blog_creadt, ' .
-            'blog_upddt, blog_status ' .
-            'FROM ' . $this->prefix . 'blog B ' .
-                '%1$s ' .
-                'WHERE NULL IS NULL ' .
-                '%2$s ';
+            $sql->columns(array('B.blog_id', 'blog_uid', 'blog_url', 'blog_name', 'blog_desc', 'blog_creadt', 'blog_upddt', 'blog_status'));
 
             if (!empty($params['order'])) {
-                $strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' ';
+                $sql->order($sql->escape($params['order']));
             } else {
-                $strReq .= 'ORDER BY B.blog_id ASC ';
+                $sql->order('B.blog_id ASC');
             }
 
             if (!empty($params['limit'])) {
-                $strReq .= $this->con->limit($params['limit']);
+                $sql->limit($params['limit']);
             }
         }
 
         if ($this->auth->userID() && !$this->auth->isSuperAdmin()) {
-            $join  = 'INNER JOIN ' . $this->prefix . 'permissions PE ON B.blog_id = PE.blog_id ';
-            $where =
-            "AND PE.user_id = '" . $this->con->escape($this->auth->userID()) . "' " .
-                "AND (permissions LIKE '%|usage|%' OR permissions LIKE '%|admin|%' OR permissions LIKE '%|contentadmin|%') " .
-                "AND blog_status IN (1,0) ";
+            $sql->join('INNER JOIN ' . $this->prefix . 'permissions PE ON B.blog_id = PE.blog_id');
+            $sql->where("AND PE.user_id = " . $sql->quote($this->auth->userID()) .
+                " AND (permissions LIKE '%|usage|%' OR permissions LIKE '%|admin|%' OR permissions LIKE '%|contentadmin|%')" .
+                " AND blog_status IN (1,0)");
         } elseif (!$this->auth->userID()) {
-            $where = 'AND blog_status IN (1,0) ';
+            $sql->where('blog_status IN (1,0)');
         }
 
         if (isset($params['blog_status']) && $params['blog_status'] !== '' && $this->auth->isSuperAdmin()) {
-            $where .= 'AND blog_status = ' . (integer) $params['blog_status'] . ' ';
+            $sql->where('blog_status = ' . (integer) $params['blog_status']);
         }
 
@@ -1002,19 +1022,17 @@
                 $params['blog_id'] = array($params['blog_id']);
             }
-            $where .= 'AND B.blog_id ' . $this->con->in($params['blog_id']);
+            $sql->where('B.blog_id ' . $sql->in($params['blog_id']));
         }
 
         if (!empty($params['q'])) {
             $params['q'] = strtolower(str_replace('*', '%', $params['q']));
-            $where .=
-            'AND (' .
-            "LOWER(B.blog_id) LIKE '" . $this->con->escape($params['q']) . "' " .
-            "OR LOWER(B.blog_name) LIKE '" . $this->con->escape($params['q']) . "' " .
-            "OR LOWER(B.blog_url) LIKE '" . $this->con->escape($params['q']) . "' " .
-                ') ';
-        }
-
-        $strReq = sprintf($strReq, $join, $where);
-        return $this->con->select($strReq);
+            $sql->where('(' .
+                "LOWER(B.blog_id) LIKE " . $sql->quote($params['q']) .
+                " OR LOWER(B.blog_name) LIKE " . $sql->quote($params['q']) .
+                " OR LOWER(B.blog_url) LIKE " . $sql->quote($params['q']) .
+                ')');
+        }
+
+        return $this->con->select($sql->statement());
     }
 
@@ -1051,5 +1069,7 @@
         $cur->blog_upddt = date('Y-m-d H:i:s');
 
-        $cur->update("WHERE blog_id = '" . $this->con->escape($id) . "'");
+        $sql = new dcUpdateStatement($this, 'coreUpdBlog');
+        $sql->where('blog_id = ' . $sql->quote($id));
+        $cur->update($sql->whereStatement());
     }
 
@@ -1090,8 +1110,10 @@
         }
 
-        $strReq = 'DELETE FROM ' . $this->prefix . 'blog ' .
-        "WHERE blog_id = '" . $this->con->escape($id) . "' ";
-
-        $this->con->execute($strReq);
+        $sql = new dcDeleteStatement($this, 'coreDelBlog');
+        $sql
+            ->from($this->prefix . 'blog')
+            ->where('blog_id = ' . $sql->quote($id));
+
+        $this->con->execute($sql->statement());
     }
 
@@ -1104,9 +1126,11 @@
     public function blogExists($id)
     {
-        $strReq = 'SELECT blog_id ' .
-        'FROM ' . $this->prefix . 'blog ' .
-        "WHERE blog_id = '" . $this->con->escape($id) . "' ";
-
-        $rs = $this->con->select($strReq);
+        $sql = new dcSelectStatement($this, 'coreBlogExists');
+        $sql
+            ->columns('blog_id')
+            ->from($this->prefix . 'blog')
+            ->where('blog_id = ' . $sql->quote($id));
+
+        $rs = $this->con->select($sql->statement());
 
         return !$rs->isEmpty();
@@ -1122,13 +1146,15 @@
     public function countBlogPosts($id, $type = null)
     {
-        $strReq = 'SELECT COUNT(post_id) ' .
-        'FROM ' . $this->prefix . 'post ' .
-        "WHERE blog_id = '" . $this->con->escape($id) . "' ";
+        $sql = new dcSelectStatement($this, 'coreCountBlogPosts');
+        $sql
+            ->columns('COUNT(post_id)')
+            ->from($this->prefix . 'post')
+            ->where('blog_id = ' . $sql->quote($id));
 
         if ($type) {
-            $strReq .= "AND post_type = '" . $this->con->escape($type) . "' ";
-        }
-
-        return $this->con->select($strReq)->f(0);
+            $sql->where('post_type = ' . $sql->quote($type));
+        }
+
+        return $this->con->select($sql->statement())->f(0);
     }
     //@}
@@ -1468,20 +1494,28 @@
     public function indexAllPosts($start = null, $limit = null)
     {
-        $strReq = 'SELECT COUNT(post_id) ' .
-        'FROM ' . $this->prefix . 'post';
-        $rs    = $this->con->select($strReq);
+        $sql = new dcSelectStatement($this, 'coreIndexAllPostsCount');
+        $sql
+            ->columns('COUNT(post_id)')
+            ->from($this->prefix . 'post');
+
+        $rs    = $this->con->select($sql->statement());
         $count = $rs->f(0);
-
-        $strReq = 'SELECT post_id, post_title, post_excerpt_xhtml, post_content_xhtml ' .
-        'FROM ' . $this->prefix . 'post ';
+        unset($sql);
+
+        $sql = new dcSelectStatement($this, 'coreIndexAllPosts');
+        $sql
+            ->columns(array('post_id', 'post_title', 'post_excerpt_xhtml', 'post_content_xhtml'))
+            ->from($this->prefix . 'post');
 
         if ($start !== null && $limit !== null) {
-            $strReq .= $this->con->limit($start, $limit);
-        }
-
-        $rs = $this->con->select($strReq, true);
+            $sql->limit($start, $limit);
+        }
+
+        $rs = $this->con->select($sql->statement());
+        unset($sql);
 
         $cur = $this->con->openCursor($this->prefix . 'post');
 
+        $sql = new dcUpdateStatement($this, 'coreIndexAllPosts');
         while ($rs->fetch()) {
             $words = $rs->post_title . ' ' . $rs->post_excerpt_xhtml . ' ' .
@@ -1489,5 +1523,6 @@
 
             $cur->post_words = implode(' ', text::splitWords($words));
-            $cur->update('WHERE post_id = ' . (integer) $rs->post_id);
+            $sql->where('post_id = ' . (integer) $rs->post_id, true);
+            $cur->update($sql->whereStatement());
             $cur->clean();
         }
@@ -1509,23 +1544,32 @@
     public function indexAllComments($start = null, $limit = null)
     {
-        $strReq = 'SELECT COUNT(comment_id) ' .
-        'FROM ' . $this->prefix . 'comment';
-        $rs    = $this->con->select($strReq);
+        $sql = new dcSelectStatement($this, 'coreIndexAllCommentsCount');
+        $sql
+            ->columns('COUNT(comment_id)')
+            ->from($this->prefix . 'comment');
+
+        $rs    = $this->con->select($sql->statement());
         $count = $rs->f(0);
-
-        $strReq = 'SELECT comment_id, comment_content ' .
-        'FROM ' . $this->prefix . 'comment ';
+        unset($sql);
+
+        $sql = new dcSelectStatement($this, 'coreIndexAllComments');
+        $sql
+            ->columns(array('comment_id', 'comment_content'))
+            ->from($this->prefix . 'comment');
 
         if ($start !== null && $limit !== null) {
-            $strReq .= $this->con->limit($start, $limit);
-        }
-
-        $rs = $this->con->select($strReq);
+            $sql->limit($start, $limit);
+        }
+
+        $rs = $this->con->select($sql->statement());
+        unset($sql);
 
         $cur = $this->con->openCursor($this->prefix . 'comment');
 
+        $sql = new dcUpdateStatement($this, 'coreIndexAllComments');
         while ($rs->fetch()) {
             $cur->comment_words = implode(' ', text::splitWords($rs->comment_content));
-            $cur->update('WHERE comment_id = ' . (integer) $rs->comment_id);
+            $sql->where('comment_id = ' . (integer) $rs->comment_id, true);
+            $cur->update($sql->whereStatement());
             $cur->clean();
         }
@@ -1542,19 +1586,24 @@
     public function countAllComments()
     {
-
-        $updCommentReq = 'UPDATE ' . $this->prefix . 'post P ' .
-        'SET nb_comment = (' .
-        'SELECT COUNT(C.comment_id) from ' . $this->prefix . 'comment C ' .
-            'WHERE C.post_id = P.post_id AND C.comment_trackback <> 1 ' .
-            'AND C.comment_status = 1 ' .
-            ')';
-        $updTrackbackReq = 'UPDATE ' . $this->prefix . 'post P ' .
-        'SET nb_trackback = (' .
-        'SELECT COUNT(C.comment_id) from ' . $this->prefix . 'comment C ' .
-            'WHERE C.post_id = P.post_id AND C.comment_trackback = 1 ' .
-            'AND C.comment_status = 1 ' .
-            ')';
-        $this->con->execute($updCommentReq);
-        $this->con->execute($updTrackbackReq);
+        $sqlCount = new dcSelectStatement($this, 'coreCountAllComments');
+        $sqlCount
+            ->columns('COUNT(C.comment_id)')
+            ->from($this->prefix . 'comment C')
+            ->where(array(
+                'C.post_id = P.post_id',
+                'C.comment_status = 1'
+            ));
+
+        $sql = new dcUpdateStatement($this, 'coreCountAllComments');
+        $sql
+            ->ref($this->prefix . 'post P');
+
+        $sqlCount->cond('AND C.comment_trackback <> 1', true);
+        $sql->set('nb_comment = (' . $sqlCount->statement() . ')', true);
+        $this->con->execute($sql->statement());
+
+        $sqlCount->cond('AND C.comment_trackback = 1', true);
+        $sql->set('nb_trackback = (' . $sqlCount->statement() . ')', true);
+        $this->con->execute($sql->statement());
     }
 
Index: inc/core/class.dc.rs.extensions.php
===================================================================
--- inc/core/class.dc.rs.extensions.php	(revision 3731)
+++ inc/core/class.dc.rs.extensions.php	(revision 3761)
@@ -444,13 +444,13 @@
             return $rs->_nb_media[$rs->index()];
         } else {
-            $strReq =
-            'SELECT count(media_id) ' .
-            'FROM ' . $rs->core->prefix . 'post_media ' .
-            'WHERE post_id = ' . (integer) $rs->post_id . ' ';
+            $sql = new dcSelectStatement($rs->core, 'coreRsExtCountMedia');
+            $sql
+                ->columns(array('count(media_id)'))
+                ->from($rs->core->prefix . 'post_media')
+                ->where('post_id = ' . $sql->quote((integer) $rs->post_id));
             if ($link_type != null) {
-                $strReq .= "AND link_type = '" . $rs->core->con->escape($link_type) . "'";
+                $sql->where('link_type = ' . $sql->quote($link_type));
             }
-
-            $res                         = (integer) $rs->core->con->select($strReq)->f(0);
+            $res                         = (integer) $rs->core->con->select($sql->statement())->f(0);
             $rs->_nb_media[$rs->index()] = $res;
             return $res;
Index: inc/core/class.dc.sql.statement.php
===================================================================
--- inc/core/class.dc.sql.statement.php	(revision 3754)
+++ inc/core/class.dc.sql.statement.php	(revision 3761)
@@ -300,6 +300,6 @@
      * May be used for debugging purpose as:
      *
-     * if (!$sql->isSame($sql->statement(), $oldRequest)) {
-     *     trigger_error('SQL statement error: ' . $sql->statement() . ' / ' . $oldRequest, E_USER_ERROR);
+     * if (!$sql->isSame($sql->statement(), $strReq)) {
+     *     trigger_error('SQL statement error: ' . $sql->statement() . ' / ' . $strReq, E_USER_ERROR);
      * }
      *
