data.php 4.42 KB
Newer Older
Denis Valdenaire's avatar
Denis Valdenaire committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<?php

class data {

	public $db_name;
    public $db_handle; ///< Resource id of the connexion

    public $sql_error = false;
    public $sql_error_code;
    public $sql_error_message;

    /** Constructor of the data class
     * Make a new data object. Connect only if there's no active connexion
    */
    public function data($db_handle = NULL) {
        if ($db_handle) {
            $this->db_handle = $db_handle;
        } else {
            $this->connect();
        }
    }

    /**
     * Make connection to the database
     * You shouldn't use it alone.
     * @see data
     */
    public function connect () {
        $this->db_handle = mysqli_connect($GLOBALS["db_host"], $GLOBALS["db_user"],
            $GLOBALS["db_passwd"], $GLOBALS["db_name"]);

        if (mysqli_connect_error()) {
            throw new data_exception(
                    mysqli_connect_errno(),
                    mysqli_connect_error(),
                    "Connection");
        }
38
        $this->db_handle->set_charset("utf8");
Denis Valdenaire's avatar
Denis Valdenaire committed
39 40 41 42 43 44 45 46

        return true;
    }

    public function db_escape_string($string) {
        return mysqli_real_escape_string($this->db_handle, $string);
    }

47 48 49 50 51 52 53 54 55 56
    public function find($query, &$rset, $object) {
        $this->select($query, $rset, $object);
        if(!is_object($rset)) {
            error_log("object not found : ".preg_replace('!\s+!', ' ', $query));
            throw new object_not_found_exception(
                    "Object not found");
        }
        return $rset;
    }

Denis Valdenaire's avatar
Denis Valdenaire committed
57 58 59 60 61
    /*
     *  Execute a select query
    * @param $query the select sql statement to execute
    * @param $rset
    */
62
    public function select ($query, &$rset, $object = false, $force_array = false) {
Denis Valdenaire's avatar
Denis Valdenaire committed
63 64 65 66 67 68 69 70 71

        $conn = "db_handle";
        if (! ($this->db_handle && ($result = $this->$conn->query($query)))) {
            error_log($query);
            throw new data_exception(
                    mysqli_errno($this->$conn),
                    mysqli_error($this->$conn),
                    $query);
        }
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
72 73 74 75 76 77 78
		if($object) {
			if(!class_exists($object)) {
				throw new data_exception(
					0,
					"Calling select to object w/ a class <b>$object</b> that doesn't even exists",
					"select (\$query, &\$rset, $object)");
			}
79
			if($result->num_rows <= 1 && !$force_array) {
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
80 81
				$rset = $result->fetch_object($object);
			} else {
Denis Valdenaire's avatar
Denis Valdenaire committed
82
			    $rset = array();
Denis S. Valdenaire's avatar
Denis S. Valdenaire committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
				while ($obj = $result->fetch_object($object)) {
					$rset[] = $obj;
			    }
			}
		} else {
			$rset = new rset(
					mysqli_field_count($this->$conn),
					$result->num_rows
			);
			$rset->raw_content = $result;
			$rset->content[0]  = mysqli_fetch_array($rset->raw_content, MYSQLI_ASSOC);
			$finfo             = $result->fetch_fields();
			foreach ($finfo as $val) {
				$rset->table[] = $val->name;
			}
		}
Denis Valdenaire's avatar
Denis Valdenaire committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        return true;
    }

    /*
     * Execute an insert query
     * @param $query the insert sql statement to execute
     * @returns how many rows were affected
     */
    public function insert ($query) {

        if (!$this->db_handle->query($query)) {
            throw new data_exception(
                    mysqli_errno($this->db_handle),
                    mysqli_error($this->db_handle),
                    $query);
        }

        if (!mysqli_affected_rows($this->db_handle)) {
            return false;
        } else {
            return mysqli_insert_id($this->db_handle);
        }
    }

    /**
     * Execute an update query
     * @param $query the update sql statement to execute
     * @returns how many rows were affected
     */
    public function update ($query) {
        if (!$this->db_handle->query($query)) {
            throw new data_exception(
                    mysqli_errno($this->db_handle),
                    mysqli_error($this->db_handle),
                    $query);
        }
        return  mysqli_affected_rows($this->db_handle);
    }

    /**
     * Execute a delete query
     * @param $query the delete sql statement to execute
     * @returns how many rows were deleted
     */
    public function delete ($query) {

        if (!$this->db_handle->query($query)) {
            throw new data_exception(
                    mysqli_errno($this->db_handle),
                    mysqli_error($this->db_handle),
                    $query);
        }

        return mysqli_affected_rows($this->db_handle);
    }

    /**
     * Return the public function now() for the database
     */
    public function get_sysdate_function() {
        return " now() ";
    }
}