Exemplo n.º 1
0
        public override int ExecuteSql(string sql, QueryParameterCollection parameters = null)
        {
            lock (this) // although sqlite3 is thread-safe, we need to make sure that the last_insert_rowid is correct
            {
                var stmt = CreateCommand(sql, parameters);

                try
                {
                    SqliteReturnCode returnCode = _sqlite3.step(stmt);

                    if (returnCode == SqliteReturnCode.Row)
                    {
                        return(0); // quietly eat any rows being returned
                    }
                    if (returnCode != SqliteReturnCode.Done)
                    {
                        throw new SqliteException(returnCode, _sqlite3.extended_errcode(DbHandle), _sqlite3.errmsg(DbHandle));
                    }

                    _lastRowId.Value = _sqlite3.last_insert_rowid(DbHandle);

                    return(_sqlite3.changes(DbHandle));
                }
                finally
                {
                    _sqlite3.finalize(stmt);
                }
            }
        }
Exemplo n.º 2
0
 public SqliteException(SqliteReturnCode returnCode, SqliteExtendedErrorCode extendedErrorCode, string message) : base(message)
 {
     ReturnCode        = returnCode;
     ExtendedErrorCode = extendedErrorCode;
 }
Exemplo n.º 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);
        }