Skip to content

Commit 48d9847

Browse files
committed
Examples enhanced
1 parent 7022029 commit 48d9847

File tree

10 files changed

+242
-71
lines changed

10 files changed

+242
-71
lines changed

core/query/Query.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Query implements iQuery {
1313
* @var string the query
1414
*/
1515
protected $query;
16+
protected $type;
1617

1718
/**
1819
* @var PDO the connection
@@ -22,6 +23,9 @@ class Query implements iQuery {
2223
public function __construct($query) {
2324
$this->pdo = Bootstrap::getPDO();
2425
$this->query = trim($query);
26+
27+
$firstWord = explode(" ", $this->query);
28+
$this->type = strtolower($firstWord[0]);
2529
}
2630

2731
/**
@@ -32,9 +36,11 @@ public function __construct($query) {
3236
* @return array A list of objects.
3337
*/
3438
public function getResultList() {
35-
$statement = $this->pdo->query($this->query);
36-
37-
return $this->getResultListInternal($statement);
39+
if ($this->type == "select") {
40+
return $this->getResultListInternal($this->pdo->query($this->query));
41+
} else {
42+
return $this->getSingeResult();
43+
}
3844
}
3945

4046
/**
@@ -44,14 +50,30 @@ public function getResultList() {
4450
*/
4551
public function getSingeResult() {
4652
$statement = $this->pdo->query($this->query); # TODO: Prepared statement
47-
48-
if ($statement->columnCount() == 1) {
49-
return $statement->fetchColumn();
50-
} else {
51-
return $this->getResultListInternal($statement)[0];
53+
54+
switch ($this->type) {
55+
case 'select': {
56+
if ($statement->columnCount() == 1) {
57+
$result = $statement->fetchColumn();
58+
} else {
59+
$result = $this->getResultListInternal($statement)[0];
60+
}
61+
break;
62+
}
63+
case 'update':
64+
case 'delete':
65+
$result = $statement->rowCount();
66+
break;
67+
case 'insert':
68+
$result = $this->pdo->lastInsertId();
69+
break;
70+
default:
71+
break;
5272
}
73+
74+
return $result;
5375
}
54-
76+
5577
/**
5678
* Returns a list of objects. The kind of object depends on the $full_qualified_classname
5779
* parameter.

core/query/TypedQuery.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,13 @@ class TypedQuery extends Query {
2020
protected $metaDataMap;
2121

2222
public function __construct($query, $fullyQualifiedClassname) {
23-
$firstWord = explode(" ", trim($query));
24-
$firstWord = strtolower($firstWord[0]);
23+
parent::__construct($query);
2524

2625
# TODO: exclude also DDLs
27-
if (in_array($firstWord, array("update", "insert", "delete"))) {
26+
if (in_array($this->type, array("update", "insert", "delete"))) {
2827
throw new DomainException("Cannot be an UPDATE- or INSERT- or DELETE-statement.");
2928
}
3029

31-
parent::__construct($query);
32-
3330
$this->classname = trim($fullyQualifiedClassname);
3431
$this->metaDataMap = EntityMetaDataMap::getInstance();
3532
}

examples/entity/Order.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ class Order extends Entity {
1919
protected $orderPos;
2020

2121

22-
23-
24-
public function __construct() {
25-
26-
}
22+
/**
23+
* @column
24+
*/
25+
private $customer;
2726

2827
}
2928

examples/entity/OrderPosition.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@ class OrderPosition extends Entity {
1818
/**
1919
* @column(name = "order_id")
2020
*/
21-
private $orderId;
21+
private $orderId;
22+
23+
/**
24+
* @column
25+
*/
26+
private $article;
27+
28+
/**
29+
* @column
30+
*/
31+
private $price;
2232

2333
public function __construct() {
2434

examples/entity/Right.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ class Right extends Entity {
2020
*/
2121
private $desc;
2222

23-
public function __construct() {
24-
parent::__construct();
25-
// echo get_class($this) . "<br>";
26-
}
27-
28-
public function deny() {
29-
echo "perm denied!";
30-
}
31-
3223
}
3324

3425
?>

examples/entity/Role.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PPA\core\Entity;
66

7+
/*
8+
* @table(name='role')
9+
*/
710
class Role extends Entity {
811

912
/**
@@ -18,7 +21,7 @@ class Role extends Entity {
1821
private $name;
1922

2023
/**
21-
* @manyToMany(fetch = "lazy", mappedBy = "_PPA_examples_entity_Right")
24+
* @manyToMany(fetch = "eager", mappedBy = "_PPA_examples_entity_Right")
2225
* @joinTable(name = "role2right", column = "role_id", x_column = "right_id")
2326
*/
2427
private $rights = array();
@@ -32,7 +35,6 @@ public function getRights() {
3235
return $this->rights;
3336
}
3437

35-
3638
public function __construct() {
3739

3840
}

examples/entity/User.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class User extends Entity {
2020
*/
2121
private $username;
2222

23+
/**
24+
* @Column(name="password")
25+
*/
26+
private $password;
27+
2328
/**
2429
* @Column(name="role_id");
2530
* @oneToOne(fetch="lazy", mappedBy = "_PPA_examples_entity_Role")
@@ -29,11 +34,6 @@ class User extends Entity {
2934
public function getRole() {
3035
return $this->role;
3136
}
32-
33-
34-
public function __construct() {
35-
36-
}
3737

3838
}
3939

examples/entity/ppa.sql

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
-- --------------------------------------------------------
2+
-- Host: 127.0.0.1
3+
-- Server Version: 5.5.27 - MySQL Community Server (GPL)
4+
-- Server Betriebssystem: Win32
5+
-- HeidiSQL Version: 8.1.0.4545
6+
-- --------------------------------------------------------
7+
8+
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
9+
/*!40101 SET NAMES utf8 */;
10+
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
11+
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
12+
13+
-- Exportiere Struktur von Tabelle ppa.order
14+
CREATE TABLE IF NOT EXISTS `order` (
15+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
16+
`customer` varchar(50) NOT NULL,
17+
`crdate` datetime NOT NULL,
18+
PRIMARY KEY (`id`)
19+
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
20+
21+
-- Exportiere Daten aus Tabelle ppa.order: ~3 rows (ungefähr)
22+
/*!40000 ALTER TABLE `order` DISABLE KEYS */;
23+
INSERT INTO `order` (`id`, `customer`, `crdate`) VALUES
24+
(1, 'david gueatta', '2014-01-20 08:15:00'),
25+
(2, 'Jerry Lewis', '1961-05-12 09:59:30'),
26+
(3, 'Dean Martin', '1975-12-24 18:20:00');
27+
/*!40000 ALTER TABLE `order` ENABLE KEYS */;
28+
29+
30+
-- Exportiere Struktur von Tabelle ppa.orderpos
31+
CREATE TABLE IF NOT EXISTS `orderpos` (
32+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
33+
`order_id` int(10) unsigned NOT NULL DEFAULT '0',
34+
`article` varchar(50) NOT NULL,
35+
`price` varchar(50) NOT NULL,
36+
PRIMARY KEY (`id`),
37+
KEY `order_id` (`order_id`),
38+
CONSTRAINT `FK_orderpos_order` FOREIGN KEY (`order_id`) REFERENCES `order` (`id`)
39+
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
40+
41+
-- Exportiere Daten aus Tabelle ppa.orderpos: ~9 rows (ungefähr)
42+
/*!40000 ALTER TABLE `orderpos` DISABLE KEYS */;
43+
INSERT INTO `orderpos` (`id`, `order_id`, `article`, `price`) VALUES
44+
(1, 1, 'knife', '300'),
45+
(2, 1, 'spoon', '2500'),
46+
(3, 1, 'fork', '35'),
47+
(4, 2, 'hut', '55.1'),
48+
(5, 2, 'bath', '0.5'),
49+
(6, 3, 'pignose', '0.5'),
50+
(7, 3, 'baseballball', '650'),
51+
(8, 3, 'baseballcap', '651'),
52+
(9, 3, 'basballbat', '652');
53+
/*!40000 ALTER TABLE `orderpos` ENABLE KEYS */;
54+
55+
56+
-- Exportiere Struktur von Tabelle ppa.right
57+
CREATE TABLE IF NOT EXISTS `right` (
58+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
59+
`name` varchar(50) NOT NULL,
60+
PRIMARY KEY (`id`),
61+
UNIQUE KEY `name` (`name`)
62+
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
63+
64+
-- Exportiere Daten aus Tabelle ppa.right: ~5 rows (ungefähr)
65+
/*!40000 ALTER TABLE `right` DISABLE KEYS */;
66+
INSERT INTO `right` (`id`, `name`) VALUES
67+
(3, 'ch-pw'),
68+
(5, 'create_order'),
69+
(4, 'delete_order'),
70+
(1, 'login'),
71+
(2, 'logout');
72+
/*!40000 ALTER TABLE `right` ENABLE KEYS */;
73+
74+
75+
-- Exportiere Struktur von Tabelle ppa.role
76+
CREATE TABLE IF NOT EXISTS `role` (
77+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
78+
`name` varchar(50) NOT NULL,
79+
PRIMARY KEY (`id`),
80+
UNIQUE KEY `name` (`name`)
81+
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
82+
83+
-- Exportiere Daten aus Tabelle ppa.role: ~3 rows (ungefähr)
84+
/*!40000 ALTER TABLE `role` DISABLE KEYS */;
85+
INSERT INTO `role` (`id`, `name`) VALUES
86+
(1, 'admin'),
87+
(2, 'user');
88+
/*!40000 ALTER TABLE `role` ENABLE KEYS */;
89+
90+
91+
-- Exportiere Struktur von Tabelle ppa.role2right
92+
CREATE TABLE IF NOT EXISTS `role2right` (
93+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
94+
`role_id` int(10) unsigned NOT NULL DEFAULT '0',
95+
`right_id` int(10) unsigned NOT NULL DEFAULT '0',
96+
PRIMARY KEY (`id`),
97+
KEY `role_id` (`role_id`),
98+
KEY `right_id` (`right_id`),
99+
CONSTRAINT `FK_role2right_right` FOREIGN KEY (`right_id`) REFERENCES `right` (`id`),
100+
CONSTRAINT `FK_role2right_role` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`)
101+
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
102+
103+
-- Exportiere Daten aus Tabelle ppa.role2right: ~0 rows (ungefähr)
104+
/*!40000 ALTER TABLE `role2right` DISABLE KEYS */;
105+
INSERT INTO `role2right` (`id`, `role_id`, `right_id`) VALUES
106+
(4, 1, 3),
107+
(5, 1, 1),
108+
(7, 1, 2),
109+
(8, 1, 5),
110+
(9, 1, 4),
111+
(11, 2, 1),
112+
(12, 2, 5),
113+
(14, 2, 2);
114+
/*!40000 ALTER TABLE `role2right` ENABLE KEYS */;
115+
116+
117+
-- Exportiere Struktur von Tabelle ppa.user
118+
CREATE TABLE IF NOT EXISTS `user` (
119+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
120+
`username` varchar(50) NOT NULL DEFAULT '0',
121+
`password` varchar(50) NOT NULL DEFAULT '0',
122+
`role_id` int(10) unsigned NOT NULL DEFAULT '0',
123+
PRIMARY KEY (`id`),
124+
UNIQUE KEY `username` (`username`),
125+
KEY `role` (`role_id`),
126+
CONSTRAINT `FK_user_role` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`)
127+
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
128+
129+
-- Exportiere Daten aus Tabelle ppa.user: ~3 rows (ungefähr)
130+
/*!40000 ALTER TABLE `user` DISABLE KEYS */;
131+
INSERT INTO `user` (`id`, `username`, `password`, `role_id`) VALUES
132+
(1, 'root', 'ba018160fc26e0cc2e929b8e071f052d', 1),
133+
(2, 'newby', 'ba018160fc26e0cc2e929b8e071f052d', 2),
134+
(4, 'adam', 'ba018160fc26e0cc2e929b8e071f052d', 2);
135+
/*!40000 ALTER TABLE `user` ENABLE KEYS */;
136+
/*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
137+
/*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
138+
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;

0 commit comments

Comments
 (0)