Exemplo n.º 1
0
  public void updateMultipleFields() {
    database.open("admin", "admin");

    List<OClusterPosition> positions = getValidPositions(3);

    OIdentifiable result =
        database
            .command(
                new OCommandSQL(
                    "  INSERT INTO Account SET id= 3232,name= 'my name',map= {\"key\":\"value\"},dir= '',user= #3:"
                        + positions.get(0)))
            .execute();
    Assert.assertNotNull(result);

    ODocument record = result.getRecord();

    Assert.assertEquals(record.field("id"), 3232);
    Assert.assertEquals(record.field("name"), "my name");
    Map<String, String> map = record.field("map");
    Assert.assertTrue(map.get("key").equals("value"));
    Assert.assertEquals(record.field("dir"), "");
    Assert.assertEquals(record.field("user", OType.LINK), new ORecordId(3, positions.get(0)));

    database.close();
  }
  @Test
  public void queryProjectionOk() {
    database.open("admin", "admin");

    List<ODocument> result =
        database
            .command(
                new OSQLSynchQuery<ODocument>(
                    " select nick, followings, followers from Profile where nick is defined and followings is defined and followers is defined"))
            .execute();

    Assert.assertTrue(result.size() != 0);

    for (ODocument d : result) {
      String[] colNames = d.fieldNames();
      Assert.assertEquals(colNames.length, 3);
      Assert.assertEquals(colNames[0], "nick");
      Assert.assertEquals(colNames[1], "followings");
      Assert.assertEquals(colNames[2], "followers");

      Assert.assertNull(d.getClassName());
      Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE);
    }

    database.close();
  }
Exemplo n.º 3
0
  @Test
  @SuppressWarnings("unchecked")
  public void insertList() {
    database.open("admin", "admin");

    ODocument doc =
        (ODocument)
            database
                .command(
                    new OCommandSQL(
                        "insert into cluster:default (equaledges, name, list) values ('yes', 'square', ['bottom', 'top','left','right'] )"))
                .execute();

    Assert.assertTrue(doc != null);

    doc = (ODocument) new ODocument(doc.getIdentity()).load();

    Assert.assertEquals(doc.field("equaledges"), "yes");
    Assert.assertEquals(doc.field("name"), "square");
    Assert.assertTrue(doc.field("list") instanceof List);

    List<Object> entries = ((List<Object>) doc.field("list"));
    Assert.assertEquals(entries.size(), 4);

    Assert.assertEquals(entries.get(0), "bottom");
    Assert.assertEquals(entries.get(1), "top");
    Assert.assertEquals(entries.get(2), "left");
    Assert.assertEquals(entries.get(3), "right");

    database.delete(doc);

    doc =
        (ODocument)
            database
                .command(
                    new OCommandSQL(
                        "insert into cluster:default SET equaledges = 'yes', name = 'square', list = ['bottom', 'top','left','right'] "))
                .execute();

    Assert.assertTrue(doc != null);

    doc = (ODocument) new ODocument(doc.getIdentity()).load();

    Assert.assertEquals(doc.field("equaledges"), "yes");
    Assert.assertEquals(doc.field("name"), "square");
    Assert.assertTrue(doc.field("list") instanceof List);

    entries = ((List<Object>) doc.field("list"));
    Assert.assertEquals(entries.size(), 4);

    Assert.assertEquals(entries.get(0), "bottom");
    Assert.assertEquals(entries.get(1), "top");
    Assert.assertEquals(entries.get(2), "left");
    Assert.assertEquals(entries.get(3), "right");

    database.close();
  }
Exemplo n.º 4
0
  @Test
  public void createProperty() {
    database.open("admin", "admin");

    database.command(new OCommandSQL("create property account.timesheet string")).execute();

    Assert.assertEquals(
        database.getMetadata().getSchema().getClass("account").getProperty("timesheet").getType(),
        OType.STRING);

    database.close();
  }
Exemplo n.º 5
0
  @Test
  @SuppressWarnings("unchecked")
  public void insertMap() {
    database.open("admin", "admin");

    ODocument doc =
        (ODocument)
            database
                .command(
                    new OCommandSQL(
                        "insert into cluster:default (equaledges, name, properties) values ('no', 'circle', {'round':'eeee', 'blaaa':'zigzag'} )"))
                .execute();

    Assert.assertTrue(doc != null);

    doc = (ODocument) new ODocument(doc.getIdentity()).load();

    Assert.assertEquals(doc.field("equaledges"), "no");
    Assert.assertEquals(doc.field("name"), "circle");
    Assert.assertTrue(doc.field("properties") instanceof Map);

    Map<Object, Object> entries = ((Map<Object, Object>) doc.field("properties"));
    Assert.assertEquals(entries.size(), 2);

    Assert.assertEquals(entries.get("round"), "eeee");
    Assert.assertEquals(entries.get("blaaa"), "zigzag");

    database.delete(doc);

    doc =
        (ODocument)
            database
                .command(
                    new OCommandSQL(
                        "insert into cluster:default SET equaledges = 'no', name = 'circle', properties = {'round':'eeee', 'blaaa':'zigzag'} "))
                .execute();

    Assert.assertTrue(doc != null);

    doc = (ODocument) new ODocument(doc.getIdentity()).load();

    Assert.assertEquals(doc.field("equaledges"), "no");
    Assert.assertEquals(doc.field("name"), "circle");
    Assert.assertTrue(doc.field("properties") instanceof Map);

    entries = ((Map<Object, Object>) doc.field("properties"));
    Assert.assertEquals(entries.size(), 2);

    Assert.assertEquals(entries.get("round"), "eeee");
    Assert.assertEquals(entries.get("blaaa"), "zigzag");
    database.close();
  }
Exemplo n.º 6
0
  @Test
  public void insertOperator() {
    database.open("admin", "admin");

    int addressId = database.getMetadata().getSchema().getClass("Address").getDefaultClusterId();

    List<OClusterPosition> positions = getValidPositions(addressId);

    ODocument doc =
        (ODocument)
            database
                .command(
                    new OCommandSQL(
                        "insert into Profile (name, surname, salary, location, dummy) values ('Luca','Smith', 109.9, #"
                            + addressId
                            + ":"
                            + positions.get(3)
                            + ", 'hooray')"))
                .execute();

    Assert.assertTrue(doc != null);
    Assert.assertEquals(doc.field("name"), "Luca");
    Assert.assertEquals(doc.field("surname"), "Smith");
    Assert.assertEquals(((Number) doc.field("salary")).floatValue(), 109.9f);
    Assert.assertEquals(
        doc.field("location", OType.LINK), new ORecordId(addressId, positions.get(3)));
    Assert.assertEquals(doc.field("dummy"), "hooray");

    doc =
        (ODocument)
            database
                .command(
                    new OCommandSQL(
                        "insert into Profile SET name = 'Luca', surname = 'Smith', salary = 109.9, location = #"
                            + addressId
                            + ":"
                            + positions.get(3)
                            + ", dummy =  'hooray'"))
                .execute();

    database.delete(doc);

    Assert.assertTrue(doc != null);
    Assert.assertEquals(doc.field("name"), "Luca");
    Assert.assertEquals(doc.field("surname"), "Smith");
    Assert.assertEquals(((Number) doc.field("salary")).floatValue(), 109.9f);
    Assert.assertEquals(
        doc.field("location", OType.LINK), new ORecordId(addressId, positions.get(3)));
    Assert.assertEquals(doc.field("dummy"), "hooray");

    database.close();
  }
Exemplo n.º 7
0
  @Test(dependsOnMethods = "createLinkedTypeProperty")
  public void removeProperty() {
    database.open("admin", "admin");

    database.command(new OCommandSQL("drop property account.timesheet")).execute();
    database.command(new OCommandSQL("drop property account.tags")).execute();

    Assert.assertFalse(
        database.getMetadata().getSchema().getClass("account").existsProperty("timesheet"));
    Assert.assertFalse(
        database.getMetadata().getSchema().getClass("account").existsProperty("tags"));

    database.close();
  }
Exemplo n.º 8
0
  @Test(dependsOnMethods = "createLinkedClassProperty")
  public void createLinkedTypeProperty() {
    database.open("admin", "admin");

    database.command(new OCommandSQL("create property account.tags embeddedlist string")).execute();

    Assert.assertEquals(
        database.getMetadata().getSchema().getClass("account").getProperty("tags").getType(),
        OType.EMBEDDEDLIST);
    Assert.assertEquals(
        database.getMetadata().getSchema().getClass("account").getProperty("tags").getLinkedType(),
        OType.STRING);

    database.close();
  }
  @Test(expectedExceptions = OCommandSQLParsingException.class)
  public void queryProjectionFlattenError() {
    database.open("admin", "admin");

    try {
      database
          .command(
              new OSQLSynchQuery<ODocument>(
                  "SELECT FLATTEN( out ), in FROM OGraphVertex WHERE out TRAVERSE(1,1) (@class = 'OGraphEdge')"))
          .execute();

    } finally {
      database.close();
    }
  }
Exemplo n.º 10
0
  @Test
  public void insertWithNoSpaces() {
    database.open("admin", "admin");

    ODocument doc =
        (ODocument)
            database
                .command(
                    new OCommandSQL(
                        "insert into cluster:default(id, title)values(10, 'NoSQL movement')"))
                .execute();

    Assert.assertTrue(doc != null);

    database.close();
  }
Exemplo n.º 11
0
Arquivo: DB.java Projeto: o-nix/Kafra
  public static ODatabaseDocument getDb() {
    ODatabaseDocument db = DatabaseObjects.INSTANCE.get();
    boolean useServer = Application.getConfig().getBooleanProperty("db.server", false);

    if (db == null) {
      db = new ODatabaseDocumentTx(useServer ? "remote:localhost/main" : "local:data");

      if (!useServer && !db.exists()) db.create();

      if (db.isClosed()) db.open("admin", "admin");
    }

    ODatabaseRecordThreadLocal.INSTANCE.get().setDatabaseOwner(db);
    DatabaseObjects.INSTANCE.set(db);

    return db;
  }
Exemplo n.º 12
0
  @Test
  public void insertWithWildcards() {
    database.open("admin", "admin");

    int addressId = database.getMetadata().getSchema().getClass("Address").getDefaultClusterId();

    List<OClusterPosition> positions = getValidPositions(addressId);

    ODocument doc =
        (ODocument)
            database
                .command(
                    new OCommandSQL(
                        "insert into Profile (name, surname, salary, location, dummy) values (?,?,?,?,?)"))
                .execute(
                    "Marc", "Smith", 120.0, new ORecordId(addressId, positions.get(3)), "hooray");

    Assert.assertTrue(doc != null);
    Assert.assertEquals(doc.field("name"), "Marc");
    Assert.assertEquals(doc.field("surname"), "Smith");
    Assert.assertEquals(((Number) doc.field("salary")).floatValue(), 120.0f);
    Assert.assertEquals(
        doc.field("location", OType.LINK), new ORecordId(addressId, positions.get(3)));
    Assert.assertEquals(doc.field("dummy"), "hooray");

    database.delete(doc);

    doc =
        (ODocument)
            database
                .command(
                    new OCommandSQL(
                        "insert into Profile SET name = ?, surname = ?, salary = ?, location = ?, dummy = ?"))
                .execute(
                    "Marc", "Smith", 120.0, new ORecordId(addressId, positions.get(3)), "hooray");

    Assert.assertTrue(doc != null);
    Assert.assertEquals(doc.field("name"), "Marc");
    Assert.assertEquals(doc.field("surname"), "Smith");
    Assert.assertEquals(((Number) doc.field("salary")).floatValue(), 120.0f);
    Assert.assertEquals(
        doc.field("location", OType.LINK), new ORecordId(addressId, positions.get(3)));
    Assert.assertEquals(doc.field("dummy"), "hooray");

    database.close();
  }
Exemplo n.º 13
0
  @Test
  public void insertCluster() {
    database.open("admin", "admin");

    ODocument doc =
        (ODocument)
            database
                .command(
                    new OCommandSQL(
                        "insert into Account cluster default (id, title) values (10, 'NoSQL movement')"))
                .execute();

    Assert.assertTrue(doc != null);
    Assert.assertEquals(doc.getIdentity().getClusterId(), database.getDefaultClusterId());
    Assert.assertEquals(doc.getClassName(), "Account");

    database.close();
  }
Exemplo n.º 14
0
  @Test
  public void insertAvoidingSubQuery() {
    database.open("admin", "admin");

    final OSchema schema = database.getMetadata().getSchema();
    if (schema.getClass("test") == null) schema.createClass("test");

    ODocument doc =
        (ODocument)
            database
                .command(new OCommandSQL("INSERT INTO test(text) VALUES ('(Hello World)')"))
                .execute();

    Assert.assertTrue(doc != null);
    Assert.assertEquals(doc.field("text"), "(Hello World)");

    database.close();
  }
Exemplo n.º 15
0
  @Test
  public void insertSubQuery() {
    database.open("admin", "admin");

    final OSchema schema = database.getMetadata().getSchema();
    if (schema.getClass("test") == null) schema.createClass("test");

    ODocument doc =
        (ODocument)
            database
                .command(new OCommandSQL("INSERT INTO test SET names = (select name from OUser)"))
                .execute();

    Assert.assertTrue(doc != null);
    Assert.assertNotNull(doc.field("names"));
    Assert.assertTrue(doc.field("names") instanceof Collection);
    Assert.assertEquals(((Collection<?>) doc.field("names")).size(), 3);

    database.close();
  }
  @Test
  public void queryProjectionJSON() {
    database.open("admin", "admin");

    List<ODocument> result =
        database
            .command(new OSQLSynchQuery<ODocument>("select @this.toJson() as json from Profile"))
            .execute();

    Assert.assertTrue(result.size() != 0);

    for (ODocument d : result) {
      Assert.assertTrue(d.fieldNames().length <= 1);
      Assert.assertNotNull(d.field("json"));

      new ODocument().fromJSON((String) d.field("json"));
    }

    database.close();
  }
Exemplo n.º 17
0
  public OrientJdbcConnection(final String jdbcdDUrl, final Properties info) {
    this.dbUrl = jdbcdDUrl.replace("jdbc:orient:", "");

    this.info = info;

    readOnly = false;
    final String username = info.getProperty("user", "admin");
    final String password = info.getProperty("password", "admin");

    usePool = Boolean.parseBoolean(info.getProperty("db.usePool", "false"));
    if (usePool) {
      OPartitionedDatabasePool pool = POOL_FACTORY.get(dbUrl, username, password);
      database = pool.acquire();
    } else {
      database = new ODatabaseDocumentTx(this.dbUrl);
      database.open(username, password);
      database.activateOnCurrentThread();
    }
    status = ODatabase.STATUS.OPEN;
  }
  @Test
  public void queryProjectionContentCollection() {
    database.open("admin", "admin");

    List<ODocument> result =
        database
            .command(
                new OSQLSynchQuery<ODocument>(
                    "SELECT FLATTEN( out ) FROM OGraphVertex WHERE out TRAVERSE(1,1) (@class = 'OGraphEdge')"))
            .execute();

    Assert.assertTrue(result.size() != 0);

    for (ODocument d : result) {
      Assert.assertEquals(d.getClassName(), "OGraphEdge");
      Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE);
    }

    database.close();
  }
  @Test
  public void queryProjectionSimpleValues() {
    database.open("admin", "admin");

    List<ODocument> result =
        database
            .command(new OSQLSynchQuery<ODocument>("select 10, 'ciao' from Profile LIMIT 1"))
            .execute();

    Assert.assertTrue(result.size() != 0);

    for (ODocument d : result) {
      Assert.assertTrue(d.fieldNames().length <= 2);
      Assert.assertEquals(((Integer) d.field("10")).intValue(), 10l);
      Assert.assertEquals(d.field("ciao"), "ciao");

      Assert.assertNull(d.getClassName());
      Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE);
    }

    database.close();
  }
Exemplo n.º 20
0
  @Test(dependsOnMethods = "createProperty")
  public void createLinkedClassProperty() {
    database.open("admin", "admin");

    database
        .command(new OCommandSQL("create property account.knows embeddedmap account"))
        .execute();

    Assert.assertEquals(
        database.getMetadata().getSchema().getClass("account").getProperty("knows").getType(),
        OType.EMBEDDEDMAP);
    Assert.assertEquals(
        database
            .getMetadata()
            .getSchema()
            .getClass("account")
            .getProperty("knows")
            .getLinkedClass(),
        database.getMetadata().getSchema().getClass("account"));

    database.close();
  }
  @Test
  public void queryProjectionSameFieldTwice() {
    database.open("admin", "admin");

    List<ODocument> result =
        database
            .command(
                new OSQLSynchQuery<ODocument>(
                    "select name, name.toUppercase() from Profile where name is not null"))
            .execute();

    Assert.assertTrue(result.size() != 0);

    for (ODocument d : result) {
      Assert.assertTrue(d.fieldNames().length <= 2);
      Assert.assertNotNull(d.field("name"));
      Assert.assertNotNull(d.field("name2"));

      Assert.assertNull(d.getClassName());
      Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE);
    }

    database.close();
  }
  @Test
  public void queryProjectionFunctionsAndFieldOperators() {
    database.open("admin", "admin");

    List<ODocument> result =
        database
            .command(
                new OSQLSynchQuery<ODocument>(
                    "select max(name.append('.')).prefix('Mr. ') as name from Profile where name is not null"))
            .execute();

    Assert.assertTrue(result.size() != 0);

    for (ODocument d : result) {
      Assert.assertTrue(d.fieldNames().length <= 1);
      Assert.assertTrue(d.field("name").toString().startsWith("Mr. "));
      Assert.assertTrue(d.field("name").toString().endsWith("."));

      Assert.assertNull(d.getClassName());
      Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE);
    }

    database.close();
  }
  @Test
  public void queryProjectionAliases() {
    database.open("admin", "admin");

    List<ODocument> result =
        database
            .command(
                new OSQLSynchQuery<ODocument>(
                    "select name.append('!') as 1, surname as 2 from Profile where name is not null and surname is not null"))
            .execute();

    Assert.assertTrue(result.size() != 0);

    for (ODocument d : result) {
      Assert.assertTrue(d.fieldNames().length <= 2);
      Assert.assertTrue(d.field("1").toString().endsWith("!"));
      Assert.assertNotNull(d.field("2"));

      Assert.assertNull(d.getClassName());
      Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE);
    }

    database.close();
  }
  @Test
  public void queryProjectionStaticValues() {
    database.open("admin", "admin");

    List<ODocument> result =
        database
            .command(
                new OSQLSynchQuery<ODocument>(
                    "select location.city.country.name, address.city.country.name from Profile where location.city.country.name is not null"))
            .execute();

    Assert.assertTrue(result.size() != 0);

    for (ODocument d : result) {

      Assert.assertNotNull(d.field("location"));
      Assert.assertNull(d.field("address"));

      Assert.assertNull(d.getClassName());
      Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE);
    }

    database.close();
  }
  @Test
  public void queryProjectionLinkedAndFunction() {
    database.open("admin", "admin");

    List<ODocument> result =
        database
            .command(
                new OSQLSynchQuery<ODocument>(
                    "select name.toUppercase(), address.city.country.name from Profile"))
            .execute();

    Assert.assertTrue(result.size() != 0);

    for (ODocument d : result) {
      Assert.assertTrue(d.fieldNames().length <= 2);
      if (d.field("name") != null)
        Assert.assertTrue(d.field("name").equals(((String) d.field("name")).toUpperCase()));

      Assert.assertNull(d.getClassName());
      Assert.assertEquals(d.getRecordType(), ODocument.RECORD_TYPE);
    }

    database.close();
  }
Exemplo n.º 26
0
 @BeforeTest
 public void openDatabase() {
   database.open("admin", "admin");
 }