Class: OCI8::BFILE

Inherits:
LOB show all
Defined in:
ext/oci8/lob.c,
ext/oci8/lob.c

Overview

This class is a ruby-side class of Oracle BFILE datatype. It is a read-only LOB. You cannot change the contents.

You can read files on the server-side as follows:

  1. Connect to the Oracle server as a user who has CREATE DIRECTORY privilege.

    # create a directory object on the Oracle server.
    CREATE DIRECTORY file_storage_dir AS '/opt/file_storage';
    # grant a privilege to read files on file_storage_dir directory to a user.
    GRANT READ ON DIRECTORY file_storage_dir TO username;
    
  2. Create a file ‘hello_world.txt’ in the directory ‘/opt/file_storage’ on the server filesystem.

    echo 'Hello World!' > /opt/file_storage/hello_world.txt
    
  3. Read the file by ruby-oci8.

    require 'oci8'
    # The user must have 'READ ON DIRECTORY file_storage_dir' privilege.
    conn = OCI8.new('username/password')
    
    # The second argument is usually an uppercase string unless the directory
    # object is explicitly created as *double-quoted* lowercase characters.
    bfile = OCI8::BFILE.new(conn, 'FILE_STORAGE_DIR', 'hello_world.txt')
    bfile.read # => "Hello World!\n"
    

Instance Method Summary collapse

Methods inherited from LOB

#available?, #chunk_size, #close, #eof?, #pos, #read, #rewind, #seek, #size

Constructor Details

#initialize(conn, directory = nil, filename = nil) ⇒ OCI8::BFILE

Creates a BFILE object. This is correspond to BFILENAME.



908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
# File 'ext/oci8/lob.c', line 908

static VALUE oci8_bfile_initialize(int argc, VALUE *argv, VALUE self)
{
    oci8_lob_t *lob = TO_LOB(self);
    VALUE svc;
    VALUE dir_alias;
    VALUE filename;
    oci8_svcctx_t *svcctx;
    int rv;

    rb_scan_args(argc, argv, "12", &svc, &dir_alias, &filename);
    svcctx = oci8_get_svcctx(svc);
    rv = OCIDescriptorAlloc(oci8_envhp, &lob->base.hp.ptr, OCI_DTYPE_LOB, 0, NULL);
    if (rv != OCI_SUCCESS) {
        oci8_env_raise(oci8_envhp, rv);
    }
    lob->base.type = OCI_DTYPE_LOB;
    lob->pos = 0;
    lob->csfrm = SQLCS_IMPLICIT;
    lob->lobtype = OCI_TEMP_BLOB;
    lob->state = S_BFILE_CLOSE;
    if (argc != 1) {
        OCI8SafeStringValue(dir_alias);
        OCI8SafeStringValue(filename);
        oci8_bfile_set_name(self, dir_alias, filename);
    }
    oci8_link_to_parent(&lob->base, &svcctx->base);
    lob->svcctx = svcctx;
    return Qnil;
}

Instance Method Details

#dir_aliasString

Returns the directory object name.



945
946
947
948
949
950
951
# File 'ext/oci8/lob.c', line 945

static VALUE oci8_bfile_get_dir_alias(VALUE self)
{
    VALUE dir_alias;

    oci8_bfile_get_name(self, &dir_alias, NULL);
    return dir_alias;
}

#dir_alias=(dir_alias) ⇒ Object

Changes the directory object name.



975
976
977
978
979
980
981
982
983
984
# File 'ext/oci8/lob.c', line 975

static VALUE oci8_bfile_set_dir_alias(VALUE self, VALUE dir_alias)
{
    VALUE filename;

    OCI8SafeStringValue(dir_alias);
    oci8_bfile_get_name(self, NULL, &filename);
    oci8_bfile_set_name(self, dir_alias, filename);
    rb_ivar_set(self, id_dir_alias, dir_alias);
    return dir_alias;
}

#exists?Boolean

Returns true when the BFILE exists on the server’s operating system.



1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
# File 'ext/oci8/lob.c', line 1009

static VALUE oci8_bfile_exists_p(VALUE self)
{
    oci8_lob_t *lob = TO_LOB(self);
    oci8_svcctx_t *svcctx = check_svcctx(lob);
    boolean flag;

    chker2(OCILobFileExists_nb(svcctx, svcctx->base.hp.svc, oci8_errhp, lob->base.hp.lob, &flag),
           &svcctx->base);
    return flag ? Qtrue : Qfalse;
}

#filenameString

Returns the file name.



960
961
962
963
964
965
966
# File 'ext/oci8/lob.c', line 960

static VALUE oci8_bfile_get_filename(VALUE self)
{
    VALUE filename;

    oci8_bfile_get_name(self, NULL, &filename);
    return filename;
}

#filename=(filename) ⇒ Object

Changes the file name.



993
994
995
996
997
998
999
1000
1001
1002
# File 'ext/oci8/lob.c', line 993

static VALUE oci8_bfile_set_filename(VALUE self, VALUE filename)
{
    VALUE dir_alias;

    OCI8SafeStringValue(filename);
    oci8_bfile_get_name(self, &dir_alias, NULL);
    oci8_bfile_set_name(self, dir_alias, filename);
    rb_ivar_set(self, id_filename, filename);
    return filename;
}

#size=(dummy) ⇒ Object

Raises RuntimeError always.

Raises:

  • (RuntimeError)

    cannot modify a read-only BFILE object



1025
1026
1027
1028
# File 'ext/oci8/lob.c', line 1025

static VALUE oci8_bfile_error(VALUE self, VALUE dummy)
{
    rb_raise(rb_eRuntimeError, "cannot modify a read-only BFILE object");
}

#truncate(dummy) ⇒ Object

Raises RuntimeError always.

Raises:

  • (RuntimeError)

    cannot modify a read-only BFILE object



1025
1026
1027
1028
# File 'ext/oci8/lob.c', line 1025

static VALUE oci8_bfile_error(VALUE self, VALUE dummy)
{
    rb_raise(rb_eRuntimeError, "cannot modify a read-only BFILE object");
}

#write(dummy) ⇒ Object

Raises RuntimeError always.

Raises:

  • (RuntimeError)

    cannot modify a read-only BFILE object



1025
1026
1027
1028
# File 'ext/oci8/lob.c', line 1025

static VALUE oci8_bfile_error(VALUE self, VALUE dummy)
{
    rb_raise(rb_eRuntimeError, "cannot modify a read-only BFILE object");
}