public static function login($loginName, $loginPswd) { session_unset(); $dbc = DB::getDBConnection(); $query = "SELECT * FROM User WHERE " . loginName . " = ? AND " . loginPswd . " = ? "; $stmt = $dbc->prepare($query); $stmt->bind_param("ss", $loginName, $loginPswd); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); if ($result->num_rows <= 0) { return false; } $userNo = getUserNoByLoginName($loginName); if (!isset($userNo)) { return false; } if (session_status() != PHP_SESSION_ACTIVE) { session_start(); } $_SESSION["type"] = getUserType($userNo); UserControl::$type = getUserType($userNo); $_SESSION[userNo] = $userNo; $query = "UPDATE User SET " . loginSession . " = '" . session_id() . "' " . "WHERE " . userNo . " = '{$userNo}'"; if (DB::query($query)) { return true; } }
<?php require_once 'DB.php'; // class User { // public static $foo = 'this is a static member'; // public $name = 'Fred Green'; // } // echo User::$foo, "\n"; // $user = new User; // print_r($user); $db = DB::getDBConnection(); $statement = $db->prepare('SELECT * FROM junk'); $statement->execute();
public static function rollback() { return DB::getDBConnection()->rollBack(); }
/** * * @return array|Hypersistence */ public function execute() { $this->totalRows = 0; $this->totalPages = 0; $this->resultList = array(); $classThis = Engine::init($this->object); $tables = array(); $fields = array(); $fieldsNoAlias = array(); $objectRefs = array(); $class = $classThis; //When it is a many to many relation. if ($this->property && $this->object) { $srcClass = Engine::init($this->srcObject); $srcPk = Engine::getPk($srcClass); $srcGet = 'get' . $srcPk['var']; $srcId = $this->srcObject->{$srcGet}(); $pk = Engine::getPk($class); $tables[] = '`' . $this->property['joinTable'] . '`'; $filter = $this->property['joinTable'] . '.' . $this->property['joinColumn'] . ' = :' . $this->property['joinTable'] . '_' . $this->property['joinColumn']; $this->filters[md5($filter)] = $filter; $this->bounds[':' . $this->property['joinTable'] . '_' . $this->property['joinColumn']] = $srcId; $filter = $this->property['joinTable'] . '.' . $this->property['inverseJoinColumn'] . ' = ' . $this->chars[$pk['i']] . '.' . $pk['column']; $this->filters[md5($filter)] = $filter; } $i = 0; while ($class != 'Hypersistence') { $alias = $this->chars[$i]; $class = ltrim($class, '\\'); $joinTable = '`' . Engine::$map[$class]['table'] . '` ' . $alias; if ($i == 0) { $tables[] = $joinTable; } else { $parentAlias = $this->chars[$i]; $pk = Engine::getPk($class); $join = 'join ' . $joinTable . ' on(' . $lastAlias . '.' . Engine::$map[$lastClass]['joinColumn'] . ' = ' . $parentAlias . '.' . $pk['column'] . ')'; $this->joins[md5($join)] = $join; } $lastClass = $class; $lastAlias = $alias; foreach (Engine::$map[$class]['properties'] as $p) { //if($p['relType'] != Engine::MANY_TO_MANY){ if ($p['relType'] != Engine::ONE_TO_MANY && $p['relType'] != Engine::MANY_TO_MANY) { $fields[] = $alias . '.' . $p['column'] . ' as ' . $alias . '_' . $p['column']; $fieldsNoAlias[] = $alias . '.' . $p['column']; } $get = 'get' . $p['var']; $value = $this->object->{$get}(); if (!is_null($value)) { if ($value instanceof \Hypersistence) { if ($p['relType'] == Engine::MANY_TO_ONE || $p['relType'] == Engine::ONE_TO_MANY || $p['relType'] == Engine::MANY_TO_MANY) { $this->joinFilter($class, $p, $value, $alias); } } else { if (is_numeric($value)) { $filter = $alias . '.' . $p['column'] . ' = :' . $alias . '_' . $p['column']; $this->filters[md5($filter)] = $filter; $this->bounds[':' . $alias . '_' . $p['column']] = $value; } else { $filter = $alias . '.' . $p['column'] . ' like :' . $alias . '_' . $p['column']; $this->filters[md5($filter)] = $filter; $this->bounds[':' . $alias . '_' . $p['column']] = $this->searchMode($p, $value); } } } //} } $class = Engine::$map[$class]['parent']; $i++; } if (count($this->filters)) { $where = ' where ' . implode(' and ', $this->filters); } else { $where = ''; } if (count($this->joins) > 0) { $count = 'distinct ifnull(' . implode(', \'\'),ifnull(', $fieldsNoAlias) . ', \'\')'; } else { $count = '*'; } $sql = 'select count(' . $count . ') as total from ' . implode(',', $tables) . ' ' . implode(' ', $this->joins) . $where; $bounds = array(); foreach ($this->bounds as $key => $val) { if ($key != ':offset' && $key != ':limit') { $bounds[$key] = $val; } } if ($stmt = DB::getDBConnection()->prepare($sql)) { if ($stmt->execute($bounds) && $stmt->rowCount() > 0) { $result = $stmt->fetchObject(); $this->totalRows = $result->total; $this->totalPages = $this->rows > 0 ? ceil($this->totalRows / $this->rows) : 1; } else { return array(); } } $offset = $this->page > 0 ? ($this->page - 1) * $this->rows : $this->offset; $this->bounds[':offset'] = array($offset, DB::PARAM_INT); $this->bounds[':limit'] = array(intval($this->rows > 0 ? $this->rows : $this->totalRows), DB::PARAM_INT); if (count($this->orderBy)) { $orderBy = ' order by ' . implode(',', $this->orderBy); } else { $orderBy = ''; } $sql = 'select distinct ' . implode(',', $fields) . ' from ' . implode(',', $tables) . ' ' . implode(' ', $this->joins) . $where . $orderBy . ' LIMIT :limit OFFSET :offset'; if ($stmt = DB::getDBConnection()->prepare($sql)) { if ($stmt->execute($this->bounds) && $stmt->rowCount() > 0) { while ($result = $stmt->fetchObject()) { $class = $classThis; $object = new $class(); $i = 0; while ($class != '' && $class != 'Hypersistence') { $alias = $this->chars[$i]; $class = ltrim($class, '\\'); foreach (Engine::$map[$class]['properties'] as $p) { $var = $p['var']; $set = 'set' . $var; $get = 'get' . $var; if ($p['relType'] != Engine::MANY_TO_MANY && $p['relType'] != Engine::ONE_TO_MANY) { $column = $alias . '_' . $p['column']; if (isset($result->{$column})) { if (isset($objectRefs[$column])) { $object->{$set}($objectRefs[$column]); } else { if (method_exists($object, $set)) { if ($p['relType'] == Engine::MANY_TO_ONE) { $objClass = $p['itemClass']; Engine::init($objClass); $pk = Engine::getPk($objClass); if ($pk) { $objVar = $pk['var']; $objSet = 'set' . $objVar; $obj = new $objClass(); $obj->{$objSet}($result->{$column}); $object->{$set}($obj); if ($p['loadType'] == 'eager') { $obj->load(); } } } else { if ($p['dateTime']) { if (!is_null($result->{$column})) { $object->{$set}(new DateTime($result->{$column})); } else { $object->{$set}(null); } } else { $object->{$set}($result->{$column}); } } } } } } else { if ($p['relType'] == Engine::ONE_TO_MANY) { $objClass = $p['itemClass']; Engine::init($objClass); $objClass = ltrim($objClass, '\\'); $objFk = Engine::getPropertyByColumn($objClass, $p['joinColumn']); if ($objFk) { $obj = new $objClass(); $objSet = 'set' . $objFk['var']; $obj->{$objSet}($object); $search = $obj->search(); if ($p['loadType'] == 'eager') { $search = $search->execute(); } $object->{$set}($search); } } else { if ($p['relType'] == Engine::MANY_TO_MANY) { $objClass = $p['itemClass']; $obj = new $objClass(); $search = new QueryBuilder($obj, $object, $p); if ($p['loadType'] == 'eager') { $search = $search->execute(); } $object->{$set}($search); } } } } $class = Engine::$map[$class]['parent']; $i++; } $this->resultList[] = $object; } } } return $this->resultList; }
public function __construct($request, $origin) { parent::__construct($request); //$args is is an array of arguments after the endpoint $this->db = DB::getDBConnection(); }