<?php header('Content-Type: text/html; charset=utf-8'); include 'class/QueryStatement.php'; include 'class/ErrorHandler.php'; include 'class/DbConnection.php'; /*$stm=DbConnection::query('SELECT * FROM content'); while ($tmp=$stm->fetch()){ var_dump($tmp); } $stm=DbConnection::query('SELECT * FROM content'); var_dump('==========',$stm->fetchAll()); $stm=DbConnection::query('SELECT * FROM content 000'); var_dump(DbConnection::getErrorMsg()); $sql='INSERT INTO content (str) VALUES (\''.time().'\')'; $rst=DbConnection::exec($sql); $id=DbConnection::getLastId(); var_dump($rst,$id);*/ $arr = array(array('str' => 'a'), array('str' => 'b'), array('str' => 'c'), array('str' => 'd')); var_dump(DbConnection::insertArrs('content', $arr)); $data = array('str' => 'abcde'); var_dump(DbConnection::insert('content', $data)); $updateData = array('str' => '333'); var_dump(DbConnection::update('content', $data, 'WHERE id=4'));
function flush() { foreach ($this->toDelete as $e) { $table = @static::getClassAnnotation(get_class($e))['ORM_Table']; if ($table && strlen($table) > 0) { $primaryColumn = static::findPrimaryColumn(get_class($e)); $primaryProp = static::findPrimaryProperty(get_class($e)); $primaryProp = new \ReflectionProperty(get_class($e), $primaryProp); $primaryProp->setAccessible(true); $primaryValue = $primaryProp->getValue($e); $primaryProp->setAccessible(false); unset($primaryProp); $this->connection->delete($table, [$primaryColumn => $primaryValue]); } } $this->toDelete = array(); /** * @var $e Model */ foreach ($this->entities as $e) { if ($e->getExists()) { $class = get_class($e); $table = @static::getClassAnnotation($class)['ORM_Table']; if ($table && strlen($table) > 0) { $primaryColumn = static::findPrimaryColumn($class); $primaryProp = static::findPrimaryProperty($class); $primaryProp = new \ReflectionProperty($class, $primaryProp); $primaryProp->setAccessible(true); $primaryValue = $primaryProp->getValue($e); $primaryProp->setAccessible(false); $fields = []; $meta = static::getPropertiesMetadata($class); foreach ($meta as $property => $data) { if ($property == $primaryProp->getName()) { continue; } $p = new \ReflectionProperty($class, $property); $p->setAccessible(true); if (isset($data['ORM_Column'])) { if (!isset($data['ORM_Type'])) { throw new \Exception('ORM Type is not set.'); } if ($data['ORM_Type'] == 'numeric') { $fields[$data['ORM_Column']] = $p->getValue($e); if (strlen($fields[$data['ORM_Column']]) <= 0) { $fields[$data['ORM_Column']] = 'NULL'; } } elseif ($data['ORM_Type'] == 'string') { if (is_null($p->getValue($e))) { $fields[$data['ORM_Column']] = 'NULL'; } else { $val = $this->connection->escape($p->getValue($e)); $fields[$data['ORM_Column']] = '\'' . $val . '\''; } } elseif ($data['ORM_Type'] == 'bool') { if (is_null($p->getValue($e))) { $fields[$data['ORM_Column']] = 'NULL'; } else { $fields[$data['ORM_Column']] = $p->getValue($e) ? '1' : '0'; } } elseif ($data['ORM_Type'] == 'date') { if (is_null($p->getValue($e))) { $fields[$data['ORM_Column']] = 'NULL'; } else { $val = $p->getValue($e); if ($val instanceof \DateTime) { $val = $val->format('Y-m-d'); } $fields[$data['ORM_Column']] = '\'' . $val . '\''; } } else { throw new \Exception('Wrong ORM Type: "' . $data['ORM_Type'] . '"'); } } elseif (isset($data['ORM_ManyToOne'])) { $relData = static::getRelationData($class, $p->getName(), 'ORM_ManyToOne'); $targetClass = 'Src\\Models\\' . $relData['targetClass']; $mappedByColumn = $relData['mappedBy']; $targetPrimaryName = static::findPrimaryProperty($targetClass); $targetPrimaryProperty = new \ReflectionProperty($targetClass, $targetPrimaryName); $targetPrimaryProperty->setAccessible(true); $obj = $p->getValue($e); if ($obj instanceof RelationModel) { $obj = $obj->get(); } $targetPrimaryValue = $targetPrimaryProperty->getValue($obj); $fields[$mappedByColumn] = $targetPrimaryValue; } elseif (isset($data['ORM_OneToMany'])) { // TODO: if cascade } else { throw new \Exception('ORM annotation is not found.'); } unset($p); } $this->connection->update($fields, $table, [$primaryColumn => $primaryValue]); unset($primaryProp); unset($meta); } } else { // TODO: Добавление всех новых одним запросом $class = get_class($e); $table = @static::getClassAnnotation($class)['ORM_Table']; if ($table && strlen($table) > 0) { $fields = []; $values = []; $values[0] = []; $meta = static::getPropertiesMetadata($class); $primaryProp = static::findPrimaryProperty($class); foreach ($meta as $property => $data) { if ($property == $primaryProp) { continue; } $p = new \ReflectionProperty($class, $property); $p->setAccessible(true); if (isset($data['ORM_Column'])) { if (!isset($data['ORM_Type'])) { throw new \Exception('ORM Type is not set.'); } if ($data['ORM_Type'] == 'numeric') { $fields[] = $data['ORM_Column']; $val = $p->getValue($e); $values[0][] = strlen($val) > 0 ? $val : 'NULL'; } elseif ($data['ORM_Type'] == 'string') { if (is_null($p->getValue($e))) { $fields[] = $data['ORM_Column']; $values[0][] = 'NULL'; } else { $val = $this->connection->escape($p->getValue($e)); $fields[] = $data['ORM_Column']; $values[0][] = '\'' . $val . '\''; } } elseif ($data['ORM_Type'] == 'bool') { $fields[] = $data['ORM_Column']; //var_dump($p->getValue($e)); if (is_null($p->getValue($e))) { $values[0][] = 'NULL'; } else { $values[0][] = $p->getValue($e) ? '1' : '0'; } } elseif ($data['ORM_Type'] == 'date') { $fields[] = $data['ORM_Column']; if (is_null($p->getValue($e))) { $values[0][] = 'NULL'; } else { $val = $p->getValue($e); if ($val instanceof \DateTime) { $val = $val->format('Y-m-d'); } $values[0][] = '\'' . $val . '\''; } } else { throw new \Exception('Wrong ORM Type: "' . $data['ORM_Type'] . '"'); } } elseif (isset($data['ORM_ManyToOne'])) { $relData = static::getRelationData($class, $p->getName(), 'ORM_ManyToOne'); $targetClass = 'Src\\Models\\' . $relData['targetClass']; $mappedByColumn = $relData['mappedBy']; $targetPrimaryName = static::findPrimaryProperty($targetClass); $targetPrimaryProperty = new \ReflectionProperty($targetClass, $targetPrimaryName); $targetPrimaryProperty->setAccessible(true); $obj = $p->getValue($e); if ($obj instanceof RelationModel) { $obj = $obj->get(); } $targetPrimaryValue = $targetPrimaryProperty->getValue($obj); $fields[] = $mappedByColumn; $values[0][] = $targetPrimaryValue; } elseif (isset($data['ORM_OneToMany'])) { // TODO: if cascade } else { throw new \Exception('ORM annotation is not found.'); } unset($p); } /*var_dump('--------------'); var_dump($table); var_dump($fields); var_dump($values);*/ $this->connection->insert($table, $fields, $values); $primaryName = static::findPrimaryProperty($class); $primaryProperty = new \ReflectionProperty($class, $primaryName); $primaryProperty->setAccessible(true); $primaryValue = $this->connection->query('SELECT LAST_INSERT_ID() as :last_id;'); $primaryValue = $primaryValue[0]['last_id']; $primaryProperty->setValue($e, $primaryValue); unset($meta); unset($primaryProperty); } } } $this->entities = []; }
<?php include_once "classes/DbConnection.class.php"; $db = new DbConnection(); //create a new DbConnection object if (isset($_POST['p_id'])) { $p_id = $_POST['p_id']; $comment = $_POST['comment']; $insert_comment_sql = "INSERT INTO blog_comment \n\t(comment_id,comment,fk_post_id)\n\tVALUES ('','{$comment}','{$p_id}')"; //insert the new post using the $db object if ($db->insert($insert_comment_sql)) { header("location:blog.php"); } } else { header("location:blog.php"); }
<?php if (isset($_GET['p_id'])) { $p_id = $_GET['p_id']; $r = $_GET['rating']; } else { header("Location:blog.php"); } include_once "classes/DbConnection.class.php"; $db = new DbConnection(); $insert_sql = "INSERT INTO blog_rating (r_id,rating, fk_p_id)\nVALUES ('',{$r},'{$p_id}')"; if ($result = $db->insert($insert_sql)) { header("Location:blog.php"); } else { echo mysql_error(); }
<?php include_once "classes/DbConnection.class.php"; $db = new DbConnection(); if (isset($_POST['new_post'])) { $post = $_POST['new_post']; $insert_post_sql = "INSERT INTO blog_post\n\t\t\t\t\t(post_id, post, post_date)\n\t\t\t\t\tVALUES (null, '{$post}', CURDATE())"; //insert the new post if ($db->insert($insert_post_sql)) { header("location: admin.php"); } }
/** * @throws \Exception * @return Model[]|int */ public function query() { switch ($this->queryType) { case QueryBuilder::SELECT: $this->class = 'Src\\Models\\' . $this->class; $meta = static::getClassAnnotation($this->class); if (!isset($meta['ORM_Entity'])) { throw new \Exception('Class "' . $this->class . '" is not mapped with ORM.'); } $table = $meta['ORM_Table']; $rawResult = $this->conn->select(['*'], [$table], $this->whereClause, $this->post); $models = []; foreach ($rawResult as $raw) { $models[] = $this->hydrateData($raw, $this->class, $this->conn, $this->app); } return $models; break; case QueryBuilder::UPDATE: /** * @var $model Model */ $affected = 0; foreach ($this->modelsToUpdate as $model) { if (!$model->getExists()) { continue; } $class = get_class($model); $table = @static::getClassAnnotation($class)['ORM_Table']; if ($table && strlen($table) > 0) { $primaryColumn = static::findPrimaryColumn($class); $primaryProp = static::findPrimaryProperty($class); $primaryProp = new \ReflectionProperty($class, $primaryProp); $primaryProp->setAccessible(true); $primaryValue = $primaryProp->getValue($model); $primaryProp->setAccessible(false); $fields = []; $meta = static::getPropertiesMetadata($class); foreach ($meta as $property => $data) { if ($property == $primaryProp->getName()) { continue; } $p = new \ReflectionProperty($class, $property); $p->setAccessible(true); if (isset($data['ORM_Column'])) { if (!isset($data['ORM_Type'])) { throw new \Exception('ORM Type is not set.'); } if ($data['ORM_Type'] == 'numeric') { $fields[$data['ORM_Column']] = $p->getValue($model); } elseif ($data['ORM_Type'] == 'string') { $fields[$data['ORM_Column']] = '\'' . $p->getValue($model) . '\''; } else { throw new \Exception('Wrong ORM Type: "' . $data['ORM_Type'] . '"'); } } elseif (isset($data['ORM_ManyToOne'])) { $relData = static::getRelationData($class, $p->getName(), 'ORM_ManyToOne'); $targetClass = $relData['targetClass']; $mappedByColumn = $relData['mappedBy']; $targetPrimaryName = static::findPrimaryProperty($targetClass); $targetPrimaryProperty = new \ReflectionProperty($targetClass, $targetPrimaryName); $targetPrimaryProperty->setAccessible(true); $targetPrimaryValue = $targetPrimaryProperty->getValue($p->getValue($model)); $fields[$mappedByColumn] = $targetPrimaryValue; } elseif (isset($data['ORM_OneToMany'])) { // TODO: if cascade } else { throw new \Exception('ORM annotation is not found.'); } unset($p); } $affected += $this->conn->update($fields, $table, [$primaryColumn => $primaryValue]); unset($primaryProp); unset($meta); } } return $affected; break; case QueryBuilder::INSERT: /** * @var $model Model */ $affected = 0; foreach ($this->modelsToInsert as $model) { //if ($model->getExists()) continue; // TODO: Добавление всех новых одним запросом $class = get_class($model); $table = @static::getClassAnnotation($class)['ORM_Table']; if ($table && strlen($table) > 0) { $fields = []; $values = []; $values[0] = []; $meta = static::getPropertiesMetadata($class); $primaryProp = static::findPrimaryProperty($class); foreach ($meta as $property => $data) { if ($property == $primaryProp) { continue; } $p = new \ReflectionProperty($class, $property); $p->setAccessible(true); if (isset($data['ORM_Column'])) { if (!isset($data['ORM_Type'])) { throw new \Exception('ORM Type is not set.'); } if ($data['ORM_Type'] == 'numeric') { $fields[] = $data['ORM_Column']; $values[0][] = $p->getValue($model); } elseif ($data['ORM_Type'] == 'string') { $fields[] = $data['ORM_Column']; $values[0][] = '\'' . $p->getValue($model) . '\''; } else { throw new \Exception('Wrong ORM Type: "' . $data['ORM_Type'] . '"'); } } elseif (isset($data['ORM_ManyToOne'])) { $relData = static::getRelationData($class, $p->getName(), 'ORM_ManyToOne'); $targetClass = 'Src\\Models\\' . $relData['targetClass']; $mappedByColumn = $relData['mappedBy']; $targetPrimaryName = static::findPrimaryProperty($targetClass); $targetPrimaryProperty = new \ReflectionProperty($targetClass, $targetPrimaryName); $targetPrimaryProperty->setAccessible(true); $targetPrimaryValue = $targetPrimaryProperty->getValue($p->getValue($model)); $fields[] = $mappedByColumn; $values[0][] = $targetPrimaryValue; } elseif (isset($data['ORM_OneToMany'])) { // TODO: if cascade } else { throw new \Exception('ORM annotation is not found.'); } unset($p); } $affected += $this->conn->insert($table, $fields, $values); unset($meta); } } return $affected; break; case QueryBuilder::DELETE: $affected = 0; foreach ($this->modelsToDelete as $model) { if (!$model->getExists()) { continue; } $c = get_class($model); $table = @static::getClassAnnotation($c)['ORM_Table']; if ($table && strlen($table) > 0) { $primaryColumn = static::findPrimaryColumn($c); $primaryProp = static::findPrimaryProperty($c); $primaryProp = new \ReflectionProperty($c, $primaryProp); $primaryProp->setAccessible(true); $primaryValue = $primaryProp->getValue($model); $primaryProp->setAccessible(false); unset($primaryProp); $affected += $this->conn->delete($table, [$primaryColumn => $primaryValue]); } } return $affected; break; default: throw new \Exception('No query type specified.'); } }