From dbb83449e39952bc0b4e75843b7772d81652046a Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Notario Date: Fri, 20 Oct 2017 10:18:11 -0200 Subject: [PATCH] v1 --- .gitignore | 17 +++++ autoload.php | 26 +++++++ config.php | 7 ++ database/db_pdo.php | 154 ++++++++++++++++++++++++++++++++++++++++++ database/dbmysqli.php | 52 ++++++++++++++ database/mmsql.php | 79 ++++++++++++++++++++++ database/mpdo.php | 110 ++++++++++++++++++++++++++++++ database/mysql.php | 70 +++++++++++++++++++ database/odbc.php | 0 database/postgre.php | 50 ++++++++++++++ database/sqlite.php | 0 library/db.php | 31 +++++++++ library/untitled.php | 10 +++ library/untitled1.php | 10 +++ 14 files changed, 616 insertions(+) create mode 100644 .gitignore create mode 100644 autoload.php create mode 100644 config.php create mode 100644 database/db_pdo.php create mode 100644 database/dbmysqli.php create mode 100644 database/mmsql.php create mode 100644 database/mpdo.php create mode 100644 database/mysql.php create mode 100644 database/odbc.php create mode 100644 database/postgre.php create mode 100644 database/sqlite.php create mode 100644 library/db.php create mode 100644 library/untitled.php create mode 100644 library/untitled1.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..245583f --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +.DS_Store +/_notes +/_notesx +_notes +_notesX +._* +*.zip +._ +._* +image/cache/* +!.gitignore +system/cache/* +!.gitignore +vqmod/vqcache/* +!.gitignore +zip.lst +teste.php \ No newline at end of file diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..0f81812 --- /dev/null +++ b/autoload.php @@ -0,0 +1,26 @@ + PDO::ERRMODE_SILENT + ); + /** + * The number of rows affected by the last operation + * + * @var int + */ + private $affectedRows = 0; + /** + * The data for the database connection + * + * @var \stdClass + */ + private $params = array(); + /** + * Sets the connection and connects to the database + * + * @param string $host server Address + * @param string $user Username + * @param string $pass Password + * @param string $name The database name + * @param string $charset Encoding connection + */ + public function __construct($host, $user, $pass, $name, $charset = 'utf8') + { + $this->params = new stdClass; + # keep connection data + $this->params->host = $host; + $this->params->user = $user; + $this->params->pass = $pass; + $this->params->name = $name; + $this->params->charset = $charset; + $this->params->connstr = "mysql:host={$host};dbname={$name};charset={$charset}"; + # add the connection parameters + $this->options['PDO::MYSQL_ATTR_INIT_COMMAND'] = "SET NAMES '{$charset}'"; + $this->connect(); + } + /** + * Connect to database + */ + public function connect() + { + try { + $this->dbh = new PDO($this->params->connstr, $this->params->user, $this->params->pass, $this->options); + if (version_compare(PHP_VERSION, '5.3.6', '<=')) { + $this->dbh->exec($this->options['PDO::MYSQL_ATTR_INIT_COMMAND']); + } + } catch (PDOException $exception) { + trigger_error($exception->getMessage()); + } + } + /** + * Query the database + * + * @param string $sql + * @return \stdClass + */ + public function query($sql = null) + { + if ($this->dbh) { + $data = new stdClass; + + $sth=$this->dbh->prepare($sql); + $sth->execute(); + //$sth= $this->dbh->query($sql); + $this->affectedRows = $sth->rowCount(); + $data->rows = $sth ? $sth->fetchAll() : array(); + $data->row = isset($data->rows[0]) ? $data->rows[0] : null; + $data->num_rows = $this->affectedRows; + return $data; + } + return null; + } + /** + * Concludes the string in quotation marks to be used in the query + * + * @param mixed $string shielded line + * @return string Returns shielded line or to FALSE , if the driver does not support screening + */ + public function escape($string = null) + { + return $this->dbh ? str_replace("'", "", $this->dbh->quote($string)) : null; + } + /** + * Gets the number of rows affected by the last operation + * + * @return int + */ + public function countAffected() + { + return $this->affectedRows; + } + /** + * Gets the ID of the last inserted row or sequence of values + * + * @return int + */ + public function getLastId() + { + return $this->dbh ? $this->dbh->lastInsertId() : 0; + } + /** + * Gets the name of the driver + * + * @return string|null + */ + public function getDriverName() + { + return $this->dbh ? $this->dbh->getAttribute(PDO::ATTR_DRIVER_NAME) : null; + } + /** + * Get information about the version of the client libraries that are used by the PDO driver + * + * @return string|null + */ + public function getVersion() + { + return $this->dbh ? $this->dbh->getAttribute(PDO::ATTR_CLIENT_VERSION) : null; + } + /** + * Closing a database connection + */ + public function close() + { + $this->dbh = null; + } + public function __destruct() + { + $this->close(); + } +} \ No newline at end of file diff --git a/database/dbmysqli.php b/database/dbmysqli.php new file mode 100644 index 0000000..cbe265e --- /dev/null +++ b/database/dbmysqli.php @@ -0,0 +1,52 @@ +connection = new \mysqli($hostname, $username, $password, $database, $port); + if ($this->connection->connect_error) { + throw new \Exception('Error: ' . $this->connection->error . '
Error No: ' . $this->connection->errno); + } + $this->connection->set_charset("utf8"); + $this->connection->query("SET SQL_MODE = ''"); + } + public function query($sql) { + $query = $this->connection->query($sql); + if (!$this->connection->errno) { + if ($query instanceof \mysqli_result) { + $data = array(); + while ($row = $query->fetch_assoc()) { + $data[] = $row; + } + $result = new \stdClass(); + $result->num_rows = $query->num_rows; + $result->row = isset($data[0]) ? $data[0] : array(); + $result->rows = $data; + $query->close(); + return $result; + } else { + return true; + } + } else { + throw new \Exception('Error: ' . $this->connection->error . '
Error No: ' . $this->connection->errno . '
' . $sql); + } + } + public function escape($value) { + return $this->connection->real_escape_string($value); + } + + public function countAffected() { + return $this->connection->affected_rows; + } + public function getLastId() { + return $this->connection->insert_id; + } + + public function isConnected() { + return $this->connection->ping(); + } + + public function __destruct() { + $this->connection->close(); + } +} +?> \ No newline at end of file diff --git a/database/mmsql.php b/database/mmsql.php new file mode 100644 index 0000000..acf0da0 --- /dev/null +++ b/database/mmsql.php @@ -0,0 +1,79 @@ +connection = mssql_connect($hostname, $username, $password)) { + exit('Error: Could not make a database connection using ' . $username . '@' . $hostname); + } + + if (!mssql_select_db($database, $this->connection)) { + exit('Error: Could not connect to database ' . $database); + } + + mssql_query("SET NAMES 'utf8'", $this->connection); + mssql_query("SET CHARACTER SET utf8", $this->connection); + mssql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection); + } + + public function query($sql) { + $resource = mssql_query($sql, $this->connection); + + if ($resource) { + if (is_resource($resource)) { + $i = 0; + + $data = array(); + + while ($result = mssql_fetch_assoc($resource)) { + $data[$i] = $result; + + $i++; + } + + mssql_free_result($resource); + + $query = new stdClass(); + $query->row = isset($data[0]) ? $data[0] : array(); + $query->rows = $data; + $query->num_rows = $i; + + unset($data); + + return $query; + } else { + return true; + } + } else { + trigger_error('Error: ' . mssql_get_last_message($this->connection) . '
' . $sql); + exit(); + } + } + + public function escape($value) { + return mssql_real_escape_string($value, $this->connection); + } + + public function countAffected() { + return mssql_rows_affected($this->connection); + } + + public function getLastId() { + $last_id = false; + + $resource = mssql_query("SELECT @@identity AS id", $this->connection); + + if ($row = mssql_fetch_row($resource)) { + $last_id = trim($row[0]); + } + + mssql_free_result($resource); + + return $last_id; + } + + public function __destruct() { + mssql_close($this->connection); + } +} +?> \ No newline at end of file diff --git a/database/mpdo.php b/database/mpdo.php new file mode 100644 index 0000000..0bd1ab6 --- /dev/null +++ b/database/mpdo.php @@ -0,0 +1,110 @@ + true, + \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION); + + $this->connection = new \PDO($dsn, $username, $password, $options); + } catch(\PDOException $e) { + throw new \Exception('Failed to connect to database. Reason: \'' . $e->getMessage() . '\''); + } + $this->connection->exec("SET NAMES 'utf8'"); + $this->connection->exec("SET CHARACTER SET utf8"); + $this->connection->exec("SET CHARACTER_SET_CONNECTION=utf8"); + $this->connection->exec("SET SQL_MODE = ''"); + $this->rowCount = 0; + } + public function prepare($sql) { + $this->statement = $this->connection->prepare($sql); + } + public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0) { + if ($length) { + $this->statement->bindParam($parameter, $variable, $data_type, $length); + } else { + $this->statement->bindParam($parameter, $variable, $data_type); + } + } + public function execute() { + try { + if ($this->statement && $this->statement->execute()) { + $data = array(); + while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) { + $data[] = $row; + } + $result = new \stdClass(); + $result->row = (isset($data[0])) ? $data[0] : array(); + $result->rows = $data; + $result->num_rows = $this->statement->rowCount(); + } + } catch(\PDOException $e) { + throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode()); + } + } + public function query($sql, $params = array()) { + $this->statement = $this->connection->prepare($sql); + + $result = false; + try { + if ($this->statement && $this->statement->execute($params)) { + $data = array(); + $this->rowCount = $this->statement->rowCount(); + if($this->rowCount > 0) + { + try { + $data = $this->statement->fetchAll(\PDO::FETCH_ASSOC); + } + catch(\Exception $ex){} + } + // free up resources + $this->statement->closeCursor(); + $this->statement = null; + $result = new \stdClass(); + $result->row = (isset($data[0]) ? $data[0] : array()); + $result->rows = $data; + $result->num_rows = $this->rowCount; + } + } catch (\PDOException $e) { + throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . '
' . $sql); + } + if ($result) { + return $result; + } else { + $result = new \stdClass(); + $result->row = array(); + $result->rows = array(); + $result->num_rows = 0; + return $result; + } + } + public function escape($value) { + return str_replace(array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'), $value); + } + public function countAffected() { + if ($this->statement) { + return $this->statement->rowCount(); + } else { + return $this->rowCount; + } + } + public function getLastId() { + return $this->connection->lastInsertId(); + } + + public function isConnected() { + if ($this->connection) { + return true; + } else { + return false; + } + } + + public function __destruct() { + $this->connection = null; + } +} \ No newline at end of file diff --git a/database/mysql.php b/database/mysql.php new file mode 100644 index 0000000..cafdf6e --- /dev/null +++ b/database/mysql.php @@ -0,0 +1,70 @@ +connection = mysql_connect($hostname, $username, $password)) { + exit('Error: Could not make a database connection using ' . $username . '@' . $hostname); + } + + if (!mysql_select_db($database, $this->connection)) { + exit('Error: Could not connect to database ' . $database); + } + + mysql_query("SET NAMES 'utf8'", $this->connection); + mysql_query("SET CHARACTER SET utf8", $this->connection); + mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->connection); + mysql_query("SET SQL_MODE = ''", $this->connection); + } + + public function query($sql) { + $resource = mysql_query($sql, $this->connection); + + if ($resource) { + if (is_resource($resource)) { + $i = 0; + + $data = array(); + + while ($result = mysql_fetch_assoc($resource)) { + $data[$i] = $result; + + $i++; + } + + mysql_free_result($resource); + + $query = new stdClass(); + $query->row = isset($data[0]) ? $data[0] : array(); + $query->rows = $data; + $query->num_rows = $i; + + unset($data); + + return $query; + } else { + return true; + } + } else { + trigger_error('Error: ' . mysql_error($this->connection) . '
Error No: ' . mysql_errno($this->connection) . '
' . $sql); + exit(); + } + } + + public function escape($value) { + return mysql_real_escape_string($value, $this->connection); + } + + public function countAffected() { + return mysql_affected_rows($this->connection); + } + + public function getLastId() { + return mysql_insert_id($this->connection); + } + + public function __destruct() { + mysql_close($this->connection); + } +} +?> \ No newline at end of file diff --git a/database/odbc.php b/database/odbc.php new file mode 100644 index 0000000..e69de29 diff --git a/database/postgre.php b/database/postgre.php new file mode 100644 index 0000000..777788e --- /dev/null +++ b/database/postgre.php @@ -0,0 +1,50 @@ +link = pg_connect('host=' . $hostname . ' port=' . $port . ' user=' . $username . ' password=' . $password . ' dbname=' . $database)) { + throw new \Exception('Error: Could not make a database link using ' . $username . '@' . $hostname); + } + if (!pg_ping($this->link)) { + throw new \Exception('Error: Could not connect to database ' . $database); + } + pg_query($this->link, "SET CLIENT_ENCODING TO 'UTF8'"); + } + public function query($sql) { + $resource = pg_query($this->link, $sql); + if ($resource) { + if (is_resource($resource)) { + $i = 0; + $data = array(); + while ($result = pg_fetch_assoc($resource)) { + $data[$i] = $result; + $i++; + } + pg_free_result($resource); + $query = new \stdClass(); + $query->row = isset($data[0]) ? $data[0] : array(); + $query->rows = $data; + $query->num_rows = $i; + unset($data); + return $query; + } else { + return true; + } + } else { + throw new \Exception('Error: ' . pg_result_error($this->link) . '
' . $sql); + } + } + public function escape($value) { + return pg_escape_string($this->link, $value); + } + public function countAffected() { + return pg_affected_rows($this->link); + } + public function getLastId() { + $query = $this->query("SELECT LASTVAL() AS `id`"); + return $query->row['id']; + } + public function __destruct() { + pg_close($this->link); + } +} \ No newline at end of file diff --git a/database/sqlite.php b/database/sqlite.php new file mode 100644 index 0000000..e69de29 diff --git a/library/db.php b/library/db.php new file mode 100644 index 0000000..4fd880b --- /dev/null +++ b/library/db.php @@ -0,0 +1,31 @@ +driver = new $driver($hostname, $username, $password, $database); + } + + public function query($sql) { + return $this->driver->query($sql); + } + + public function escape($value) { + return $this->driver->escape($value); + } + + public function countAffected() { + return $this->driver->countAffected(); + } + + public function getLastId() { + return $this->driver->getLastId(); + } +} +?> \ No newline at end of file diff --git a/library/untitled.php b/library/untitled.php new file mode 100644 index 0000000..9cb1d34 --- /dev/null +++ b/library/untitled.php @@ -0,0 +1,10 @@ + + + + +Untitled Document + + + + + \ No newline at end of file diff --git a/library/untitled1.php b/library/untitled1.php new file mode 100644 index 0000000..9cb1d34 --- /dev/null +++ b/library/untitled1.php @@ -0,0 +1,10 @@ + + + + +Untitled Document + + + + + \ No newline at end of file