{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c7322d1a-d57a-4430-8447-6f0e52cfadfb",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "from DATA225utils import make_connection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "658fad79-a547-44ed-afe0-90f0021d8392",
   "metadata": {},
   "outputs": [],
   "source": [
    "conn = make_connection(config_file = 'titanic.ini')\n",
    "cursor = conn.cursor()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "211d2de0-a797-4010-ada2-65246482e683",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute('DROP PROCEDURE IF EXISTS some_passengers')\n",
    "cursor.execute('DROP PROCEDURE IF EXISTS find_passengers')\n",
    "cursor.execute('DROP PROCEDURE IF EXISTS count_passengers')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "01f38544-5d46-43f2-b21c-1bebf8ab7932",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute( \n",
    "    \"\"\"\n",
    "    CREATE PROCEDURE some_passengers()\n",
    "    BEGIN\n",
    "        SELECT *\n",
    "        FROM passengers\n",
    "        LIMIT 10;\n",
    "    END\n",
    "    \"\"\"\n",
    "              )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "78162a3b-8fc2-48ca-9aaa-1cd3cbc19946",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute( \n",
    "    \"\"\"\n",
    "    CREATE PROCEDURE find_passengers(IN sex CHAR(6), \n",
    "                                     IN min_age DOUBLE, IN max_age DOUBLE, \n",
    "                                     IN survived CHAR(3), IN klass CHAR(3))\n",
    "    BEGIN\n",
    "        SELECT p.name, p.age, p.survived, p.passenger_class\n",
    "        FROM passengers AS p\n",
    "        WHERE p.sex = sex\n",
    "        AND   p.age BETWEEN min_age AND max_age\n",
    "        AND   p.survived = survived\n",
    "        AND   p.passenger_class = klass;\n",
    "    END\n",
    "    \"\"\"\n",
    "              )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bd2e327e-8a4a-4f90-b022-aa0db5358cba",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.execute( \n",
    "    \"\"\"\n",
    "    CREATE PROCEDURE count_passengers(IN sex CHAR(6), \n",
    "                                      IN min_age DOUBLE, IN max_age DOUBLE, \n",
    "                                      IN survived CHAR(3), IN klass CHAR(3), \n",
    "                                      OUT kount INT)\n",
    "    BEGIN\n",
    "        SELECT count(*)\n",
    "        INTO kount\n",
    "        FROM passengers AS p\n",
    "        WHERE p.sex = sex\n",
    "        AND   p.age BETWEEN min_age AND max_age\n",
    "        AND   p.survived = survived\n",
    "        AND   p.passenger_class = klass;\n",
    "    END\n",
    "    \"\"\"\n",
    "              )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "31d590bd-e8c8-4eab-8277-ff8e123326d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "cursor.close()\n",
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dafbbc6b-c658-4725-971b-21ca90e54efb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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
}
