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.



952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
# File 'ext/oci8/lob.c', line 952

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.



989
990
991
992
993
994
995
# File 'ext/oci8/lob.c', line 989

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.



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
# File 'ext/oci8/lob.c', line 1019

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.



1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
# File 'ext/oci8/lob.c', line 1053

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.



1004
1005
1006
1007
1008
1009
1010
# File 'ext/oci8/lob.c', line 1004

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.



1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
# File 'ext/oci8/lob.c', line 1037

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



1069
1070
1071
1072
# File 'ext/oci8/lob.c', line 1069

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



1069
1070
1071
1072
# File 'ext/oci8/lob.c', line 1069

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



1069
1070
1071
1072
# File 'ext/oci8/lob.c', line 1069

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