Java Code Examples for javax.jdo.Query#declareParameters()

The following examples show how to use javax.jdo.Query#declareParameters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConsumerPeer.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
public Consumer findConsumer(String consumerKey) throws APIException {
	try {
		PersistenceManager pm =  jdoFactory.getPersistenceManager(Constants.API_DB);
		Query query = pm.newQuery(Consumer.class, "consumerKey == c");
		query.declareParameters("java.lang.String c");
		Collection<Consumer> c = (Collection<Consumer>) query.execute(consumerKey);
		if(c!=null && c.size()>0) {
			return c.iterator().next();
		}
		else return null;
	}
	catch(Exception e) {
		ApiLoggerServer.log(new ConsumerPeer(), e);
		throw new APIException(APIException.INTERNAL_DB_ERROR);
	}
}
 
Example 2
Source File: ShardedCounter.java    From appengine-modules-sample-java with Apache License 2.0 6 votes vote down vote up
public int getCount() {
  int sum = 0;
  final PersistenceManager pm = PMF.get().getPersistenceManager();

  try {
    final Query shardsQuery = pm.newQuery(CounterShard.class,
                                    "counterName == nameParam");
    shardsQuery.declareParameters("String nameParam");
    final List<CounterShard> shards = (List<CounterShard>) shardsQuery.execute(
        counterName);
    if (shards != null && !shards.isEmpty()) {
      for (final CounterShard current : shards) {
        sum += current.getCount();
      }
    }
  } finally {
    pm.close();
  }
  return sum;
}
 
Example 3
Source File: ShardedCounter.java    From appengine-modules-sample-java with Apache License 2.0 5 votes vote down vote up
public int getCount() {
  if (cache != null) {
    final Integer cachedCount = (Integer) cache.get("count" + counterName);
    if (cachedCount != null) {
      return cachedCount.intValue();
    }
  }

  int sum = 0;
  final PersistenceManager pm = PMF.get().getPersistenceManager();

  try {
    final Query shardsQuery = pm.newQuery(DatastoreCounterShard.class,
                                    "counterName == nameParam");
    shardsQuery.declareParameters("String nameParam");
    final List<DatastoreCounterShard> shards =
        (List<DatastoreCounterShard>) shardsQuery.execute(counterName);
    if (shards != null && !shards.isEmpty()) {
      for (final DatastoreCounterShard current : shards) {
        sum += current.getCount();
      }
    }
  } finally {
    pm.close();
  }

  if (cache != null) {
    cache.put("count" + counterName, Integer.valueOf(sum));
  }

  return sum;
}
 
Example 4
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private void alterSentryRoleRevokePrivilegeCore(PersistenceManager pm,
    String roleName, TSentryPrivilege tPrivilege)
    throws SentryNoSuchObjectException, SentryInvalidInputException {
  Query query = pm.newQuery(MSentryRole.class);
  query.setFilter("this.roleName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  MSentryRole mRole = (MSentryRole) query.execute(roleName);
  if (mRole == null) {
    throw new SentryNoSuchObjectException("Role: " + roleName + " doesn't exist");
  } else {
    query = pm.newQuery(MSentryPrivilege.class);
    MSentryPrivilege mPrivilege = getMSentryPrivilege(tPrivilege, pm);
    if (mPrivilege == null) {
      mPrivilege = convertToMSentryPrivilege(tPrivilege);
    } else {
      mPrivilege = (MSentryPrivilege) pm.detachCopy(mPrivilege);
    }

    Set<MSentryPrivilege> privilegeGraph = Sets.newHashSet();
    if (mPrivilege.getGrantOption() != null) {
      privilegeGraph.add(mPrivilege);
    } else {
      MSentryPrivilege mTure = new MSentryPrivilege(mPrivilege);
      mTure.setGrantOption(true);
      privilegeGraph.add(mTure);
      MSentryPrivilege mFalse = new MSentryPrivilege(mPrivilege);
      mFalse.setGrantOption(false);
      privilegeGraph.add(mFalse);
    }
    // Get the privilege graph
    populateChildren(pm, Sets.newHashSet(roleName), mPrivilege, privilegeGraph);
    for (MSentryPrivilege childPriv : privilegeGraph) {
      revokePrivilegeFromRole(pm, tPrivilege, mRole, childPriv);
    }
    pm.makePersistent(mRole);
  }
}
 
Example 5
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Action getAction(long actionId) {
	Action a = null;
	Query query = pm.newQuery( Action.class, "actionId == i" );
	query.declareParameters( "java.lang.Long i" );
	Collection<Action> c = (Collection<Action>) query.execute(actionId);
	if(!c.isEmpty()) {
		a = c.iterator().next();
	}
	return a;
}
 
Example 6
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Collection<Action> getUserItemActions(long itemId,long userId,int limit) {
	Query query = pm.newQuery( Action.class, "itemId == i &&  userId == u" );
	query.setOrdering("actionId desc");
	query.declareParameters( "java.lang.Long i,java.lang.Long u");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute(itemId,userId);
	return c;
}
 
Example 7
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Action> getRecentUserActions(long userId, int actionType,
		int limit) {
	Query query = pm.newQuery( Action.class, "userId == i && type == t" );
	query.setOrdering("actionId desc");
	query.declareParameters( "java.lang.Long i,java.lang.Integer t");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute(userId,actionType);
	return c;
}
 
Example 8
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Action> getRecentUserActions(String clientUserId,
		int actionType, int limit) {
	Query query = pm.newQuery( Action.class, "clientUserId == i && type == t" );
	query.setOrdering("actionId desc");
	query.declareParameters( "java.lang.String i,java.lang.Integer t");
	query.setRange(0, limit);
	Collection<Action> c = (Collection<Action>) query.execute(clientUserId,actionType);
	return c;
}
 
Example 9
Source File: TestSentryRole.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private MSentryRole getMSentryRole(PersistenceManager pm, String roleName) {
  Query query = pm.newQuery(MSentryRole.class);
  query.setFilter("this.roleName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  MSentryRole sentryRole = (MSentryRole) query.execute(roleName);
  return sentryRole;
}
 
Example 10
Source File: GuideToJDO.java    From tutorials with MIT License 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public void QueryJDOQL() {
    PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
    PersistenceManager pm = pmf.getPersistenceManager();
    Transaction tx = pm.currentTransaction();
    try {
        tx.begin();

        // Declarative JDOQL :
        LOGGER.log(Level.INFO, "Declarative JDOQL --------------------------------------------------------------");
        Query qDJDOQL = pm.newQuery(Product.class);
        qDJDOQL.setFilter("name == 'Tablet' && price == price_value");
        qDJDOQL.declareParameters("double price_value");
        List<Product> resultsqDJDOQL = qDJDOQL.setParameters(80.0).executeList();

        Iterator<Product> iterDJDOQL = resultsqDJDOQL.iterator();
        while (iterDJDOQL.hasNext()) {
            Product p = iterDJDOQL.next();
            LOGGER.log(Level.WARNING, "Product name: {0} - Price: {1}", new Object[] { p.name, p.price });
        }
        LOGGER.log(Level.INFO, "--------------------------------------------------------------");

        tx.commit();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }

        pm.close();
    }
}
 
Example 11
Source File: SqlUserAttributePeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public UserAttribute findByNameAndType(String name, Integer type) {
    Extent<UserAttribute> extent = persistenceManager.getExtent(UserAttribute.class);
    Query query = persistenceManager.newQuery(extent);
    query.setFilter("name == attrName && type == type");
    query.declareParameters("String attrName, Integer type");

    @SuppressWarnings("unchecked")
    Collection<UserAttribute> attributes = (Collection<UserAttribute>) query.execute(name, type);
    Iterator<UserAttribute> iterator = attributes.iterator();
    return iterator.hasNext() ? iterator.next() : null;
}
 
Example 12
Source File: ShardedCounter.java    From appengine-modules-sample-java with Apache License 2.0 5 votes vote down vote up
public void increment(int count) {
  int shardCount = 0;
  PersistenceManager pm = PMF.get().getPersistenceManager();
  try {
    final Counter current = getThisCounter(pm);
    shardCount = current.getShardCount();
  } finally {
    pm.close();
  }

  final Random generator = new Random();
  final int shardNum = generator.nextInt(shardCount);

  pm = PMF.get().getPersistenceManager();
  try {
    final Query randomShardQuery = pm.newQuery(CounterShard.class);
    randomShardQuery.setFilter(
        "counterName == nameParam && shardNumber == numParam");
    randomShardQuery.declareParameters("String nameParam, int numParam");
    final List<CounterShard> shards = (List<CounterShard>) randomShardQuery
        .execute(counterName, shardNum);
    if (shards != null && !shards.isEmpty()) {
      final CounterShard shard = shards.get(0);
      shard.increment(count);
      pm.makePersistent(shard);
    }
  } finally {
    pm.close();
  }
}
 
Example 13
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
public Set<String> getRoleNamesForGroups(Set<String> groups) {
  Set<String> result = new HashSet<String>();
  boolean rollbackTransaction = true;
  PersistenceManager pm = null;
  try {
    pm = openTransaction();
    Query query = pm.newQuery(MSentryGroup.class);
    query.setFilter("this.groupName == t");
    query.declareParameters("java.lang.String t");
    query.setUnique(true);
    for (String group : groups) {
      MSentryGroup sentryGroup = (MSentryGroup) query.execute(group.trim());
      if (sentryGroup != null) {
        for (MSentryRole role : sentryGroup.getRoles()) {
          result.add(role.getRoleName());
        }
      }
    }
    rollbackTransaction = false;
    commitTransaction(pm);
    return result;
  } finally {
    if (rollbackTransaction) {
      rollbackTransaction(pm);
    }
  }
}
 
Example 14
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
private void alterSentryRoleAddGroupsCore(PersistenceManager pm, String roleName,
    Set<TSentryGroup> groupNames) throws SentryNoSuchObjectException {
  String lRoleName = roleName.trim().toLowerCase();
  Query query = pm.newQuery(MSentryRole.class);
  query.setFilter("this.roleName == t");
  query.declareParameters("java.lang.String t");
  query.setUnique(true);
  MSentryRole role = (MSentryRole) query.execute(lRoleName);
  if (role == null) {
    throw new SentryNoSuchObjectException("Role: " + lRoleName + " doesn't exist");
  } else {
    query = pm.newQuery(MSentryGroup.class);
    query.setFilter("this.groupName == t");
    query.declareParameters("java.lang.String t");
    query.setUnique(true);
    List<MSentryGroup> groups = Lists.newArrayList();
    for (TSentryGroup tGroup : groupNames) {
      String groupName = tGroup.getGroupName().trim();
      MSentryGroup group = (MSentryGroup) query.execute(groupName);
      if (group == null) {
        group = new MSentryGroup(groupName, System.currentTimeMillis(), Sets.newHashSet(role));
      }
      group.appendRole(role);
      groups.add(group);
    }
    pm.makePersistentAll(groups);
  }
}
 
Example 15
Source File: DelegateSentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
/**
 * The role is global in the generic model, such as the role may be has more than one component
 * privileges, so delete role will remove all privileges related to it.
 */
@Override
public CommitContext dropRole(String component, String role, String requestor)
    throws SentryNoSuchObjectException {
  boolean rollbackTransaction = true;
  PersistenceManager pm = null;
  role = toTrimmedLower(role);
  try {
    pm = openTransaction();
    Query query = pm.newQuery(MSentryRole.class);
    query.setFilter("this.roleName == t");
    query.declareParameters("java.lang.String t");
    query.setUnique(true);
    MSentryRole sentryRole = (MSentryRole) query.execute(role);
    if (sentryRole == null) {
      throw new SentryNoSuchObjectException("Role: " + role + " doesn't exist");
    } else {
      pm.retrieve(sentryRole);
      sentryRole.removeGMPrivileges();
      sentryRole.removePrivileges();
      pm.deletePersistent(sentryRole);
    }
    CommitContext commit = commitUpdateTransaction(pm);
    rollbackTransaction = false;
    return commit;
  } finally {
    if (rollbackTransaction) {
      rollbackTransaction(pm);
    }
  }
}
 
Example 16
Source File: ShardedCounter.java    From appengine-modules-sample-java with Apache License 2.0 5 votes vote down vote up
public void increment(int count) {
  if (cache != null) {
    final Integer cachedCount = (Integer) cache.get("count" + counterName);
    if (cachedCount != null) {
      cache.put("count" + counterName,
          Integer.valueOf(count + cachedCount.intValue()));
    }
  }

  int shardCount = 0;
  PersistenceManager pm = PMF.get().getPersistenceManager();
  try {
    final DatastoreCounter current = getThisCounter(pm);
    shardCount = current.getShardCount();
  } finally {
    pm.close();
  }

  final Random generator = new Random();
  final int shardNum = generator.nextInt(shardCount);

  pm = PMF.get().getPersistenceManager();
  try {
    final Query randomShardQuery = pm.newQuery(DatastoreCounterShard.class);
    randomShardQuery.setFilter(
        "counterName == nameParam && shardNumber == numParam");
    randomShardQuery.declareParameters("String nameParam, int numParam");
    final List<DatastoreCounterShard> shards =
        (List<DatastoreCounterShard>) randomShardQuery.execute(
            counterName, shardNum);
    if (shards != null && !shards.isEmpty()) {
      final DatastoreCounterShard shard = shards.get(0);
      shard.increment(count);
      pm.makePersistent(shard);
    }
  } finally {
    pm.close();
  }
}
 
Example 17
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public ItemType getItemType(int typeId) {
	ItemType t = null;
	Query query = pm.newQuery( ItemType.class, "typeId == a" );
	query.declareParameters( "java.lang.Integer a" );
	Collection<ItemType> c = (Collection<ItemType>) query.execute(typeId);
	if(!c.isEmpty()) {
		t = c.iterator().next();
	}
	return t;
}
 
Example 18
Source File: SqlActionPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public ActionType getActionType(String name) {
	ActionType at = null;
	Query query = pm.newQuery( ActionType.class, "name == n" );
	query.declareParameters( "java.lang.String n" );
	Collection<ActionType> c = (Collection<ActionType>) query.execute(name);
	if(!c.isEmpty()) {
		at = c.iterator().next();
	}
	return at;
}
 
Example 19
Source File: SqlItemPeer.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public Item getItem(String id) {
	Item i = null;
	Query query = pm.newQuery( Item.class, "clientItemId == i" );
	query.declareParameters( "java.lang.String i" );
	Collection<Item> c = (Collection<Item>) query.execute(id);
	if(!c.isEmpty()) {
		i = c.iterator().next();
	}
	return i;
}
 
Example 20
Source File: SentryStore.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
public CommitContext alterSentryRoleDeleteGroups(String roleName,
    Set<TSentryGroup> groupNames)
        throws SentryNoSuchObjectException {
  boolean rollbackTransaction = true;
  PersistenceManager pm = null;
  roleName = roleName.trim().toLowerCase();
  try {
    pm = openTransaction();
    Query query = pm.newQuery(MSentryRole.class);
    query.setFilter("this.roleName == t");
    query.declareParameters("java.lang.String t");
    query.setUnique(true);
    MSentryRole role = (MSentryRole) query.execute(roleName);
    if (role == null) {
      throw new SentryNoSuchObjectException("Role: " + roleName + " doesn't exist");
    } else {
      query = pm.newQuery(MSentryGroup.class);
      query.setFilter("this.groupName == t");
      query.declareParameters("java.lang.String t");
      query.setUnique(true);
      List<MSentryGroup> groups = Lists.newArrayList();
      for (TSentryGroup tGroup : groupNames) {
        String groupName = tGroup.getGroupName().trim();
        MSentryGroup group = (MSentryGroup) query.execute(groupName);
        if (group != null) {
          group.removeRole(role);
          groups.add(group);
        }
      }
      pm.makePersistentAll(groups);
      CommitContext commit = commitUpdateTransaction(pm);
      rollbackTransaction = false;
      return commit;
    }
  } finally {
    if (rollbackTransaction) {
      rollbackTransaction(pm);
    }
  }
}