Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 448d8bf

Browse files
edited few section to work with mysqli(advised not to use mysqli), all the tests are passing for mysqli and pdo
1 parent 7404810 commit 448d8bf

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

‎Test/Unit/QueryBuilderTest.php‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ public function insertIntoTable()
3535

3636
protected function setUp()
3737
{
38-
$this->queryBuilder = DbQueryBuilderFactory::make('database', 'pdo', ['db_name' => 'bug_app_testing']);
39-
$this->queryBuilder->getConnection()->beginTransaction();
38+
$this->queryBuilder = DbQueryBuilderFactory::make('database', 'mysqli', ['db_name' => 'bug_app_testing']);
39+
$this->queryBuilder->beginTransaction();
4040
parent::setUp();
4141
}
4242

4343

4444
protected function tearDown()
4545
{
46-
$this->queryBuilder->getConnection()->rollback();
46+
$this->queryBuilder->rollback();
4747
unset($this->queryBuilder);
4848
parent::tearDown();
4949
}
@@ -65,7 +65,7 @@ public function testItCanPerformSelectQuery()
6565
{
6666
$id = $this->insertIntoTable();
6767
$result = $this->queryBuilder->table('reports')
68-
->select('*')->where('id',$id)->first();
68+
->select('*')->where('id',$id)->runQuery()->first();
6969

7070
self::assertSame($id, $result->id);
7171
self::assertNotNull($result);
@@ -76,7 +76,7 @@ public function testItCanPerformSelectQueryWithMultipleWhereClause()
7676
$id = $this->insertIntoTable();
7777
$results = $this->queryBuilder->table('reports')
7878
->select('*')->where('id',$id)
79-
->where('report_type','=', 'Report Type 1')->first();
79+
->where('report_type','=', 'Report Type 1')->runQuery()->first();
8080
self::assertNotNull($results);
8181
self::assertSame($id, $results->id);
8282
self::assertSame('Report Type 1', $results->report_type);
@@ -85,7 +85,7 @@ public function testItCanPerformSelectQueryWithMultipleWhereClause()
8585
public function testItCanFindById()
8686
{
8787
$id = $this->insertIntoTable();
88-
$results = $this->queryBuilder->select('id, report_type')->find($id);
88+
$results = $this->queryBuilder->table('reports')->select('id, report_type')->find($id);
8989
self::assertNotNull($results);
9090
self::assertSame($id, $results->id);
9191
self::assertSame('Report Type 1', $results->report_type);
@@ -94,7 +94,8 @@ public function testItCanFindById()
9494
public function testItCanFindOneByGivingValues()
9595
{
9696
$id = $this->insertIntoTable();
97-
$results = $this->queryBuilder->select('*')->findOneBy('report_type', 'Report Type 1');
97+
$results = $this->queryBuilder->table('reports')
98+
->select('*')->findOneBy('report_type', 'Report Type 1');
9899
self::assertNotNull($results);
99100
self::assertSame($id, $results->id);
100101
self::assertSame('Report Type 1', $results->report_type);
@@ -103,7 +104,9 @@ public function testItCanFindOneByGivingValues()
103104
public function testItCanUpdateGivenRecord()
104105
{
105106
$id = $this->insertIntoTable();
106-
$count = $this->queryBuilder->table('reports')->update(['report_type' => 'Report Type 2'])->where('id', $id)->count();
107+
$count = $this->queryBuilder->table('reports')->update(['report_type' => 'Report Type 2'])
108+
->where('id', $id)->runQuery()->affected();
109+
107110
self::assertEquals(1, $count);
108111
$results = $this->queryBuilder->select('*')->findOneBy('report_type', 'Report Type 2');
109112
self::assertNotNull($results);
@@ -115,7 +118,7 @@ public function testItCanUpdateGivenRecord()
115118
public function testItCanDeleteGivenId()
116119
{
117120
$id = $this->insertIntoTable();
118-
$count = $this->queryBuilder->table('reports')->delete()->where('id', $id)->count();
121+
$count = $this->queryBuilder->table('reports')->delete()->where('id', $id)->runQuery()->affected();
119122
self::assertEquals(1, $count);
120123

121124
$result = $this->queryBuilder->select('*')->find($id);

‎src/Database/MySQLiQueryBuilder.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ public function fetchInto($className)
7878
return $this->results = $results;
7979
}
8080

81+
public function beginTransaction()
82+
{
83+
$this->conn->begin_transaction();
84+
}
85+
86+
public function affected()
87+
{
88+
$this->statement->store_result();
89+
return $this->statement->affected_rows;
90+
}
91+
8192
//----------------------------------------- Internal Methods -----------------------------------------------//
8293
private function parseBinding(array $params)
8394
{
@@ -114,4 +125,5 @@ private function parseBindingTypes()
114125
}
115126

116127

128+
117129
}

‎src/Database/PDOQueryBuilder.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@ public function fetchInto($className)
3838
{
3939
return $this->statement->fetchAll(PDO::FETCH_CLASS, $className);
4040
}
41+
42+
public function beginTransaction()
43+
{
44+
$this->conn->beginTransaction();
45+
}
46+
47+
public function affected()
48+
{
49+
return $this->count();
50+
}
4151
}

‎src/Database/QueryBuilder.php‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public function where($column, $operator = self::OPERATORS[0], $value = null)
4848
}
4949
}
5050
$this->passWhere([$column => $value], $operator);
51+
52+
return $this;
53+
}
54+
55+
public function runQuery()
56+
{
5157
$query = $this->prepare($this->getQuery($this->operation));
5258
$this->statement = $this->execute($query);
5359
return $this;
@@ -101,27 +107,23 @@ public function raw($query)
101107

102108
public function find($id)
103109
{
104-
return $this->where('id', $id)->first();
110+
return $this->where('id', $id)->runQuery()->first();
105111
}
106112

107113
public function findOneBy(string $field, $value)
108114
{
109-
return $this->where($field, $value)->first();
115+
return $this->where($field, $value)->runQuery()->first();
110116
}
111117

112118
public function first()
113119
{
114-
return $this->count() ? $this->get()[0] : "";
120+
return $this->count() ? $this->get()[0] : null;
115121
}
116122

117-
/**
118-
* @return mixed
119-
*/
120-
public function getConnection()
121-
{
122-
return $this->conn;
123-
}
124-
123+
public function rollback():void
124+
{
125+
$this->conn->rollback();
126+
}
125127

126128

127129

@@ -143,6 +145,8 @@ abstract public function lastInsertedId();
143145
abstract public function prepare($query);
144146
abstract public function execute($statement);
145147
abstract public function fetchInto($className);
148+
abstract public function beginTransaction();
149+
abstract public function affected();
146150

147151

148152
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /