2016-12-10 56 views
0

我使用AsyncSQLClient來獲取異步。在vertx中連接到我的數據庫。現在我正在努力如何使用JOOQ DSL。我正在嘗試以下操作:使用SqlConnection的JOOQ DSL

client.getConnection(res -> { 
    if (res.succeeded()) { 
      SQLConnection connection = res.result(); 

      DSL dsl = DSL.using(connection, SQLDialect.POSTGRES_9_4); 

      connection.close(); 
      client.close(); 
    } else { 
    } 
}); 

這不起作用,因爲使用需要連接而不是SQLConnection。有沒有辦法在JOOQ中使用SQLConnection?有沒有其他方法可以爲JOOQ創建異步連接?

回答

1

您可以實現(和開源!)對於vert.x SQLConnection型「代理」。舉例來說,如果你想運行以下vert.x方法:

interface SQLConnection { 
    SQLConnection queryWithParams(
     String sql, 
     JsonArray params, 
     Handler<AsyncResult<ResultSet>> resultHandler 
    ); 
} 

您的代理會暴露的方法像這樣代替:

class jOOQSQLConnection { 
    final SQLConnection delegate; 
    <R extends Record> jOOQSQLConnection query(
     ResultQuery<R> sql, 
     Handler<AsyncResult<Result<R>>> resultHandler 
    ) { 
     wrapInjOOQSQLConnection(

      // Extract the SQL string from the jOOQ query 
      delegate.query(sql.getSQL()), 

      // Extract the bind variables from the jOOQ query and wrap them in the 
      // JsonArray type, as requested by vert.x 
      wrapjOOQParamsInJsonArray(sql.getBindValues()), 

      // This is a handler that receives a vert.x ResultSet and wraps/transforms 
      // it into a jOOQ result (which contains the <R> type for type safety) 
      r -> resultHandler.handle(wrapInjOOQResult(r.result())) 
     ); 
    } 
} 

以上可能不會運行在開箱即用,但它給你一個如何包裝的想法。

+0

可悲的是,我對框架的瞭解並不適合編寫一個庫。感謝您帶領我走向正確的方向! – perotom

+0

@perotom:好的,別擔心。也許,將來會找到這個問題的其他人可以這樣做:) –

0
using (SqlConnection con = connection.getconnection()){ 

        SqlCommand cmd = new SqlCommand("empreg", con); 
        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.Parameters.AddWithValue("@empid", txtempid.Text); 
        cmd.Parameters.AddWithValue("@empname", txtempname.Text); 
        cmd.Parameters.AddWithValue("@salary", txtsalary.Text); 
        cmd.Parameters.AddWithValue("@tell", txttell.Text); 
        cmd.Parameters.AddWithValue("@address", txtaddress.Text); 
        cmd.Parameters.AddWithValue("@blog", txtblog.Text); 
        cmd.Parameters.AddWithValue("@gender", cmbgender.Text); 
        cmd.Parameters.AddWithValue("@hiredate", dtpdate.Value); 
        cmd.Parameters.AddWithValue("@op", op); 
        int i = cmd.ExecuteNonQuery(); 

        //If i > 0 AND op = "insert" 
        if (i > 0 && op == "insert"){ 

         MessageBox.Show("1 row is saved sucessfuly "); 
         submode.readdgv("emp", DGV2); 
         submode.autoid(txtempid, "emp"); 

         txtempname.Clear(); 
         txtsalary.Clear(); 
         txttell.Clear(); 
         txtaddress.Clear(); 
         txtblog.Clear(); 
         cmbgender.SelectedIndex = -1; 
         dtpdate.Value = DateTime.Now; 
         txtempname.Focus(); 

        } 

        else if (i >= 0 && op == "update"){ 

         MessageBox.Show("1 row is updated sucessfuly "); 
         submode.readdgv("emp", DGV2); 
         submode.autoid(txtempid, "emp"); 

        } 

        else if (i >= 0 && op == "delete"){ 

         MessageBox.Show("1 row is deleted sucessfuly"); 
         submode.readdgv("emp", DGV2); 
         submode.autoid(txtempid, "emp"); 

        } 
        else{ 

         MessageBox.Show("process is failed"); 
         submode.readdgv("emp", DGV2); 
         submode.autoid(txtempid, "emp"); 

        } 

       }