sql = sqlpuzzle.select('country.name', avg).from_(table) sql.where(where).where(planet='Earth') sql.group_by('user.country_id') print(sql) # # output: # (for better reading splited to more lines) # # SELECT `country`.`name`, AVG(`age`) AS "avgAge" # FROM `user` LEFT JOIN `country` ON `user`.`country_id`=`country`.`id` # WHERE (`enable` = 1 OR `vip` = 1) AND `planet` = 'Earth' # GROUP BY `user`.`country_id` # table = sqlpuzzle.customsql('`user`') set_ = sqlpuzzle.customsql('`age` = `age` + 1') sql = sqlpuzzle.update(table).set(set_).where(where) print(sql) # output: # UPDATE `user` SET `age` = `age` + 1 WHERE (`enable` = 1 OR `vip` = 1) print(sqlpuzzle.delete_from(table).where(where)) # output: # DELETE FROM `user` WHERE (`enable` = 1 OR `vip` = 1) print(sqlpuzzle.insert_into(table).values(name='Alan')) # output: # INSERT INTO `user` (`name`) VALUES ("Alan")
print(sql) # # output: # (for better reading splited to more lines) # # SELECT `country`.`name`, AVG(`age`) AS "avgAge" # FROM `user` LEFT JOIN `country` ON `user`.`country_id`=`country`.`id` # WHERE (`enable` = 1 OR `vip` = 1) AND `planet` = 'Earth' # GROUP BY `user`.`country_id` # table = sqlpuzzle.customsql('`user`') set_ = sqlpuzzle.customsql('`age` = `age` + 1') sql = sqlpuzzle.update(table).set(set_).where(where) print(sql) # output: # UPDATE `user` SET `age` = `age` + 1 WHERE (`enable` = 1 OR `vip` = 1) print(sqlpuzzle.delete_from(table).where(where)) # output: # DELETE FROM `user` WHERE (`enable` = 1 OR `vip` = 1) print(sqlpuzzle.insert_into(table).values(name='Alan')) # output: # INSERT INTO `user` (`name`) VALUES ("Alan")
def test_update(self): sql = sqlpuzzle.update('user').set(name='Alan').where(id=42) self.assertEqual( str(sql), 'UPDATE "user" SET "name" = \'Alan\' WHERE "id" = 42')
def test_update(): sql = sqlpuzzle.update('user').set(name='Alan').where(id=42) assert str(sql) == 'UPDATE "user" SET "name" = \'Alan\' WHERE "id" = 42'
# - - ConfirmDeleteAllException # - InvalidArgumentException # - - InvalidQueryException # try: sqlpuzzle.select(True) except sqlpuzzle.exceptions.InvalidArgumentException, e: print('catched:', e) try: print(sqlpuzzle.select_from('t').on('t2')) except sqlpuzzle.exceptions.InvalidQueryException, e: print('catched:', e) try: print(sqlpuzzle.update('table').set(name='Alan')) except sqlpuzzle.exceptions.ConfirmUpdateAllException, e: print('catched:', e) try: print(sqlpuzzle.delete().from_('table')) except sqlpuzzle.exceptions.ConfirmDeleteAllException, e: print('catched:', e) # All exceptions are inherited from SqlPuzzleException. try: sqlpuzzle.select(1) except sqlpuzzle.exceptions.SqlPuzzleException, e: print('catched:', e)
def test_update(self): sql = sqlpuzzle.update('user').set(name='Alan').where(id=42) self.assertEqual(str(sql), 'UPDATE "user" SET "name" = \'Alan\' WHERE "id" = 42')
#!/usr/bin/env python # -*- coding: utf-8 -*- import datetime import sqlpuzzle import sqlpuzzle.exceptions sql = sqlpuzzle.update('table') sql.set(name='Harry') try: print(sql) except sqlpuzzle.exceptions.ConfirmUpdateAllException: pass # Update all records is not enabled by default. sql.allow_update_all() print(sql) # output: UPDATE `table` SET `name` = "Harry" try: sql.forbid_update_all() print(sql) except sqlpuzzle.exceptions.ConfirmUpdateAllException: pass # Protected of update all records can be turned on again. sql.where(id=42) print(sql) # output: UPDATE `table` SET `name` = "Harry" WHERE `id` = 42
# - InvalidArgumentException # - - InvalidQueryException # try: sqlpuzzle.select(True) except sqlpuzzle.exceptions.InvalidArgumentException, e: print('catched:', e) try: print(sqlpuzzle.select_from('t').on('t2')) except sqlpuzzle.exceptions.InvalidQueryException, e: print('catched:', e) try: print(sqlpuzzle.update('table').set(name='Alan')) except sqlpuzzle.exceptions.ConfirmUpdateAllException, e: print('catched:', e) try: print(sqlpuzzle.delete().from_('table')) except sqlpuzzle.exceptions.ConfirmDeleteAllException, e: print('catched:', e) # All exceptions are inherited from SqlPuzzleException. try: sqlpuzzle.select(1) except sqlpuzzle.exceptions.SqlPuzzleException, e: print('catched:', e)
#!/usr/bin/env python # -*- coding: utf-8 -*- import datetime import sqlpuzzle import sqlpuzzle.exceptions sql = sqlpuzzle.update('table') sql.set(name='Harry') try: print(sql) except sqlpuzzle.exceptions.ConfirmUpdateAllException: pass # Update all records is not enabled by default. sql.allow_update_all() print(sql) # output: UPDATE `table` SET `name` = "Harry" try: sql.forbid_update_all() print(sql) except sqlpuzzle.exceptions.ConfirmUpdateAllException: pass # Protected of update all records can be turned on again. sql.where(id=42) print(sql) # output: UPDATE `table` SET `name` = "Harry" WHERE `id` = 42 sql.set({