Пример #1
0
 public string ToSql(SqlDialect sqlDialect)
 {
     return(sqlDialect.JoinSql(this));
 }
Пример #2
0
        public override IEnumerable <Dictionary <string, object> > ExecuteSqlReader(string sql, QueryParameterCollection parameters)
        {
            var stopwatch = SqlLogger != null?Stopwatch.StartNew() : null;

            var stmt = CreateCommand(sql, parameters);

            try
            {
                for (;;)
                {
                    var returnCode = SQLitePCL.raw.sqlite3_step(stmt);

                    if (returnCode == SQLitePCL.raw.SQLITE_BUSY)
                    {
                        Task.Delay(100).Wait();
                        continue;
                    }

                    if (returnCode == SQLitePCL.raw.SQLITE_DONE)
                    {
                        break;
                    }

                    if (returnCode != SQLitePCL.raw.SQLITE_ROW)
                    {
                        throw new SqliteException((SqliteReturnCode)returnCode, (SqliteExtendedErrorCode)SQLitePCL.raw.sqlite3_extended_errcode(DbHandle), SQLitePCL.raw.sqlite3_errmsg(DbHandle).utf8_to_string());
                    }

                    Dictionary <string, object> record = new Dictionary <string, object>();

                    for (int i = 0; i < SQLitePCL.raw.sqlite3_column_count(stmt); i++)
                    {
                        string fieldName = SQLitePCL.raw.sqlite3_column_name(stmt, i).utf8_to_string();

                        var columnType = SQLitePCL.raw.sqlite3_column_type(stmt, i);

                        switch (columnType)
                        {
                        case SQLitePCL.raw.SQLITE_BLOB:
                            record[fieldName] = SQLitePCL.raw.sqlite3_column_blob(stmt, i).ToArray();
                            break;

                        case SQLitePCL.raw.SQLITE_TEXT:
                            record[fieldName] = SQLitePCL.raw.sqlite3_column_text(stmt, i).utf8_to_string();
                            break;

                        case SQLitePCL.raw.SQLITE_FLOAT:
                            record[fieldName] = SQLitePCL.raw.sqlite3_column_double(stmt, i);
                            break;

                        case SQLitePCL.raw.SQLITE_INTEGER:
                            record[fieldName] = SQLitePCL.raw.sqlite3_column_int64(stmt, i);
                            break;

                        case SQLitePCL.raw.SQLITE_NULL:
                            record[fieldName] = null;
                            break;
                        }
                    }

                    yield return(record);
                }
            }
            finally
            {
                SQLitePCL.raw.sqlite3_finalize(stmt);
            }

            SqlLogger?.LogSql(sql, parameters?.ToDictionary(p => SqlDialect.CreateParameterExpression(p.Name), p => p.Value), stopwatch?.Elapsed ?? TimeSpan.Zero);
        }
Пример #3
0
        private IntPtr CreateCommand(string sql, QueryParameterCollection parameters)
        {
            IntPtr stmt;

            SqliteReturnCode returnCode = _sqlite3.prepare_v2(DbHandle, sql, out stmt);

            if (returnCode != SqliteReturnCode.Ok)
            {
                throw new SqliteException(returnCode, _sqlite3.extended_errcode(DbHandle), _sqlite3.errmsg(DbHandle));
            }

            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    int paramNumber = _sqlite3.bind_parameter_index(stmt, SqlDialect.CreateParameterExpression(parameter.Name));

                    var value = parameter.Value;

                    if (value == null)
                    {
                        _sqlite3.bind_null(stmt, paramNumber);
                    }
                    else
                    {
                        var parameterType = parameter.Type.Inspector();

                        if (parameterType.Is(TypeFlags.Boolean))
                        {
                            _sqlite3.bind_int(stmt, paramNumber, value.Convert <bool>() ? 1 : 0);
                        }
                        else if (parameterType.Is(TypeFlags.Array | TypeFlags.Byte))
                        {
                            _sqlite3.bind_blob(stmt, paramNumber, (byte[])value);
                        }
                        else if (parameterType.Is(TypeFlags.Integer64))
                        {
                            _sqlite3.bind_int64(stmt, paramNumber, value.Convert <long>());
                        }
                        else if (parameterType.Is(TypeFlags.Integer))
                        {
                            _sqlite3.bind_int(stmt, paramNumber, value.Convert <int>());
                        }
                        else if (parameterType.Is(TypeFlags.FloatingPoint))
                        {
                            _sqlite3.bind_double(stmt, paramNumber, value.Convert <double>());
                        }
                        else if (parameterType.Is(TypeFlags.String))
                        {
                            _sqlite3.bind_text(stmt, paramNumber, value.Convert <string>());
                        }
                        else if (parameterType.Is(TypeFlags.DateTime))
                        {
                            switch (DateFormat)
                            {
                            case SqliteDateFormat.String:
                                _sqlite3.bind_text(stmt, paramNumber, ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss.fff"));
                                break;

//                                case SqliteDateFormat.Julian:
//                                    _sqlite3.bind_int64(stmt, paramNumber, ((DateTime)value).Ticks);
//                                    break;
                            case SqliteDateFormat.Unix:
                                _sqlite3.bind_int(stmt, paramNumber, (int)(((DateTime)value) - new DateTime(1970, 1, 1)).TotalSeconds);
                                break;

                            case SqliteDateFormat.Ticks:
                                _sqlite3.bind_int64(stmt, paramNumber, ((DateTime)value).Ticks);
                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        }
                        else
                        {
                            _sqlite3.bind_text(stmt, paramNumber, value.Convert <string>());
                        }
                    }
                }
            }

            return(stmt);
        }
Пример #4
0
        private SQLitePCL.sqlite3_stmt CreateCommand(string sql, QueryParameterCollection parameters)
        {
            var returnCode = SQLitePCL.raw.sqlite3_prepare_v2(DbHandle, sql, out SQLitePCL.sqlite3_stmt stmt);

            if (returnCode != SQLitePCL.raw.SQLITE_OK)
            {
                throw new SqliteException((SqliteReturnCode)returnCode, (SqliteExtendedErrorCode)SQLitePCL.raw.sqlite3_extended_errcode(DbHandle), SQLitePCL.raw.sqlite3_errmsg(DbHandle).utf8_to_string());
            }

            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    int paramNumber = SQLitePCL.raw.sqlite3_bind_parameter_index(stmt, SqlDialect.CreateParameterExpression(parameter.Name));

                    var value = parameter.Value;

                    if (value == null)
                    {
                        SQLitePCL.raw.sqlite3_bind_null(stmt, paramNumber);
                    }
                    else
                    {
                        var parameterType = parameter.Type.Inspector();

                        if (parameterType.Is(TypeFlags.Boolean))
                        {
                            SQLitePCL.raw.sqlite3_bind_int(stmt, paramNumber, value.Convert <bool>() ? 1 : 0);
                        }
                        else if (parameterType.Is(TypeFlags.Array | TypeFlags.Byte))
                        {
                            SQLitePCL.raw.sqlite3_bind_blob(stmt, paramNumber, (byte[])value);
                        }
                        else if (parameterType.Is(TypeFlags.Integer64))
                        {
                            SQLitePCL.raw.sqlite3_bind_int64(stmt, paramNumber, value.Convert <long>());
                        }
                        else if (parameterType.Is(TypeFlags.Integer))
                        {
                            SQLitePCL.raw.sqlite3_bind_int(stmt, paramNumber, value.Convert <int>());
                        }
                        else if (parameterType.Is(TypeFlags.FloatingPoint))
                        {
                            SQLitePCL.raw.sqlite3_bind_double(stmt, paramNumber, value.Convert <double>());
                        }
                        else if (parameterType.Is(TypeFlags.String))
                        {
                            SQLitePCL.raw.sqlite3_bind_text(stmt, paramNumber, value.Convert <string>());
                        }
                        else if (parameterType.Is(TypeFlags.Guid))
                        {
                            SQLitePCL.raw.sqlite3_bind_text(stmt, paramNumber, ((Guid)value).ToString("N"));
                        }
                        else if (parameterType.Is(TypeFlags.DateTime))
                        {
                            switch (DateFormat)
                            {
                            case SqliteDateFormat.String:
                                SQLitePCL.raw.sqlite3_bind_text(stmt, paramNumber, ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss.fff"));
                                break;

                            case SqliteDateFormat.Unix:
                                SQLitePCL.raw.sqlite3_bind_int(stmt, paramNumber, (int)(((DateTime)value) - new DateTime(1970, 1, 1)).TotalSeconds);
                                break;

                            case SqliteDateFormat.Ticks:
                                SQLitePCL.raw.sqlite3_bind_int64(stmt, paramNumber, ((DateTime)value).Ticks);
                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        }
                        else
                        {
                            SQLitePCL.raw.sqlite3_bind_text(stmt, paramNumber, value.Convert <string>());
                        }
                    }
                }
            }

            return(stmt);
        }