def test_override_table(self): builder = UpdateBuilder() \ .table("B") \ .where("id", Integer(), 1) \ .add("a", Integer(), 1) \ .add("b", Integer(), 3) \ .table("C") self.assertEqual(builder.build(), 'UPDATE "C" SET "a" = 1, "b" = 3 WHERE "id" = 1')
def test_build(self): builder = UpdateBuilder() \ .table("B") \ .where("id", Integer(), 1) \ .add("a", Text(), "The quick brown fox jumps over the lazy dog") \ .add("b", Integer(), 12) \ .add("c", Float(), 1.0) \ .add("d", TimeStamp(), datetime.datetime(2004, 10, 19, 10, 23, 54, tzinfo=datetime.timezone.utc)) self.assertRegex( builder.build(), r'UPDATE "B" SET "a" = \$[A-Za-z0-9]{5}\$The quick brown fox jumps over the lazy dog\$[A-Za-z0-9]{5}\$, "b" = 12, "c" = 1\.0, "d" = TIMESTAMP \'2004-10-19 10:23:54\' WHERE "id" = 1' )
def test_no_table(self): builder = UpdateBuilder() \ .add("a", Integer(), 1) \ .add("b", Integer(), 3) \ .add("a", Float(), 1.0) self.assertRaises(AssertionError, builder.build)
def test_is_empty(self): builder = UpdateBuilder() self.assertTrue(builder.is_empty()) builder.add("a", Integer(), 1) self.assertFalse(builder.is_empty()) builder.add("b", Integer(), 3) \ .add("c", Float(), 1.0) self.assertFalse(builder.is_empty())
def _execute_sql_function(self, entity: Entity, query_type: str): tables, primary_key = self._get_tables_and_primary_key_of_entity( entity) assert primary_key is not None primary_key_field_name, primary_key_name, primary_key_type = primary_key primary_key_value = getattr(entity, primary_key_field_name) primary_key_saved_value = entity.get_primary_key() entity._primary_key = primary_key_value for table_name in tables: builder = None if query_type is self.__insert_query: builder = InsertBuilder().into(table_name) elif query_type is self.__update_query: builder = UpdateBuilder().table(table_name) elif query_type is self.__delete_query: builder = DeleteBuilder().table(table_name) assert builder is not None if query_type in (self.__insert_query, self.__update_query): types, names, values = find_names_types_values_of_column( table_name, entity, self.__all_data, self.__class_names) for field_name in types.keys(): store_type = types[field_name] column_name = names[field_name] value = values[field_name] # case for values that are not already assigned (NULL) or are assigned with None if not isinstance(value, Column) and \ not isinstance(value, PrimaryKey) and \ not isinstance(value, Relationship) and \ value is not None: builder.add(column_name, store_type, value) if query_type in (self.__update_query, self.__delete_query): builder.where(primary_key_name, primary_key_type, primary_key_saved_value) self._execute_query(builder.build(), query_type)
def test_empty(self): builder = UpdateBuilder() \ .table("B") \ .where("id", Integer(), 1) self.assertRaises(AssertionError, builder.build)