/* -*- Mode: idl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Oracle Corporation code.
 *
 * The Initial Developer of the Original Code is
 *  Oracle Corporation
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#include "nsISupports.idl"

interface mozIStorageFunction;
interface mozIStorageStatement;
interface nsIFile;

[scriptable, uuid(623b8b2e-c9f9-4cc3-b15a-f3c96df2cc1c)]
interface mozIStorageConnection : nsISupports {
  /*
   * Initialization and status
   */

  /**
   * whether the database is open or not
   */
  readonly attribute boolean connectionReady;

  /**
   * the current database filename
   */
  readonly attribute AUTF8String databaseName;

  /**
   * lastInsertRowID returns the row ID from the last INSERT
   * operation.
   */
  readonly attribute long long lastInsertRowID;

  /**
   * lastError returns the last error code
   */
  readonly attribute long lastError;

  /**
   * lastErrorString returns the last error as a string
   */
  readonly attribute AUTF8String lastErrorString;

  /*
   * Statement creation
   */

  /**
   * Create a mozIStorageStatement for the given SQL expression.
   * The expression may use ?0 ?1 etc. to indicate numbered arguments.
   *
   * @param aSQLStatement  The SQL statement to execute
   *
   * @returns a new mozIStorageStatement
   */
  mozIStorageStatement createStatement (in AUTF8String aSQLStatement);

  /**
   * Execute a SQL expression, expecting no arguments.
   *
   * @param aSQLStatement  The SQL statement to execute
   */
  void executeSimpleSQL (in AUTF8String aSQLStatement);

  /**
   * Check if the given table exists.
   *
   * @param aTableName   The table to check
   * @returns TRUE if table exists, FALSE otherwise.
   */
  boolean tableExists (in AUTF8String aTableName);

  /*
   * Transactions
   */

  /**
   * Returns true if a transaction is active on this connection.
   */
  readonly attribute boolean transactionInProgress;

  /**
   * Begin a new transaction.  If a transaction is active,
   * @throws NS_ERROR_STORAGE_INVALID_OPERATION.
   */
  void beginTransaction ();

  /**
   * Commits the current transaction.  If no transaction is active,
   * @throws NS_ERROR_STORAGE_NO_TRANSACTION.
   */
  void commitTransaction ();

  /**
   * Rolls back the current transaction.  If no transaction is active,
   * @throws NS_ERROR_STORAGE_NO_TRANSACTION.
   */
  void rollbackTransaction ();

  /*
   * Tables
   */

  /**
   * Create the table with the given name and schema.  If the table
   * already exists with the given schema, the non-error
   * NS_STORAGE_TABLE_EXISTS is returned.  If the table exists but with
   * a different schema, NS_ERROR_STORAGE_TABLE_CONFLICT (?) is thrown.
   *
   * @param aID the owner of this table -- XXXtodo
   *
   * @param aTableName the table name to be created, consisting of [A-Za-z0-9_], and beginning with a letter.
   *
   * @param aTableSchema the schema of the table; what would normally go between the parens in a CREATE TABLE
   *                     statement: "foo INTEGER, bar STRING".
   */
  void createTable(in string aTableName,
                   in string aTableSchema);

  /*
   * Functions and Triggers
   */

  /**
   * Create a new SQLite function
   */
  void createFunction (in string aFunctionName,
                       in long aNumArguments,
                       in mozIStorageFunction aFunction);

  /**
   * Create a new trigger.  Note that we only support AFTER triggers here,
   * not BEFORE (so that we can support IPC notification of triggers).
   * All triggers created this way are TEMPORARY triggers, i.e. they are only
   * valid for the duration of this connection.  Without this, triggers would
   * persist between connections, even if the functions they refer to may no
   * longer be defined; this would be bad.
   *
   * aParameters is a comma-separated list of parameters, referencing
   * either "new.*" or "old.*", as appropriate for the trigger event
   * type.
   */
  const long TRIGGER_EVENT_DELETE = 1;
  const long TRIGGER_EVENT_INSERT = 2;
  const long TRIGGER_EVENT_UPDATE = 3;

  void createTrigger (in string aTriggerName,
                      in long aTriggerType,
                      in string aTableName,
                      in string aTriggerFunction,
                      in string aParameters);

  void removeTrigger (in string aTriggerName);
};
