1 | <?php |
2 | |
3 | class Bd |
4 | { |
5 | private static ?PDO $pdo = null; |
6 | |
7 | static function pdo(): PDO |
8 | { |
9 | if (self::$pdo === null) { |
10 | |
11 | self::$pdo = new PDO( |
12 | |
13 | "sqlite:srvarchivos.db", |
14 | |
15 | null, |
16 | |
17 | null, |
18 | |
19 | [PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] |
20 | ); |
21 | |
22 | self::$pdo->exec( |
23 | 'CREATE TABLE IF NOT EXISTS ARCHIVO ( |
24 | ARCH_ID INTEGER, |
25 | ARCH_BYTES BLOB NOT NULL, |
26 | CONSTRAINT ARCH_PK |
27 | PRIMARY KEY(ARCH_ID) |
28 | )' |
29 | ); |
30 | self::$pdo->exec( |
31 | 'CREATE TABLE IF NOT EXISTS PRODUCTO ( |
32 | PROD_ID INTEGER, |
33 | PROD_NOMBRE TEXT NOT NULL, |
34 | ARCH_ID INTEGER NOT NULL, |
35 | CONSTRAINT PROD_PK |
36 | PRIMARY KEY(PROD_ID), |
37 | CONSTRAINT PROD_NOM_UNQ |
38 | UNIQUE(PROD_NOMBRE), |
39 | CONSTRAINT PROD_NOM_NV |
40 | CHECK(LENGTH(PROD_NOMBRE) > 0), |
41 | CONSTRAINT PROD_ARCH_FK |
42 | FOREIGN KEY (ARCH_ID) REFERENCES ARCHIVO(ARCH_ID) |
43 | )' |
44 | ); |
45 | } |
46 | |
47 | return self::$pdo; |
48 | } |
49 | } |
50 | |