Exemple #1
0
        // Insert methods
        public async Task <T> InsertAsync <T>(string insertStatement, dynamic vars, bool ignoreIfDuplicate = false)
        {
            InsertStatement statement = new InsertStatement(this.database, insertStatement);
            object          result    = await this.InsertAsync(statement, vars, ignoreIfDuplicate : ignoreIfDuplicate);

            return((T)Convert.ChangeType(result, typeof(T)));
        }
Exemple #2
0
        public async Task <object> InsertAsync(InsertStatement insertStatement, dynamic vars, bool ignoreIfDuplicate = false)
        {
            // Convert statementParams
            Dict varsDict = insertStatement.ConvertParamsToDict(vars);

            this.database.PreprocessInput(insertStatement.StatementFromRefs[0].table.Name, varsDict);
            Dict varsOverrides = this.database.GetOverrideValues(insertStatement.StatementFromRefs[0].table);

            varsDict.UpdateFrom(varsOverrides);
            Dict varsDefaults = this.database.GetDefaultValues(insertStatement.StatementFromRefs[0].table);

            // Get the executable sql and params
            (string executableSql, Dict executableParams) = insertStatement.GetExecutableSqlAndParams(varsDict, varsDefaults);

            // Execute insert and return getGenerateId lambda
            Func <object> getGeneratedId;

            try {
                getGeneratedId = await this.DoInsertAsync(executableSql, executableParams, ignoreIfDuplicate);
            }
            catch (DuplicateKeyDatabaseException) {
                if (ignoreIfDuplicate)
                {
                    return(null);
                }
                throw;
            }

            // Determine keyValue (either keyValue is from a generated id or was included in the statement params)
            object keyValue;

            if (insertStatement.StatementFromRefs[0].table.AutoIncrementFieldName != null && getGeneratedId != null)
            {
                keyValue = getGeneratedId();
            }
            else
            {
                keyValue = executableParams.GetKeyValue(insertStatement.StatementFromRefs[0].table.Indexes[0].FieldNames);
            }

            // Create data event
            this.dataEvents.Add(new KeyValueDataEvent(DataEventType.Insert, insertStatement.StatementFromRefs[0].table.Name, keyValue));

            this.database.InsertCount++;

            return(keyValue);
        }