class SQLRConnection - RDoc Documentation

# class SQLRConnection

### Public Class Methods

new(host, port, socket, user, password, retrytime, tries) click to toggle source

Initiates a connection to “host” on “port” or to the unix “socket” on the local machine and auths with “user” and “password”. Failed connections will be retried for “tries” times, waiting “retrytime” seconds between each try. If “tries” is 0 then retries will continue forever. If “retrytime” is 0 then retries will be attempted on a default interval. If the “socket” parameter is nether nil nor “” then an attempt will be made to connect through it before attempting to connect to “host” on “port”. If it is nil or “” then no attempt will be made to connect through the socket.

    static VALUE sqlrcon_new(VALUE self, VALUE host, VALUE port, VALUE socket,
                                    VALUE user, VALUE password,
                                    VALUE retrytime, VALUE tries) {
            const char     *socketstr;
            if (socket==Qnil) {
                    socketstr="";
            } else {
                    socketstr=STR2CSTR(socket);
            }
            sqlrconnection *sqlrcon=new sqlrconnection(STR2CSTR(host),
                                                            NUM2INT(port),
                                                            socketstr,
                                                            STR2CSTR(user),
                                                            STR2CSTR(password),
                                                            NUM2INT(retrytime),
                                                            NUM2INT(tries),
                                                            true);
            return Data_Wrap_Struct(self,0,sqlrcon_free,(void *)sqlrcon);
    }

### Public Instance Methods

autoCommitOff() click to toggle source

Instructs the database to wait for the client to tell it when to commit.

    static VALUE sqlrcon_autoCommitOff(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->autoCommitOff());
    }

autoCommitOn() click to toggle source

Instructs the database to perform a commit after every successful query.

    static VALUE sqlrcon_autoCommitOn(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->autoCommitOn());
    }

begin() click to toggle source

Begins a transaction. Returns true if the begin succeeded, false if it failed. If the database automatically begins a new transaction when a commit or rollback is issued then this doesn't do anything unless SQL Relay is faking transaction blocks.

    static VALUE sqlrcon_begin(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->begin());
    }

bindFormat() click to toggle source

Returns a string representing the format of the bind variables used in the db.

    static VALUE sqlrcon_bindFormat(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->bindFormat();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

clientVersion() click to toggle source

Returns the version of the sqlrelay client software.

    static VALUE sqlrcon_clientVersion(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->clientVersion();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

commit() click to toggle source

Issues a commit. Returns true if the commit succeeded, false if it failed.

    static VALUE sqlrcon_commit(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->commit());
    }

dbHostName() click to toggle source

Returns the host name of the database

    static VALUE sqlrcon_dbHostName(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->dbHostName();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

dbIpAddress() click to toggle source

Returns the ip address of the database

    static VALUE sqlrcon_dbIpAddress(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->dbIpAddress();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

dbVersion() click to toggle source

Returns the version of the database

    static VALUE sqlrcon_dbVersion(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->dbVersion();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

debugOff() click to toggle source

Turns debugging off.

    static VALUE sqlrcon_debugOff(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->debugOff();
            return Qnil;
    }

debugOn() click to toggle source

Causes verbose debugging information to be sent to standard output. Another way to do this is to start a query with “– debugn”. Yet another way is to set the environment variable SQLR_CLIENT_DEBUG to “ON”

    static VALUE sqlrcon_debugOn(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->debugOn();
            return Qnil;
    }

disableEncryption() click to toggle source

Disables encryption.

    static VALUE sqlrcon_disableEncryption(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->disableEncryption();
            return Qnil;
    }

enableKerberos(p1, p2, p3) click to toggle source

Enables Kerberos authentication and encryption.

“service” indicates the Kerberos service name of the SQL Relay server. If left empty or NULL then the service name “sqlrelay” will be used. “sqlrelay” is the default service name of the SQL Relay server. Note that on Windows platforms the service name must be fully qualified, including the host and realm name. For example: “sqlrelay/sqlrserver.firstworks.com@AD.FIRSTWORKS.COM”.

“mech” indicates the specific Kerberos mechanism to use. On Linux/Unix platforms, this should be a string representation of the mechnaism's OID, such as:

    { 1 2 840 113554 1 2 2 }

On Windows platforms, this should be a string like:

    Kerberos

If left empty or NULL then the default mechanism will be used. Only set this if you know that you have a good reason to.

“flags” indicates what Kerberos flags to use. Multiple flags may be specified, separated by commas. If left empty or NULL then a defalt set of flags will be used. Only set this if you know that you have a good reason to.

Valid flags include:

    * GSS_C_MUTUAL_FLAG
    * GSS_C_REPLAY_FLAG
    * GSS_C_SEQUENCE_FLAG
    * GSS_C_CONF_FLAG
    * GSS_C_INTEG_FLAG

For a full list of flags, consult the GSSAPI documentation, though note that only the flags listed above are supported on Windows.

    static VALUE sqlrcon_enableKerberos(VALUE self,
                                    VALUE service, VALUE mech, VALUE flags) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->enableKerberos(STR2CSTR(service),
                                    STR2CSTR(mech),
                                    STR2CSTR(flags));
            return Qnil;
    }

enableTls(p1, p2, p3, p4, p5, p6, p7) click to toggle source

Enables TLS/SSL encryption, and optionally authentication.

“version” specifies the TLS/SSL protocol version that the client will attempt to use. Valid values include SSL2, SSL3, TLS1, TLS1.1, TLS1.2 or any more recent version of TLS, as supported by and enabled in the underlying TLS/SSL library. If left blank or empty then the highest supported version will be negotiated.

“cert” is the file name of the certificate chain file to send to the SQL Relay server. This is only necessary if the SQL Relay server is configured to authenticate and authorize clients by certificate.

If “cert” contains a password-protected private key, then “password” may be supplied to access it. If the private key is not password-protected, then this argument is ignored, and may be left empty or NULL.

“ciphers” is a list of ciphers to allow. Ciphers may be separated by spaces, commas, or colons. If “ciphers” is empty or NULL then a default set is used. Only set this if you know that you have a good reason to.

For a list of valid ciphers on Linux/Unix platforms, see:

    man ciphers

For a list of valid ciphers on Windows platforms, see:

    https://msdn.microsoft.com/en-us/library/windows/desktop/aa375549%28v=vs.85%29.aspx

On Windows platforms, the ciphers (alg_id's) should omit CALG_ and may be given with underscores or dashes. For example: 3DES_112

“validate” indicates whether to validate the SQL Relay's server certificate, and may be set to one of the following:

    "no" - Don't validate the server's certificate.
    "ca" - Validate that the server's certificate was
           signed by a trusted certificate authority.
    "ca+host" - Perform "ca" validation and also validate
           that one of the subject altenate names (or the
           common name if no SANs are present) in the
           certificate matches the host parameter.
           (Falls back to "ca" validation when a unix
           socket is used.)
    "ca+domain" - Perform "ca" validation and also validate
           that the domain name of one of the subject
           alternate names (or the common name if no SANs
           are present) in the certificate matches the
           domain name of the host parameter.  (Falls back
           to "ca" validation when a unix socket is used.)

“ca” is the location of a certificate authority file to use, in addition to the system's root certificates, when validating the SQL Relay server's certificate. This is useful if the SQL Relay server's certificate is self-signed.

On Windows, “ca” must be a file name.

On non-Windows systems, “ca” can be either a file or directory name. If it is a directory name, then all certificate authority files found in that directory will be used. If it a file name, then only that file will be used.

Note that the supported “cert” and “ca” file formats may vary between platforms. A variety of file formats are generally supported on Linux/Unix platfoms (.pem, .pfx, etc.) but only the .pfx format is currently supported on Windows.

    static VALUE sqlrcon_enableTls(VALUE self,
                                    VALUE version, VALUE cert, VALUE password,
                                    VALUE ciphers, VALUE validate, VALUE ca,
                                    VALUE depth) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->enableTls(STR2CSTR(version),
                                    STR2CSTR(cert),
                                    STR2CSTR(password),
                                    STR2CSTR(ciphers),
                                    STR2CSTR(validate),
                                    STR2CSTR(ca),
                                    NUM2INT(depth));
            return Qnil;
    }

endSession() click to toggle source

Ends the session.

    static VALUE sqlrcon_endSession(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->endSession();
            return Qnil;
    }

errorMessage() click to toggle source

If an operation failed and generated an error, the error message is available here. If there is no error then this method returns nil

    static VALUE sqlrcon_errorMessage(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->errorMessage();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

errorNumber() click to toggle source

If an operation failed and generated an error, the error number is available here. If there is no error then this method returns 0.

    static VALUE sqlrcon_errorNumber(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->errorNumber());
    }

getClientInfo() click to toggle source

Returns the string that was set by setClientInfo().

    static VALUE sqlrcon_getClientInfo(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->getClientInfo();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

getConnectionPort() click to toggle source

Returns the inet port that the connection is communicating over. This parameter may be passed to another connection for use in the sqlrcon_resumeSession() command. Note: The result this function returns is only valid after a call to suspendSession().

    static VALUE sqlrcon_getConnectionPort(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->getConnectionPort());
    }

getConnectionSocket() click to toggle source

Returns the unix socket that the connection is communicating over. This parameter may be passed to another connection for use in the sqlrcon_resumeSession() command. Note: The result this function returns is only valid after a call to suspendSession().

    static VALUE sqlrcon_getConnectionSocket(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->getConnectionSocket();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

getCurrentDatabase() click to toggle source

Returns the database/schema that is currently in use.

    static VALUE sqlrcon_getCurrentDatabase(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->getCurrentDatabase();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

getDebug() click to toggle source

Returns 0 if debugging is off and 1 if debugging is on.

    static VALUE sqlrcon_getDebug(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->getDebug());
    }

getLastInsertId() click to toggle source

Returns the value of the autoincrement column for the last insert

    static VALUE sqlrcon_getLastInsertId(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->getLastInsertId());
    }

identify() click to toggle source

Returns the type of database: oracle, postgresql, mysql, etc.

    static VALUE sqlrcon_identify(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->identify();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

ping() click to toggle source

Returns 1 if the database is up and 0 if it's down.

    static VALUE sqlrcon_ping(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->ping());
    }

resumeSession(port,socket) click to toggle source

Resumes a session previously left open using sqlrcon_suspendSession(). Returns 1 on success and 0 on failure.

    static VALUE sqlrcon_resumeSession(VALUE self, VALUE port, VALUE socket) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->resumeSession(NUM2INT(port),
                                                            STR2CSTR(socket)));
    }

rollback() click to toggle source

Issues a rollback. Returns true if the rollback succeeded, false if it failed.

    static VALUE sqlrcon_rollback(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->rollback());
    }

selectDatabase(database) click to toggle source

Sets the current database/schema to “database”

    static VALUE sqlrcon_selectDatabase(VALUE self, VALUE db) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->selectDatabase(STR2CSTR(db)));
    }

serverVersion() click to toggle source

Returns the version of the sqlrelay server software.

    static VALUE sqlrcon_serverVersion(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            const char     *result=sqlrcon->serverVersion();
            if (result) {
                    return rb_str_new2(result);
            } else {
                    return Qnil;
            }
    }

setAuthenticationTimeout(timeoutsec,timeoutusec) click to toggle source

Sets the authentication timeout in seconds and milliseconds. Setting either parameter to -1 disables the timeout. You can also set this timeout using the SQLR_CLIENT_AUTHENTICATION_TIMEOUT environment variable.

    static VALUE sqlrcon_setAuthenticationTimeout(VALUE self,
                                    VALUE timeoutsec, VALUE timeoutusec) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->setAuthenticationTimeout(NUM2INT(timeoutsec),
                                                    NUM2INT(timeoutusec));
            return Qnil;
    }

setClientInfo(clientinfo) click to toggle source

Allows you to set a string that will be passed to the server and ultimately included in server-side logging along with queries that were run by this instance of the client.

    static VALUE sqlrcon_setClientInfo(VALUE self, VALUE clientinfo) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->setClientInfo(STR2CSTR(clientinfo));
            return Qnil;
    }

setConnectTimeout(timeoutsec,timeoutusec) click to toggle source

Sets the server connect timeout in seconds and milliseconds. Setting either parameter to -1 disables the timeout. You can also set this timeout using the SQLR_CLIENT_CONNECT_TIMEOUT environment variable.

    static VALUE sqlrcon_setConnectTimeout(VALUE self,
                                    VALUE timeoutsec, VALUE timeoutusec) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->setConnectTimeout(NUM2INT(timeoutsec),NUM2INT(timeoutusec));
            return Qnil;
    }

setDebugFile(filename) click to toggle source

Allows you to specify a file to write debug to. Setting “filename” to NULL or an empty string causes debug to be written to standard output (the default).

    static VALUE sqlrcon_setDebugFile(VALUE self, VALUE filename) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->setDebugFile(STR2CSTR(filename));
            return Qnil;
    }

setResponseTimeout(timeoutsec,timeoutusec) click to toggle source

Sets the response timeout (for queries, commits, rollbacks, pings, etc.) in seconds and milliseconds. Setting either parameter to -1 disables the timeout. You can also set this timeout using the SQLR_CLIENT_RESPONSE_TIMEOUT environment variable.

    static VALUE sqlrcon_setResponseTimeout(VALUE self,
                                    VALUE timeoutsec, VALUE timeoutusec) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            sqlrcon->setResponseTimeout(NUM2INT(timeoutsec),NUM2INT(timeoutusec));
            return Qnil;
    }

suspendSession() click to toggle source

Disconnects this connection from the current session but leaves the session open so that another connection can connect to it using sqlrcon_resumeSession().

    static VALUE sqlrcon_suspendSession(VALUE self) {
            sqlrconnection *sqlrcon;
            Data_Get_Struct(self,sqlrconnection,sqlrcon);
            return INT2NUM(sqlrcon->suspendSession());
    }