Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"files.associations": {
"table.h": "c"
"table.h": "c",
"secondary_index.h": "c",
"input_handling.h": "c"
}
}
16 changes: 16 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,22 @@ Upon running the application, you'll enter an interactive shell where you can ex
DELETE FROM students WHERE id = 1
```

### Transaction Commands

Transactions ensure that database operations are atomic, consistent, isolated, and durable (ACID).

- **Enable Transactions:**

- **Begin a Transaction:**

- **Commit a Transaction:**

- **Rollback a Transaction:**

- **View Transaction Status:**

- **Disable Transactions:**

### Meta-Commands

- **Exit the Application:**
Expand Down
130 changes: 130 additions & 0 deletions flask-database-app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
import subprocess
import json
import os
from db_connector import DatabaseConnector

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key_here'

# Get the absolute path to the root of your project
project_root = os.path.dirname(os.path.abspath(__file__))
db_root = os.path.dirname(project_root) # Parent directory of flask-database-app

# Initialize database connector
db = DatabaseConnector(db_root)

@app.route('/')
def index():
"""Main dashboard showing database statistics"""
tables = db.get_tables()
table_stats = {}
for table in tables:
count = len(db.get_records(table))
table_stats[table] = count

return render_template('index.html', tables=tables, table_stats=table_stats)

# User routes
@app.route('/users')
def list_users():
"""List all users"""
users = db.get_records('users')
return render_template('users/list.html', users=users)

@app.route('/users/<int:user_id>')
def view_user(user_id):
"""View user details"""
user = db.get_record_by_id('users', user_id)
if not user:
flash('User not found', 'error')
return redirect(url_for('list_users'))

return render_template('users/view.html', user=user)

@app.route('/users/create', methods=['GET', 'POST'])
def create_user():
"""Create a new user"""
if request.method == 'POST':
# Get the next available ID
next_id = db.get_next_id('users')

# Collect form data
user_data = {
'id': next_id,
'name': request.form['name'],
'email': request.form['email']
}

# Validate data
errors = []
if not user_data['name']:
errors.append('Name is required')
if not user_data['email']:
errors.append('Email is required')

if errors:
for error in errors:
flash(error, 'error')
return render_template('users/create.html', user=user_data)

# Insert user
if db.insert_record('users', user_data):
flash('User created successfully!', 'success')
return redirect(url_for('list_users'))
else:
flash('Failed to create user', 'error')
return render_template('users/create.html', user=user_data)

return render_template('users/create.html', user={})

@app.route('/users/<int:user_id>/edit', methods=['GET', 'POST'])
def edit_user(user_id):
"""Edit an existing user"""
user = db.get_record_by_id('users', user_id)
if not user:
flash('User not found', 'error')
return redirect(url_for('list_users'))

if request.method == 'POST':
# Collect form data
user_data = {
'id': user_id,
'name': request.form['name'],
'email': request.form['email']
}

# Validate data
errors = []
if not user_data['name']:
errors.append('Name is required')
if not user_data['email']:
errors.append('Email is required')

if errors:
for error in errors:
flash(error, 'error')
return render_template('users/edit.html', user=user_data)

# Update user
if db.update_record('users', user_id, user_data):
flash('User updated successfully!', 'success')
return redirect(url_for('view_user', user_id=user_id))
else:
flash('Failed to update user', 'error')
return render_template('users/edit.html', user=user_data)

return render_template('users/edit.html', user=user)

@app.route('/users/<int:user_id>/delete', methods=['POST'])
def delete_user(user_id):
"""Delete a user"""
if db.delete_record('users', user_id):
flash('User deleted successfully!', 'success')
else:
flash('Failed to delete user', 'error')

return redirect(url_for('list_users'))

if __name__ == '__main__':
app.run(debug=True)
16 changes: 16 additions & 0 deletions flask-database-app/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import os

class Config:
"""Configuration settings"""
# Secret key for session management
SECRET_KEY = os.environ.get('SECRET_KEY') or 'dev-key-change-in-production'

# Path to the database directory
DATABASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Database binary location
DB_BINARY = os.path.join(DATABASE_PATH, 'bin', 'db-project')

# Database and table names
DATABASE_NAME = 'myusers'
USERS_TABLE = 'users'
94 changes: 94 additions & 0 deletions flask-database-app/data_cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{
"databases": [
"school",
"company"
],
"tables": {
"users": {
"database": "company",
"columns": [
{
"name": "id",
"type": "INT"
},
{
"name": "name",
"type": "STRING"
},
{
"name": "email",
"type": "STRING"
}
]
},
"students": {
"database": "school",
"columns": [
{
"name": "id",
"type": "INT"
},
{
"name": "name",
"type": "STRING"
},
{
"name": "father_name",
"type": "STRING"
},
{
"name": "gpa",
"type": "FLOAT"
},
{
"name": "age",
"type": "INT"
},
{
"name": "gender",
"type": "STRING"
}
]
}
},
"records": {
"users": [
{
"id": 2,
"name": "Jane Smith",
"email": "jane@example.com"
},
{
"id": 12,
"name": "M Ahmed Butt",
"email": "admin@kurras.com"
}
],
"students": [
{
"id": 1,
"name": "John Doe",
"father_name": "Richard Roe",
"gpa": 3.5,
"age": 20,
"gender": "M"
},
{
"id": 2,
"name": "Jane Smith",
"father_name": "John Smith",
"gpa": 3.8,
"age": 22,
"gender": "F"
},
{
"id": 3,
"name": "Alice Johnson",
"father_name": "Robert Johnson",
"gpa": 3.2,
"age": 19,
"gender": "F"
}
]
}
}
Loading