Exemplo n.º 1
0
def main():
  # Establish a connection to use for querying.
  connection = gclouddatastore.get_connection(
      DATASET_ID, CLIENT_EMAIL, PRIVATE_KEY_PATH)

  # Start with a Query for Things.
  query = connection.query().kind('Thing')

  print '\nCreating a new Thing called Toy...'
  toy = connection.new_entity('Thing')
  toy.update({'name': 'Toy', 'some_int_value': 1234})
  toy.save()

  print '\nLooking up the Toy...'
  print connection.get_entities([toy.key()])

  print '\nDeleting the Toy...'
  toy.delete()

  print '\nLooking up the Toy again (this should be empty)...'
  print connection.get_entities([toy.key()])

  print '\nShowing first 2 Things...'
  print query.limit(2).fetch()

  print '\nShowing things in "other-namespace"...'
  print query.namespace('other-namespace').fetch()

  print '\nShowing Things named Computer...'
  print query.filter('name =', 'Computer').fetch()

  print '\nFilter by multiple things...'
  print query.filter('name =', 'Computer').filter(
      'my_int_value =', 1234).fetch()
Exemplo n.º 2
0
def main():
  """A full example script demonstrating how to use the client."""

  # Establish a connection to use for querying.
  connection = gclouddatastore.get_connection(CLIENT_EMAIL, PRIVATE_KEY_PATH)
  dataset = connection.dataset(DATASET_ID)

  print '\nCreating a new Thing called Toy...'
  toy = dataset.entity('Thing')
  toy.update({'name': 'Toy', 'some_int_value': 1234})
  toy.save()

  print '\nLooking up the Toy...'
  print dataset.get_entities([toy.key()])

  print '\nDeleting the Toy...'
  toy.delete()

  print '\nLooking up the Toy again (this should be empty)...'
  print dataset.get_entities([toy.key()])

  query = dataset.query().kind('Thing')

  print '\nShowing first 2 Things...'
  print query.limit(2).fetch()

  print '\nShowing Things named Computer...'
  print query.filter('name =', 'Computer').fetch()

  print '\nFilter by multiple things...'
  print query.filter('name =', 'Computer').filter(
      'my_int_value =', 1234).fetch()

  print '\nStarting a transaction...'
  with dataset.transaction():
    print 'Creating and savng an entity...'
    thing = dataset.entity('Thing')
    thing.key(thing.key().name('foo'))
    thing['age'] = 10
    thing.save()

    print 'Creating and saving another entity...'
    thing2 = dataset.entity('Thing')
    thing2.key(thing2.key().name('bar'))
    thing2['age'] = 15
    thing2.save()

    print 'Committing the transaction...',

  print 'done.'

  print '\nDeleting the entities...'
  print thing.delete(), thing2.delete()

  print '\nStarting another transaction...'
  with dataset.transaction() as t:
    print 'Creating an entity...'
    thing = dataset.entity('Thing')
    thing.key(thing.key().name('another'))
    thing.save()

    print 'Rolling back the transaction...'
    t.rollback()

  print 'Was the entity actually created? ... ',

  if dataset.get_entities([thing.key()]):
    print 'yes.'
  else:
    print 'no.'

  print '\nStarting one more transaction...'
  with dataset.transaction():
    print 'Creating a simple thing'
    thing = dataset.entity('Thing')
    thing.save()

    print 'Before committing, the key should be incomplete...', thing.key()

  print 'After committing, the key should be complete...', thing.key()

  print 'Deleting the entity...'
  thing.delete()