Exemplo n.º 1
0
 public object this[SqlIdentifier fieldName]
 {
     get
     {
         if (Fields == null)
         {
             return(null);
         }
         else if (Fields[fieldName].Value is DBNull)
         {
             return(null);
         }
         else
         {
             return(Fields[fieldName].Value);
         }
     }
     set
     {
         if (Fields == null)
         {
             Fields = new ColumnValueCollection();
         }
         if (Fields[fieldName].Value != value && this.IsModified == false)
         {
             this.IsModified = true;
         }
         Fields[fieldName].Value = value;
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <exception cref="InvalidQueryException">The generated query is invalid.</exception>
        public override string ToString()
        {
            var values = ColumnValueCollection.GetValues();

            if (values == null || values.Length == 0)
            {
                throw InvalidQueryException.CreateEmptyColumnList();
            }

            var sb = new StringBuilder();

            sb.Append("UPDATE ");
            sb.Append(Settings.EscapeTable(Table));
            sb.Append(" SET ");

            foreach (var kvp in values)
            {
                sb.Append(Settings.EscapeColumn(kvp.Key));
                sb.Append("=");
                sb.Append(kvp.Value);
                sb.Append(",");
            }

            sb.Length--;

            return(sb.ToString());
        }
Exemplo n.º 3
0
 public Row()
 {
     this.IsNew = true;
     if (Fields == null)
     {
         Fields = new ColumnValueCollection();
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <exception cref="InvalidQueryException">The query is invalid.</exception>
        public override string ToString()
        {
            var values = ColumnValueCollection.GetValues();

            if (values == null || values.Length == 0)
            {
                throw InvalidQueryException.CreateEmptyColumnList();
            }

            var sb = new StringBuilder();

            // Base function
            sb.Append("INSERT ");

            if (IgnoreExistsValue)
            {
                sb.Append("IGNORE ");
            }

            sb.Append("INTO ");
            sb.Append(Settings.EscapeTable(Table));

            // Columns
            sb.Append(" (");

            foreach (var kvp in values)
            {
                sb.Append("`");
                sb.Append(kvp.Key);
                sb.Append("`,");
            }

            sb.Length--;

            // Values
            sb.Append(") VALUES (");

            foreach (var kvp in values)
            {
                sb.Append(kvp.Value);
                sb.Append(",");
            }

            sb.Length--;
            sb.Append(") ");

            return(sb.ToString().Trim());
        }
Exemplo n.º 5
0
        /// <summary>
        /// Returns an SQL representation of a collection of identifier names (i.e. column names) and
        /// values.
        /// </summary>
        /// <param name="columnValues">The collection containin one or more pair of column name and value.</param>
        /// <param name="style">Update or Insert style.</param>
        /// <param name="useParameters">Use parameter names instead of actual values.</param>
        /// <returns>A string containing the SQL portion.</returns>
        private string SqlText(ColumnValueCollection columnValues, ColumnValueFormatStyles style, bool useParameters, int parameterNameSuffix)
        {
            switch (style)
            {
            case ColumnValueFormatStyles.UpdateStyle:
                // UPDATE style: col1=val1, col2=val2, ...
                var ResUpd = new StringBuilder();
                foreach (var Col in columnValues)
                {
                    if (ResUpd.Length > 0)
                    {
                        ResUpd.Append(", ");
                    }
                    ResUpd.Append(this.SqlText(Col.ColumnIdentifier));
                    ResUpd.Append("=");
                    if (useParameters && Col.ValueCanBeParameter)
                    {
                        ResUpd.Append("@" + this.GetParameterName(Col.ColumnIdentifier, parameterNameSuffix));
                    }
                    else
                    {
                        ResUpd.Append(this.FormatValue(Col.Value));
                    }
                }
                return(ResUpd.ToString());

            case ColumnValueFormatStyles.InsertStyle:
                // INSERT style: (col1, col2, ...) VAUES (val1, val2, ...)
                var ResInsCols = new StringBuilder();
                var ResInsVals = new StringBuilder();

                foreach (var Col in columnValues)
                {
                    if (ResInsCols.Length > 0)
                    {
                        ResInsCols.Append(", ");
                        ResInsVals.Append(", ");
                    }
                    ResInsCols.Append(this.SqlText(Col.ColumnIdentifier));
                    if (useParameters && Col.ValueCanBeParameter)
                    {
                        ResInsVals.Append("@" + this.GetParameterName(Col.ColumnIdentifier, parameterNameSuffix));
                    }
                    else
                    {
                        ResInsVals.Append(this.FormatValue(Col.Value));
                    }
                }
                return("(" + ResInsCols.ToString() + ") VALUES (" + ResInsVals.ToString() + ")");

            case ColumnValueFormatStyles.InsertStyleValuesOnly:
                // INSERT style: (col1, col2, ...) VAUES (val1, val2, ...)
                var ResInsValsOnly = new StringBuilder();

                foreach (var Col in columnValues)
                {
                    if (ResInsValsOnly.Length > 0)
                    {
                        ResInsValsOnly.Append(", ");
                    }
                    if (useParameters && Col.ValueCanBeParameter)
                    {
                        ResInsValsOnly.Append("@" + this.GetParameterName(Col.ColumnIdentifier, parameterNameSuffix));
                    }
                    else
                    {
                        ResInsValsOnly.Append(this.FormatValue(Col.Value));
                    }
                }
                return("(" + ResInsValsOnly.ToString() + ")");

            default:
                throw new NotImplementedException("Unknown ColumnValueFormatStyles " + style.ToString());
            }
        }
Exemplo n.º 6
0
        public void PopulateParameters(IConnection connection, IDataParameterCollection parameters, ColumnValueCollection columnValues, int parameterNameSuffix)
        {
            foreach (IColumnValue Fld in columnValues)
            {
                if (Fld.ValueCanBeParameter)
                {
                    var Param = connection.Factory.Driver.GetParameter();
                    Param.ParameterName = "@" + this.GetParameterName(Fld.ColumnIdentifier, parameterNameSuffix);
                    if (Fld.Value is DbDateTime && Fld.Value != null)
                    {
                        Param.Value = ((DbDateTime)(Fld.Value)).Value;
                    }
                    else if (Fld.Value != null && Fld.Value.GetType().IsEnum)
                    {
                        Param.Value = System.Convert.ToInt32(Fld.Value);
                    }
                    else
                    {
                        Param.Value = Fld.Value;
                    }
                    if (Fld.DataType == Lazaro.Orm.ColumnTypes.Blob)
                    {
                        Param.DbType = System.Data.DbType.Binary;
                    }

                    // FIXME: no debería hacer una excepción para OdbcDriver
                    if (connection.Factory.Driver is OdbcDriver && Fld.DataType == Lazaro.Orm.ColumnTypes.Blob)
                    {
                        ((System.Data.Odbc.OdbcParameter)Param).OdbcType = System.Data.Odbc.OdbcType.VarBinary;
                    }

                    parameters.Add(Param);
                }
            }
        }
Exemplo n.º 7
0
 /// <summary>
 /// Gets the column name and value pairs in this query.
 /// </summary>
 /// <returns>The column name and value pairs in this query.</returns>
 public KeyValuePair <string, string>[] GetColumnValueCollectionValues()
 {
     return(ColumnValueCollection.GetValues());
 }