Esempio n. 1
0
def insert(cursor, table, objects):
    objects = utilities.forceList(objects)
    if len(objects) == 0:
        return

    # find columns of the table, only data in columns can be set
    fields = fieldNameList(cursor, table)

    cursor.executemany(
        """
        INSERT INTO {table} VALUES ({fieldValues})
    """.format(table=table, fieldValues=placeholders(fields)),
        tuples(objects, fields))
Esempio n. 2
0
def update(cursor, table, objects):
    objects = utilities.forceList(objects)
    if len(objects) == 0:
        return

    # find columns of the table, only data in columns can be updated
    fields = fieldNameList(cursor, table)

    # upgrade rows where the primary keys hold
    pkFields = pkFieldNameList(cursor, table)

    # gather objects that have the same fields set
    # used so we can use executemany
    queryCategories = {}
    for obj in objects:
        objectFields = []
        for field in fields:
            if field in obj:
                objectFields.append(field)

        category = ' '.join(objectFields)
        try:
            existing = queryCategories[category]
        except:
            existing = []
        existing.append(obj)
        queryCategories[category] = existing

    for category, objects in queryCategories.items():
        fields = category.split(" ")

        cursor.executemany(
            """
            UPDATE {table}
            SET {fieldAssignments}
            WHERE {pkFieldQueries}
        """.format(table=table,
                   fieldAssignments=assignments(fields),
                   pkFieldQueries=queries(pkFields)),
            tuples(objects, [*fields, *pkFields]))