Skip to content

Commit

Permalink
Added ability to stream video files
Browse files Browse the repository at this point in the history
Added ability to retrieve results in camel case format
Sub folder calculation fixes
Moved default srcs to src sub folder to avoid server confusion
  • Loading branch information
andrevanzuydam committed Jun 28, 2020
1 parent 55ca289 commit b121ba4
Show file tree
Hide file tree
Showing 11 changed files with 425 additions and 146 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Created by .ignore support plugin (hsz.mobi)
.idea
.vscode
/vendor/
/cache/
/assets/index.twig
Expand Down
77 changes: 68 additions & 9 deletions Tina4/DataRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
* Notes: A record is a single part of the result set
*/
namespace Tina4;
use JsonSerializable;

/**
* Class DataRecord
* @package Tina4
*/
class DataRecord
class DataRecord implements JsonSerializable
{
private $original;

/**
* This tests a string result from the DB to see if it is binary or not so it gets base64 encoded on the result
* @param $string
* @return bool
*/
public function isBinary($string):bool
{
$isBinary=false;
Expand All @@ -28,6 +34,42 @@ public function isBinary($string):bool
return $isBinary;
}

/**
* Gets a proper object name for returning back data
* @param string $name Improper object name
* @param array $fieldMapping Field mapping to map fields
* @return string Proper object name
*/
function getObjectName($name, $fieldMapping=[])
{
if (!empty($fieldMapping) && $fieldMapping[$name]) {
return $fieldMapping[$name];
} else {
$fieldName = "";
if (strpos($name, "_") !== false) {
$name = strtolower($name);
for ($i = 0; $i < strlen($name); $i++) {
if ($name[$i] === "_") {
$i++;
$fieldName .= strtoupper($name[$i]);
} else {
$fieldName .= $name[$i];
}
}
} else {
for ($i = 0; $i < strlen($name); $i++) {
if ($name[$i] !== strtolower($name[$i])) {
$fieldName .= "_" . strtolower($name[$i]);
} else {
$fieldName .= $name[$i];
}
}
}
return $fieldName;
}
}


/**
* DataRecord constructor Converts array to object
* @param array $record Array of records
Expand All @@ -36,28 +78,40 @@ function __construct($record)
{
if (!empty($record)) {
$this->original = (object)$record;

foreach ($record as $column => $value) {
if ($this->isBinary($value)) {
$value = \base64_encode($value);
$this->original->{$column} = $value;
}
$columnName = $column;
$this->$columnName = $value;
$columnName = strtoupper($column);
$this->$columnName = $value;
$this->{$column} = $value;
}
}
}

/**
* Transform to a camel case result
*/
function transformObject () {
$object = (object)[];
foreach ($this->original as $column => $value) {
$columnName = $this->getObjectName($column);
$object->$columnName = $value;
}

//print_r ($record);
return $object;
}

/**
* Converts array to object
* @param bool $original Whether to get the result as original field names
* @return object
*/
function asObject () {
return $this->original;
function asObject ($original=false) {
if ($original) {
return $this->original;
} else {
return $this->transformObject();
}
}

function asJSON () {
Expand All @@ -75,4 +129,9 @@ function byName ($name) {
return $this->$columnName;
}
}

public function jsonSerialize()
{
return json_encode($this->original,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
}
}
55 changes: 40 additions & 15 deletions Tina4/DataResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ class DataResult implements JsonSerializable
/**
* @var resource Records returned from query
*/
private $records;
public $records;

/**
* @var array Fields in the table and their types
*/
private $fields;
public $fields;

/**
* @var integer Number of records
*/
private $noOfRecords;
public $noOfRecords;

/**
* @var integer Data row offset
*/
private $offSet;
public $offSet;

/**
* @var DataError Database error
*/
private $error;
public $error;

/**
* DataResult constructor.
Expand Down Expand Up @@ -92,24 +92,53 @@ function fields()

/**
* Converts returned results as array of objects
* @param boolean $original Original field name
* @return array|null
* @example examples\exampleDataResultRecords.php
*/
function records()
function records($original=false)
{
$results = null;
if (!empty($this->records)) {
foreach ($this->records as $rid => $record) {
$results[] = $record->asObject();
$results[] = $record->asObject($original);
}
}

return $results;
}

/**
* Gets an array of objects
* @param boolean $original Original field names
* @return array|mixed
*/
public function asObject($original=false) {
return $this->records($original);
}


/**
* Gets an array of objects in the original form
* @return array|mixed
*/
public function asOriginal() {
return $this->records(true);
}

/**
* Gets the result as a generic array without the extra object information
* @param boolean $original Original field names
* @return array
*/
public function asArray($original=false) {
//$records = $this->jsonSerialize();
$result = [];
foreach ($this->records() as $id => $record) {
$result[] = (array)$record;
}
return $result;
}

/**
* Converts array of records to array of objects
* @return false|string
Expand All @@ -128,14 +157,10 @@ function __toString()
}
}





if (!empty($results)) {
return json_encode((object)["recordsTotal" => $this->noOfRecords, "recordsFiltered" => $this->noOfRecords, "data" => $results, "error" => null]);
return json_encode((object)["recordsTotal" => $this->noOfRecords, "recordsFiltered" => $this->noOfRecords, "fields" => $this->fields, "data" => $results, "error" => null]);
} else {
return json_encode((object)["recordsTotal" => 0, "recordsFiltered" => 0, "data" => [], "error" => $this->error->getErrorText()]);
return json_encode((object)["recordsTotal" => 0, "recordsFiltered" => 0, "fields" => [], "data" => [], "error" => $this->error->getErrorText()]);
}

}
Expand All @@ -156,7 +181,7 @@ public function jsonSerialize() {
}
}

return (object)["recordsTotal" => $this->noOfRecords, "recordsFiltered" => $this->noOfRecords, "data" => $results, "error" => $this->getError()];
return (object)["recordsTotal" => $this->noOfRecords, "recordsFiltered" => $this->noOfRecords, "fields" => $this->fields, "data" => $results, "error" => $this->getError()];
}

/**
Expand Down
5 changes: 1 addition & 4 deletions Tina4/DataSQLite3.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function native_exec() {
}

public function native_error() {
return (new DataError( $this->dbh->lastErrorCode(), $this->dbh->lastErrorMsg()));
return (new \Tina4\DataError( $this->dbh->lastErrorCode(), $this->dbh->lastErrorMsg()));
}

public function native_fetch($sql="", $noOfRecords=10, $offSet=0) {
Expand All @@ -86,7 +86,6 @@ public function native_fetch($sql="", $noOfRecords=10, $offSet=0) {
$fid = 0;
$fields = [];
foreach ($records[0] as $field => $value) {

$fields[] = (new DataField($fid, $recordCursor->columnName($fid), $recordCursor->columnName($fid), $recordCursor->columnType($fid)));
$fid++;
}
Expand All @@ -98,8 +97,6 @@ public function native_fetch($sql="", $noOfRecords=10, $offSet=0) {

$error = $this->error();



return (new DataResult($records, $fields, $countRecords, $offSet, $error));
}

Expand Down
2 changes: 1 addition & 1 deletion Tina4/DebugLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DebugLog
{
static function message($message, $debugType=DEBUG_NONE){

if($debugType == DEBUG_NONE) return;
if($debugType == DEBUG_NONE || TINA4_DEBUG === false) return;

if (is_array($message)|| is_object($message)) {
$message = print_r ($message, 1);
Expand Down
Loading

0 comments on commit b121ba4

Please sign in to comment.