<commit_before><?php
/**
* $Id: Sqlite.php,v 1.7 2006-06-29 20:52:47 matteo Exp $
*
* db_sqlite
*
* The db_sqlite class provides methods and functions for a sqlite database.
*
* @author       <NAME> <<EMAIL>>
* @author       <NAME> <<EMAIL>>
* @package      db_sqlite
* @since        2005-06-27
*
* Copyright:    (c) 2006 phpMyFAQ Team
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*/

class db_sqlite
{
    /**
     * The connection object
     *
     * @var   mixed
     * @see   connect(), query(), dbclose()
     */
    private $conn = false;

    /**
     * The query log string
     *
     * @var   string
     * @see   query()
     */
    private $sqllog = '';

    /**
     * Constructor
     *
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function __construct()
    {
    }

    /**
     * Connects to the database.
     *
     * This function connects to a sqlite database
     *
     * @param   string
     * @return  boolean
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function connect($host, $user = false, $passwd = false, $db = false)
    {
        $this->conn = sqlite_open($host, 0666);
        if (!$this->conn) {
            print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
            print "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";
            print "<head>\n";
            print "    <title>phpMyFAQ Error</title>\n";
            print "    <meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=utf-8\" />\n";
            print "</head>\n";
            print "<body>\n";
            print "<p align=\"center\">The connection to the sqlite server could not be established.</p>\n";
            print "<p align=\"center\">The error message of the sqlite server:<br />".sqlite_error_string(sqlite_last_error($this->conn))."</p>\n";
            print "</body>\n";
            print "</html>";
            return false;
        }
        return true;
    }

    /**
     * Sends a query to the database.
     *
     * This function sends a query to the database.
     *
     * @param   string $query
     * @return  mixed $result
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function query($query)
    {
        $this->sqllog .= pmf_debug($query);
        return sqlite_query($this->conn, $query);
    }

    /**
    * Escapes a string for use in a query
    *
    * @param   string
    * @return  string
    * @access  public
    * @author  <NAME> <<EMAIL>>
    * @since   2004-12-16
    */
    public function escape_string($string)
    {
      return sqlite_escape_string($string);
    }

    /**
     * Fetch a result row as an object
     *
     * This function fetches a result row as an object.
     *
     * @param   mixed $result
     * @return  mixed
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function fetch_object($result)
    {
        return sqlite_fetch_object($result);
    }



    /**
     * Fetch a result row as an object
     *
     * This function fetches a result as an associative array.
     *
     * @param   mixed $result
     * @return  array
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function fetch_assoc($result)
    {
        return sqlite_fetch_array($result, SQLITE_ASSOC);
    }



    /**
     * Number of rows in a result
     *
     * @param   mixed $result
     * @return  integer
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function num_rows($result)
    {
        return sqlite_num_rows($result);
    }

    /**
     * Returns the ID of the latest insert
     *
     * @return  integer
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function insert_id($table, $field)
    {
        return sqlite_last_insert_rowid($this->conn);
    }

    /**
     * Logs the queries
     *
     * @param   mixed $result
     * @return  integer
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function sqllog()
    {
        return $this->sqllog;
    }



    /**
     * Generates a result based on search a search string.
     *
     * This function generates a result set based on a search string.
     *
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function search($table, $assoc, $joinedTable = '', $joinAssoc = array(), $match = array(), $string = '', $cond = array(), $orderBy = array())
    {
        $string = trim($string);
        $fields = '';
        $join = '';
        $joined = '';
        $where = '';

        foreach ($assoc as $field) {
            if (empty($fields)) {
                $fields = $field;
            } else {
                $fields .= ", ".$field;
            }
        }

        if (isset($joinedTable) && $joinedTable != '') {
            $joined .= ' LEFT JOIN '.$joinedTable.' ON ';
        }

        if (is_array($joinAssoc)) {
            foreach ($joinAssoc as $joinedFields) {
                $join .= $joinedFields.' AND ';
                }
            $joined .= substr($join, 0, -4);
        }

        $keys = preg_split("/\s+/", $string);
        $numKeys = count($keys);
        $numMatch = count($match);

        for ($i = 0; $i < $numKeys; $i++) {
            if (strlen($where) != 0 ) {
                $where = $where." OR";
            }
            $where = $where." (";
            for ($j = 0; $j < $numMatch; $j++) {
                if ($j != 0) {
                    $where = $where." OR ";
                }
                $where = $where.$match[$j]." LIKE '%".$keys[$i]."%'";
            }

            $where .= ")";
        }

        foreach ($cond as $field => $data) {
            if (empty($where)) {
                $where .= $field." = ".$data;
            } else {
                $where .= " AND ".$field." = ".$data;
            }
        }

        $query = "SELECT ".$fields." FROM ".$table.$joined." WHERE";

        if (!empty($where)) {
            $query .= " (".$where.")";
        }

        if (is_numeric($string)) {
            $query = "SELECT ".$fields." FROM ".$table.$joined." WHERE ".$match." = ".$string;
        }

        $firstOrderBy = true;
        foreach ($orderBy as $field) {
            if ($firstOrderBy) {
                $query .= " ORDER BY ".$field;
                $firstOrderBy = false;
            } else {
                $query .= ", ".$field;
            }
        }

        return $this->query($query);
    }

    /**
    * Returns the error string.
    *
    * This function returns the table status.
    *
    * @access  public
    * @return  array
    * @author  <NAME> <<EMAIL>>
    * @since   2005-06-27
    */
    public function getTableStatus()
    {
        $arr = array();
        $result = $this->query("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
        while ($row = $this->fetch_assoc($result)) {
            $num_result = $this->query('SELECT * FROM '.$row['name']);
            $arr[$row['name']] = $this->num_rows($num_result);
        }
        return $arr;
    }

    /**
    * Returns the next ID of a table
    *
    * This function is a replacement for sqlite's auto-increment so that
    * we don't need it anymore.
    *
    * @param   string      the name of the table
    * @param   string      the name of the ID column
    * @return  int
    * @access  public
    * @author  <NAME> <<EMAIL>>
    * @since   2005-06-27
    */
    public function nextID($table, $id)
    {
        $result = $this->query('SELECT max('.$id.') AS current_id FROM '.$table);
        $currentID = intval(sqlite_fetch_single($result));
        return ($currentID + 1);
    }

    /**
    * Returns the error string.
    *
    * This function returns the last error string.
    *
    * @access  public
    * @author  <NAME> <<EMAIL>>
    * @since   2005-06-27
    */
    public function error()
    {
        return sqlite_error_string(sqlite_last_error($this->conn));
    }

    /**
    * Returns the libary version string.
    *
    * This function returns the version string.
    *
    * @access  public
    * @author  <NAME> <<EMAIL>><commit_msg>Funny Bugfix: SQLite returns 0 on succesful queries...<commit_after><?php
/**
* $Id: Sqlite.php,v 1.8 2006-08-19 10:49:14 thorstenr Exp $
*
* db_sqlite
*
* The db_sqlite class provides methods and functions for a sqlite database.
*
* @author       <NAME> <<EMAIL>>
* @author       <NAME> <<EMAIL>>
* @package      db_sqlite
* @since        2005-06-27
*
* Copyright:    (c) 2006 phpMyFAQ Team
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*/

class db_sqlite
{
    /**
     * The connection object
     *
     * @var   mixed
     * @see   connect(), query(), dbclose()
     */
    private $conn = false;

    /**
     * The query log string
     *
     * @var   string
     * @see   query()
     */
    private $sqllog = '';

    /**
     * Constructor
     *
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function __construct()
    {
    }

    /**
     * Connects to the database.
     *
     * This function connects to a sqlite database
     *
     * @param   string
     * @return  boolean
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function connect($host, $user = false, $passwd = false, $db = false)
    {
        $this->conn = sqlite_open($host, 0666);
        if (!$this->conn) {
            print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
            print "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n";
            print "<head>\n";
            print "    <title>phpMyFAQ Error</title>\n";
            print "    <meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=utf-8\" />\n";
            print "</head>\n";
            print "<body>\n";
            print "<p align=\"center\">The connection to the sqlite server could not be established.</p>\n";
            print "<p align=\"center\">The error message of the sqlite server:<br />".sqlite_error_string(sqlite_last_error($this->conn))."</p>\n";
            print "</body>\n";
            print "</html>";
            return false;
        }
        return true;
    }

    /**
     * Sends a query to the database.
     *
     * This function sends a query to the database.
     *
     * @param   string $query
     * @return  mixed $result
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function query($query)
    {
        $this->sqllog .= pmf_debug($query);
        return sqlite_query($this->conn, $query);
    }

    /**
    * Escapes a string for use in a query
    *
    * @param   string
    * @return  string
    * @access  public
    * @author  <NAME> <<EMAIL>>
    * @since   2004-12-16
    */
    public function escape_string($string)
    {
      return sqlite_escape_string($string);
    }

    /**
     * Fetch a result row as an object
     *
     * This function fetches a result row as an object.
     *
     * @param   mixed $result
     * @return  mixed
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function fetch_object($result)
    {
        return sqlite_fetch_object($result);
    }



    /**
     * Fetch a result row as an object
     *
     * This function fetches a result as an associative array.
     *
     * @param   mixed $result
     * @return  array
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function fetch_assoc($result)
    {
        return sqlite_fetch_array($result, SQLITE_ASSOC);
    }



    /**
     * Number of rows in a result
     *
     * @param   mixed $result
     * @return  integer
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function num_rows($result)
    {
        return sqlite_num_rows($result);
    }

    /**
     * Returns the ID of the latest insert
     *
     * @return  integer
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function insert_id($table, $field)
    {
        return sqlite_last_insert_rowid($this->conn);
    }

    /**
     * Logs the queries
     *
     * @param   mixed $result
     * @return  integer
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function sqllog()
    {
        return $this->sqllog;
    }



    /**
     * Generates a result based on search a search string.
     *
     * This function generates a result set based on a search string.
     *
     * @access  public
     * @author  <NAME> <<EMAIL>>
     * @author  <NAME> <<EMAIL>>
     * @since   2005-06-27
     */
    public function search($table, $assoc, $joinedTable = '', $joinAssoc = array(), $match = array(), $string = '', $cond = array(), $orderBy = array())
    {
        $string = trim($string);
        $fields = '';
        $join = '';
        $joined = '';
        $where = '';

        foreach ($assoc as $field) {
            if (empty($fields)) {
                $fields = $field;
            } else {
                $fields .= ", ".$field;
            }
        }

        if (isset($joinedTable) && $joinedTable != '') {
            $joined .= ' LEFT JOIN '.$joinedTable.' ON ';
        }

        if (is_array($joinAssoc)) {
            foreach ($joinAssoc as $joinedFields) {
                $join .= $joinedFields.' AND ';
                }
            $joined .= substr($join, 0, -4);
        }

        $keys = preg_split("/\s+/", $string);
        $numKeys = count($keys);
        $numMatch = count($match);

        for ($i = 0; $i < $numKeys; $i++) {
            if (strlen($where) != 0 ) {
                $where = $where." OR";
            }
            $where = $where." (";
            for ($j = 0; $j < $numMatch; $j++) {
                if ($j != 0) {
                    $where = $where." OR ";
                }
                $where = $where.$match[$j]." LIKE '%".$keys[$i]."%'";
            }

            $where .= ")";
        }

        foreach ($cond as $field => $data) {
            if (empty($where)) {
                $where .= $field." = ".$data;
            } else {
                $where .= " AND ".$field." = ".$data;
            }
        }

        $query = "SELECT ".$fields." FROM ".$table.$joined." WHERE";

        if (!empty($where)) {
            $query .= " (".$where.")";
        }

        if (is_numeric($string)) {
            $query = "SELECT ".$fields." FROM ".$table.$joined." WHERE ".$match." = ".$string;
        }

        $firstOrderBy = true;
        foreach ($orderBy as $field) {
            if ($firstOrderBy) {
                $query .= " ORDER BY ".$field;
                $firstOrderBy = false;
            } else {
                $query .= ", ".$field;
            }
        }

        return $this->query($query);
    }

    /**
    * Returns the error string.
    *
    * This function returns the table status.
    *
    * @access  public
    * @return  array
    * @author  <NAME> <<EMAIL>>
    * @since   2005-06-27
    */
    public function getTableStatus()
    {
        $arr = array();
        $result = $this->query("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name");
        while ($row = $this->fetch_assoc($result)) {
            $num_result = $this->query('SELECT * FROM '.$row['name']);
            $arr[$row['name']] = $this->num_rows($num_result);
        }
        return $arr;
    }

    /**
    * Returns the next ID of a table
    *
    * This function is a replacement for sqlite's auto-increment so that
    * we don't need it anymore.
    *
    * @param   string      the name of the table
    * @param   string      the name of the ID column
    * @return  int
    * @access  public
    * @author  <NAME> <<EMAIL>>
    * @since   2005-06-27
    */
    public function nextID($table, $id)
    {
        $result = $this->query('SELECT max('.$id.') AS current_id FROM '.$table);
        $currentID = intval(sqlite_fetch_single($result));
        return ($currentID + 1);
    }

    /**
    * Returns the error string.
    *
    * This function returns the last error string.
    *
    * @access  public
    * @author  <NAME> <<EMAIL>>
    * @since   2005-06-27
    */
    public function error()
    {
        if (0 == sqlite_last_error($this->conn)) {
            return;
        }
        return sqlite_error_string(sqlite_last_error($this->conn));
    }

    /**
    * Returns the libary version string.
    *
    * This function returns the version string.
    *
    * @access  public
    * @author  <NAME> <<EMAIL>>
{'fraction_non_alphanumeric': 0.14886659646400266, 'fraction_numerical': 0.017513717231059135, 'mean_word_length': 1.6983699715866607, 'pattern_counts': {'":': 0, '<': 141, '<?xml version=': 0, '>': 166, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 6}, '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': '4714619', 'n_tokens_mistral': 5884, 'n_tokens_neox': 5516, 'n_words': 1508}