Skip to content

Commit

Permalink
#22 Rename ResultSet functions to use underscrores like mySQli
Browse files Browse the repository at this point in the history
Renamed num_fields to field_count
  • Loading branch information
sbuberl committed May 26, 2019
1 parent e966472 commit 8c3c558
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 59 deletions.
24 changes: 12 additions & 12 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ private function query_insert($query)
return $this->set_error('Invalid INSERT Query ');
$result = $this->query_select($the_rest);
$dataRows = [];
while (($values = $result->fetchRow()) !== null) {
while (($values = $result->fetch_row()) !== null) {
$row = array_map(
function ($value) {
if (is_string($value)) $value = "'$value'";
Expand Down Expand Up @@ -2427,27 +2427,27 @@ private function parse_trim_function($join_info, $where_type, $paramsStr)

public function fetch_all(ResultSet $results, $type = ResultSet::FETCH_ASSOC)
{
return $results->fetchAll($type);
return $results->fetch_all($type);
}

public function fetch_array(ResultSet $results, $type = ResultSet::FETCH_ASSOC)
{
return $results->fetchArray($type);
return $results->fetch_array($type);
}

public function fetch_assoc(ResultSet $results)
{
return $results->fetchAssoc();
return $results->fetch_assoc();
}

public function fetch_row(ResultSet $results)
{
return $results->fetchRow();
return $results->fetch_row();
}

public function fetch_both(ResultSet $results)
{
return $results->fetchBoth();
return $results->fetch_both();
}

public function fetch_single(ResultSet $results, $column = 0)
Expand All @@ -2457,32 +2457,32 @@ public function fetch_single(ResultSet $results, $column = 0)

public function fetch_object(ResultSet $results)
{
return $results->fetchObject();
return $results->fetch_object();
}

public function data_seek(ResultSet $results, $i)
{
return $results->dataSeek($i);
return $results->data_seek($i);
}

public function num_rows(ResultSet $results)
{
return $results->numRows();
return $results->num_rows();
}

public function num_fields(ResultSet $results)
{
return $results->numFields();
return $results->field_count();
}

public function fetch_field(ResultSet $results)
{
return $results->fetchField();
return $results->fetch_field();
}

public function field_seek(ResultSet $results, $i)
{
return $results->fieldSeek($i);
return $results->field_seek($i);
}

public function free_result(ResultSet $results)
Expand Down
34 changes: 17 additions & 17 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public function __construct(array $columnNames, array $data)
$this->dataCursor = new TableCursor($data);
}

public function createMetadata()
public function create_metadata()
{
return new ResultSet($this->columnNames, []);
}

public function fetchAll($type = self::FETCH_ASSOC)
public function fetch_all($type = self::FETCH_ASSOC)
{
if ($type === self::FETCH_NUM) {
return $this->data;
Expand All @@ -49,7 +49,7 @@ public function fetchAll($type = self::FETCH_ASSOC)
return $result_array;
}

public function fetchArray($type = self::FETCH_ASSOC)
public function fetch_array($type = self::FETCH_ASSOC)
{
if (!$this->dataCursor->valid()) {
return null;
Expand All @@ -66,55 +66,55 @@ public function fetchArray($type = self::FETCH_ASSOC)
}
}

public function fetchAssoc()
public function fetch_assoc()
{
return $this->fetchArray(self::FETCH_ASSOC);
return $this->fetch_array(self::FETCH_ASSOC);
}

public function fetchRow()
public function fetch_row()
{
return $this->fetchArray(self::FETCH_NUM);
return $this->fetch_array(self::FETCH_NUM);
}

public function fetchBoth()
public function fetch_both()
{
return $this->fetchArray(self::FETCH_BOTH);
return $this->fetch_array(self::FETCH_BOTH);
}

public function fetchSingle($column = 0)
{
$type = is_numeric($column) ? self::FETCH_NUM : self::FETCH_ASSOC;
$row = $this->fetchArray($type);
$row = $this->fetch_array($type);

return $row !== null && array_key_exists($column, $row) ? $row[$column] : null;
}

public function fetchObject()
public function fetch_object()
{
$row = $this->fetchAssoc();
$row = $this->fetch_assoc();
if ($row === null) {
return null;
}

return (object) $row;
}

public function dataSeek($i)
public function data_seek($i)
{
return $this->dataCursor->seek($i);
}

public function numRows()
public function num_rows()
{
return $this->dataCursor->count();
}

public function numFields()
public function field_count()
{
return count($this->columnNames);
}

public function fetchField()
public function fetch_field()
{
$pos = $this->columnsIndex;
if (!isset($this->columnNames[$pos])) {
Expand All @@ -127,7 +127,7 @@ public function fetchField()
return $field;
}

public function fieldSeek($i)
public function field_seek($i)
{
if (!isset($this->columnNames[$i])) {
return false;
Expand Down
10 changes: 5 additions & 5 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function field_count()
return $this->set_error("Unable to perform a field_count without a prepare");
}

return $this->result->numFields();
return $this->result->field_count();
}

public function data_seek($offset)
Expand All @@ -95,7 +95,7 @@ public function data_seek($offset)
return $this->set_error("Unable to perform a data_seek without a store_result");
}

$result = $this->result->dataSeek($offset);
$result = $this->result->data_seek($offset);
return $result !== false;
}

Expand All @@ -107,7 +107,7 @@ public function num_rows()
return $this->set_error("Unable to perform a num_rows without a store_result");
}

return $this->result->numRows();
return $this->result->num_rows();
}

public function bind_param($types, &...$params)
Expand Down Expand Up @@ -187,7 +187,7 @@ function($match) use ($parameters, &$count) { return $parameters[$count++]; },
$result = $this->environment->query($realQuery);
if($result instanceof ResultSet) {
$this->result = $result;
$this->metadata = $result->createMetadata();
$this->metadata = $result->create_metadata();
return true;
} else if($result === false) {
return $this->set_error(trim($this->environment->error()));
Expand All @@ -206,7 +206,7 @@ public function fetch()
return $this->set_error("Unable to perform a fetch without a bind_result");
}

$result = $this->result->fetchRow();
$result = $this->result->fetch_row();
if($result !== null) {
$i = 0;
foreach ($result as $value) {
Expand Down
48 changes: 24 additions & 24 deletions tests/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ class ResultSetTest extends BaseTest
array(10, 'jon', 'doe', 'new york'),
);

public function testFetchAllEmpty()
public function testfetch_allEmpty()
{
$empty = array();
$results = new ResultSet(array('myColumn'), $empty);

$this->assertEquals($empty, $results->fetchAll(ResultSet::FETCH_NUM));
$this->assertEquals($empty, $results->fetchAll(ResultSet::FETCH_ASSOC));
$this->assertEquals($empty, $results->fetchAll(ResultSet::FETCH_BOTH));
$this->assertEquals($empty, $results->fetch_all(ResultSet::FETCH_NUM));
$this->assertEquals($empty, $results->fetch_all(ResultSet::FETCH_ASSOC));
$this->assertEquals($empty, $results->fetch_all(ResultSet::FETCH_BOTH));
}

public function testFetchAll()
public function testfetch_all()
{
$results = new ResultSet(self::$columns, self::$entries);

$this->assertEquals(self::$entries, $results->fetchAll(ResultSet::FETCH_NUM));
$this->assertEquals(self::$entries, $results->fetch_all(ResultSet::FETCH_NUM));

$assocResult = $results->fetchAll(ResultSet::FETCH_ASSOC);
$assocResult = $results->fetch_all(ResultSet::FETCH_ASSOC);
foreach ($assocResult as $entry) {
$this->assertEquals(array_combine(self::$columns, $entry), $entry);
}

$bothResult = $results->fetchAll(ResultSet::FETCH_BOTH);
$bothResult = $results->fetch_all(ResultSet::FETCH_BOTH);
foreach ($assocResult as $entry) {
$this->assertEquals(array_merge($entry, array_combine(self::$columns, $entry)), $entry);
}
Expand All @@ -53,7 +53,7 @@ public function testFetchAssoc()
$results = new ResultSet(self::$columns, self::$entries);

$i = 0;
while (($row = $results->fetchAssoc()) !== null) {
while (($row = $results->fetch_assoc()) !== null) {
$this->assertEquals(array_combine(self::$columns, self::$entries[$i++]), $row);
}
$this->assertEquals(count(self::$entries), $i);
Expand All @@ -64,7 +64,7 @@ public function testFetchRow()
$results = new ResultSet(self::$columns, self::$entries);

$i = 0;
while (($row = $results->fetchRow()) !== null) {
while (($row = $results->fetch_row()) !== null) {
$this->assertEquals(self::$entries[$i++], $row);
}
$this->assertEquals(count(self::$entries), $i);
Expand All @@ -75,7 +75,7 @@ public function testFetchBoth()
$results = new ResultSet(self::$columns, self::$entries);

$i = 0;
while (($row = $results->fetchBoth()) !== null) {
while (($row = $results->fetch_both()) !== null) {
$entry = self::$entries[$i++];
$this->assertEquals(array_merge($entry, array_combine(self::$columns, $entry)), $row);
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public function testFetchObject()
$results = new ResultSet(self::$columns, self::$entries);

$i = 0;
while (($row = $results->fetchObject()) !== null) {
while (($row = $results->fetch_object()) !== null) {
$this->assertEquals((object) array_combine(self::$columns, self::$entries[$i++]), $row);
}
$this->assertEquals(count(self::$entries), $i);
Expand All @@ -115,42 +115,42 @@ public function testDataSeekBad()
{
$results = new ResultSet(self::$columns, self::$entries);

$this->assertFalse($results->dataSeek(-1));
$this->assertFalse($results->dataSeek(count(self::$entries)));
$this->assertFalse($results->data_seek(-1));
$this->assertFalse($results->data_seek(count(self::$entries)));
}

public function testDataSeek()
{
$results = new ResultSet(self::$columns, self::$entries);

$success = $results->dataSeek(7);
$success = $results->data_seek(7);
$this->assertTrue($success !== null);

$row = $results->fetchRow();
$row = $results->fetch_row();
$this->assertEquals(self::$entries[7], $row);
}

public function testNumRows()
{
$emptySet = new ResultSet(self::$columns, array());
$this->assertEquals(0, $emptySet->numRows());
$this->assertEquals(0, $emptySet->num_rows());

$set = new ResultSet(self::$columns, self::$entries);
$this->assertEquals(count(self::$entries), $set->numRows());
$this->assertEquals(count(self::$entries), $set->num_rows());
}

public function testNumFields()
{
$set = new ResultSet(self::$columns, self::$entries);
$this->assertEquals(count(self::$columns), $set->numFields());
$this->assertEquals(count(self::$columns), $set->field_count());
}

public function testFetchField()
{
$results = new ResultSet(self::$columns, self::$entries);

$i = 0;
while (($field = $results->fetchField()) !== false) {
while (($field = $results->fetch_field()) !== false) {
$this->assertTrue(is_object($field));
$this->assertEquals(self::$columns[$i++], $field->name);
}
Expand All @@ -161,18 +161,18 @@ public function testFieldSeekBad()
{
$results = new ResultSet(self::$columns, self::$entries);

$this->assertFalse($results->fieldSeek(-1));
$this->assertFalse($results->fieldSeek(count(self::$columns)));
$this->assertFalse($results->field_seek(-1));
$this->assertFalse($results->field_seek(count(self::$columns)));
}

public function testFieldSeek()
{
$results = new ResultSet(self::$columns, self::$entries);

$success = $results->fieldSeek(2);
$success = $results->field_seek(2);
$this->assertTrue($success !== false);

$field = $results->fetchField();
$field = $results->fetch_field();
$this->assertEquals(self::$columns[2], $field->name);
}
}
2 changes: 1 addition & 1 deletion tests/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public function testDataSeek()
$expected = ['stephen', 'king', 'derry'];
$result = $statement->get_result();
$this->assertTrue($passed === true);
$this->assertEquals($expected, $result->fetchRow());
$this->assertEquals($expected, $result->fetch_row());
}

public function testClose()
Expand Down

0 comments on commit 8c3c558

Please sign in to comment.