Hasan A Yousef
Hasan A Yousef

Reputation: 24948

DART lang, FUTURE function, chain of then

I'm using dartOracle, and wanted to use many SQL statements together, but need to make sure the INSERT statement is executed only after getting the table created, I wrote the below code after reading this and this and this and this but it is not working.. any thought!

var resultset;

Future buildDB() {
     var completer = new Completer(); 
     print("Hello, from Future!");
     return completer.future; 
}  

void createTables() {
   Future result= buildDB();

    connect(
       "SYSTEM",
       "password",
       "(DESCRIPTION="
       "(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))"
       "(CONNECT_DATA=(SERVICE_NAME=XE)(SERVER=DEDICATED)))")
  .then(  
  (oracleConnection) {

  result
        .then((_) => resultset = oracleConnection.select('''
                        CREATE TABLE vendors (
                                     vendor_id NUMBER,
                                     vCode NUMBER,
                                     vName VARCHAR(255),
                                     vEmail VARCHAR(255),
                                     PRIMARY KEY (vendor_id))
                      '''))   
        .then((_) => resultset.next())
        .then((_) => resultset = oracleConnection.select('''
                        INSERT INTO myVendors (vendor_id, vCode, vName,vEmail) 
                            values (1,'code1','name1','email1')")
                      '''))
        .then((_) => resultset.next())
        .then((_) => resultset = oracleConnection.select('''
                        INSERT INTO myVendors (vendor_id, vCode, vName,vEmail) 
                            values (2,'code2','name2','email2')")
                      '''))
        .then((_) => resultset.next())
        .then((_) => resultset = oracleConnection.select('''
                        INSERT INTO myVendors (vendor_id, vCode, vName,vEmail) 
                            values (3,'code3','name3','email3')")
                      '''))
        .then((_) => resultset.next())
        .then((_) => print('tables created!'));  
  }, 
  onError: (error) {
    print("Failed to create tables, error found: $error");
  });   
}

once I execute the function, I get this:

Observatory listening on http://127.0.0.1:54590
 Hello, vendor!
 Hello, from Future!
 Listening for GET and POST on http://127.0.0.1:8004

and nothing happen after that, I waited for 5 minutes, with no changes!

Upvotes: 0

Views: 790

Answers (2)

Hasan A Yousef
Hasan A Yousef

Reputation: 24948

thanks everyone, the below code worked perfectly with me:

pubspec.yaml:

dependencies:
  oracledart: any

the main.dart file:

import 'dart:async';
import 'package:oracledart/oracledart.dart';

void main() {
   Future buildDB() => new Future.value(true);     // dummy Future function to ensure SQL statements done in the proper sequence
   print("Hello to vendor tables setup!");
   var vendors = <Map>[];

   connect(
    "SYSTEM",
    "password",
    "(DESCRIPTION="
      "(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))"
      "(CONNECT_DATA=(SERVICE_NAME=XE)(SERVER=DEDICATED)))")
.then(  
  (oracleConnection) {

  buildDB()
        .then((_) => print('Pls wait, sequence of SQL statements will be executed now'))
        .then((_) => oracleConnection.select("""
                    CREATE TABLE vendors (
                                 vendor_id NUMBER,
                                 vCode NUMBER,
                                 vName VARCHAR(255),
                                 vEmail VARCHAR(255),
                                 PRIMARY KEY (vendor_id))
                     """))
        .then((_) => print('table had been created, now will start inserting initial availabe data!'))
        .then((_) => oracleConnection.select("INSERT INTO vendors (vendor_id, vCode, vName,vEmail) values (1,101,'vName1','[email protected]')"))
        .then((_) => oracleConnection.select("INSERT INTO vendors (vendor_id, vCode, vName,vEmail) values (2,102,'vName2','[email protected]')"))
        .then((_) => oracleConnection.select("INSERT INTO vendors (vendor_id, vCode, vName,vEmail) values (3,103,'vName3','[email protected]')"))
        .then((_) => print('data had been inserted, now will run a SELECT statement to show you what had been inserted!'))
        .then((_) {
            var resultset = oracleConnection.select("select * from vendors");
            while(resultset.next()) {
                 print("hello this is: ${resultset.getStringByName('VNAME')}");
                  vendors.add({"code":"${resultset.getStringByName('VCODE')}",
                    "name": "${resultset.getStringByName('VNAME')}",
                    "email": "${resultset.getStringByName('VEMAIL')}"
                  });
               }              
                print('the data entered is:  $vendors'); 
              })
        .then((_) => print('Done, SQL statement completed!'));
  }, 
  onError: (error) {
    print("Failed to connect: $error");
  });   
}

Upvotes: 0

Robert
Robert

Reputation: 5662

You never call completer.complete(). So your result Future will never get any data and thus the .then()-chain will never execute.

Upvotes: 3

Related Questions