3.2. The Connection Class


Figure 3-1. class Connection

namespace Oracle
{
    class Connection
    {
        public:
            // types
            enum status_t { not_connected, connected };

            // constructors/destructor
            Connection(
                const string&,
                const string&,
                const string& = "");
            Connection(const string&);
            virtual ~Connection();

            // implementors
            virtual void open();
            virtual Stmt* prepare(const string&);
            virtual void rollback();
            virtual void commit();
            virtual void close();

        friend class Stmt;
    };
}

3.2.1. Public Member Functions

3.2.1.1. Constructors

Connection(const string& connect_string);

Connection(const string& username, const string& password, const string& database_name);

The first form takes a standard Oracle connect string of the form "username/password@database". The second form takes the username, password and database name as separate arguments and is deprecated. The constructors simply initialize the object's data members; no connection is attempted.

3.2.1.2. close()

void close(void);

This function rolls back any pending transactions and disconnects from the server.

3.2.1.3. commit()

void commit(void);

This function performs a commit.

3.2.1.4. open()

void open(void);

This function opens the database connection.

3.2.1.5. prepare()

Stmt* prepare(const string& stmt_text);

This method takes as its single argument a string containing a SQL statement and returns a pointer to a Stmt object. The actual type of object created is either Select_Stmt or Non_Sel_Stmt.

The prepare() method creates a Stmt object with new, so the user must use delete to free the object when it is no longer needed.

This method is most useful for interactive programs where the type of the SQL statement is not known ahead of time. If the type of statement is known, the specific statement type class constructors (Select_Stmt::Select_Stmt() or Non_Sel_Stmt::Non_Sel_Stmt()) are preferred over prepare().

3.2.1.6. rollback()

void rollback(void);

This function performs a rollback.