1 | <?php |
2 | |
3 | require_once __DIR__ . "/ventaEnCapturaAgrega.php"; |
4 | |
5 | class Bd |
6 | { |
7 | |
8 | private static ?PDO $pdo = null; |
9 | |
10 | public static function pdo(): PDO |
11 | { |
12 | if (self::$pdo === null) { |
13 | self::$pdo = new PDO( |
14 | |
15 | "sqlite:srvcompras.db", |
16 | |
17 | null, |
18 | |
19 | null, |
20 | |
21 | [PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] |
22 | ); |
23 | |
24 | self::$pdo->exec( |
25 | 'CREATE TABLE IF NOT EXISTS VENTA ( |
26 | VENT_ID INTEGER, |
27 | VENT_EN_CAPTURA INTEGER NOT NULL, |
28 | CONSTRAINT VENT_PK |
29 | PRIMARY KEY(VENT_ID) |
30 | )' |
31 | ); |
32 | self::$pdo->exec( |
33 | 'CREATE TABLE IF NOT EXISTS PRODUCTO ( |
34 | PROD_ID INTEGER, |
35 | PROD_NOMBRE TEXT NOT NULL, |
36 | PROD_EXISTENCIAS REAL NOT NULL, |
37 | PROD_PRECIO REAL NOT NULL, |
38 | CONSTRAINT PROD_PK |
39 | PRIMARY KEY(PROD_ID), |
40 | CONSTRAINT PROD_NOM_UNQ |
41 | UNIQUE(PROD_NOMBRE), |
42 | CONSTRAINT PROD_NOM_NV |
43 | CHECK(LENGTH(PROD_NOMBRE) > 0) |
44 | )' |
45 | ); |
46 | self::$pdo->exec( |
47 | 'CREATE TABLE IF NOT EXISTS DET_VENTA ( |
48 | VENT_ID INTEGER NOT NULL, |
49 | PROD_ID INTEGER NOT NULL, |
50 | DTV_CANTIDAD REAL NOT NULL, |
51 | DTV_PRECIO REAL NOT NULL, |
52 | CONSTRAINT DTV_PK |
53 | PRIMARY KEY (VENT_ID, PROD_ID), |
54 | CONSTRAINT DTV_VENT_FK |
55 | FOREIGN KEY (VENT_ID) REFERENCES VENTA(VENT_ID), |
56 | CONSTRAINT DTV_PROD_FK |
57 | FOREIGN KEY (PROD_ID) REFERENCES PRODUCTO(PROD_ID) |
58 | )' |
59 | ); |
60 | |
61 | $cantidadDeProductos = |
62 | self::$pdo->query("SELECT COUNT(PROD_ID) FROM PRODUCTO")->fetchColumn(); |
63 | |
64 | if ($cantidadDeProductos === 0) { |
65 | self::$pdo->exec( |
66 | "INSERT INTO PRODUCTO |
67 | (PROD_NOMBRE, PROD_EXISTENCIAS, PROD_PRECIO) |
68 | VALUES |
69 | ('Sandwich', 50, 15), |
70 | ('Hot dog', 40, 30), |
71 | ('Hamburguesa', 30, 40)" |
72 | ); |
73 | } |
74 | |
75 | $cantidadDeVentas = |
76 | self::$pdo->query("SELECT COUNT(VENT_ID) FROM VENTA")->fetchColumn(); |
77 | |
78 | if ($cantidadDeVentas === 0) { |
79 | ventaEnCapturaAgrega(self::$pdo); |
80 | } |
81 | } |
82 | |
83 | return self::$pdo; |
84 | } |
85 | } |
86 | |