// File: /workspace/github_cpp_makefile_star2_O2/maplechori[P]stata-reader-writer/SQLite.cpp
// Function: SQLite::write(Context&)
bool SQLite::write(Context & ctx) {
    char *zErrMsg = 0;
    int rc;
    stringstream sql_query;
    sqlite3_stmt *stmt = NULL;

    


    rc = sqlite3_exec(db,  "create table variables ("
                           "varpos integer primary key asc,"
                           "varname varchar(255),"
                           "vartype integer,"
                           "varformat varchar(20),"
                           "valuelabel varchar(255),"
                           "variablelabel varchar(255));" , NULL, 0, &zErrMsg);
    
    if( rc != SQLITE_OK )
    {
      cerr << "SQL error: " << zErrMsg << endl;
      sqlite3_free(zErrMsg);
      return false;
    }

    sql_query.str("");

    rc = sqlite3_prepare_v2(db, "INSERT INTO VARIABLES (VARPOS, VARNAME, VARTYPE, VARFORMAT, VALUELABEL, VARIABLELABEL ) VALUES ( NULL, ?, ?, ?, ?, ? )", -1, &stmt, NULL);

    if (rc != SQLITE_OK) {
        cerr << "SQLError prepare failed: " << sqlite3_errmsg(db) << endl;
        return false;
    } 

    sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, &zErrMsg);

    for (int j = 0; j < ctx.vList.size(); j++)
    {
        rc = sqlite3_bind_text(stmt, 1, ((StataVariables)*ctx.vList[j]).varname.c_str(), ((StataVariables)*ctx.vList[j]).varname.length(), SQLITE_STATIC);

        if (rc != SQLITE_OK) {
            cerr << "SQLError: varname bind failed: " << sqlite3_errmsg(db) << endl;
            return false;
        }

        rc = sqlite3_bind_int(stmt, 2, ((StataVariables)*ctx.vList[j]).type);

        if (rc != SQLITE_OK) {
            cerr << "SQLError: type bind failed: " << sqlite3_errmsg(db) << endl;
            return false;
        }

        rc = sqlite3_bind_text(stmt, 3, ((StataVariables)*ctx.vList[j]).format.c_str(), ((StataVariables)*ctx.vList[j]).format.length(), SQLITE_STATIC);

        if (rc != SQLITE_OK) {
            cerr << "SQLError: format bind failed: " << sqlite3_errmsg(db) << endl;
            return false;
        }

        rc = sqlite3_bind_text(stmt, 4, ((StataVariables)*ctx.vList[j]).vallbl.c_str(), ((StataVariables)*ctx.vList[j]).vallbl.length(), SQLITE_STATIC);

        if (rc != SQLITE_OK) {
            cerr << "SQLError: vallbl bind failed: " << sqlite3_errmsg(db) << endl;
            return false;
        }

        rc = sqlite3_bind_text(stmt, 5, ((StataVariables)*ctx.vList[j]).varlbl.c_str(), ((StataVariables)*ctx.vList[j]).varlbl.length(), SQLITE_STATIC);

        if (rc != SQLITE_OK) {
            cerr << "SQLError: varlbl bind failed: " << sqlite3_errmsg(db) << endl;
            return false;
        }
        
        rc =  sqlite3_step(stmt);

        if( rc != SQLITE_DONE )
        {
            cerr << "SQL error: "  << sqlite3_errmsg(db) << endl;
            return false;
        }
                    
        sqlite3_clear_bindings(stmt);    /* Clear bindings */
        sqlite3_reset(stmt);
    

    }  

    sqlite3_exec(db, "END TRANSACTION", NULL, NULL, &zErrMsg);

  
    rc = sqlite3_exec(db,  "create table vardata ("
                           "obs integer,"
                           "varpos integer,"
                           "value blob,"
                           "sizer int);" , NULL, 0, &zErrMsg);
    
    if( rc != SQLITE_OK )
    {
      cerr << "SQL error: " << zErrMsg << endl;
      sqlite3_free(zErrMsg);
      return false;
    }

    sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, &zErrMsg);

    rc = sqlite3_prepare_v2(db, "INSERT INTO VARDATA ( OBS, VARPOS, VALUE, SIZER ) VALUES ( ?, ?, ?, ? )", -1, &stmt, NULL);
    
    if (rc != SQLITE_OK) {
        cerr << "SQLError prepare failed: " << sqlite3_errmsg(db) << endl;
        return false;
    } 
    else 
    {
    
        for (int k = 0; k < ctx.vData.size(); k++)
        {
            for (int j = 0; j < ctx.vList.size(); j++)
            {
                int sz = 0;
                rc = sqlite3_bind_int(stmt, 1, k+1);

                
                if (rc != SQLITE_OK) {
                    cerr << "SQLError: obs bind failed: " << sqlite3_errmsg(db) << endl;
                    return false;
                }

                rc = sqlite3_bind_int(stmt, 2, j+1);
                if (rc != SQLITE_OK) {
                    cerr << "SQLError: varpos bind failed: " << sqlite3_errmsg(db) << endl;
                    return false;
                }                 
                
                switch(ctx.vList[j]->type)
                {
                    case ST_STRL:
                        cout << "Strl " << endl;             
                        break;
                    case ST_DOUBLE:  
                        sz=8;
                        break;
                    case ST_FLOAT:
                        sz=4;
                        break;
                    case ST_LONG:
                        sz=4;
                        break;
                    case ST_INT:
                        sz=2;
                        break;
                    case ST_BYTE:
                        sz=1;
                        break;
                    default: 
                        sz=strlen((*ctx.vData[k])[j]);
                        break;
                }                  

                rc = sqlite3_bind_blob(stmt,3, (*ctx.vData[k])[j], sz, SQLITE_STATIC);
                
                if (rc != SQLITE_OK) {
                    cerr << "SQLError blob bind failed: " << sqlite3_errmsg(db) << endl;
                    return false;
                } 

                rc = sqlite3_bind_int(stmt, 4, sz);
                if (rc != SQLITE_OK) {
                    cerr << "SQLError: sizer bind failed: " << sqlite3_errmsg(db) << endl;
                    return false;
                } 
                
                rc =  sqlite3_step(stmt);

                if( rc != SQLITE_DONE )
                {
                    cerr << "SQL error: "  << sqlite3_errmsg(db) << endl;
                    return false;
                }
                    
                sqlite3_clear_bindings(stmt);    /* Clear bindings */
                sqlite3_reset(stmt);
            }
        }
    } 
    
    sqlite3_exec(db, "END TRANSACTION", NULL, NULL, &zErrMsg);
    sqlite3_finalize(stmt);    
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2b8, %rsp            # imm = 0x2B8
movq %rsi, %r14
movq %rdi, %r15
leaq -0x40(%rbp), %rbx
andq $0x0, (%rbx)
leaq -0x2e0(%rbp), %rdi
callq 0x7230
andq $0x0, -0x30(%rbp)
movq 0x8(%r15), %rdi
leaq 0x1fbe(%rip), %rsi      # 0xfc34
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbx, %r8
callq 0x7430
testl %eax, %eax
je 0xdcbb
movq 0xa363(%rip), %rdi      # 0x17ff0
leaq 0x2047(%rip), %rsi      # 0xfcdb
callq 0x7300
movq -0x40(%rbp), %rsi
movq %rax, %rdi
callq 0x7300
movq %rax, %rdi
callq 0x7160
movq -0x40(%rbp), %rdi
callq 0x73a0
jmp 0xe124
leaq 0x1441(%rip), %rsi      # 0xf103
leaq -0x158(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x8190
leaq -0x2c8(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x74d0
leaq -0x158(%rbp), %rdi
callq 0x7150
movq 0x8(%r15), %rdi
leaq 0x1fe8(%rip), %rsi      # 0xfce7
pushq $-0x1
popq %rdx
leaq -0x30(%rbp), %rcx
xorl %r8d, %r8d
callq 0x72e0
testl %eax, %eax
je 0xdd49
movq 0xa2d7(%rip), %rdi      # 0x17ff0
leaq 0x203e(%rip), %rsi      # 0xfd5e
callq 0x7300
movq %rax, %r14
movq 0x8(%r15), %rdi
callq 0x72f0
movq %r14, %rdi
movq %rax, %rsi
callq 0x7300
movq %rax, %rdi
callq 0x7160
jmp 0xe124
movq 0x8(%r15), %rdi
leaq 0x2024(%rip), %rsi      # 0xfd78
leaq -0x40(%rbp), %r8
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x7430
movq %r15, -0x38(%rbp)
xorl %r12d, %r12d
leaq -0x158(%rbp), %r13
xorl %ecx, %ecx
movq 0xc0(%r14), %rdi
movq 0xc8(%r14), %rax
subq %rdi, %rax
sarq $0x4, %rax
cmpq %rcx, %rax
jbe 0xdfdb
movq %rcx, -0x48(%rbp)
movq -0x30(%rbp), %r15
addq %r12, %rdi
callq 0xe506
movq %r13, %rdi
movq %rax, %rsi
callq 0xe532
movq -0x158(%rbp), %rbx
movq 0xc0(%r14), %rdi
addq %r12, %rdi
callq 0xe506
leaq -0xd0(%rbp), %rdi
movq %rax, %rsi
callq 0xe532
movl -0xc8(%rbp), %ecx
movq %r15, %rdi
pushq $0x1
popq %rsi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x72a0
movl %eax, %ebx
leaq -0xd0(%rbp), %rdi
callq 0xcc0a
movq %r13, %rdi
callq 0xcc0a
testl %ebx, %ebx
jne 0xe02f
movq -0x30(%rbp), %rbx
movq 0xc0(%r14), %rdi
addq %r12, %rdi
callq 0xe506
movq %r13, %rdi
movq %rax, %rsi
callq 0xe532
movl -0x138(%rbp), %edx
movq %rbx, %rdi
pushq $0x2
popq %rsi
callq 0x7040
movl %eax, %ebx
movq %r13, %rdi
callq 0xcc0a
testl %ebx, %ebx
jne 0xe057
movq -0x30(%rbp), %r15
movq 0xc0(%r14), %rdi
addq %r12, %rdi
callq 0xe506
movq %r13, %rdi
movq %rax, %rsi
callq 0xe532
movq -0x130(%rbp), %rbx
movq 0xc0(%r14), %rdi
addq %r12, %rdi
callq 0xe506
leaq -0xd0(%rbp), %rdi
movq %rax, %rsi
callq 0xe532
movl -0xa0(%rbp), %ecx
movq %r15, %rdi
pushq $0x3
popq %rsi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x72a0
movl %eax, %ebx
leaq -0xd0(%rbp), %rdi
callq 0xcc0a
movq %r13, %rdi
callq 0xcc0a
testl %ebx, %ebx
jne 0xe07f
movq -0x30(%rbp), %r15
movq 0xc0(%r14), %rdi
addq %r12, %rdi
callq 0xe506
movq %r13, %rdi
movq %rax, %rsi
callq 0xe532
movq -0x110(%rbp), %rbx
movq 0xc0(%r14), %rdi
addq %r12, %rdi
callq 0xe506
leaq -0xd0(%rbp), %rdi
movq %rax, %rsi
callq 0xe532
movl -0x80(%rbp), %ecx
movq %r15, %rdi
pushq $0x4
popq %rsi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x72a0
movl %eax, %ebx
leaq -0xd0(%rbp), %rdi
callq 0xcc0a
movq %r13, %rdi
callq 0xcc0a
testl %ebx, %ebx
jne 0xe0a4
movq -0x30(%rbp), %r15
movq 0xc0(%r14), %rdi
addq %r12, %rdi
callq 0xe506
movq %r13, %rdi
movq %rax, %rsi
callq 0xe532
movq -0xf0(%rbp), %rbx
movq 0xc0(%r14), %rdi
addq %r12, %rdi
callq 0xe506
leaq -0xd0(%rbp), %rdi
movq %rax, %rsi
callq 0xe532
movl -0x60(%rbp), %ecx
movq %r15, %rdi
pushq $0x5
popq %rsi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x72a0
movl %eax, %ebx
leaq -0xd0(%rbp), %rdi
callq 0xcc0a
movq %r13, %rdi
callq 0xcc0a
testl %ebx, %ebx
jne 0xe0c9
movq -0x30(%rbp), %rdi
callq 0x7470
cmpl $0x65, %eax
jne 0xe0ee
movq -0x30(%rbp), %rdi
callq 0x7630
movq -0x30(%rbp), %rdi
callq 0x70f0
movq -0x48(%rbp), %rcx
incq %rcx
addq $0x10, %r12
jmp 0xdd71
movq -0x38(%rbp), %r15
movq 0x8(%r15), %rdi
leaq 0x1e3a(%rip), %rsi      # 0xfe24
leaq -0x40(%rbp), %r8
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x7430
movq 0x8(%r15), %rdi
leaq 0x1e32(%rip), %rsi      # 0xfe34
leaq -0x40(%rbp), %r8
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x7430
testl %eax, %eax
je 0xe146
movq 0x9fd2(%rip), %rdi      # 0x17ff0
leaq 0x1cb6(%rip), %rsi      # 0xfcdb
callq 0x7300
jmp 0xdc99
movq 0x9fba(%rip), %rdi      # 0x17ff0
leaq 0x1d4d(%rip), %rsi      # 0xfd8a
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
jmp 0xe111
movq 0x9f92(%rip), %rdi      # 0x17ff0
leaq 0x1d45(%rip), %rsi      # 0xfdaa
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
jmp 0xe111
movq 0x9f6a(%rip), %rdi      # 0x17ff0
leaq 0x1d3a(%rip), %rsi      # 0xfdc7
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
jmp 0xe111
movq 0x9f45(%rip), %rdi      # 0x17ff0
leaq 0x1d34(%rip), %rsi      # 0xfde6
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
jmp 0xe111
movq 0x9f20(%rip), %rdi      # 0x17ff0
leaq 0x1d2e(%rip), %rsi      # 0xfe05
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
jmp 0xe111
movq 0x9efb(%rip), %rdi      # 0x17ff0
leaq 0x1bdf(%rip), %rsi      # 0xfcdb
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
movq %r14, %rdi
movq %rax, %rsi
callq 0x7300
movq %rax, %rdi
callq 0x7160
xorl %ebx, %ebx
leaq -0x2e0(%rbp), %rdi
callq 0x7260
movl %ebx, %eax
addq $0x2b8, %rsp            # imm = 0x2B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r15), %rdi
leaq 0x1c27(%rip), %rsi      # 0xfd78
leaq -0x40(%rbp), %r8
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x7430
movq 0x8(%r15), %rdi
leaq 0x1d13(%rip), %rsi      # 0xfe7c
pushq $-0x1
popq %rdx
leaq -0x30(%rbp), %rcx
xorl %r8d, %r8d
callq 0x72e0
testl %eax, %eax
je 0xe1a0
movq 0x9e6d(%rip), %rdi      # 0x17ff0
leaq 0x1bd4(%rip), %rsi      # 0xfd5e
callq 0x7300
movq %rax, %r14
movq 0x8(%r15), %rdi
callq 0x72f0
jmp 0xdd31
xorl %ebx, %ebx
movq 0xe0(%r14), %rax
subq 0xd8(%r14), %rax
sarq $0x3, %rax
cmpq %rbx, %rax
jbe 0xe3ec
leaq 0x1(%rbx), %rax
movq %rax, -0x48(%rbp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movq 0xc8(%r14), %rax
subq 0xc0(%r14), %rax
sarq $0x4, %rax
cmpq %r15, %rax
jbe 0xe30d
movq -0x30(%rbp), %rdi
pushq $0x1
popq %rsi
movq -0x48(%rbp), %rdx
callq 0x7040
testl %eax, %eax
jne 0xe31a
movq -0x30(%rbp), %rdi
leal 0x1(%r15), %edx
pushq $0x2
popq %rsi
callq 0x7040
testl %eax, %eax
jne 0xe342
movq 0xc0(%r14), %rdi
addq %r13, %rdi
callq 0xc136
movl 0x20(%rax), %eax
leal -0xfff6(%rax), %ecx
cmpl $0x4, %ecx
ja 0xe24d
leaq 0x19de(%rip), %rdx      # 0xfc18
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
pushq $0x8
popq %rcx
movl %ecx, %r12d
jmpq *%rax
pushq $0x4
jmp 0xe296
cmpl $0x8000, %eax           # imm = 0x8000
jne 0xe274
movq 0x9d65(%rip), %rdi      # 0x17fc0
leaq 0x1c9d(%rip), %rsi      # 0xfeff
callq 0x7300
xorl %r12d, %r12d
movq %rax, %rdi
callq 0x7160
jmp 0xe29a
movq 0xd8(%r14), %rax
movq (%rax,%rbx,8), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rdi
callq 0x7140
movq %rax, %r12
jmp 0xe29a
pushq $0x1
jmp 0xe296
pushq $0x2
popq %rax
movl %eax, %r12d
movq -0x30(%rbp), %rdi
movq 0xd8(%r14), %rax
movq (%rax,%rbx,8), %rax
movq (%rax), %rax
movq (%rax,%r15,8), %rdx
pushq $0x3
popq %rsi
movl %r12d, %ecx
xorl %r8d, %r8d
callq 0x70e0
testl %eax, %eax
jne 0xe367
movq -0x30(%rbp), %rdi
pushq $0x4
popq %rsi
movl %r12d, %edx
callq 0x7040
testl %eax, %eax
jne 0xe38c
movq -0x30(%rbp), %rdi
callq 0x7470
cmpl $0x65, %eax
jne 0xe3b1
movq -0x30(%rbp), %rdi
callq 0x7630
movq -0x30(%rbp), %rdi
addq $0x10, %r13
incq %r15
callq 0x70f0
jmp 0xe1cb
movq -0x48(%rbp), %rbx
movq -0x38(%rbp), %r15
jmp 0xe1a2
movq 0x9ccf(%rip), %rdi      # 0x17ff0
leaq 0x1b9c(%rip), %rsi      # 0xfec4
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
jmp 0xe3d4
movq 0x9ca7(%rip), %rdi      # 0x17ff0
leaq 0x1b90(%rip), %rsi      # 0xfee0
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
jmp 0xe3d4
movq 0x9c82(%rip), %rdi      # 0x17ff0
leaq 0x1b90(%rip), %rsi      # 0xff05
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
jmp 0xe3d4
movq 0x9c5d(%rip), %rdi      # 0x17ff0
leaq 0x1b87(%rip), %rsi      # 0xff21
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
jmp 0xe3d4
movq 0x9c38(%rip), %rdi      # 0x17ff0
leaq 0x191c(%rip), %rsi      # 0xfcdb
callq 0x7300
movq %rax, %r14
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x72f0
movq %r14, %rdi
movq %rax, %rsi
callq 0x7300
movq %rax, %rdi
callq 0x7160
jmp 0xe124
movq 0x8(%r15), %rdi
leaq 0x1a2d(%rip), %rsi      # 0xfe24
leaq -0x40(%rbp), %r8
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x7430
movb $0x1, %bl
movq -0x30(%rbp), %rdi
callq 0x7550
jmp 0xe126
jmp 0xe469
jmp 0xe469
movq %rax, %rbx
leaq -0x158(%rbp), %rdi
callq 0x7150
jmp 0xe46c
jmp 0xe469
jmp 0xe469
jmp 0xe443
jmp 0xe469
jmp 0xe454
jmp 0xe443
jmp 0xe454
jmp 0xe469
jmp 0xe443
jmp 0xe454
jmp 0xe469
jmp 0xe454
jmp 0xe469
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0xcc0a
jmp 0xe457
movq %rax, %rbx
leaq -0x158(%rbp), %rdi
callq 0xcc0a
jmp 0xe46c
jmp 0xe469
jmp 0xe469
movq %rax, %rbx
leaq -0x2e0(%rbp), %rdi
callq 0x7260
movq %rbx, %rdi
callq 0x75b0