Metadata-Version: 2.4
Name: py_gazie
Version: 0.1.5
Summary: A Seamless Bridge Between Custom Python Applications and Easy GAzie Database Consultation
Home-page: https://www.newstechnology.eu
Author: Daniele Frulla
Author-email: daniele.frulla@newstechnology.eu
Keywords: python,GAzie,ERP,Gestione Aziendale,py-gazie,gazie,gazie database,php-crud-api
Description-Content-Type: text/markdown
Requires-Dist: requests==2.34.2
Requires-Dist: py_client_cap
Dynamic: author
Dynamic: author-email
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: keywords
Dynamic: requires-dist
Dynamic: summary

# Libreria Python Consultazione DB GAzie

La consultazione del DB di GAzie tramite CRUD PHP Api.
Questo documento vuol dare line guida Quick start per navigare nelle tabelle GAzie.

## Installazione

1. **PIP**:
    ```python
    pip install py_gazie
    ```
## Adattare GAzie a CRUD PHP API

Per adattare il database gazie a crud php api occorre inserire il campo 'id' come campo di primary key ove non vi sia.

### Gli script utili per eseguirli.

#### Procedure Eseguire Su DB Gazie:

```
DELIMITER //

DROP PROCEDURE IF EXISTS AddIdPrimaryKeyToGazie //

CREATE PROCEDURE AddIdPrimaryKeyToGazie()
BEGIN
    -- =========================================================================
    -- 1. TUTTE LE DICHIARAZIONI (Devono stare in cima)
    -- =========================================================================
    DECLARE done INT DEFAULT FALSE;
    DECLARE current_table VARCHAR(255);
    DECLARE old_pk_columns VARCHAR(512);
    
    -- Cursore per escludere tabelle con 'id' o con qualsiasi altro AUTO_INCREMENT
    DECLARE table_cursor CURSOR FOR 
        SELECT t.TABLE_NAME 
        FROM information_schema.TABLES t
        WHERE t.TABLE_SCHEMA = DATABASE()
          AND t.TABLE_NAME LIKE 'gaz_%'
          AND NOT EXISTS (
              SELECT 1 
              FROM information_schema.COLUMNS c 
              WHERE c.TABLE_SCHEMA = DATABASE() 
                AND c.TABLE_NAME = t.TABLE_NAME 
                AND c.COLUMN_NAME = 'id'
          )
          AND NOT EXISTS (
              SELECT 1 
              FROM information_schema.COLUMNS c 
              WHERE c.TABLE_SCHEMA = DATABASE() 
                AND c.TABLE_NAME = t.TABLE_NAME 
                AND c.EXTRA LIKE '%auto_increment%'
          );

    -- L'handler deve essere l'ultima cosa dichiarata
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;


    -- =========================================================================
    -- 2. ISTRUZIONI ESEGUIBILI
    -- =========================================================================
    
    -- Creazione della tabella temporanea per il LOG
    DROP TEMPORARY TABLE IF EXISTS temp_log_alter_tables;
    CREATE TEMPORARY TABLE temp_log_alter_tables (
        id INT AUTO_INCREMENT PRIMARY KEY,
        eseguito_alle TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        tabella_modificata VARCHAR(255),
        query_eseguita TEXT
    );

    OPEN table_cursor;

    read_loop: LOOP
        FETCH table_cursor INTO current_table;
        
        IF done THEN
            LEAVE read_loop;
        END IF;

        -- Recupera dinamicamente le colonne che compongono l'attuale chiave primaria
        SELECT GROUP_CONCAT(COLUMN_NAME SEPARATOR ', ')
        INTO old_pk_columns
        FROM information_schema.KEY_COLUMN_USAGE
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = current_table
          AND CONSTRAINT_NAME = 'PRIMARY';

        -- Se esiste una vecchia chiave primaria, procediamo
        IF old_pk_columns IS NOT NULL THEN
            
            SET @dynamic_sql = CONCAT(
                'ALTER TABLE `', current_table, '` ',
                'DROP PRIMARY KEY, ',
                'ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST, ',
                'ADD UNIQUE KEY `uk_', current_table, '_old_pk` (', old_pk_columns, ');'
            );

            -- Scrittura nel LOG
            INSERT INTO temp_log_alter_tables (tabella_modificata, query_eseguita) 
            VALUES (current_table, @dynamic_sql);

            -- Esecuzione della query
            PREPARE stmt FROM @dynamic_sql;
            EXECUTE stmt;
            DEALLOCATE PREPARE stmt;
            
        END IF;

    END LOOP;

    CLOSE table_cursor;
    
    -- 3. Stampa a schermo del risultato finale
    SELECT * FROM temp_log_alter_tables;
    
    -- Pulizia finale
    DROP TEMPORARY TABLE IF EXISTS temp_log_alter_tables;

END //

DELIMITER ;
```

#### Chiamare la Procedura

```
CALL AddIdPrimaryKeyToGazie();
```


## Uso

### 1. Utilizzo della libreria

```python
from py_gazie import GazieClient

base_url = "http://127.0.0.1:8082/modules/api/index.php"
api_key = "test-token"

client = GazieClient(
        base_url=base_url,
        api_key=api_key,
        timeout=15,
        default_company="001" # Azienda predefinita di lavoro
    )
```

# Gestire le Tabelle

## Classe GazieTable

Ci sono diversi esempi nella cartella *examples/*.

Faccio qui un ulteriore esempio di lettura:

```
from py_gazie import GazieClient

client = GazieClient(base_url="http://127.0.0.1:8082/modules/api/index.php", api_key="test-token", default_company="001")
    
product_table = client.get_table("product", company=1)
product_table.page(limit=5)
response = product_table.search()
print(response)
```
