Class: SQLite3::Backup

Inherits:
Object
  • Object
show all
Defined in:
ext/sqlite3/backup.c

Instance Method Summary collapse

Constructor Details

#SQLite3::Backup.new(dstdb, dstname, srcdb, srcname) ⇒ Object

Initialize backup the backup.

dstdb:

the destination SQLite3::Database object.

dstname:

the destination's database name.

srcdb:

the source SQLite3::Database object.

srcname:

the source's database name.

The database name is "main", "temp", or the name specified in an ATTACH statement.

This feature requires SQLite 3.6.11 or later.

require 'sqlite3'
sdb = SQLite3::Database.new('src.sqlite3')

ddb = SQLite3::Database.new(':memory:')
b = SQLite3::Backup.new(ddb, 'main', sdb, 'main')
p [b.remaining, b.pagecount] # invalid value; for example [0, 0]
begin
p b.step(1) #=> OK or DONE
p [b.remaining, b.pagecount]
end while b.remaining > 0
b.finish

ddb = SQLite3::Database.new(':memory:')
b = SQLite3::Backup.new(ddb, 'main', sdb, 'main')
b.step(-1) #=> DONE
b.finish


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'ext/sqlite3/backup.c', line 74

static VALUE
initialize(VALUE self, VALUE dstdb, VALUE dstname, VALUE srcdb, VALUE srcname)
{
    sqlite3BackupRubyPtr ctx;
    sqlite3RubyPtr ddb_ctx, sdb_ctx;
    sqlite3_backup *pBackup;

    TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
    ddb_ctx = sqlite3_database_unwrap(dstdb);
    sdb_ctx = sqlite3_database_unwrap(srcdb);

    if (!sdb_ctx->db) {
        rb_raise(rb_eArgError, "cannot backup from a closed database");
    }
    if (!ddb_ctx->db) {
        rb_raise(rb_eArgError, "cannot backup to a closed database");
    }

    pBackup = sqlite3_backup_init(ddb_ctx->db, StringValuePtr(dstname),
                                  sdb_ctx->db, StringValuePtr(srcname));
    if (pBackup) {
        ctx->p = pBackup;
    } else {
        CHECK(ddb_ctx->db, sqlite3_errcode(ddb_ctx->db));
    }

    return self;
}

Instance Method Details

#SQLite3::Backup ⇒ Object

Destroy the backup object.



133
134
135
136
137
138
139
140
141
142
143
# File 'ext/sqlite3/backup.c', line 133

static VALUE
finish(VALUE self)
{
    sqlite3BackupRubyPtr ctx;

    TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
    REQUIRE_OPEN_BACKUP(ctx);
    (void)sqlite3_backup_finish(ctx->p);
    ctx->p = NULL;
    return Qnil;
}

#SQLite3::Backup ⇒ Object

Returns the total number of pages in the source database file.

Note that the value is only updated after step() is called, so before calling step() returned value is invalid.



169
170
171
172
173
174
175
176
177
# File 'ext/sqlite3/backup.c', line 169

static VALUE
pagecount(VALUE self)
{
    sqlite3BackupRubyPtr ctx;

    TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
    REQUIRE_OPEN_BACKUP(ctx);
    return INT2NUM(sqlite3_backup_pagecount(ctx->p));
}

#SQLite3::Backup ⇒ Object

Returns the number of pages still to be backed up.

Note that the value is only updated after step() is called, so before calling step() returned value is invalid.



152
153
154
155
156
157
158
159
160
# File 'ext/sqlite3/backup.c', line 152

static VALUE
remaining(VALUE self)
{
    sqlite3BackupRubyPtr ctx;

    TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
    REQUIRE_OPEN_BACKUP(ctx);
    return INT2NUM(sqlite3_backup_remaining(ctx->p));
}

#SQLite3::Backup ⇒ Object

Copy database pages up to nPage. If negative, copy all remaining source pages.

If all pages are copied, it returns SQLite3::Constants::ErrorCode::DONE. When coping is not done, it returns SQLite3::Constants::ErrorCode::OK. When some errors occur, it returns the error code.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'ext/sqlite3/backup.c', line 112

static VALUE
step(VALUE self, VALUE nPage)
{
    sqlite3BackupRubyPtr ctx;
    rb_sqlite3_callback_frame_t frame;
    int pages, status, callback_state;

    TypedData_Get_Struct(self, sqlite3BackupRuby, &backup_type, ctx);
    REQUIRE_OPEN_BACKUP(ctx);
    pages = NUM2INT(nPage);

    rb_sqlite3_push_callback_frame(&frame);
    status = sqlite3_backup_step(ctx->p, pages);
    if ((callback_state = rb_sqlite3_pop_callback_frame(&frame))) { rb_jump_tag(callback_state); }
    return INT2NUM(status);
}