"""Generate a sample project with triggers""" from AndroidCodeGenerator.generator import Generator from AndroidCodeGenerator.sql_validator import SQLTester from AndroidCodeGenerator.db_table import (Table, Column, ForeignKey, Unique, Trigger, Check) from AndroidCodeGenerator.database_triggers import DatabaseTriggers tables = [] triggers = [] tasklists = Table('tasklist') tasklists.add_cols( Column('title').text.not_null.default("''"), Column('updated').integer, Column('listtype').text, Column('sorting').text, Column('deleted').integer.not_null.default(0), # New fields Column('ctime').timestamp.default_current_timestamp, Column('mtime').timestamp.default_current_timestamp, # GTask fields Column('account').text, Column('remoteid').text) tasklists.add_constraints(Unique('account', 'remoteid').on_conflict_replace) tables.append(tasklists) tasks = Table('task') tasks.add_cols( Column('title').text.not_null.default("''"),
"""Generate a sample project""" from AndroidCodeGenerator.generator import Generator from AndroidCodeGenerator.db_table import Table, Column, ForeignKey, Unique persons = Table('Person').add_cols(Column('firstname').text.not_null.default("''"),\ Column('lastname').text.not_null.default("''"),\ Column('bio').text.not_null.default("''")) g = Generator(srcdir='./sample/src/', pkg='com.example.appname.database') g.add_tables(persons) g.write()
"""Generate a sample project with triggers""" from AndroidCodeGenerator.generator import Generator from AndroidCodeGenerator.db_table import (Table, Column, ForeignKey, Unique, Trigger) from AndroidCodeGenerator.database_triggers import DatabaseTriggers persons = Table('Person').add_cols(Column('firstname').text.not_null.default("''"),\ Column('lastname').text.not_null.default("''"),\ Column('bio').text.not_null.default("''"))\ .add_constraints(Unique('firstname').on_conflict_replace) log = Table('Log').add_cols( Column('pId').integer.not_null, Column('firstname').text.not_null, Column('lastname').text.not_null, Column('bio').text.not_null, Column('time').timestamp.default_current_timestamp) # Create a trigger that keeps the log up to date # I recommend using temp triggers unless you see a performance hit trigger = Trigger('tr_log').temp.if_not_exists trigger.after.update_on(log.name) # Raw sql trigger.do_sql("INSERT INTO {table} ({cols}) VALUES\ ({oldcols})".format(table=log.name, cols=log.list_column_names(exclude=['_id', 'time']), oldcols=persons.list_column_names(exclude=[], prefix="old.")))
"""Generate a sample project""" from AndroidCodeGenerator.generator import Generator from AndroidCodeGenerator.db_table import (Table, Column, ForeignKey, Unique, Trigger) from AndroidCodeGenerator.sql_validator import SQLTester links = Table('Link').add_cols(Column('sha').text.not_null, Column('url').text.not_null, Column('timestamp').timestamp.not_null\ .default_current_timestamp, Column('deleted').integer.not_null\ .default(0), Column('synced').integer.not_null\ .default(0)) links.add_constraints(Unique('url').on_conflict_ignore, Unique('sha').on_conflict_ignore) ''' deltrigger = Trigger("tr_del_link").temp.if_not_exists deltrigger.after.delete_on(links.name) deltrigger.do_sql("INSERT INTO {table} (sha, url, timestamp, deleted) VALUES\ (old.sha, old.url, old.timestamp, 1)".format(table=synclinks.name)) intrigger = Trigger("tr_ins_link").temp.if_not_exists intrigger.after.insert_on(links.name) intrigger.do_sql("INSERT INTO {table} (sha, url, timestamp) \ VALUES (new.sha, new.url, new.timestamp)"\ .format(table=synclinks.name))