Files
nibiru-framework.com/core/c/pdo.php
Stephan Kasdorf ca9ff28194 Update Version 0.4.0 beta 09.07.2018
- Bugfix on the form classes, now the select option is correctly set back.
- Update for the database adapter
- Improvement of the form elements, added onchange on the select boxes, the form tag now can have no element if needed.
- Implementation of the Postgres and MySQL Adapter with propper Namespacing.
- Minor bugfixing
2018-07-09 18:48:03 +02:00

248 lines
8.6 KiB
PHP

<?php
namespace Nibiru;
/**
* User - stephan
* Date - 01.02.17
* Time - 18:55
* @author - alllinux.de GbR
* @category - [PLEASE SPECIFIY]
* @license - BSD License
*/
final class Pdo extends Mysql implements IPdo
{
/**
* @param string $string
*
* @return array
*/
public static function query( $string = self::PLACE_NO_QUERY )
{
$query = parent::getInstance()->getConn()->query( $string );
while($row = $query->fetch())
{
$keys = array_keys($row);
for($i=0;sizeof($keys)>$i;$i += 2)
{
$row_values[] = $row[$keys[$i]];
$key_values[] = $keys[$i];
}
$result = array_combine($key_values, $row_values);
}
return $result;
}
public static function selectDatasetByFieldAndValue($tablename = self::PLACE_TABLE_NAME, $fieldAndValue = array() )
{
$result = parent::getInstance()->getConn()->query("SELECT * FROM " . $tablename . " WHERE " . $fieldAndValue['name'] . " = '" . $fieldAndValue['value'] . "';");
return $result->fetchAll();
}
public static function updateColumnByFieldWhere( $tablename = self::PLACE_TABLE_NAME,
$column_name = IMysql::PLACE_COLUMN_NAME,
$parameter_name = IMysql::PLACE_SEARCH_TERM,
$field_name = IMysql::PLACE_FIELD_NAME,
$where_value = IMysql::PLACE_WHERE_VALUE )
{
$statement = parent::getInstance()->getConn();
$query = "UPDATE " . $tablename . " SET " . $column_name . " = :" . $column_name . " WHERE " . $field_name . " = :". $field_name;
$insert = $statement->prepare($query);
$insert->bindParam( ':'.$column_name, $parameter_name );
$insert->bindParam( ':'.$field_name, $where_value );
$insert->execute();
}
public static function fetchRowInArrayById($tablename = self::PLACE_TABLE_NAME, $id = self::NO_ID )
{
$result = array();
$statement = parent::getInstance()->getConn();
$describe = $statement->query('DESC ' . $tablename);
$describe->execute();
$tableInformation = $describe->fetchAll( \PDO::FETCH_ASSOC );
foreach ( $tableInformation as $entry )
{
if( $entry["Key"] == IMysql::PLACE_PRIMARY_KEY )
{
$id_name = $entry["Field"];
}
}
$prepare = $statement->prepare("SELECT * FROM " . $tablename . " WHERE " . $id_name . " = :" . $id_name . ";");
$prepare->execute($id);
$fetchAll = $prepare->fetchAll();
$rowset = array_shift($fetchAll);
foreach(array_keys($rowset) as $entry)
{
if(is_string($entry))
{
$result[$entry] = $rowset[$entry];
}
}
return $result;
}
/**
* @desc selects the given table row by given parameter and column
* @param string $tablename
* @param string $column_name
* @param string $parameter_name
* @return mixed
*/
public static function fetchRowInArrayByWhere($tablename = IMysql::PLACE_TABLE_NAME,
$column_name = IMysql::PLACE_COLUMN_NAME,
$parameter_name = IMysql::PLACE_SEARCH_TERM)
{
$statement = parent::getInstance()->getConn();
$prepare = $statement->prepare("SELECT * FROM " . $tablename . " WHERE " . $column_name . " = :" . $column_name . ";");
$prepare->bindParam(":".$column_name, $parameter_name, \PDO::PARAM_STR);
$prepare->execute();
$r = $prepare->fetchAll();
$rowset = array_shift( $r );
foreach(array_keys($rowset) as $entry)
{
if(is_string($entry))
{
$result[$entry] = $rowset[$entry];
}
}
return $result;
}
public static function getLastInsertedID()
{
// TODO: Implement getLastInsertedID() method.
}
public static function fetchTableAsArray( $tablename = self::PLACE_TABLE_NAME )
{
$statement = parent::getInstance()->getConn()->query('SElECT * FROM ' . $tablename);
$statement->execute();
$result = $statement->fetchAll( \PDO::FETCH_ASSOC );
return $result;
}
/**
* @desc will insert the array with fieldnames into the database, if the last parameter is set it should be a string containing the
* fieldname that should be encrypted
* @param string $tablename
* @param string $array_name
* @param bool $encrypted
*/
public static function insertArrayIntoTable( $tablename = IMysql::PLACE_TABLE_NAME, $array_name = IMysql::PLACE_ARRAY_NAME, $encrypted = IMysql::PLACE_DES_ENCRYPT )
{
$statement = parent::getInstance()->getConn();
if(is_array($array_name))
{
if(array_key_exists(0, $array_name))
{
foreach( $array_name as $entry )
{
$field_names = array_keys( $entry );
$numItems = sizeof( $field_names );
$i = 0;
$row = " ( ";
foreach( $field_names as $field )
{
if( ++$i === $numItems )
{
$row .= $field;
}
else
{
$row .= $field . ", ";
}
}
$i = 0;
$row .= " ) VALUES ( ";
foreach ( $field_names as $field )
{
if( ++$i === $numItems )
{
if($field == $encrypted)
{
$row .= "DES_ENCRYPT(:".$encrypted.", :key)";
}
else
{
$row .= ":" . $field ;
}
}
else
{
if($field == $encrypted)
{
$row .= "DES_ENCRYPT(:".$encrypted.", :key), ";
}
else
{
$row .= ":" . $field . ", ";
}
}
}
$row .= " )";
$query = $statement->prepare('INSERT INTO ' . $tablename . $row);
if($encrypted)
{
$array_name['key'] = Config::getInstance()->getConfig()[View::NIBIRU_SECURITY]["password_hash"];
}
$query->execute( $entry );
}
}
else
{
$field_names = array_keys($array_name);
$numItems = sizeof($field_names);
$i = 0;
$row = " ( ";
foreach( $field_names as $field )
{
if( ++$i === $numItems )
{
$row .= $field;
}
else
{
$row .= $field . ", ";
}
}
$row .= " ) VALUES ( ";
$i = 0;
foreach ( $field_names as $field )
{
if( ++$i === $numItems )
{
if($field == $encrypted)
{
$row .= "DES_ENCRYPT(:".$encrypted.", :key)";
}
else
{
$row .= ":" . $field ;
}
}
else
{
if($field == $encrypted)
{
$row .= "DES_ENCRYPT(:".$encrypted.", :key), ";
}
else
{
$row .= ":" . $field . ", ";
}
}
}
$row .= " )";
$query = $statement->prepare('INSERT INTO ' . $tablename . $row);
if($encrypted)
{
$array_name['key'] = Config::getInstance()->getConfig()[View::NIBIRU_SECURITY]["password_hash"];
}
$query->execute( $array_name );
}
}
}
}