private DbTBool<T> CreateInsertIfNotExists() { if (!Info.InsertColumns.Any()) return DoNothing; string insertIntoCmd = InsertIntoCmd(); string insertedValues = InsertedValues(Info.InsertColumns); string whereEquals = WhereEquals(EqualityColumns); if (Info.AutoKeyColumn == null) { if (!Info.InsertAutoSyncColumns.Any()) { return (connection, obj, transaction, commandTimeout) => { string cmd = $"IF NOT EXISTS (\nSELECT * FROM {FullyQualifiedTableName}\nWHERE \t{whereEquals})\n{insertIntoCmd}{insertedValues}"; try { int count = connection.Execute(cmd, obj, transaction, commandTimeout); return count > 0; } catch (Exception ex) { throw new InvalidOperationException(ex.Message + "\n" + cmd); } }; } DbTVoid<T> insertAutoSync = Queries.InsertAutoSync; return (connection, obj, transaction, commandTimeout) => { string cmd = $"IF NOT EXISTS (\nSELECT * FROM {FullyQualifiedTableName}\nWHERE \t{whereEquals})\n{insertIntoCmd}{insertedValues}"; int count; try { count = connection.Execute(cmd, obj, transaction, commandTimeout); } catch (Exception ex) { throw new InvalidOperationException(ex.Message + "\n" + cmd); } insertAutoSync(connection, obj, transaction, commandTimeout); return count > 0; }; } string selectAutoKey = SelectAutoKey(); MemberSetter autoKeySetter = Info.AutoKeyColumn.Setter; if (!Info.InsertAutoSyncColumns.Any()) { return (connection, obj, transaction, commandTimeout) => { string cmd = $"IF NOT EXISTS (\nSELECT * FROM {FullyQualifiedTableName}\nWHERE \t{whereEquals})\n{insertIntoCmd}{insertedValues};\n{selectAutoKey}"; object key; try { key = connection.QueryFirst<dynamic>(cmd, obj, transaction, commandTimeout).Id; } catch (Exception ex) { throw new InvalidOperationException(ex.Message + "\n" + cmd); } if (key != null) { autoKeySetter(obj, key); return true; } return false; }; } else { DbTVoid<T> insertAutoSync = Queries.InsertAutoSync; return (connection, obj, transaction, commandTimeout) => { string cmd = $"IF NOT EXISTS (\nSELECT * FROM {FullyQualifiedTableName}\nWHERE \t{whereEquals})\n{insertIntoCmd}{insertedValues};\n{selectAutoKey}"; object key; try { key = connection.QueryFirst<dynamic>(cmd, obj, transaction, commandTimeout).Id; } catch (Exception ex) { throw new InvalidOperationException(ex.Message + "\n" + cmd); } if (key != null) { insertAutoSync(connection, obj, transaction, commandTimeout); autoKeySetter(obj, key); return true; } return false; }; } }
private DbTVoid<T> CreateInsert() { if (!Info.InsertColumns.Any()) // NoInsertsAttribute return DoNothingVoid; string insertIntoCmd = InsertIntoCmd(); string insertedValues = InsertedValues(Info.InsertColumns); if (Info.AutoKeyColumn == null) { if (!Info.InsertAutoSyncColumns.Any()) { return (connection, obj, transaction, commandTimeout) => { string cmd = insertIntoCmd + insertedValues; try { connection.Execute(cmd, obj, transaction, commandTimeout); } catch (Exception ex) { throw new InvalidOperationException(ex.Message + "\n" + cmd); } }; } DbTVoid<T> insertAutoSync = Queries.InsertAutoSync; return (connection, obj, transaction, commandTimeout) => { string cmd = insertIntoCmd + insertedValues; try { connection.Execute(cmd, obj, transaction, commandTimeout); } catch (Exception ex) { throw new InvalidOperationException(ex.Message + "\n" + cmd); } insertAutoSync(connection, obj, transaction, commandTimeout); }; } string selectAutoKey = SelectAutoKey(); MemberSetter autoKeySetter = Info.AutoKeyColumn.Setter; if (!Info.InsertAutoSyncColumns.Any()) { return (connection, obj, transaction, commandTimeout) => { string cmd = insertIntoCmd + insertedValues + ";\n" + selectAutoKey; IDictionary<string, object> key; try { key = connection.QueryFirst(cmd, obj, transaction, commandTimeout); } catch (Exception ex) { throw new InvalidOperationException(ex.Message + "\n" + cmd); } autoKeySetter(obj, key.Values.First()); }; } else { DbTVoid<T> insertAutoSync = Queries.InsertAutoSync; return (connection, obj, transaction, commandTimeout) => { string cmd = insertIntoCmd + insertedValues + ";\n" + selectAutoKey; IDictionary<string, object> key; try { key = connection.QueryFirst(cmd, obj, transaction, commandTimeout); } catch (Exception ex) { throw new InvalidOperationException(ex.Message + "\n" + cmd); } autoKeySetter(obj, key.Values.First()); insertAutoSync(connection, obj, transaction, commandTimeout); }; } }