Package de.mhus.lib.sql

Examples of de.mhus.lib.sql.DbConnection


      DbPoolBundle bundle = createPool();
     
      DbPool pool = bundle.getPool("test");
      assertEquals(0, pool.getSize());
     
      DbConnection con1 = pool.getConnection();
      assertEquals(1, pool.getSize());
     
      DbConnection con2 = pool.getConnection();
      assertEquals(2, pool.getSize());
     
      con1.close();
      pool.cleanup(false);
      assertEquals(2, pool.getSize());
      pool.cleanup(true);
      assertEquals(1, pool.getSize());
     
      DbConnection con3 = pool.getConnection();
      assertEquals(2, pool.getSize());
      con3.close();
     
      pool.cleanup(true);
      assertEquals(1, pool.getSize());

      con2.close();
View Full Code Here


     
    }

    public void testConnect() throws Exception {
      DbPool pool = createPool().getPool("test");
      DbConnection con = pool.getConnection();
     
      // create database
      DbStatement sthc = con.getStatement("create");
      try {
        sthc.executeUpdate(null);
          con.commit();
      } catch (Exception e) {e.printStackTrace();}
     
      DbStatement sth = con.getStatement("select");
      try {
        DbResult res = sth.executeQuery(null);
        res.close();
      } catch (ClassNotFoundException e1) {
        System.out.println("testConnect: sql driver not found - skip test");
        return;
      } catch (SQLException e2) {
        if (e2.getMessage().startsWith("Communications link failure")) {
          System.out.println("testConnect: sql db not found - skip test");
          return;
        } else {
          throw e2;
        }
      }
     
      con.commit();

    }
View Full Code Here

    }
   
    public void testAttributes() throws IOException, Exception {
      DbPool pool = createPool().getPool("test");
      DbConnection con = pool.getConnection();
      try {
        con.getStatement("cleanup").execute(null);
          con.commit();
      } catch (ClassNotFoundException e1) {
        System.out.println("testAttributes: sql driver not found - skip test");
        return;
      } catch (SQLException e2) {
        if (e2.getMessage().startsWith("Communications link failure")) {
          System.out.println("testConnect: sql db not found - skip test");
          return;
        } else {
          throw e2;
        }
      }
      HashMap<String, Object> attr = new HashMap<String, Object>();
      attr.put("text", "abc'");
      con.getStatement("insert").execute(attr);

      DbStatement sth = con.getStatement("select");
      DbResult res1 = sth.executeQuery(null);
      assertEquals(true,res1.next());
      String res2 = res1.getString("a_text");
      assertEquals(attr.get("text"), res2);
      res1.close();
     
      int res3 = con.getStatement("cleanup").executeUpdate(null);
      assertEquals(1, res3);
     
      con.commit();

    }
View Full Code Here

    }
   
    @SuppressWarnings("deprecation")
  public void testAttributes2() throws IOException, Exception {
      DbPool pool = createPool().getPool("test");
      DbConnection con = pool.getConnection();
      try {
        con.getStatement("cleanup2").execute(null);
          con.commit();
      } catch (ClassNotFoundException e1) {
        System.out.println("testAttributes: sql driver not found - skip test");
        return;
      } catch (SQLException e2) {
        if (e2.getMessage().startsWith("Communications link failure")) {
          System.out.println("testConnect: sql db not found - skip test");
          return;
        } else {
          // throw e2;
        }
      }
     
      // create database
      DbStatement sthc = con.getStatement("create2");
      try {
        sthc.executeUpdate(null);
          con.commit();
      } catch (Exception e) {e.printStackTrace();}

      int rows = 0;
     
      HashMap<String, Object> attr = new HashMap<String, Object>();
      attr.put("text", "abc'");
      attr.put("int", Integer.MAX_VALUE);
      attr.put("date", new Date());
      attr.put("bool", true);
      attr.put("blob", new ByteArrayInputStream("adsjansdjakdjajkhdkjhajkdhkjahdkjakjdkad".getBytes()));
      attr.put("float", 0x1.fffffeP+126f); // 0x1.fffffeP+127f not possible in MySQL
      attr.put("double", Double.MAX_VALUE);
      attr.put("long", Long.MAX_VALUE);
      con.getStatement("insert2").execute(attr);
      rows++;
     
      attr.put("text", "abc'");
      attr.put("int", Integer.MIN_VALUE);
      attr.put("date", new MDate());
      attr.put("bool", false);
      attr.put("blob", new ByteArrayInputStream("adsjansdjakdjajkhdkjhajkdhkjahdkjakjdkad".getBytes()));
      attr.put("float", Float.MIN_VALUE);
      attr.put("double", Double.MIN_VALUE);
      attr.put("long", Long.MIN_VALUE);
      con.getStatement("insert2").execute(attr);
      rows++;

      attr.put("text", "abc'");
      attr.put("int", Integer.MIN_VALUE);
      attr.put("date", new java.sql.Date(0));
      attr.put("bool", false);
      attr.put("blob", new ByteArrayInputStream("adsjansdjakdjajkhdkjhajkdhkjahdkjakjdkad".getBytes()));
      attr.put("float", Float.MIN_VALUE);
      attr.put("double", Double.MIN_VALUE);
      attr.put("long", Long.MIN_VALUE);
      con.getStatement("insert2").execute(attr);
      rows++;

      attr.put("text", "abc'");
      attr.put("int", Integer.MIN_VALUE);
      attr.put("date", new java.sql.Date(2069,12,31)); // max for mysql
      attr.put("bool", false);
      attr.put("blob", new ByteArrayInputStream("adsjansdjakdjajkhdkjhajkdhkjahdkjakjdkad".getBytes()));
      attr.put("float", Float.MIN_VALUE);
      attr.put("double", Double.MIN_VALUE);
      attr.put("long", Long.MIN_VALUE);
      con.getStatement("insert2").execute(attr);
      rows++;
     
      int res3 = con.getStatement("cleanup2").executeUpdate(null);
      assertEquals(rows, res3);
     
      con.commit();

    }
View Full Code Here

    }
   
    public void testStructure() throws Exception {
     
      DbPool pool = createPool().getPool("test");
      DbConnection con = pool.getConnection();
     
      try {
        con.getStatement("cleanup").execute(null);
          con.commit();
      } catch (ClassNotFoundException e1) {
        System.out.println("testStructure: sql driver not found - skip test");
        return;
      } catch (SQLException e2) {
        if (e2.getMessage().startsWith("Communications link failure")) {
          System.out.println("testStructure: mysql db not found - skip test");
          return;
        } else {
          throw e2;
        }
      }
     
      con.getStatement("dropblub").execute(null);
     
      HashConfig cstr = new HashConfig();
      IConfig ctable = cstr.createConfig("table");
      ctable.setProperty("name", "blub");
      IConfig cfield = ctable.createConfig("field");
      cfield.setProperty("name", "t_string");
      cfield.setProperty("type", "string");
      cfield.setProperty("size", "200");
      cfield.setProperty("default", "aloa");
     
      Dialect dialect = pool.getDialect();
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);     
      dialect.createStructure(cstr, con, null);

      cfield = ctable.createConfig("field");
      cfield.setProperty("name", "t_int");
      cfield.setProperty("type", "int");
      cfield.setProperty("default", "10");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);
     
      cfield.setProperty("type", "float");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);
     
      cfield.setProperty("default", "20");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);
     
      cfield.removeProperty("default");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);
     
      cfield.setProperty("default", "15");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);

      cfield.setProperty("notnull", "yes");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);
     
      ctable.setProperty("primary_key", "t_int");
     
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);
     
      ctable.setProperty("primary_key", "t_int,t_string");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);
     
      ctable.removeProperty("primary_key");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);
     
     
      // index
     
      IConfig cindex = cstr.createConfig("index");
      cindex.setString("name", "blub_idx");
      cindex.setString("table", "blub");
      cindex.setString("fields", "t_string");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);

      cindex.setString("fields", "t_string,t_int");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      dialect.createStructure(cstr, con, null);
     
      // data
      IConfig cdata = cstr.createConfig("data");
      IConfig cexec = cdata.createConfig("execute");
      String tName = pool.getDialect().normalizeTableName("blub");
      cexec.setProperty("sql", "insert into " + tName + " (t_string,t_int) values ('aaa',1)");
      cexec = cdata.createConfig("execute");
      cexec.setProperty("sql", "insert into " + tName + " (t_string,t_int) values ('bbb',2)");
     
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);

      cdata.setProperty("select", "select * from " + tName + " where t_string='ccc'");
      cdata.setProperty("condition", "not found");
      cexec = cdata.createConfig("execute");
      cexec.setProperty("sql", "insert into " + tName + " (t_string,t_int) values ('ccc',3)");

      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
      System.out.println("--------------------------------------------------");
      dialect.createStructure(cstr, con, null);
     
      con.commit();
      con.close();
    }
View Full Code Here

   * @throws MException
   */
  public <T> DbCollection<T> executeQuery(DbConnection con, T clazz, String registryName, String query, Map<String,Object> attributes) throws MException {
    Map<String, Object> map = null;
   
    DbConnection myCon = null;
    if (con == null) {
      try {
        myCon = pool.getConnection();
      } catch (Throwable t) {
        throw new MException(con,query,attributes,t);
View Full Code Here

   * @return
   * @throws MException
   */
  public Object getObject(DbConnection con, String registryName, Object ... keys) throws MException {
   
    DbConnection myCon = null;
    if (con == null) {
      try {
        myCon = pool.getConnection();
        con = myCon;
      } catch (Throwable t) {
        throw new MException(t);
      }
    }
   
    Table c = cIndex.get(registryName);
    if (c == null)
      throw new MException("class definition not found in schema",registryName);
   
    try {
      Object out = c.getObject(con,keys);
     
      if (myCon != null) {
        try {
          myCon.commit();
        } catch (Throwable t) {
          throw new MException(t);
        }
        myCon.close();
      }
     
      return out;
    } catch (Throwable t) {
      throw new MException(registryName,t);
View Full Code Here

   * @param object The object to fill
   * @throws MException
   */
  public void reloadObject(DbConnection con, String registryName, Object object) throws MException {

    DbConnection myCon = null;
    if (con == null) {
      try {
        myCon = pool.getConnection();
        con = myCon;
      } catch (Throwable t) {
        throw new MException(t);
      }
    }
   
    if (registryName == null) {
      Class<?> clazz = schema.findClassForObject(object,this);
      if (clazz == null)
        throw new MException("class definition not found for object",object.getClass().getCanonicalName(),registryName);
      registryName = clazz.getCanonicalName();
    }

    Table c = cIndex.get(registryName);
    if (c == null)
      throw new MException("class definition not found in schema",registryName);

    LinkedList<Object> keys = new LinkedList<Object>();
    try {
      for (Field f : c.getPrimaryKeys()) {
        keys.add(f.getFromTarget(object));
      }
     
      if (c.fillObject(con,object,keys.toArray()) == null)
        throw new MException("object not found");
     
      schema.doPostLoad(c,object,con,this);
     
    } catch (Throwable t) {
      throw new MException(registryName,t);
    }
   
    if (myCon != null) {
      try {
        myCon.commit();
      } catch (Throwable t) {
        throw new MException(t);
      }
      myCon.close();
    }
  }
View Full Code Here

   * @return
   * @throws MException
   */
  public boolean objectChanged(DbConnection con, String registryName, Object object) throws MException {

    DbConnection myCon = null;
    if (con == null) {
      try {
        myCon = pool.getConnection();
        con = myCon;
      } catch (Throwable t) {
        throw new MException(t);
      }
    }
   
    if (registryName == null) {
      Class<?> clazz = schema.findClassForObject(object,this);
      if (clazz == null)
        throw new MException("class definition not found for object",object.getClass().getCanonicalName(),registryName);
      registryName = clazz.getCanonicalName();
    }

    Table c = cIndex.get(registryName);
    if (c == null)
      throw new MException("class definition not found in schema",registryName);

    boolean ret = false;
    LinkedList<Object> keys = new LinkedList<Object>();
    try {
      for (Field f : c.getPrimaryKeys()) {
        keys.add(f.getFromTarget(object));
      }
     
      ret = c.objectChanged(con,object,keys.toArray());
     
      // schema.doPostLoad(c,object,con,this);
     
    } catch (Throwable t) {
      throw new MException(registryName,t);
    }
   
    if (myCon != null) {
      try {
        myCon.commit();
      } catch (Throwable t) {
        throw new MException(t);
      }
      myCon.close();
    }
   
    return ret;
  }
View Full Code Here

   * @param keys The primary keys
   * @throws MException
   */
  public void fillObject(DbConnection con, String registryName, Object object, Object ... keys) throws MException {

    DbConnection myCon = null;
    if (con == null) {
      try {
        myCon = pool.getConnection();
        con = myCon;
      } catch (Throwable t) {
        throw new MException(t);
      }
    }
   
    if (registryName == null) {
      Class<?> clazz = schema.findClassForObject(object,this);
      if (clazz == null)
        throw new MException("class definition not found for object",object.getClass().getCanonicalName(),registryName);
      registryName = clazz.getCanonicalName();
    }

    Table c = cIndex.get(registryName);
    if (c == null)
      throw new MException("class definition not found in schema",registryName);
   
    try {
      c.fillObject(con,object,keys);
     
      schema.doPostLoad(c,object,con,this);
     
    } catch (Throwable t) {
      throw new MException(registryName,t);
    }
   
    if (myCon != null) {
      try {
        myCon.commit();
      } catch (Throwable t) {
        throw new MException(t);
      }
      myCon.close();
    }
   
  }
View Full Code Here

TOP

Related Classes of de.mhus.lib.sql.DbConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.