/// <summary>
        /// Execute an INSERT query.
        /// </summary>
        /// <param name="tableName">The table in which you wish to INSERT.</param>
        /// <param name="keyValuePairs">The key-value pairs for the row you wish to INSERT.</param>
        /// <returns>A DataTable containing the results.</returns>
        public DataTable Insert(string tableName, Dictionary <string, object> keyValuePairs)
        {
            if (String.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (keyValuePairs == null || keyValuePairs.Count < 1)
            {
                throw new ArgumentNullException(nameof(keyValuePairs));
            }

            #region Build-Key-Value-Pairs

            string keys   = "";
            string values = "";
            int    added  = 0;
            foreach (KeyValuePair <string, object> curr in keyValuePairs)
            {
                if (String.IsNullOrEmpty(curr.Key))
                {
                    continue;
                }

                if (added == 0)
                {
                    #region First

                    keys += PostgresqlHelper.PreparedFieldname(curr.Key);
                    if (curr.Value != null)
                    {
                        if (curr.Value is DateTime || curr.Value is DateTime?)
                        {
                            values += "'" + DbTimestamp((DateTime)curr.Value) + "'";
                        }
                        else if (curr.Value is int || curr.Value is long || curr.Value is decimal)
                        {
                            values += curr.Value.ToString();
                        }
                        else
                        {
                            if (Helper.IsExtendedCharacters(curr.Value.ToString()))
                            {
                                values += PostgresqlHelper.PreparedUnicodeValue(curr.Value.ToString());
                            }
                            else
                            {
                                values += PostgresqlHelper.PreparedStringValue(curr.Value.ToString());
                            }
                        }
                    }
                    else
                    {
                        values += "null";
                    }

                    #endregion
                }
                else
                {
                    #region Subsequent

                    keys += "," + PostgresqlHelper.PreparedFieldname(curr.Key);
                    if (curr.Value != null)
                    {
                        if (curr.Value is DateTime || curr.Value is DateTime?)
                        {
                            values += ",'" + DbTimestamp((DateTime)curr.Value) + "'";
                        }
                        else if (curr.Value is int || curr.Value is long || curr.Value is decimal)
                        {
                            values += "," + curr.Value.ToString();
                        }
                        else
                        {
                            if (Helper.IsExtendedCharacters(curr.Value.ToString()))
                            {
                                values += "," + PostgresqlHelper.PreparedUnicodeValue(curr.Value.ToString());
                            }
                            else
                            {
                                values += "," + PostgresqlHelper.PreparedStringValue(curr.Value.ToString());
                            }
                        }
                    }
                    else
                    {
                        values += ",null";
                    }

                    #endregion
                }

                added++;
            }

            #endregion

            #region Build-INSERT-Query-and-Submit

            return(Query(PostgresqlHelper.InsertQuery(tableName, keys, values)));

            #endregion
        }
        /// <summary>
        /// Execute an UPDATE query.
        /// The updated rows are returned.
        /// </summary>
        /// <param name="tableName">The table in which you wish to UPDATE.</param>
        /// <param name="keyValuePairs">The key-value pairs for the data you wish to UPDATE.</param>
        /// <param name="filter">The expression containing the UPDATE filter (i.e. WHERE clause data).</param>
        /// <returns>DataTable containing the updated rows.</returns>
        public DataTable Update(string tableName, Dictionary <string, object> keyValuePairs, Expression filter)
        {
            if (String.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (keyValuePairs == null || keyValuePairs.Count < 1)
            {
                throw new ArgumentNullException(nameof(keyValuePairs));
            }

            #region Build-Key-Value-Clause

            string keyValueClause = "";
            int    added          = 0;
            foreach (KeyValuePair <string, object> curr in keyValuePairs)
            {
                if (String.IsNullOrEmpty(curr.Key))
                {
                    continue;
                }

                if (added == 0)
                {
                    if (curr.Value != null)
                    {
                        if (curr.Value is DateTime || curr.Value is DateTime?)
                        {
                            keyValueClause += PostgresqlHelper.PreparedFieldname(curr.Key) + "='" + DbTimestamp((DateTime)curr.Value) + "'";
                        }
                        else if (curr.Value is int || curr.Value is long || curr.Value is decimal)
                        {
                            keyValueClause += PostgresqlHelper.PreparedFieldname(curr.Key) + "=" + curr.Value.ToString();
                        }
                        else
                        {
                            if (Helper.IsExtendedCharacters(curr.Value.ToString()))
                            {
                                keyValueClause += PostgresqlHelper.PreparedFieldname(curr.Key) + "=" + PostgresqlHelper.PreparedUnicodeValue(curr.Value.ToString());
                            }
                            else
                            {
                                keyValueClause += PostgresqlHelper.PreparedFieldname(curr.Key) + "=" + PostgresqlHelper.PreparedStringValue(curr.Value.ToString());
                            }
                        }
                    }
                    else
                    {
                        keyValueClause += PostgresqlHelper.PreparedFieldname(curr.Key) + "= null";
                    }
                }
                else
                {
                    if (curr.Value != null)
                    {
                        if (curr.Value is DateTime || curr.Value is DateTime?)
                        {
                            keyValueClause += "," + PostgresqlHelper.PreparedFieldname(curr.Key) + "='" + DbTimestamp((DateTime)curr.Value) + "'";
                        }
                        else if (curr.Value is int || curr.Value is long || curr.Value is decimal)
                        {
                            keyValueClause += "," + PostgresqlHelper.PreparedFieldname(curr.Key) + "=" + curr.Value.ToString();
                        }
                        else
                        {
                            if (Helper.IsExtendedCharacters(curr.Value.ToString()))
                            {
                                keyValueClause += "," + PostgresqlHelper.PreparedFieldname(curr.Key) + "=" + PostgresqlHelper.PreparedUnicodeValue(curr.Value.ToString());
                            }
                            else
                            {
                                keyValueClause += "," + PostgresqlHelper.PreparedFieldname(curr.Key) + "=" + PostgresqlHelper.PreparedStringValue(curr.Value.ToString());
                            }
                        }
                    }
                    else
                    {
                        keyValueClause += "," + PostgresqlHelper.PreparedFieldname(curr.Key) + "= null";
                    }
                }
                added++;
            }

            #endregion

            #region Build-UPDATE-Query-and-Submit

            return(Query(PostgresqlHelper.UpdateQuery(tableName, keyValueClause, filter)));

            #endregion
        }
Exemple #3
0
        /// <summary>
        /// Execute an INSERT query with multiple values within a transaction.
        /// </summary>
        /// <param name="tableName">The table in which you wish to INSERT.</param>
        /// <param name="keyValuePairList">List of dictionaries containing key-value pairs for the rows you wish to INSERT.</param>
        public void InsertMultiple(string tableName, List <Dictionary <string, object> > keyValuePairList)
        {
            if (String.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (keyValuePairList == null || keyValuePairList.Count < 1)
            {
                throw new ArgumentNullException(nameof(keyValuePairList));
            }

            #region Validate-Inputs

            Dictionary <string, object> reference = keyValuePairList[0];

            if (keyValuePairList.Count > 1)
            {
                foreach (Dictionary <string, object> dict in keyValuePairList)
                {
                    if (!(reference.Count == dict.Count) || !(reference.Keys.SequenceEqual(dict.Keys)))
                    {
                        throw new ArgumentException("All supplied dictionaries must contain exactly the same keys.");
                    }
                }
            }

            #endregion

            #region Build-Keys

            string keys      = "";
            int    keysAdded = 0;
            foreach (KeyValuePair <string, object> curr in reference)
            {
                if (keysAdded > 0)
                {
                    keys += ",";
                }
                keys += PostgresqlHelper.PreparedFieldName(curr.Key);
                keysAdded++;
            }

            #endregion

            #region Build-Values

            List <string> values = new List <string>();

            foreach (Dictionary <string, object> currDict in keyValuePairList)
            {
                string vals      = "";
                int    valsAdded = 0;

                foreach (KeyValuePair <string, object> currKvp in currDict)
                {
                    if (valsAdded > 0)
                    {
                        vals += ",";
                    }

                    if (currKvp.Value != null)
                    {
                        if (currKvp.Value is DateTime || currKvp.Value is DateTime?)
                        {
                            vals += "'" + DbTimestamp((DateTime)currKvp.Value) + "'";
                        }
                        else if (currKvp.Value is int || currKvp.Value is long || currKvp.Value is decimal)
                        {
                            vals += currKvp.Value.ToString();
                        }
                        else
                        {
                            if (Helper.IsExtendedCharacters(currKvp.Value.ToString()))
                            {
                                vals += PostgresqlHelper.PreparedUnicodeValue(currKvp.Value.ToString());
                            }
                            else
                            {
                                vals += PostgresqlHelper.PreparedStringValue(currKvp.Value.ToString());
                            }
                        }
                    }
                    else
                    {
                        vals += "null";
                    }

                    valsAdded++;
                }

                values.Add(vals);
            }

            #endregion

            #region Build-INSERT-Query-and-Submit

            Query(PostgresqlHelper.InsertMultipleQuery(tableName, keys, values));

            #endregion
        }