2. srv / bd / Bd.php

1<?php
2
3require_once __DIR__ . "/../modelo/Venta.php";
4require_once __DIR__ . "/../modelo/Producto.php";
5require_once __DIR__ . "/bdCrea.php";
6require_once __DIR__ . "/productoCuenta.php";
7require_once __DIR__ . "/productoAgrega.php";
8require_once __DIR__ . "/ventaCuenta.php";
9require_once __DIR__ . "/ventaAgrega.php";
10
11class Bd
12{
13
14 private static ?PDO $conexion = null;
15
16 public static function getConexion(): PDO
17 {
18 if (self::$conexion === null) {
19 self::$conexion = new PDO(
20 // cadena de conexión
21 "sqlite:srvcompras.db",
22 // usuario
23 null,
24 // contraseña
25 null,
26 // Opciones: conexiones persistentes y lanza excepciones.
27 [PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
28 );
29
30 bdCrea(self::$conexion);
31 if (productoCuenta() === 0) {
32 productoAgrega(
33 new Producto(nombre: "Sandwich", existencias: 50, precio: 15)
34 );
35 productoAgrega(
36 new Producto(nombre: "Hot dog", existencias: 40, precio: 30)
37 );
38 productoAgrega(
39 new Producto(nombre: "Hamburguesa", existencias: 30, precio: 40)
40 );
41 }
42
43 if (ventaCuenta() === 0) {
44 ventaAgrega(new Venta(enCaptura: true));
45 }
46 }
47
48 return self::$conexion;
49 }
50}
51
skip_previous skip_next