<commit_before><?php
/**
 * $Id$
 *
 * Utility class for writing prepared SQL statements
 * It always grabs current SqlHandler::getInstance(),
 * so you can write code like:
 *
 *   $id = Sql:pInsert($q, 'i', $val);
 *
 * @author <NAME>, 2011 <<EMAIL>>
 */

//STATUS: experimental. most code copied from DatabaseMysql

class Sql
{
    /** Executes a prepared statement and binds parameters
     * @param @arg[0] sql query
     * @param @arg[1-n] params
     */
    protected static function pExecStmt($args)
    {
        if (!$args[0])
            throw new Exception ('no query');

        $db = SqlHandler::getInstance();
        $db->connect();

        if ($db instanceof DatabaseMysqlProfiler)
            $db->startMeasure();

        if (! ($stmt = $db->db_handle->prepare($args[0])) ) {
            bt();
            throw new Exception ('FAIL prepare: '.$args[0]);
        }

        $params = array();
        for ($i = 1; $i < count($args); $i++)
            $params[] = $args[$i];

        if ($params)
            $res = call_user_func_array(array($stmt, 'bind_param'), self::refValues($params));

        $stmt->execute();

        $prof = &$db->measureQuery($args[0]);
        $prof->prepared = true;

        if (isset($args[1]))
            $prof->format = $args[1];

        if (isset($args[2])) {
            $params = array();
            for ($i = 2; $i < count($args); $i++)
                $prof->params[] = $args[$i];
        }

        if ($res === false)
            $prof->error = $this->db_handle->error;

        return $stmt;
    }

    /**
     * Prepared select
     *
     * @param $args[0] query
     * @param $args[1] prepare format (isdb), integer, string, double/float, binary
     * @param $args[2,3,..] variables
     *
     * STATUS: in development
     * SEE http://devzone.zend.com/article/686 for bind prepare statements
     */
    public static function pSelect()
    {
        $stmt = self::pExecStmt( func_get_args() );

        $data = array();

        $meta = $stmt->result_metadata();

        while ($field = $meta->fetch_field())<commit_msg>Sql::escape() added<commit_after><?php
/**
 * $Id$
 *
 * Utility class for writing prepared SQL statements
 * It always grabs current SqlHandler::getInstance(),
 * so you can write code like:
 *
 *   $id = Sql:pInsert($q, 'i', $val);
 *
 * @author <NAME>, 2011 <<EMAIL>>
 */

//STATUS: experimental. most code copied from DatabaseMysql

class Sql
{
    /** Executes a prepared statement and binds parameters
     * @param @arg[0] sql query
     * @param @arg[1-n] params
     */
    protected static function pExecStmt($args)
    {
        if (!$args[0])
            throw new Exception ('no query');

        $db = SqlHandler::getInstance();
        $db->connect();

        if ($db instanceof DatabaseMysqlProfiler)
            $db->startMeasure();

        if (! ($stmt = $db->db_handle->prepare($args[0])) ) {
            bt();
            throw new Exception ('FAIL prepare: '.$args[0]);
        }

        $params = array();
        for ($i = 1; $i < count($args); $i++)
            $params[] = $args[$i];

        if ($params)
            $res = call_user_func_array(array($stmt, 'bind_param'), self::refValues($params));

        $stmt->execute();

        $prof = &$db->measureQuery($args[0]);
        $prof->prepared = true;

        if (isset($args[1]))
            $prof->format = $args[1];

        if (isset($args[2])) {
            $params = array();
            for ($i = 2; $i < count($args); $i++)
                $prof->params[] = $args[$i];
        }

        if ($res === false)
            $prof->error = $this->db_handle->error;

        return $stmt;
    }

    /**
     * Escapes a string for use in queries
     *
     * @param $q is the query to escape
     * @return the escaped string, taking db-connection locale into account
     */
    public static function escape($q)
    {
        //db handle is needed to use the escape function... XXX, REALLY?, WHY???
        $db = SqlHandler::getInstance();
        $db->connect();

        return $db->db_handle->real_escape_string($q);
    }

    /**
     * Prepared select
     *
     * @param $args[0] query
     * @param $args[1] prepare format (isdb), integer, string, double/float, binary
     * @param $args[2,3,..] variables
     *
     * STATUS: in development
     * SEE http://devzone.zend.com/article/686 for bind prepare statements
     */
    public static function pSelect()
    {
        $stmt = self::pExecStmt( func_get_args() );

        $data = array();

        $meta = $stmt->result_metadata();

        while ($field = $meta->fetch_field())
{'fraction_non_alphanumeric': 0.16713452817965882, 'fraction_numerical': 0.009501187648456057, 'mean_word_length': 2.106639839034205, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3682662', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1382, 'n_words': 372}