Esempio n. 1
0
def put(entities, connection=None, dataset_id=None):
    """Save the entities in the Cloud Datastore.

    :type entities: list of :class:`gcloud.datastore.entity.Entity`
    :param entities: The entities to be saved to the datastore.

    :type connection: :class:`gcloud.datastore.connection.Connection`
    :param connection: Optional connection used to connect to datastore.
                       If not passed, inferred from the environment.

    :type dataset_id: :class:`gcloud.datastore.connection.Connection`
    :param dataset_id: Optional. The dataset ID used to connect to datastore.
                       If not passed, inferred from the environment.

    :raises: EnvironmentError if ``connection`` or ``dataset_id`` not passed,
             and cannot be inferred from the environment.  ValueError if
             one or more entities has a key with a dataset ID not matching
             the passed / inferred dataset ID.
    """
    if not entities:
        return

    connection = _require_connection(connection)
    dataset_id = _require_dataset_id(dataset_id, entities[0].key)

    current = Batch.current()
    in_batch = current is not None
    if not in_batch:
        current = Batch(dataset_id=dataset_id, connection=connection)
    for entity in entities:
        current.put(entity)
    if not in_batch:
        current.commit()
Esempio n. 2
0
def put(entities, connection=None):
    """Save the entities in the Cloud Datastore.

    :type entities: list of :class:`gcloud.datastore.entity.Entity`
    :param entities: The entities to be saved to the datastore.

    :type connection: :class:`gcloud.datastore.connection.Connection`
    :param connection: Optional connection used to connect to datastore.
    """
    if not entities:
        return

    connection = connection or _implicit_environ.CONNECTION

    current = Batch.current()
    in_batch = current is not None
    if not in_batch:
        keys = [entity.key for entity in entities]
        dataset_id = _get_dataset_id_from_keys(keys)
        current = Batch(dataset_id=dataset_id, connection=connection)
    for entity in entities:
        current.put(entity)
    if not in_batch:
        current.commit()