1 | <?php |
2 | |
3 | require_once __DIR__ . "/../lib/php/selectFirst.php"; |
4 | require_once __DIR__ . "/../lib/php/insert.php"; |
5 | require_once __DIR__ . "/../lib/php/insertBridges.php"; |
6 | require_once __DIR__ . "/../lib/php/insert.php"; |
7 | require_once __DIR__ . "/TABLA_USUARIO.php"; |
8 | require_once __DIR__ . "/TABLA_ROL.php"; |
9 | require_once __DIR__ . "/TABLA_USU_ROL.php"; |
10 | require_once __DIR__ . "/ROL_ID_CLIENTE.php"; |
11 | require_once __DIR__ . "/ROL_ID_ADMINISTRADOR.php"; |
12 | |
13 | class Bd |
14 | { |
15 | |
16 | private static ?PDO $pdo = null; |
17 | |
18 | static function pdo(): PDO |
19 | { |
20 | if (self::$pdo === null) { |
21 | |
22 | self::$pdo = new PDO( |
23 | |
24 | "sqlite:srvaut.db", |
25 | |
26 | null, |
27 | |
28 | null, |
29 | |
30 | [PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] |
31 | ); |
32 | |
33 | self::$pdo->exec( |
34 | 'CREATE TABLE IF NOT EXISTS USUARIO ( |
35 | USU_ID INTEGER, |
36 | USU_CUE TEXT NOT NULL, |
37 | USU_MATCH TEXT NOT NULL, |
38 | CONSTRAINT USU_PK |
39 | PRIMARY KEY(USU_ID), |
40 | CONSTRAINT USU_CUE_UNQ |
41 | UNIQUE(USU_CUE), |
42 | CONSTRAINT USU_CUE_NV |
43 | CHECK(LENGTH(USU_CUE) > 0) |
44 | )' |
45 | ); |
46 | self::$pdo->exec( |
47 | 'CREATE TABLE IF NOT EXISTS ROL ( |
48 | ROL_ID TEXT NOT NULL, |
49 | ROL_DESCRIPCION TEXT NOT NULL, |
50 | CONSTRAINT ROL_PK |
51 | PRIMARY KEY(ROL_ID), |
52 | CONSTRAINT ROL_ID_NV |
53 | CHECK(LENGTH(ROL_ID) > 0), |
54 | CONSTRAINT ROL_DESCR_UNQ |
55 | UNIQUE(ROL_DESCRIPCION), |
56 | CONSTRAINT ROL_DESCR_NV |
57 | CHECK(LENGTH(ROL_DESCRIPCION) > 0) |
58 | )' |
59 | ); |
60 | self::$pdo->exec( |
61 | 'CREATE TABLE IF NOT EXISTS USU_ROL ( |
62 | USU_ID INTEGER NOT NULL, |
63 | ROL_ID TEXT NOT NULL, |
64 | CONSTRAINT USU_ROL_PK |
65 | PRIMARY KEY(USU_ID, ROL_ID), |
66 | CONSTRAINT USU_ROL_USU_FK |
67 | FOREIGN KEY (USU_ID) REFERENCES USUARIO(USU_ID), |
68 | CONSTRAINT USU_ROL_ROL_FK |
69 | FOREIGN KEY (ROL_ID) REFERENCES ROL(ROL_ID) |
70 | )' |
71 | ); |
72 | |
73 | if (selectFirst( |
74 | pdo: self::$pdo, |
75 | from: ROL, |
76 | where: [ROL_ID => ROL_ID_ADMINISTRADOR] |
77 | ) === false) { |
78 | insert( |
79 | pdo: self::$pdo, |
80 | into: ROL, |
81 | values: [ |
82 | ROL_ID => ROL_ID_ADMINISTRADOR, |
83 | ROL_DESCRIPCION => "Administra el sistema." |
84 | ] |
85 | ); |
86 | } |
87 | |
88 | if (selectFirst(self::$pdo, ROL, [ROL_ID => ROL_ID_CLIENTE]) === false) { |
89 | insert( |
90 | pdo: self::$pdo, |
91 | into: ROL, |
92 | values: [ |
93 | ROL_ID => ROL_ID_CLIENTE, |
94 | ROL_DESCRIPCION => "Realiza compras." |
95 | ] |
96 | ); |
97 | } |
98 | } |
99 | |
100 | if (selectFirst(self::$pdo, USUARIO, [USU_CUE => "pepito"]) === false) { |
101 | insert( |
102 | pdo: self::$pdo, |
103 | into: USUARIO, |
104 | values: [ |
105 | USU_CUE => "pepito", |
106 | USU_MATCH => password_hash("cuentos", PASSWORD_DEFAULT) |
107 | ] |
108 | ); |
109 | $usuId = self::$pdo->lastInsertId(); |
110 | insertBridges( |
111 | pdo: self::$pdo, |
112 | into: USU_ROL, |
113 | valuesDePadre: [USU_ID => $usuId], |
114 | valueDeHijos: [ROL_ID => [ROL_ID_CLIENTE]] |
115 | ); |
116 | } |
117 | |
118 | if (selectFirst(self::$pdo, USUARIO, [USU_CUE => "susana"]) === false) { |
119 | insert( |
120 | pdo: self::$pdo, |
121 | into: USUARIO, |
122 | values: [ |
123 | USU_CUE => "susana", |
124 | USU_MATCH => password_hash("alegria", PASSWORD_DEFAULT) |
125 | ] |
126 | ); |
127 | $usuId = self::$pdo->lastInsertId(); |
128 | insertBridges( |
129 | pdo: self::$pdo, |
130 | into: USU_ROL, |
131 | valuesDePadre: [USU_ID => $usuId], |
132 | valueDeHijos: [ROL_ID => [ROL_ID_ADMINISTRADOR]] |
133 | ); |
134 | } |
135 | |
136 | if (selectFirst(self::$pdo, USUARIO, [USU_CUE => "bebe"]) === false) { |
137 | insert( |
138 | pdo: self::$pdo, |
139 | into: USUARIO, |
140 | values: [ |
141 | USU_CUE => "bebe", |
142 | USU_MATCH => password_hash("saurio", PASSWORD_DEFAULT) |
143 | ] |
144 | ); |
145 | $usuId = self::$pdo->lastInsertId(); |
146 | insertBridges( |
147 | pdo: self::$pdo, |
148 | into: USU_ROL, |
149 | valuesDePadre: [USU_ID => $usuId], |
150 | valueDeHijos: [ROL_ID => [ROL_ID_ADMINISTRADOR, ROL_ID_CLIENTE]] |
151 | ); |
152 | } |
153 | |
154 | return self::$pdo; |
155 | } |
156 | } |
157 | |