diff --git a/article-two.html b/article-two.html new file mode 100644 index 0000000000..c1b8e6f52f --- /dev/null +++ b/article-two.html @@ -0,0 +1,6 @@ + + +
+ hi + + diff --git a/copytst.php b/copytst.php new file mode 100644 index 0000000000..013dc477a0 --- /dev/null +++ b/copytst.php @@ -0,0 +1,789 @@ + 'value' + * + * @var array + */ + $_where = array(), + /** + * Dynamic type list for order by condition value + */ + $_orderBy = array(), + /** + * Dynamic type list for group by condition value + */ + $_groupBy = array(), + /** + * Dynamic array that holds a combination of where condition/table data value types and parameter references + * + * @var array|null + */ + $_bindParams = null, + /** + * Name of the auto increment column + * + */ + $_lastInsertId = null, + /** + * Variable which holds last statement error + * + * @var string + */ + $_stmtError = null, + /** + * Allows the use of the tableNameToClassName method + * + * @var string + */ + $_autoClass = true, + /** + * Name of table we're performing the action on + * + * @var string + */ + $_tableName, + /** + * Type of fetch to perform + * + * @var string + */ + $_fetchType = PDO::FETCH_ASSOC, + /** + * Fetch argument + * + * @var string + */ + $_fetchArg; + public + /** + * Variable which holds an amount of returned rows during get/getOne/select queries + * + * @var string + */ + $count = 0; + private + /** + * Used for connecting to the database + * + * @var string + */ + $_connstr; + public function __construct($db, $host = localhost:5432, $user = bhanu836, $pass = db-bhanu836-69865){ + $this->_connstr = "pgsql:host=$host user=$user password=$pass dbname=$db options='--client_encoding=UTF8'"; + } + protected function _connect(){ + $this->_conn = new PDO($this->_connstr); + $this->_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); + } + /** @return PDO */ + public function pdo(){ + if (!$this->_conn){ + $this->_connect(); + } + return $this->_conn; + } + /** + * Method attempts to prepare the SQL query + * and throws an error if there was a problem. + * + * @return PDOStatement + */ + protected function _prepareQuery(){ + try { + $stmt = $this->pdo()->prepare($this->_query); + } + catch (PDOException $e){ + throw new Exception ("Problem preparing query ($this->_query) ".$e->getMessage()); + } + return $stmt; + } + /** + * Function to replace ? with variables from bind variable + * + * @param string $str + * @param Array $vals + * + * @return string + */ + protected function _replacePlaceHolders($str, $vals){ + $i = 0; + $valcount = count($vals); + while (strpos($str, "?") !== false && $i < $valcount){ + $val = $vals[$i++]; + if (is_object($val)){ + $val = '[object]'; + } + else if ($val === null){ + $val = 'NULL'; + } + if (is_string($val)){ + $val = "'$val'"; + } + $str = preg_replace('/\?/', $val, $str, 1); + } + return $str; + } + /** + * Helper function to add variables into bind parameters array + * + * @param string Variable value + */ + protected function _bindParam($value){ + $this->_bindParams[] = $value; + } + /** + * Helper function to add variables into bind parameters array in bulk + * + * @param Array Variable with values + */ + protected function _bindParams($values){ + foreach ($values as $value){ + $this->_bindParam($value); + } + } + /** + * Helper function to add variables into bind parameters array and will return + * its SQL part of the query according to operator in ' $operator ?' + * + * @param array $operator Variable with values + * @param $value + * + * @return string + */ + protected function _buildPair($operator, $value){ + $this->_bindParam($value); + return ' '.$operator.' ? '; + } + /** + * Abstraction method that will build an JOIN part of the query + */ + protected function _buildJoin(){ + if (empty ($this->_join)){ + return; + } + foreach ($this->_join as $data){ + list ($joinType, $joinTable, $joinCondition) = $data; + $this->_query .= " $joinType JOIN ".$this->_escapeTableName($joinTable)." on $joinCondition"; + } + } + protected function _escapeApostrophe($str){ + return preg_replace('~(^|[^\'])\'~', '$1\'\'', $str); + } + /** + * Abstraction method that will build the part of the WHERE conditions + */ + protected function _buildWhere(){ + if (empty ($this->_where)){ + return; + } + //Prepare the where portion of the query + $this->_query .= ' WHERE'; + foreach ($this->_where as $cond){ + list ($concat, $varName, $operator, $val) = $cond; + if (preg_match('~^"?([a-z_\-\d]+)"?(?:->>\'?([\w\d\-]+)\'?)?$~', $varName, $match)){ + $varName = "\"$match[1]\"".(isset($match[2]) ? "->>'".self::_escapeApostrophe($match[2])."'" : ''); + } + $this->_query .= ' '.trim("$concat $varName"); + switch (strtolower($operator)) { + case 'not in': + case 'in': + $comparison = ' '.$operator.' ('; + if (is_object($val)){ + $comparison .= $this->_buildPair("", $val); + } + else { + foreach ($val as $v){ + $comparison .= ' ?,'; + $this->_bindParam($v); + } + } + $this->_query .= rtrim($comparison, ',').' ) '; + break; + case 'not between': + case 'between': + $this->_query .= " $operator ? AND ? "; + $this->_bindParams($val); + break; + case 'not exists': + case 'exists': + $this->_query .= $operator.$this->_buildPair("", $val); + break; + default: + if (is_array($val)){ + $this->_bindParams($val); + } + else if ($val === null){ + $this->_query .= $operator." NULL"; + } + else if ($val != 'DBNULL' || $val == '0'){ + $this->_query .= $this->_buildPair($operator, $val); + } + } + } + $this->_query = rtrim($this->_query); + } + public function _buildDataPairs($tableData, $tableColumns, $isInsert){ + foreach ($tableColumns as $column){ + $value = $tableData[$column]; + if (!$isInsert){ + $this->_query .= "\"$column\" = "; + } + // Simple value + if (!is_array($value)){ + if (is_bool($value)){ + $value = $value ? 'true' : 'false'; + } + $this->_bindParam($value); + $this->_query .= '?, '; + continue; + } + } + $this->_query = rtrim($this->_query, ', '); + } + /** + * Abstraction method that will build an INSERT or UPDATE part of the query + * + * @param array $tableData + */ + protected function _buildInsertQuery($tableData){ + if (!is_array($tableData)){ + return; + } + $isInsert = preg_match('/^[INSERT|REPLACE]/', $this->_query); + $dataColumns = array_keys($tableData); + if ($isInsert){ + $this->_query .= ' ("'.implode($dataColumns, '", "').'") VALUES ('; + } + else $this->_query .= " SET "; + $this->_buildDataPairs($tableData, $dataColumns, $isInsert); + if ($isInsert){ + $this->_query .= ')'; + } + } + /** + * Abstraction method that will build the GROUP BY part of the WHERE statement + * + */ + protected function _buildGroupBy(){ + if (empty ($this->_groupBy)){ + return; + } + $this->_query .= " GROUP BY "; + foreach ($this->_groupBy as $value){ + $this->_query .= "$value, "; + } + $this->_query = rtrim($this->_query, ', ').' '; + } + /** + * Abstraction method that will build the LIMIT part of the WHERE statement + * + */ + protected function _buildOrderBy(){ + if (empty($this->_orderBy)){ + return; + } + $this->_query .= " ORDER BY "; + foreach ($this->_orderBy as $prop => $value){ + if (strtolower(str_replace(' ', '', $prop)) == 'rand()'){ + $this->_query .= 'rand(), '; + } + else $this->_query .= "$prop $value, "; + } + $this->_query = rtrim($this->_query, ', '); + } + /** + * Internal function to build and execute INSERT/REPLACE calls + * + * @param string $tableName The name of the table. + * @param array $insertData Data containing information for inserting into the DB. + * @param $operation + * @param string|null $returnColumn What column to return after insert + * + * @return boolean Boolean indicating whether the insert query was completed succesfully. + */ + private function _buildInsert($tableName, $insertData, $operation, $returnColumn = null){ + $tableName = $this->_escapeTableName($tableName); + $this->_query = "$operation INTO $tableName"; + if (!empty($returnColumn)){ + $returnColumn = trim($returnColumn); + } + $stmt = $this->_buildQuery(null, $insertData, $returnColumn); + $res = $this->_execStatement($stmt); + if ($res === false || $this->count < 1){ + return false; + } + if (!empty($res[0][$returnColumn])){ + return $res[0][$returnColumn]; + } + return true; + } + /** + * Abstraction method that will build the LIMIT part of the WHERE statement + * + * @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) + * or only $count + */ + protected function _buildLimit($numRows){ + if (!isset($numRows)){ + return; + } + $this->_query .= ' LIMIT '.( + is_array($numRows) + ? (int) $numRows[1].' OFFSET '.(int) $numRows[0] + : (int) $numRows + ); + } + /** + * Abstraction method that will compile the WHERE statement, + * any passed update data, and the desired rows. + * It then builds the SQL query. + * + * @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) + * or only $count + * @param array $tableData Should contain an array of data for updating the database. + * @param string|null $returning What column to return after inserting + * + * @return PDOStatement Returns the $stmt object. + */ + protected function _buildQuery($numRows = null, $tableData = null, $returning = null){ + $this->_buildJoin(); + $this->_buildInsertQuery($tableData); + if (!empty($returning)){ + $this->_query .= " RETURNING \"$returning\""; + } + $this->_buildWhere(); + $this->_buildGroupBy(); + $this->_buildOrderBy(); + $this->_buildLimit($numRows); + $this->_alterQuery(); + $this->_lastQuery = $this->_replacePlaceHolders($this->_query, $this->_bindParams); + // Prepare query + $stmt = $this->_prepareQuery(); + return $stmt; + } + /** + * Execute raw SQL query. + * + * @param string $query User-provided query to execute. + * @param array $bindParams Variables array to bind to the SQL statement. + * + * @return array Contains the returned rows from the query. + */ + public function rawQuery($query, $bindParams = null){ + $params = array(null); // Create the empty 0 index + $this->_query = $query; + $this->_alterQuery(); + $stmt = $this->_prepareQuery(); + if (empty($bindParams)){ + $this->_bindParams = null; + } + else if (!is_array($bindParams)){ + throw new Exception('$bindParams must be an array'); + } + else { + $this->_bindParams = $bindParams; + } + $res = $this->_execStatement($stmt); + return $res; + } + /** + * Execute rawQuery with specified parameters an return a single row only + * + * @param array $args Arguments to be forwarded to rawQuery + * + * @return array + */ + public function rawQuerySingle(...$args){ + return $this->_singleRow($this->rawQuery(...$args)); + } + /** + * + * @param string $query Contains a user-provided select query. + * @param integer|array $numRows Array to define SQL limit in format Array ($count, $offset) + * + * @return array Contains the returned rows from the query. + */ + public function query($query, $numRows = null){ + $this->_query = $query; + $stmt = $this->_buildQuery($numRows); + $res = $this->_execStatement($stmt); + return $res; + } + /** + * Get number of rows in database table + * + * @param string $table Name of table + * + * @return int + */ + public function count($table){ + return $this->disableAutoClass()->getOne($table, 'COUNT(*)::int as c')['c']; + } + /** + * This method allows you to specify multiple (method chaining optional) AND WHERE statements for SQL queries. + * + * @uses $db->where('id', 7)->where('title', 'MyTitle'); + * + * @param string $whereProp The name of the database field. + * @param mixed $whereValue The value of the database field. + * @param string $operator + * @param string $cond + * + * @return self + */ + public function where($whereProp, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND'){ + if (is_array($whereValue) && ($key = key($whereValue)) != "0"){ + $operator = $key; + $whereValue = $whereValue[$key]; + } + if (count($this->_where) == 0){ + $cond = ''; + } + $this->_where[] = array($cond, $whereProp, $operator, $whereValue); + return $this; + } + /** + * Sets a class to be used as the PDO::fetchAll argument + * + * @param object|string $class + * + * @return self + */ + public function setClass($class){ + $this->_fetchType = PDO::FETCH_CLASS; + $this->_fetchArg = $class; + return $this; + } + /** + * Disabled the tableNameToClassName method + * + * @return self + */ + public function disableAutoClass(){ + $this->_autoClass = false; + return $this; + } + /** + * This method allows you to specify multiple (method chaining optional) OR WHERE statements for SQL queries. + * + * @uses $db->orWhere('id', 7)->orWhere('title', 'MyTitle'); + * + * @param string $whereProp The name of the database field. + * @param mixed $whereValue The value of the database field. + * @param string $operator + * + * @return self + */ + public function orWhere($whereProp, $whereValue = 'DBNULL', $operator = '='){ + return $this->where($whereProp, $whereValue, $operator, 'OR'); + } + /** + * This method allows you to specify multiple (method chaining optional) ORDER BY statements for SQL queries. + * + * @uses $db->orderBy('id', 'desc')->orderBy('name', 'desc'); + * + * @param string $orderByField The name of the database field. + * @param string $orderbyDirection Order direction. + * + * @return self + */ + public function orderBy($orderByField, $orderbyDirection = "DESC"){ + $allowedDirection = array("ASC", "DESC"); + $orderbyDirection = strtoupper(trim($orderbyDirection)); + $orderByField = preg_replace('/[^-a-z0-9\.\(\),_"\*]+/i', '', $orderByField); + if (empty($orderbyDirection) || !in_array($orderbyDirection, $allowedDirection)){ + die ('Wrong order direction: '.$orderbyDirection); + } + $this->_orderBy[$orderByField] = $orderbyDirection; + return $this; + } + /** + * Replacement for the orderBy method, which would screw up complex order statements + * + * @param string $orderstr Raw ordering sting + * @param string $direction Order direction + * + * @return self + */ + public function orderByLiteral($orderstr, $direction = 'ASC'){ + $this->_orderBy[$orderstr] = $direction; + return $this; + } + /** + * A convenient SELECT * function. + * + * @param string $tableName The name of the database table to work with. + * @param int|int[] $numRows Array to define SQL limit in format Array ($count, $offset) + * or only $count + * @param string $columns + * + * @return array Contains the returned rows from the select query. + */ + public function get($tableName, $numRows = null, $columns = null){ + if (empty ($columns)){ + $columns = '*'; + } + $column = is_array($columns) ? implode(', ', $columns) : $columns; + $tableName = $this->_escapeTableName($tableName); + $this->_query = "SELECT $column FROM $tableName"; + $stmt = $this->_buildQuery($numRows); + if ($this->_autoClass) + $this->_tableName = $tableName; + $res = $this->_execStatement($stmt); + return $res; + } + /** + * A convenient SELECT * function to get one record. + * + * @param string $tableName The name of the database table to work with. + * @param string $columns + * + * @return array|object|null + */ + public function getOne($tableName, $columns = '*'){ + $res = $this->get($tableName, 1, $columns); + if (is_array($res) && isset ($res[0])){ + return $res[0]; + } + else if ($res){ + return $res; + } + return null; + } + /** + * A convenient function that returns TRUE if exists at least an element that + * satisfy the where condition specified calling the "where" method before this one. + * + * @param string $tableName The name of the database table to work with. + * + * @return bool + */ + public function has($tableName){ + return $this->count($tableName) >= 1; + } + /** + * Update query. Be sure to first call the "where" method. + * + * @param string $tableName The name of the database table to work with. + * @param array $tableData Array of data to update the desired row. + * + * @return bool + */ + public function update($tableName, $tableData){ + $tableName = $this->_escapeTableName($tableName); + $this->_query = "UPDATE $tableName"; + $stmt = $this->_buildQuery(null, $tableData); + $res = $this->_execStatement($stmt); + return (bool) $res; + } + /** + * Insert method to add new row + * + * @param* required field.
+ + +Your Input:"; +echo $name; +echo "