generateInsertRelationshipSQLs method

FutureOr<List<SQL>> generateInsertRelationshipSQLs(
  1. Transaction transaction,
  2. String entityName,
  3. String table,
  4. String field,
  5. dynamic id,
  6. String otherTableName,
  7. List otherIds,
)

Implementation

FutureOr<List<SQL>> generateInsertRelationshipSQLs(
    Transaction transaction,
    String entityName,
    String table,
    String field,
    dynamic id,
    String otherTableName,
    List otherIds) {
  var retTableScheme = getTableScheme(table);

  return retTableScheme.resolveMapped((tableScheme) {
    if (tableScheme == null) {
      var errorMsg = "Can't find `TableScheme` for table: $table";
      _log.severe(errorMsg);
      throw StateError(errorMsg);
    }

    var relationship = tableScheme.getTableRelationshipReference(
        sourceTable: table, sourceField: field, targetTable: otherTableName);

    if (relationship == null) {
      throw StateError(
          "Can't find TableRelationshipReference for tables: $table -> $otherTableName\n$tableScheme");
    }

    var sqls = otherIds.isEmpty
        ? [SQL.dummy]
        : otherIds
            .map((otherId) =>
                _generateInsertRelationshipSQL(relationship, id, otherId))
            .toList();

    var constrainSQL = _generateConstrainRelationshipSQL(
        tableScheme, table, field, id, otherTableName, otherIds);

    var lastIdx = sqls.lastIndex;

    var sqlsLast = sqls[lastIdx].ensureNotSharedDummy();
    sqls[lastIdx] = sqlsLast;

    sqlsLast.posSQL = [constrainSQL];

    return sqls;
  });
}