{# query_edit_select.html #}

{% extends "base.html" %}

{% block extrahead %}
    <style type="text/css">{{ sql_highlight_css }}</style>
{% endblock %}

{% block content %}

    {% include "query_nav.html" %}

    <h1>Create/edit SQL query</h1>

    <form action="{% url 'query' %}" method="post">
        {% csrf_token %}
        <table class="formtable">
            {{ form }}
        </table>
        <input type="submit" name="submit_add" value="Add" />
        <input type="submit" name="submit_run" value="Run" />
        <input type="submit" name="submit_builder"
               value="Send to query builder" />
    </form>

    <h2>Tips</h2>
    <ul>
        <li>Most browsers will let you resize the SQL edit box; try dragging
            the corner.</li>

        <li>If you’re new to SQL, try a tutorial such as those by
            <a href="http://sqlzoo.net/">SQLZoo</a>,
            <a href="http://www.sql-tutorial.net/">SQL-Tutorial</a>,
            <a href="https://www.codecademy.com/learn/learn-sql">Codecademy</a>, or
            <a href="http://www.w3schools.com/sql/">W3Schools</a>.</li>

        {% if dialect_mysql %}
            <li>The research database runs MySQL.</li>
            <li>Append <b><code>LIMIT row_count</code></b> or
                <b><code>LIMIT offset, row_count</code></b> to limit the results,
                where <code>offset</code> is numbered from 0 (not 1), and
                <code>row_count</code> is the number of records to retrieve
                (so <code>LIMIT 50</code> or <code>LIMIT 0, 50</code> retrieves
                the first 50 records, and <code>LIMIT 50, 50</code> retrieves the
                next 50).</li>
        {% elif dialect_mssql %}
            <li>The research database runs Microsoft SQL Server.</li>
            <li>Use <b><code>SELECT TOP 100 ...</code></b>
                to limit the results to the first 100 rows.
                </li>
        {% endif %}

        <li>The query builder is not as sophisticated as raw SQL, so it may
            fail to alter complex SQL properly.</li>
    </ul>

    <h1>Previous queries</h1>

    {% include "expand_collapse_buttons.html" %}

    {% if queries %}
        <table>
            <tr>
                <th>Created at</th>
                <th>Delete query</th>
                <th>Active?</th>
                <th>Choose this one</th>
                <th>SQL</th>
            </tr>
            {% for query in queries %}
                <tr>
                    <td>{{ query.created }}</td>
                    <td>
                        <form action="{% url 'delete_query' query.id %}" method="post">
                            {% csrf_token %}
                            <input type="submit" value="Delete" />
                        </form>
                    </td>
                    <td>
                        {% if query.active %}<b>{% endif %}
                        {{ query.active|yesno }}
                        {% if query.active %}</b>{% endif %}
                    </td>
                    <td>
                        {% if query.active %}
                            (activated)
                        {% else %}
                            <form action="{% url 'activate_query' query.id %}" method="post">
                                {% csrf_token %}
                                <input type="submit" value="Choose" />
                            </form>
                        {% endif %}
                    </td>
                    {% comment %}Widest, so last:{% endcomment %}
                    <td>{{ query.formatted_query_safe|safe }}</td>
                </tr>
            {% endfor %}
        </table>
        {% include "page_nav.html" with page=queries %}

    {% else %}
        <div>None.</div>
    {% endif %}

{% endblock %}
