2014-01-22 28 views
-1

该类不能继承最终类SQLiteStatement无法扩展。扩展SQLiteStatement,是否有可用的测试封装类?

与java不支持扩展方法的事实一起带给我这里。 似乎没有任何流畅的方式来为该类做扩展。

我没有时间编写和测试包装类SQLiteStatement的类。因此,我问是否有一个值得信赖的包装类呢?

我现在所做的是一个包含方法的类,该方法将发送给SQLiteStatement的引用以及我想添加到SQLiteStatement的值,但这不像扩展类那样流畅。

+0

Android的SQLite的API有许多设计问题,它不是非常可扩展。但是,它是开源的,请考虑从AOSP分叉是否是您的选择。 – laalto

+0

是的,我想过,但我想我需要密切关注它的新版本并保持它。 – Johan

回答

1

缺乏更好的替代方案,或者如果没有其他人回答我的问题,这将是我的解决方案。

这是一类与方法开始这一步,以避免重复空检查报表等

public class SQLiteStatementExtension { 

    public static void BindNullable(SQLiteStatement statement, int index, String value) 
    { 
     if(value == null){ 
      statement.bindNull(index); 
     } else { 
      statement.bindString(index, value); 
     } 
    } 

    public static void BindNullable(SQLiteStatement statement, int index, Calendar value) 
    { 
     if(value == null){ 
      statement.bindNull(index); 
     } else { 
      statement.bindLong(index, value.getTimeInMillis()); 
     } 
    } 

    public static void BindNullable(SQLiteStatement statement, int index, byte[] value) 
    { 
     if(value == null){ 
      statement.bindNull(index); 
     } else { 
      statement.bindBlob(index, value); 
     } 
    } 

    public static void Bind(SQLiteStatement satement, int index, boolean value) { 
     satement.bindLong(index, value ? 1 : 0); 
    } 
} 
相关问题