{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ab899624-5f05-4985-b743-469e2f3da4b5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from DATA225utils import make_connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "79e1fb24-0186-43f0-bdd3-03d0518ae4d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "conn = make_connection(config_file = 'school.ini')\n",
    "cursor = conn.cursor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2f5cb9df-043b-415d-8920-5f0f6529c6b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute('DROP PROCEDURE IF EXISTS teachers_names')\n",
    "cursor.execute('DROP PROCEDURE IF EXISTS students_of')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d5c42666-5d60-4dfc-801e-62831aa1fbd3",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "cursor.execute( \n",
    "    \"\"\"\n",
    "    CREATE PROCEDURE teachers_names()\n",
    "    BEGIN\n",
    "        SELECT first, last FROM teacher\n",
    "        ORDER BY last;\n",
    "    END\n",
    "    \"\"\"\n",
    "              )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b1b83e48-ea33-4830-b756-c0f452564988",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CALL teachers_names\n",
      "\n",
      "Mabel Flynn\n",
      "John Lane\n",
      "Pat Rogers\n",
      "Sidney Thompson\n"
     ]
    }
   ],
   "source": [
    "sql = 'CALL teachers_names'\n",
    "print(sql)\n",
    "print()\n",
    "\n",
    "generator = cursor.execute(sql, multi=True)\n",
    "\n",
    "for result in generator:\n",
    "    rows = cursor.fetchall()\n",
    "    \n",
    "    for row in rows:\n",
    "        name = row[0] + ' ' + row[1]\n",
    "        print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b33205f0-a081-4f9b-9d6c-e64145220a99",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute( \n",
    "    \"\"\"\n",
    "    CREATE PROCEDURE students_of(IN first VARCHAR(32), \n",
    "                                 IN last VARCHAR(32))\n",
    "    BEGIN\n",
    "        SELECT student.id, student.first, student.last, \n",
    "               class.subject\n",
    "        FROM student, teacher, class, takes\n",
    "        WHERE teacher.last = last\n",
    "        AND teacher.first = first\n",
    "        AND class.teacher_id = teacher.id \n",
    "        AND takes.class_code = class.code\n",
    "        AND takes.student_id = student.id \n",
    "        ORDER BY class.subject, student.last;\n",
    "    END\n",
    "    \"\"\"\n",
    "              )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e75a0128-2c2b-44a6-9acc-33009a48a71b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CALL students_of('Mabel', 'Flynn')\n",
      "\n",
      "('S1014', 'Mary', 'Jane', 'Machine Learning')\n",
      "('S1005', 'Tim', 'Novak', 'Machine Learning')\n",
      "('S1014', 'Mary', 'Jane', 'Python Programming')\n",
      "('S1021', 'Kim', 'Smith', 'Python Programming')\n"
     ]
    }
   ],
   "source": [
    "sql = \"CALL students_of('Mabel', 'Flynn')\"\n",
    "print(sql)\n",
    "print()\n",
    "\n",
    "generator = cursor.execute(sql, multi=True)\n",
    "\n",
    "for result in generator:\n",
    "    rows = cursor.fetchall()\n",
    "    \n",
    "    for row in rows:\n",
    "        print(row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4d7b914e-b5bb-4a17-b3ae-89175ba0d13f",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.close()\n",
    "conn.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
