Method: SQLite3::Backup#initialize

Defined in:
ext/sqlite3/backup.c

#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


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/sqlite3/backup.c', line 59

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

  Data_Get_Struct(self, sqlite3BackupRuby, ctx);
  Data_Get_Struct(dstdb, sqlite3Ruby, ddb_ctx);
  Data_Get_Struct(srcdb, sqlite3Ruby, sdb_ctx);

  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;
}