sqlite-jdbc icon indicating copy to clipboard operation
sqlite-jdbc copied to clipboard

where is the implement code of the json_each function, i want define one method that is similar but something different, please help me

Open pine-byte opened this issue 7 years ago • 3 comments

where is the implement code of the json_each function, i want to define one method that is similar but something different, please help me

pine-byte avatar Mar 29 '18 03:03 pine-byte

note: i want to understand how to implement one to many , like this

  @Test
   public void json_each_Test1() throws SQLException {
       Connection conn = getConnection();

       conn.createStatement().execute("create table user (name, phone)");

       conn.createStatement().execute("insert into user values('james', json_array('704-100-0000','604-100-0000'))");
       conn.createStatement().execute("insert into user values('sally', json_array('604-200-0000','404-200-0000'))");
       conn.createStatement().execute("insert into user values('frank', json_array('704-200-0000','604-200-0000'))");
       conn.createStatement().execute("insert into user values('harry', json_array('504-200-0000','304-200-0000'))");

       String q = "SELECT DISTINCT user.name, je1.value, je1.id"
               + " FROM user, json_each(user.phone) as je1";
//                + " where json_each.value LIKE '704-%'";

       ResultSet rs = conn.createStatement().executeQuery(q);
       for (int idx=0; idx<8; idx++) {
           assertTrue(rs.next());
           System.out.println(String.format("%s %s %d",
                   rs.getString(1), rs.getObject(2), rs.getObject(3)));
//            assertEquals("james", rs.getString(1));
//            assertTrue(rs.next());
//            System.out.println(String.format("%s %s", rs.getString(1), rs.getObject(2)));
//            assertEquals("frank", rs.getString(1));
       }
       assertFalse(rs.next());
   }

run result is that :

james 704-100-0000 1
james 604-100-0000 2
sally 604-200-0000 1
sally 404-200-0000 2
frank 704-200-0000 1
frank 604-200-0000 2
harry 504-200-0000 1
harry 304-200-0000 2

pine-byte avatar Mar 29 '18 03:03 pine-byte

The json_array implementation is not part of the JDBC driver. This is part of the json sqlite extension (see https://www.sqlite.org/json1.html). I am not 100% sure, what are trying to do, but I think this is all possible with the existing json sqlite extension functions.

trohwer avatar Apr 18 '18 06:04 trohwer

This is quite old, is this still relevant, or can we close the issue?

gotson avatar Jul 29 '22 05:07 gotson