public override object ExecuteScalar()
        {
            if (OpenReader != null)
            {
                throw new InvalidOperationException(Strings.OpenReaderExists);
            }
            if (_connection == null ||
                _connection.State != ConnectionState.Open)
            {
                throw new InvalidOperationException(Strings.FormatCallRequiresOpenConnection("ExecuteScalar"));
            }
            if (string.IsNullOrWhiteSpace(_commandText))
            {
                throw new InvalidOperationException(Strings.FormatCallRequiresSetCommandText("ExecuteScalar"));
            }

            ValidateTransaction();
            Prepare();
            Bind();

            object result    = null;
            var    gotResult = false;

            foreach (var handle in _handles)
            {
                try
                {
                    var rc = NativeMethods.sqlite3_step(handle);
                    if (rc != Constants.SQLITE_DONE &&
                        rc != Constants.SQLITE_ROW)
                    {
                        MarshalEx.ThrowExceptionForRC(rc);
                    }

                    var hasResults = NativeMethods.sqlite3_stmt_readonly(handle) != 0;

                    if (!gotResult && hasResults)
                    {
                        if (rc == Constants.SQLITE_ROW)
                        {
                            var declaredType = NativeMethods.sqlite3_column_decltype(handle, 0);
                            var sqliteType   = (SQLiteType)NativeMethods.sqlite3_column_type(handle, 0);
                            var map          = SQLiteTypeMap.FromDeclaredType(declaredType, sqliteType);
                            var value        = ColumnReader.Read(map.SQLiteType, handle, 0);

                            result = map.FromInterop(value);
                        }

                        gotResult = true;
                    }
                }
                finally
                {
                    var rc = NativeMethods.sqlite3_reset(handle);
                    MarshalEx.ThrowExceptionForRC(rc);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        public override RelationalTypeMapping GetTypeMapping(
            string specifiedType, string storageName, Type propertyType, bool isKey, bool isConcurrencyToken)
        {
            var map = SQLiteTypeMap.FromClrType(propertyType);

            if (specifiedType != null)
            {
                map = SQLiteTypeMap.FromDeclaredType(specifiedType, map.SQLiteType);
            }

            // TODO: Leverage base implementation more
            return(new RelationalTypeMapping(map.DeclaredTypes.First(), map.DbType));
        }
        public override IValueGeneratorFactory Select(IProperty property)
        {
            if (property.ValueGenerationOnAdd == ValueGenerationOnAdd.Client)
            {
                // If INTEGER PRIMARY KEY column...
                var keyProperties = property.EntityType.GetKey().Properties;
                if (keyProperties.Count == 1 &&
                    keyProperties[0] == property)
                {
                    // TODO: Consider using RelationalTypeMapper service
                    var integerClrType = SQLiteTypeMap.FromDeclaredType("INTEGER", SQLiteType.Integer).ClrType;
                    if (property.PropertyType == integerClrType ||
                        string.Equals(property.ColumnType(), "INTEGER", StringComparison.OrdinalIgnoreCase))
                    {
                        // NOTE: This assumes no WITHOUT ROWID tables
                        return(_tempFactory);
                    }
                }
            }

            return(base.Select(property));
        }
Exemplo n.º 4
0
 private SQLiteTypeMap GetTypeMap(int ordinal)
 {
     return(SQLiteTypeMap.FromDeclaredType(GetDataTypeName(ordinal), GetSQLiteType(ordinal)));
 }