File size: 8,939 Bytes
d419ea2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
Pharmaceutical Analytics Database Schema

This module defines the SQLite database schema for the pharmaceutical analytics demo.
It includes tables for sales data, products, regions, territories, prescribers,
pharmacies, marketing campaigns, and other relevant entities.
"""

import sqlite3
from typing import Dict, List, Any

# SQLite database schema definitions
SCHEMA_DEFINITIONS = {
    "regions": """
        CREATE TABLE IF NOT EXISTS regions (
            region_id TEXT PRIMARY KEY,
            region_name TEXT NOT NULL,
            country TEXT NOT NULL,
            division TEXT NOT NULL,
            population INTEGER NOT NULL
        )
    """,
    
    "territories": """
        CREATE TABLE IF NOT EXISTS territories (
            territory_id TEXT PRIMARY KEY,
            territory_name TEXT NOT NULL,
            region_id TEXT NOT NULL,
            sales_rep_id TEXT NOT NULL,
            FOREIGN KEY (region_id) REFERENCES regions (region_id)
        )
    """,
    
    "products": """
        CREATE TABLE IF NOT EXISTS products (
            product_id TEXT PRIMARY KEY,
            product_name TEXT NOT NULL,
            therapeutic_area TEXT NOT NULL,
            molecule TEXT NOT NULL,
            launch_date DATE NOT NULL,
            status TEXT NOT NULL,
            list_price REAL NOT NULL
        )
    """,
    
    "competitor_products": """
        CREATE TABLE IF NOT EXISTS competitor_products (
            competitor_product_id TEXT PRIMARY KEY,
            product_name TEXT NOT NULL,
            manufacturer TEXT NOT NULL,
            therapeutic_area TEXT NOT NULL,
            molecule TEXT NOT NULL,
            launch_date DATE NOT NULL,
            list_price REAL NOT NULL,
            competing_with_product_id TEXT NOT NULL,
            FOREIGN KEY (competing_with_product_id) REFERENCES products (product_id)
        )
    """,
    
    "prescribers": """
        CREATE TABLE IF NOT EXISTS prescribers (
            prescriber_id TEXT PRIMARY KEY,
            name TEXT NOT NULL,
            specialty TEXT NOT NULL,
            practice_type TEXT NOT NULL,
            territory_id TEXT NOT NULL,
            decile INTEGER NOT NULL,
            FOREIGN KEY (territory_id) REFERENCES territories (territory_id)
        )
    """,
    
    "pharmacies": """
        CREATE TABLE IF NOT EXISTS pharmacies (
            pharmacy_id TEXT PRIMARY KEY,
            name TEXT NOT NULL,
            address TEXT NOT NULL,
            territory_id TEXT NOT NULL,
            pharmacy_type TEXT NOT NULL,
            monthly_rx_volume INTEGER NOT NULL,
            FOREIGN KEY (territory_id) REFERENCES territories (territory_id)
        )
    """,
    
    "distribution_centers": """
        CREATE TABLE IF NOT EXISTS distribution_centers (
            dc_id TEXT PRIMARY KEY,
            dc_name TEXT NOT NULL,
            region_id TEXT NOT NULL,
            inventory_capacity INTEGER NOT NULL,
            FOREIGN KEY (region_id) REFERENCES regions (region_id)
        )
    """,
    
    "sales": """
        CREATE TABLE IF NOT EXISTS sales (
            sale_id INTEGER PRIMARY KEY,
            sale_date DATE NOT NULL,
            product_id TEXT NOT NULL,
            region_id TEXT NOT NULL,
            territory_id TEXT NOT NULL,
            prescriber_id TEXT NOT NULL,
            pharmacy_id TEXT NOT NULL,
            units_sold INTEGER NOT NULL,
            revenue REAL NOT NULL,
            cost REAL NOT NULL,
            margin REAL NOT NULL,
            FOREIGN KEY (product_id) REFERENCES products (product_id),
            FOREIGN KEY (region_id) REFERENCES regions (region_id),
            FOREIGN KEY (territory_id) REFERENCES territories (territory_id),
            FOREIGN KEY (prescriber_id) REFERENCES prescribers (prescriber_id),
            FOREIGN KEY (pharmacy_id) REFERENCES pharmacies (pharmacy_id)
        )
    """,
    
    "marketing_campaigns": """
        CREATE TABLE IF NOT EXISTS marketing_campaigns (
            campaign_id INTEGER PRIMARY KEY,
            campaign_name TEXT NOT NULL,
            start_date DATE NOT NULL,
            end_date DATE NOT NULL,
            product_id TEXT NOT NULL,
            campaign_type TEXT NOT NULL,
            target_audience TEXT NOT NULL,
            channels TEXT NOT NULL,
            budget REAL NOT NULL,
            spend REAL NOT NULL,
            FOREIGN KEY (product_id) REFERENCES products (product_id)
        )
    """,
    
    "market_events": """
        CREATE TABLE IF NOT EXISTS market_events (
            event_id INTEGER PRIMARY KEY,
            event_date DATE NOT NULL,
            event_type TEXT NOT NULL,
            description TEXT NOT NULL,
            affected_products TEXT NOT NULL,
            affected_regions TEXT NOT NULL,
            impact_score REAL NOT NULL
        )
    """,
    
    "sales_targets": """
        CREATE TABLE IF NOT EXISTS sales_targets (
            target_id INTEGER PRIMARY KEY,
            product_id TEXT NOT NULL,
            region_id TEXT NOT NULL,
            period TEXT NOT NULL,
            target_units REAL NOT NULL,
            target_revenue REAL NOT NULL,
            FOREIGN KEY (product_id) REFERENCES products (product_id),
            FOREIGN KEY (region_id) REFERENCES regions (region_id)
        )
    """,
    
    "inventory": """
        CREATE TABLE IF NOT EXISTS inventory (
            inventory_id INTEGER PRIMARY KEY,
            product_id TEXT NOT NULL,
            dc_id TEXT NOT NULL,
            date DATE NOT NULL,
            units_available INTEGER NOT NULL,
            units_allocated INTEGER NOT NULL,
            units_in_transit INTEGER NOT NULL,
            days_of_supply REAL NOT NULL,
            FOREIGN KEY (product_id) REFERENCES products (product_id),
            FOREIGN KEY (dc_id) REFERENCES distribution_centers (dc_id)
        )
    """,
    
    "external_factors": """
        CREATE TABLE IF NOT EXISTS external_factors (
            factor_id INTEGER PRIMARY KEY,
            date DATE NOT NULL,
            region_id TEXT NOT NULL,
            factor_type TEXT NOT NULL,
            factor_value REAL NOT NULL,
            description TEXT NOT NULL,
            FOREIGN KEY (region_id) REFERENCES regions (region_id)
        )
    """
}

# Indexes for optimizing queries
INDEX_DEFINITIONS = [
    "CREATE INDEX IF NOT EXISTS idx_sales_date ON sales (sale_date)",
    "CREATE INDEX IF NOT EXISTS idx_sales_product ON sales (product_id)",
    "CREATE INDEX IF NOT EXISTS idx_sales_region ON sales (region_id)",
    "CREATE INDEX IF NOT EXISTS idx_sales_territory ON sales (territory_id)",
    "CREATE INDEX IF NOT EXISTS idx_inventory_date ON inventory (date)",
    "CREATE INDEX IF NOT EXISTS idx_inventory_product ON inventory (product_id)",
    "CREATE INDEX IF NOT EXISTS idx_marketing_dates ON marketing_campaigns (start_date, end_date)",
    "CREATE INDEX IF NOT EXISTS idx_external_factors_date ON external_factors (date)",
    "CREATE INDEX IF NOT EXISTS idx_external_factors_region ON external_factors (region_id)"
]

def create_schema(db_path: str = "data/pharma_db.sqlite") -> None:
    """Create database schema in the SQLite database"""
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    
    # Create tables
    for table_name, schema_sql in SCHEMA_DEFINITIONS.items():
        cursor.execute(schema_sql)
    
    # Create indexes
    for index_sql in INDEX_DEFINITIONS:
        cursor.execute(index_sql)
    
    conn.commit()
    conn.close()
    
    print(f"Database schema created in {db_path}")

def get_table_info(db_path: str = "data/pharma_db.sqlite") -> Dict[str, List[Dict[str, Any]]]:
    """Get information about all tables in the database"""
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    
    # Get list of tables
    cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
    tables = cursor.fetchall()
    
    table_info = {}
    
    for table in tables:
        table_name = table[0]
        
        # Get column information
        cursor.execute(f"PRAGMA table_info({table_name})")
        columns = cursor.fetchall()
        
        # Format column information
        column_info = []
        for col in columns:
            column_info.append({
                "name": col[1],
                "type": col[2],
                "notnull": col[3],
                "default": col[4],
                "primary_key": col[5]
            })
        
        table_info[table_name] = column_info
    
    conn.close()
    return table_info

if __name__ == "__main__":
    # Create the database schema when run directly
    create_schema()
    
    # Print table information
    table_info = get_table_info()
    for table_name, columns in table_info.items():
        print(f"Table: {table_name}")
        for col in columns:
            print(f"  - {col['name']} ({col['type']})")
        print()