# ShopDB Quick Reference for Claude.ai

## Database Tables

### machines (unified - all devices)
| Column | Type | Notes |
|--------|------|-------|
| machineid | INT | Primary key |
| machinetypeid | INT | FK to machinetypes |
| machinenumber | VARCHAR | Equipment number or hostname |
| hostname | VARCHAR | PC hostname |
| serialnumber | VARCHAR | Serial number |
| alias | VARCHAR | Friendly name |
| pctypeid | INT | NOT NULL = PC, NULL = equipment |
| osid | INT | FK to operatingsystems (PCs) |
| modelnumberid | INT | FK to models |
| businessunitid | INT | FK to businessunits |
| machinestatusid | INT | FK to machinestatus |
| isactive | TINYINT | 1=active, 0=inactive |
| lastupdated | DATETIME | Auto-updated |

### communications (network interfaces)
| Column | Type | Notes |
|--------|------|-------|
| communicationid | INT | Primary key |
| machineid | INT | FK to machines |
| comstypeid | INT | FK to comstypes (1=IP, 2=Serial) |
| address | VARCHAR | IP address or COM port |
| macaddress | VARCHAR | MAC address |
| port | INT | Port number |
| isprimary | TINYINT | Primary interface flag |

### machinerelationships
| Column | Type | Notes |
|--------|------|-------|
| relationshipid | INT | Primary key |
| machineid | INT | Source machine (e.g., PC) |
| related_machineid | INT | Target machine (e.g., Equipment) |
| relationshiptypeid | INT | FK to relationshiptypes |

### printers (separate table)
| Column | Type | Notes |
|--------|------|-------|
| printerid | INT | Primary key |
| name | VARCHAR | Printer name |
| address | VARCHAR | IP or hostname |
| modelid | INT | FK to models |
| isactive | TINYINT | Active flag |

## Machine Type IDs

### Equipment (1-15)
- 1: LocationOnly
- 2-14: Various equipment (Lathe, Mill, CMM, etc.)
- 15: Printer (legacy)

### Network Devices (16-20)
- 16: Access Point
- 17: IDF
- 18: Camera
- 19: Switch
- 20: Server

### PCs (33-35)
- 33: Standard PC
- 34: Engineering PC
- 35: Shopfloor PC

## Common Queries

```sql
-- All active PCs with details
SELECT m.machineid, m.hostname, m.serialnumber,
       pt.pctype, mo.modelnumber, os.osname
FROM machines m
LEFT JOIN pctype pt ON m.pctypeid = pt.pctypeid
LEFT JOIN models mo ON m.modelnumberid = mo.modelnumberid
LEFT JOIN operatingsystems os ON m.osid = os.osid
WHERE m.pctypeid IS NOT NULL AND m.isactive = 1;

-- PC's network interfaces
SELECT m.hostname, c.address, c.macaddress
FROM machines m
JOIN communications c ON m.machineid = c.machineid
WHERE m.pctypeid IS NOT NULL;

-- PC controlling equipment
SELECT
    pc.hostname AS pc_name,
    eq.machinenumber AS equipment
FROM machinerelationships mr
JOIN machines pc ON mr.machineid = pc.machineid
JOIN machines eq ON mr.related_machineid = eq.machineid
JOIN relationshiptypes rt ON mr.relationshiptypeid = rt.relationshiptypeid
WHERE rt.relationshiptype = 'Controls';

-- Network devices
SELECT m.machineid, m.machinenumber, mt.machinetype, c.address
FROM machines m
JOIN machinetypes mt ON m.machinetypeid = mt.machinetypeid
LEFT JOIN communications c ON m.machineid = c.machineid AND c.isprimary = 1
WHERE m.machinetypeid IN (16,17,18,19,20);
```

## ASP Code Patterns

### Safe Database Query
```vbscript
Dim cmd, rs
Set cmd = Server.CreateObject("ADODB.Command")
cmd.ActiveConnection = objConn
cmd.CommandText = "SELECT hostname, serialnumber FROM machines WHERE machineid = ?"
cmd.Parameters.Append cmd.CreateParameter("@id", 3, 1, , Request("id"))
Set rs = cmd.Execute()

If NOT rs.EOF Then
    Dim hostname, serial
    hostname = rs("hostname") & ""  ' Convert to string
    serial = rs("serialnumber") & ""
    Response.Write("<p>" & Server.HTMLEncode(hostname) & "</p>")
End If

rs.Close
Set rs = Nothing
Set cmd = Nothing
```

### Form Handling
```vbscript
' Get and sanitize input
Dim machineId, hostname
machineId = Request.Form("machineid")
hostname = Trim(Request.Form("hostname"))

' Validate
If machineId = "" Or Not IsNumeric(machineId) Then
    Response.Write("Invalid machine ID")
    Response.End
End If

' Update with parameterized query
Dim cmdUpdate
Set cmdUpdate = Server.CreateObject("ADODB.Command")
cmdUpdate.ActiveConnection = objConn
cmdUpdate.CommandText = "UPDATE machines SET hostname = ? WHERE machineid = ?"
cmdUpdate.Parameters.Append cmdUpdate.CreateParameter("@host", 200, 1, 100, hostname)
cmdUpdate.Parameters.Append cmdUpdate.CreateParameter("@id", 3, 1, , CLng(machineId))
cmdUpdate.Execute
```

### Error Handling
```vbscript
On Error Resume Next
' risky operation
If Err.Number <> 0 Then
    Response.Write("Error: " & Server.HTMLEncode(Err.Description))
    Err.Clear
End If
On Error GoTo 0
```

## File Reference

### Main Pages
| File | Purpose |
|------|---------|
| displaymachines.asp | List all machines |
| displaymachine.asp | Single machine details |
| displaypcs.asp | List all PCs |
| displaypc.asp | Single PC details |
| displayprinters.asp | List printers |
| network_map.asp | Visual network map |
| network_devices.asp | Network device list |
| api.asp | REST API endpoint |

### Form Pages
| File | Purpose |
|------|---------|
| addmachine.asp | Add new machine form |
| editmachine.asp | Edit machine form |
| savemachine.asp | Save machine handler |
| addprinter.asp | Add printer form |
| editprinter.asp | Edit printer form |

### Includes
| File | Purpose |
|------|---------|
| includes/header.asp | Page header, nav |
| includes/footer.asp | Page footer |
| includes/sql.asp | Database connection |
| includes/functions.asp | Helper functions |

## Environment

- **Dev Server:** 192.168.122.151:8080
- **Database:** MySQL in Docker (dev-mysql container)
- **Git:** Gitea at localhost:3000
- **Project Path:** /home/camp/projects/windows/shopdb/
