mirror of
https://gitlab.com/CodeSolutionsProject/DBWrapper.git
synced 2026-02-15 01:21:34 +01:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d366ae0f1f | |||
| 3ee9010bee | |||
| 7dd89fd33f | |||
| 55db830eac | |||
| 45cb93991c | |||
| 69a9f230da | |||
| b738eda270 | |||
| 926a16ef65 | |||
|
|
4a4b641dc6 | ||
|
|
8098979bdf |
13
README.md
13
README.md
@@ -1,7 +1,16 @@
|
|||||||
# DBWrapper - Version 1.4.1
|
# DBWrapper - Version 1.5.0
|
||||||
|
|
||||||
This script is a simple wrapper for SQLite3, MySQL and PgSQL, for make possible to use different BD systems without changing the functions.
|
This script is a simple wrapper for SQLite3, MySQL and PgSQL, for make possible to use different BD systems without changing the functions.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
In composer:
|
||||||
|
```bash
|
||||||
|
composer require jkanetwork/dbwrapper "~1.5.0"
|
||||||
|
```
|
||||||
|
|
||||||
|
By hand: Download the php file inside src folder
|
||||||
|
|
||||||
## Avalible functions:
|
## Avalible functions:
|
||||||
* To connect
|
* To connect
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
"type": "library",
|
"type": "library",
|
||||||
"description": "MySQL, PgSQL and SQLite wrapper",
|
"description": "MySQL, PgSQL and SQLite wrapper",
|
||||||
"keywords": ["mysql", "PgSQL", "sqlite", "wrapper"],
|
"keywords": ["mysql", "PgSQL", "sqlite", "wrapper"],
|
||||||
"license": "APACHE",
|
"license": "Apache-2.0",
|
||||||
"authors": [
|
"authors": [
|
||||||
{"name": "Kevin Puertas Ruiz", "email": "kevin01010@gmail.com"}
|
{"name": "Kevin Puertas Ruiz", "email": "kevin01010@gmail.com"}
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
/* DBWrapper.php - Version 1.4.1
|
/* DBWrapper.php - Version 1.5
|
||||||
This script is a simple wrapper for SQLite3, MySQL and PgSQL,
|
This script is a simple wrapper for SQLite3, MySQL and PgSQL,
|
||||||
for make possible to use different BD systems without changing the functions.
|
for make possible to use different BD systems without changing the functions.
|
||||||
For use, in dbw_connect you have to specify type of database (see below)
|
For use, in dbw_connect you have to specify type of database (see below)
|
||||||
@@ -12,19 +12,19 @@ function dbw_connect($tdb,$server,$database=NULL,$user = NULL,$password=NULL){
|
|||||||
switch ($tdb){
|
switch ($tdb){
|
||||||
case "mysql":
|
case "mysql":
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
$return[0] = mysqli_connect($server,$user,$password,$database) or die("Error de conexion");
|
$return[0] = mysqli_connect($server,$user,$password,$database) or throwExceptionDBConn();
|
||||||
$return[1] = "mysqli"; //Return standard mysqli for other funcs.
|
$return[1] = "mysqli"; //Return standard mysqli for other funcs.
|
||||||
break;
|
break;
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
case "sqlite3":
|
case "sqlite3":
|
||||||
$return[0] = new SQLite3($server);
|
$return[0] = new SQLite3($server);
|
||||||
if (!$return[0]) die ("Error de conexion");
|
if (!$return[0]) throwExceptionDBConn();
|
||||||
$return[1] = "sqlite"; //Return standard SQLite3 for other funcs.
|
$return[1] = "sqlite"; //Return standard SQLite3 for other funcs.
|
||||||
break;
|
break;
|
||||||
case "PostgreSQL":
|
case "PostgreSQL":
|
||||||
case "pg":
|
case "pg":
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
$return[0] = pg_connect("host=$server dbname=$database user=$user password=$password") or die ('Error de conexion: ' . pg_last_error());
|
$return[0] = pg_connect("host=$server dbname=$database user=$user password=$password") or throwExceptionDBConn();
|
||||||
$return[1] = "PgSQL"; //Return standard PgSQL for other funcs.
|
$return[1] = "PgSQL"; //Return standard PgSQL for other funcs.
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -34,6 +34,10 @@ function dbw_connect($tdb,$server,$database=NULL,$user = NULL,$password=NULL){
|
|||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function throwExceptionDBConn() {
|
||||||
|
throw new Exception('Database connection error');
|
||||||
|
}
|
||||||
|
|
||||||
/** Escapes conflictive chars for inserting into database */
|
/** Escapes conflictive chars for inserting into database */
|
||||||
function dbw_escape_string($conn,$string){
|
function dbw_escape_string($conn,$string){
|
||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
@@ -49,14 +53,20 @@ function dbw_escape_string($conn,$string){
|
|||||||
|
|
||||||
/** Make query */
|
/** Make query */
|
||||||
function dbw_query($conn,$query){
|
function dbw_query($conn,$query){
|
||||||
|
$time = microtime(true);
|
||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
return mysqli_query($conn[0],$query);
|
$ret = mysqli_query($conn[0],$query);
|
||||||
|
break;
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
return $conn[0]->query($query);
|
$ret = $conn[0]->query($query);
|
||||||
|
break;
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
return pg_query($query); //Last error (pg_last_error()) not implemented
|
$ret = pg_query($query);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
//echo "<p>".$query."->".(microtime(true)-$time)." milisegundos</p>";
|
||||||
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Fetch array from query */
|
/** Fetch array from query */
|
||||||
@@ -83,23 +93,27 @@ function dbw_fetch_array($conn,$result,$typearray = NULL){
|
|||||||
|
|
||||||
/** Make query and fetch array */
|
/** Make query and fetch array */
|
||||||
function dbw_query_fetch_array($conn,$query){
|
function dbw_query_fetch_array($conn,$query){
|
||||||
|
$time = microtime(true);
|
||||||
|
|
||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
$query = mysqli_query($conn[0],$query);
|
$result = mysqli_query($conn[0],$query);
|
||||||
if ($query == false || $query == NULL){return false;}
|
if (!$result){return false;}
|
||||||
return mysqli_fetch_array($query);
|
$ret = mysqli_fetch_array($result);
|
||||||
break;
|
break;
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
$query = $conn[0]->query($query);
|
$result = $conn[0]->query($query);
|
||||||
if ($query == false || $query == NULL){return false;}
|
if (!$result){return false;}
|
||||||
return $query->fetchArray();
|
$ret = $result->fetchArray();
|
||||||
break;
|
break;
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
$query = pg_query($query);
|
$result = pg_query($query);
|
||||||
if ($query == false || $query == NULL){return false;}
|
if (!$result){return false;}
|
||||||
return pg_fetch_array($query); //Last error (pg_last_error()) not implemented
|
$ret = pg_fetch_array($result);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
//echo "<p>".$query."->".(microtime(true)-$time)." milisegundos</p>";
|
||||||
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Goes a query to $row. $row starts in 0 as first row as if not specified */
|
/** Goes a query to $row. $row starts in 0 as first row as if not specified */
|
||||||
@@ -121,11 +135,16 @@ function dbw_query_goto($conn,$result,$row = 0){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Does multiple querys in one command */
|
/** Does multiple querys in one command
|
||||||
function dbw_multi_query($conn,$query){
|
* The erasebuffer command, in mysqli is neccesary if its only a insert for avoid problems in next querys
|
||||||
|
*/
|
||||||
|
function dbw_multi_query($conn,$query,$erasebuffer = 0){
|
||||||
switch ($conn[1]){
|
switch ($conn[1]){
|
||||||
case "mysqli":
|
case "mysqli":
|
||||||
mysqli_multi_query($conn[0],$query);
|
mysqli_multi_query($conn[0],$query);
|
||||||
|
if ($erasebuffer){
|
||||||
|
while(mysqli_next_result($conn[0])){;} //Erase multiquery output for avoid error in next query
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
$conn[0]->exec($query);
|
$conn[0]->exec($query);
|
||||||
@@ -155,12 +174,31 @@ function dbw_num_rows($conn,$result){
|
|||||||
case "mysqli":
|
case "mysqli":
|
||||||
return mysqli_num_rows($result);
|
return mysqli_num_rows($result);
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
return $result->numRows();
|
//Emulating num_rows. Is faster to use a COUNT(*), but for make it work...
|
||||||
|
$count=0;
|
||||||
|
$result->reset(); //Reset pointer
|
||||||
|
while($row = $result->fetchArray(SQLITE3_ASSOC)){
|
||||||
|
$count++;
|
||||||
|
}
|
||||||
|
$result->reset(); //Reset pointer
|
||||||
|
return $count;
|
||||||
case "PgSQL":
|
case "PgSQL":
|
||||||
return pg_num_rows ($result);
|
return pg_num_rows ($result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Escapes conflictive chars for inserting into database */
|
||||||
|
function dbw_free_result($conn,$result){
|
||||||
|
switch ($conn[1]){
|
||||||
|
case "mysqli":
|
||||||
|
return mysqli_free_result($string);
|
||||||
|
case "sqlite":
|
||||||
|
return NULL; //Not neccesary I think
|
||||||
|
case "PgSQL":
|
||||||
|
return NULL; //¿?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Close connection */
|
/** Close connection */
|
||||||
function dbw_close($conn){
|
function dbw_close($conn){
|
||||||
|
|||||||
Reference in New Issue
Block a user