Example #1
0
        private static string GetInsertStatement(string tableName, DataRow row)
        {
            string columns = "";
            string values  = "";

            foreach (DataColumn column in row.Table.Columns)
            {
                if (row[column] != DBNull.Value)
                {
                    if (!string.IsNullOrWhiteSpace(columns))
                    {
                        columns += ", ";
                        values  += ", ";
                    }

                    columns += string.Format("[{0}]", column.ColumnName);
                    dynamic value = row[column];
                    values += LiteralHelpers.ToSqlString(value);
                }
            }
            return(string.Format("INSERT INTO [{0}] ({1}) VALUES ({2})", tableName, columns, values));
        }
Example #2
0
            private string VisitConstantExpression(TypeUsage expressionType, object expressionValue)
            {
                StringBuilder result = new StringBuilder();

                PrimitiveTypeKind typeKind;

                // Model Types can be (at the time of this implementation):
                //      Binary, Boolean, Byte, DateTime, Decimal, Double, Guid, Int16, Int32, Int64,Single, String
                if (expressionType.TryGetPrimitiveTypeKind(out typeKind))
                {
                    switch (typeKind)
                    {
                    case PrimitiveTypeKind.Int32:
                    case PrimitiveTypeKind.Byte:
                        result.Append(expressionValue);
                        break;

                    case PrimitiveTypeKind.Binary:
                        result.Append(LiteralHelpers.ToSqlString((Byte[])expressionValue));
                        break;

                    case PrimitiveTypeKind.Boolean:
                        result.Append(LiteralHelpers.ToSqlString((bool)expressionValue));
                        break;


                    case PrimitiveTypeKind.DateTime:
                        result.Append(LiteralHelpers.SqlDateTime((System.DateTime)expressionValue));
                        break;

                    case PrimitiveTypeKind.Time:
                        result.Append(LiteralHelpers.SqlDayTime((System.DateTime)expressionValue));
                        break;

                    case PrimitiveTypeKind.DateTimeOffset:
                        throw new NotImplementedException("Jet does not implement DateTimeOffset");


                    case PrimitiveTypeKind.Decimal:
                        string strDecimal = ((Decimal)expressionValue).ToString(CultureInfo.InvariantCulture);
                        // if the decimal value has no decimal part, cast as decimal to preserve type
                        // if the number has precision > int64 max precision, it will be handled as decimal by sql server
                        // and does not need cast. if precision is lest then 20, then cast using Max(literal precision, sql default precision)
                        if (-1 == strDecimal.IndexOf('.') && (strDecimal.TrimStart(new char[] { '-' }).Length < 20))
                        {
                            byte             precision = (Byte)strDecimal.Length;
                            FacetDescription precisionFacetDescription;

                            if (!expressionType.EdmType.TryGetTypeFacetDescriptionByName("precision", out precisionFacetDescription))
                            {
                                throw new InvalidOperationException("Decimal primitive type must have Precision facet");
                            }

                            if (precisionFacetDescription.DefaultValue != null)
                            {
                                precision = Math.Max(precision, (byte)precisionFacetDescription.DefaultValue);
                            }

                            if (precision <= 0)
                            {
                                throw new InvalidOperationException("Precision must be greater than zero");
                            }
                            result.Append("cast(");
                            result.Append(strDecimal);
                            result.Append(" as decimal(");
                            result.Append(precision.ToString(CultureInfo.InvariantCulture));
                            result.Append("))");
                        }
                        else
                        {
                            result.Append(strDecimal);
                        }
                        break;

                    case PrimitiveTypeKind.Double:
                        result.Append(((Double)expressionValue).ToString(CultureInfo.InvariantCulture));
                        break;

                    case PrimitiveTypeKind.Single:
                        result.Append(((Single)expressionValue).ToString(CultureInfo.InvariantCulture));
                        break;

                    case PrimitiveTypeKind.Int16:
                    case PrimitiveTypeKind.Int64:
                        result.Append(expressionValue);
                        break;

                    case PrimitiveTypeKind.String:
                        result.Append(LiteralHelpers.ToSqlString(expressionValue as string));
                        break;

                    case PrimitiveTypeKind.Guid:
                        result.Append(LiteralHelpers.ToSqlString((Guid)expressionValue));
                        break;

                    default:
                        // all known scalar types should been handled already.
                        throw new NotSupportedException("Primitive type kind " + typeKind + " is not supported by the Jet Provider");
                    }
                }
                else
                {
                    throw new NotSupportedException();
                }

                return(result.ToString());
            }