Gua baru belajar web2py nih buat implementasi dan mutasi program tradeERP gua... Web2py ada 3 bagian utama yaitu Models Controller dan View, untuk sementara gua learning Data Models dulu..
Di web2py support many data source... diantaranya Gluon dari Google App Engine, PostgreSQL, Mysql, dan SQLlite (default)
contoh menggunakan Gluon
try:
from gluon.contrib.gql import * # if running on Google App Engine
except:
db = SQLDB('sqlite://storage.db') # if not, use SQLite or other DB
else:
db = GQLDB() # connect to Google BigTable
session.connect(request, response, db=db) # and store sessions there
Deklarasi standar database , table, insert, count, delete, dan update
db = SQLDB(‘postgres://user:password@hostname/db’, pools=10)
db.define_table(‘person’,db.Field(’name’,’string’))
id= db.person.insert(name=’max’)
query=(db.person.id==id)
db(query).count()
db(query).delete()
db(query).update(name=’Max’)
rows = db(query).select(orderby=db.person.name)
for row in rows: print row.name
drop table dan truncate
db.person.truncate()
db.person.drop()
Contoh deklarasi table , dan validasi table, serta reference table (foreign key)
db.define_table('products', SQLField('name'), SQLField('description',
'text'))
# MANY (users) TO MANY (purchases)
db.define_table('purchases', SQLField('buyer_id', db.users),
SQLField('product_id', db.products), SQLField('quantity'
, 'integer'))
purchased = (db.users.id == db.purchases.buyer_id) & (db.products.id
== db.purchases.product_id)
db.users.name.requires = IS_NOT_EMPTY()
db.users.email.requires = [IS_EMAIL(), IS_NOT_IN_DB(db, 'users.email')]
db.dogs.owner_id.requires = IS_IN_DB(db, 'users.id', 'users.name')
db.dogs.name.requires = IS_NOT_EMPTY()
db.dogs.type.requires = IS_IN_SET(['small', 'medium', 'large'])
db.purchases.buyer_id.requires = IS_IN_DB(db, 'users.id', 'users.name')
db.purchases.product_id.requires = IS_IN_DB(db, 'products.id',
'products.name')
db.purchases.quantity.requires = IS_INT_IN_RANGE(0, 10)
Pretty clean and readable dibanding JAVA .. hehehehehehe :D
Di web2py support many data source... diantaranya Gluon dari Google App Engine, PostgreSQL, Mysql, dan SQLlite (default)
contoh menggunakan Gluon
try:
from gluon.contrib.gql import * # if running on Google App Engine
except:
db = SQLDB('sqlite://storage.db') # if not, use SQLite or other DB
else:
db = GQLDB() # connect to Google BigTable
session.connect(request, response, db=db) # and store sessions there
Deklarasi standar database , table, insert, count, delete, dan update
db = SQLDB(‘postgres://user:password@hostname/db’, pools=10)
db.define_table(‘person’,db.Field(’name’,’string’))
id= db.person.insert(name=’max’)
query=(db.person.id==id)
db(query).count()
db(query).delete()
db(query).update(name=’Max’)
rows = db(query).select(orderby=db.person.name)
for row in rows: print row.name
drop table dan truncate
db.person.truncate()
db.person.drop()
Contoh deklarasi table , dan validasi table, serta reference table (foreign key)
db.define_table('products', SQLField('name'), SQLField('description',
'text'))
# MANY (users) TO MANY (purchases)
db.define_table('purchases', SQLField('buyer_id', db.users),
SQLField('product_id', db.products), SQLField('quantity'
, 'integer'))
purchased = (db.users.id == db.purchases.buyer_id) & (db.products.id
== db.purchases.product_id)
db.users.name.requires = IS_NOT_EMPTY()
db.users.email.requires = [IS_EMAIL(), IS_NOT_IN_DB(db, 'users.email')]
db.dogs.owner_id.requires = IS_IN_DB(db, 'users.id', 'users.name')
db.dogs.name.requires = IS_NOT_EMPTY()
db.dogs.type.requires = IS_IN_SET(['small', 'medium', 'large'])
db.purchases.buyer_id.requires = IS_IN_DB(db, 'users.id', 'users.name')
db.purchases.product_id.requires = IS_IN_DB(db, 'products.id',
'products.name')
db.purchases.quantity.requires = IS_INT_IN_RANGE(0, 10)
Pretty clean and readable dibanding JAVA .. hehehehehehe :D
Powered by ScribeFire.
2 komentar:
Dear, ingin meluruskan beberapa pernyataan di atas.
Sebenarnya, Gluon itu bukan data source. Gluon adalah sebenarnya adalah web2py itu sendiri. Untuk data source dari Google App Engine, datanya dalam bentuk BigTable, dibungkus dengan bahasa GQL.
Ok ... thanks ya atas koreksinya ...
Posting Komentar