function build_deck_words_sql()
{
    $table_name = get_deck_words_table_name();
    $sql = 'CREATE TABLE `' . $table_name . '` (
    `deck_id` int(10) NOT NULL,
    `dictionary_id` int(10) NOT NULL,
    PRIMARY KEY (deck_id, dictionary_id),
    FOREIGN KEY (deck_id) REFERENCES ' . get_decks_table_name() . '(id) ON DELETE CASCADE,

    FOREIGN KEY (dictionary_id) REFERENCES ' . get_dictionary_table_name() . '(id) ON DELETE CASCADE
    )ENGINE=MyISAM DEFAULT CHARSET=latin1;';
    return $sql;
}
function wordpleh_uninstall()
{
    global $wpdb;
    // Get all the table names
    $deck_word_table_name = get_deck_words_table_name();
    $dictionary_table_name = get_dictionary_table_name();
    $deck_table_name = get_decks_table_name();
    $word_category_table_name = get_word_categories_table_name();
    // Drop all the tables
    $wpdb->query("DROP TABLE IF EXISTS {$deck_word_table_name}");
    $wpdb->query("DROP TABLE IF EXISTS {$dictionary_table_name}");
    $wpdb->query("DROP TABLE IF EXISTS {$deck_table_name}");
    $wpdb->query("DROP TABLE IF EXISTS {$word_category_table_name}");
    // Update the db version to 0 so the next time the plugin is run it will reinstall
    update_option("panno_db_version", PANO_DB_VERSION);
}
function wordpleh_install()
{
    global $wpdb;
    $installed_ver = get_option("pano_db_version");
    $table_name = get_dictionary_table_name();
    if ($wpdb->get_var("SHOW TABLES LIKE '{$table_name}'") != $table_name || $installed_ver != pano_DB_VERSION) {
        // Create tables
        $build_word_categories_sql = build_word_categories_sql();
        $build_dictionary_sql = build_dictionary_sql();
        $build_decks_sql = build_decks_sql();
        $build_deck_words_sql = build_deck_words_sql();
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        // Run the sql for the database
        dbDelta($build_word_categories_sql);
        dbDelta($build_dictionary_sql);
        dbDelta($build_decks_sql);
        dbDelta($build_deck_words_sql);
        update_option("pano_db_version", PANO_DB_VERSION);
        // create_first_row();
    }
}
function delete_word($word_id)
{
    global $wpdb;
    $word_table_name = get_dictionary_table_name();
    $wpdb->delete($word_table_name, array('id' => $word_id));
}