Skip to content

Commit

Permalink
updated query method
Browse files Browse the repository at this point in the history
  • Loading branch information
joykumarbera committed Aug 17, 2022
1 parent 3117b57 commit 32c5688
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Select data

```php
$db->query('SELECT * FROM songs')->all()
$db->query('SELECT * FROM songs WHERE id = ?', [1])->one()
```


Expand Down Expand Up @@ -120,11 +121,18 @@ $db->query('SELECT * FROM songs')->all()
```php
$db->query($sql, $params = [])
```
#### Get total nunmber of rows
#### Get total nunmber of affected rows

```php
$db->getNumRows()
$db->getAffectedRows()
```

#### Get last insert id

```php
$db->lastInsertId()
```

#### Get single record as an array

```php
Expand Down
62 changes: 51 additions & 11 deletions src/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private function initConnecton()
* @param string $table
* @param array $data
*
* @return Db
* @return int|bool
*/
public function insert($table, $data = [])
{
Expand All @@ -152,7 +152,21 @@ public function insert($table, $data = [])
$value_placeholder = rtrim($value_placeholder, ', ');
$sql = "INSERT INTO $table ( `" . implode('`,`', $columns ) ."` ) VALUES( $value_placeholder )";

return $this->query($sql, $values);
if( $this->_runQuery($sql, $values) ) {
return $this->lastInsertId();
}

return false;
}

/**
* Get last insert id
*
* @return int
*/
public function lastInsertId()
{
return $this->stmt->insert_id;
}

/**
Expand All @@ -162,7 +176,7 @@ public function insert($table, $data = [])
* @param array $data
* @param array $conditions
*
* @return Db
* @return int|bool
*/
public function update($table, $data = [], $conditions = [], $glue = 'AND')
{
Expand All @@ -179,15 +193,19 @@ public function update($table, $data = [], $conditions = [], $glue = 'AND')
foreach($conditions as $key => $value) {
$where_clause .= '`' . $key . '`' . ' = ' . '? ' . $glue;
}

$where_clause = rtrim($where_clause, " $glue");
} else {
$where_clause = 1;
}

$sql = "UPDATE $table SET $update_columns WHERE $where_clause";
$final_params = array_merge( array_values($data), array_values($conditions));

return $this->query($sql, array_merge( array_values($data), array_values($conditions)));
if( $this->_runQuery($sql, $final_params) ) {
return $this->getAffectedRows();
}

return false;
}

/**
Expand All @@ -196,7 +214,7 @@ public function update($table, $data = [], $conditions = [], $glue = 'AND')
* @param string $table
* @param array $conditions
*
* @return Db
* @return int|bool
*/
public function delete($table, $conditions=[], $glue = 'AND')
{
Expand All @@ -213,7 +231,11 @@ public function delete($table, $conditions=[], $glue = 'AND')

$sql = "DELETE FROM $table WHERE $where_clause";

return $this->query($sql, array_values($conditions));
if( $this->_runQuery($sql, array_values($conditions)) ) {
return $this->getAffectedRows();
}

return false;
}

/**
Expand Down Expand Up @@ -251,8 +273,26 @@ public function deleteUsingOr($table, $conditions=[])
public function query($sql, $params = [])
{
$this->sql = $sql;
if($this->_runQuery($this->sql, $params)) {
return $this;
} else {
throw new DbErrorException("DB error :: " . $this->stmt->error);
}
}

/**
* Run the actual query
*
* @param string $sql
* @param array $params
*
* @return bool
* @throws DbErrorException
*/
private function _runQuery($sql, $params)
{
try {
$this->stmt = $this->con->prepare($this->sql);
$this->stmt = $this->con->prepare($sql);
if($this->stmt === false) {
throw new DbErrorException('DB error :: ' . $this->con->error );
}
Expand All @@ -279,18 +319,18 @@ public function query($sql, $params = [])

if($this->stmt->execute()) {
$this->query_result = $this->stmt->get_result();
return $this;
return true;
} else {
throw new DbErrorException("DB error :: " . $this->stmt->error);
}
}

/**
* Get total nunmber of rows
* Get total nunmber of rows affected rows
*
* @return int
*/
public function getNumRows()
public function getAffectedRows()
{
if( $this->stmt == null ) {
return 0;
Expand Down

0 comments on commit 32c5688

Please sign in to comment.