PageRenderTime 219ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/sorma/sorma-test/src/main/java/com/gaoshin/sorma/test/ContentResolverTestMock.java

https://github.com/zhangyongjiang/SORMA
Java | 374 lines | 336 code | 38 blank | 0 comment | 89 complexity | 749e988ab92f2292eac5e3b853bd2a33 MD5 | raw file
  1. package com.gaoshin.sorma.test;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.InputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ParameterMetaData;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.sql.Types;
  12. import android.content.ContentValues;
  13. import android.database.Cursor;
  14. import com.gaoshin.sorma.SormaContentResolver;
  15. import com.gaoshin.sorma.SqlExecutor;
  16. import com.gaoshin.sorma.annotation.AnnotatedSchema;
  17. public class ContentResolverTestMock implements SormaContentResolver {
  18. private String dbname;
  19. private AnnotatedSchema ormDefinition;
  20. public ContentResolverTestMock(String dbname, AnnotatedSchema ormDefinition) throws Exception {
  21. this.dbname = dbname;
  22. this.ormDefinition = ormDefinition;
  23. Class.forName("org.sqlite.JDBC");
  24. createDb();
  25. }
  26. private void createDb() throws Exception {
  27. Connection connection = getConnection();
  28. final Statement statement = connection.createStatement();
  29. ormDefinition.createDatabase(new SqlExecutor() {
  30. public void execute(String sql) throws Exception {
  31. statement.execute(sql);
  32. }
  33. });
  34. statement.close();
  35. connection.close();
  36. }
  37. public Cursor query(String uri, String[] projection, String selection,
  38. String[] selectionArgs, String sortOrder) {
  39. String table = getTableNameFromUri(uri);
  40. StringBuilder sb = new StringBuilder();
  41. if(table.equals("nativequery")) {
  42. sb.append(selection);
  43. }
  44. else {
  45. sb.append("select ");
  46. if(projection == null || projection.length ==0) {
  47. sb.append(" * ");
  48. }
  49. else {
  50. boolean first = true;
  51. for(String col : projection) {
  52. if(first) {
  53. first = false;
  54. } else {
  55. sb.append(",");
  56. }
  57. sb.append(" ").append(col);
  58. }
  59. }
  60. sb.append(" from ").append(table);
  61. if(selection != null && selection.trim().length() > 0) {
  62. sb.append(" where ").append(selection);
  63. }
  64. if(sortOrder != null && sortOrder.trim().length() > 0) {
  65. sb.append(" order by ").append(sortOrder);
  66. }
  67. }
  68. try {
  69. PreparedStatement statement = getConnection().prepareStatement(sb.toString());
  70. if(selectionArgs != null && selectionArgs.length > 0) {
  71. ParameterMetaData metaData = statement.getParameterMetaData();
  72. for(int i = 0; i<selectionArgs.length; i++) {
  73. int columnType = metaData.getParameterType(i+1);
  74. if(selectionArgs[i] == null) {
  75. statement.setNull(i+1, columnType);
  76. continue;
  77. }
  78. switch(columnType) {
  79. case Types.CHAR:
  80. case Types.VARCHAR:
  81. case Types.NVARCHAR:
  82. case Types.LONGVARCHAR:
  83. case Types.LONGNVARCHAR:
  84. statement.setString(i+1, selectionArgs[i]);
  85. break;
  86. case Types.SMALLINT:
  87. statement.setShort(i+1, Short.parseShort(selectionArgs[i]));
  88. break;
  89. case Types.INTEGER:
  90. statement.setInt(i+1, Integer.parseInt(selectionArgs[i]));
  91. break;
  92. case Types.BIGINT:
  93. statement.setLong(i+1, Integer.parseInt(selectionArgs[i]));
  94. break;
  95. case Types.FLOAT:
  96. statement.setFloat(i+1, Float.parseFloat(selectionArgs[i]));
  97. break;
  98. case Types.DOUBLE:
  99. statement.setDouble(i+1, Double.parseDouble(selectionArgs[i]));
  100. break;
  101. }
  102. }
  103. }
  104. ResultSet resultSet = statement.executeQuery();
  105. return new ResultSetCursor(resultSet);
  106. } catch (Exception e) {
  107. throw new RuntimeException(e);
  108. }
  109. }
  110. @Override
  111. public long insert(String uri, ContentValues values) {
  112. String table = getTableNameFromUri(uri);
  113. StringBuilder sb = new StringBuilder();
  114. sb.append("insert into ").append(table);
  115. sb.append("(");
  116. boolean first = true;
  117. StringBuilder quesmarks = new StringBuilder();
  118. String[] keySet = values.getMap().keySet().toArray(new String[0]);
  119. for(String col : keySet) {
  120. if(values.get(col)==null)
  121. continue;
  122. if(first) {
  123. first = false;
  124. } else {
  125. sb.append(",");
  126. quesmarks.append(",");
  127. }
  128. sb.append(col);
  129. quesmarks.append("?");
  130. }
  131. sb.append(")");
  132. sb.append(" values (");
  133. sb.append(quesmarks.toString());
  134. sb.append(" )");
  135. try {
  136. Connection connection = getConnection();
  137. PreparedStatement statement = connection.prepareStatement(sb.toString());
  138. for(int i = 0; i<keySet.length; i++) {
  139. Object val = values.get(keySet[i]);
  140. if(val == null) {
  141. continue;
  142. }
  143. if(val instanceof InputStream)
  144. statement.setBlob(i+1, (InputStream)val);
  145. else if(val instanceof byte[])
  146. statement.setBlob(i+1, new ByteArrayInputStream((byte[])val));
  147. else if(val instanceof String)
  148. statement.setString(i+1, (String)val);
  149. else if(val instanceof Integer)
  150. statement.setInt(i+1, (Integer)val);
  151. else if(val instanceof Long)
  152. statement.setLong(i+1, (Long)val);
  153. else if(val instanceof Short)
  154. statement.setShort(i+1, (Short)val);
  155. else if(val instanceof Float)
  156. statement.setFloat(i+1, (Float)val);
  157. else if(val instanceof Double)
  158. statement.setDouble(i+1, (Double)val);
  159. else
  160. throw new RuntimeException("unsupport type " + val.getClass());
  161. }
  162. int resultSet = statement.executeUpdate();
  163. ResultSet keys = statement.getGeneratedKeys();
  164. if(keys.next()) {
  165. return keys.getLong(1);
  166. } else {
  167. return resultSet;
  168. }
  169. } catch (Exception e) {
  170. throw new RuntimeException(e);
  171. }
  172. }
  173. @Override
  174. public int delete(String uri, String selection, String[] selectionArgs) {
  175. String table = getTableNameFromUri(uri);
  176. StringBuilder sb = new StringBuilder();
  177. sb.append("delete ");
  178. sb.append(" from ").append(table);
  179. if(selection != null && selection.trim().length() > 0) {
  180. sb.append(" where ").append(selection);
  181. }
  182. try {
  183. PreparedStatement statement = getConnection().prepareStatement(sb.toString());
  184. if(selectionArgs != null && selectionArgs.length > 0) {
  185. ParameterMetaData metaData = statement.getParameterMetaData();
  186. for(int i = 0; i<selectionArgs.length; i++) {
  187. int columnType = metaData.getParameterType(i+1);
  188. if(selectionArgs[i] == null) {
  189. statement.setNull(i+1, columnType);
  190. continue;
  191. }
  192. switch(columnType) {
  193. case Types.CHAR:
  194. case Types.VARCHAR:
  195. case Types.NVARCHAR:
  196. case Types.LONGVARCHAR:
  197. case Types.LONGNVARCHAR:
  198. statement.setString(i+1, selectionArgs[i]);
  199. break;
  200. case Types.SMALLINT:
  201. statement.setShort(i+1, Short.parseShort(selectionArgs[i]));
  202. break;
  203. case Types.INTEGER:
  204. statement.setInt(i+1, Integer.parseInt(selectionArgs[i]));
  205. break;
  206. case Types.BIGINT:
  207. statement.setLong(i+1, Integer.parseInt(selectionArgs[i]));
  208. break;
  209. case Types.FLOAT:
  210. statement.setFloat(i+1, Float.parseFloat(selectionArgs[i]));
  211. break;
  212. case Types.DOUBLE:
  213. statement.setDouble(i+1, Double.parseDouble(selectionArgs[i]));
  214. break;
  215. }
  216. }
  217. }
  218. return statement.executeUpdate();
  219. } catch (Exception e) {
  220. throw new RuntimeException(e);
  221. }
  222. }
  223. @Override
  224. public int update(String uri, ContentValues values, String selection,
  225. String[] selectionArgs) {
  226. String table = getTableNameFromUri(uri);
  227. StringBuilder sb = new StringBuilder();
  228. sb.append("update ");
  229. sb.append(table);
  230. sb.append(" set ");
  231. boolean first = true;
  232. String[] keySet = values.getMap().keySet().toArray(new String[0]);
  233. for(String col : keySet) {
  234. if(values.get(col)==null)
  235. continue;
  236. if(first) {
  237. first = false;
  238. } else {
  239. sb.append(",");
  240. }
  241. sb.append(col).append("=?");
  242. }
  243. if(selection != null && selection.trim().length() > 0) {
  244. sb.append(" where ").append(selection);
  245. }
  246. try {
  247. PreparedStatement statement = getConnection().prepareStatement(sb.toString());
  248. int i = 0;
  249. for(i = 0; i<keySet.length; i++) {
  250. Object val = values.get(keySet[i]);
  251. if(val == null) {
  252. continue;
  253. }
  254. if(val instanceof InputStream)
  255. statement.setBlob(i+1, (InputStream)val);
  256. else if(val instanceof byte[])
  257. statement.setBlob(i+1, new ByteArrayInputStream((byte[])val));
  258. else if(val instanceof String)
  259. statement.setString(i+1, (String)val);
  260. else if(val instanceof Integer)
  261. statement.setInt(i+1, (Integer)val);
  262. else if(val instanceof Long)
  263. statement.setLong(i+1, (Long)val);
  264. else if(val instanceof Short)
  265. statement.setShort(i+1, (Short)val);
  266. else if(val instanceof Float)
  267. statement.setFloat(i+1, (Float)val);
  268. else if(val instanceof Double)
  269. statement.setDouble(i+1, (Double)val);
  270. else
  271. throw new RuntimeException("unsupport type " + val.getClass());
  272. }
  273. if(selectionArgs != null && selectionArgs.length > 0) {
  274. ParameterMetaData metaData = statement.getParameterMetaData();
  275. for(int j = 0; j<selectionArgs.length; j++, i++) {
  276. int columnType = metaData.getParameterType(j+1);
  277. if(selectionArgs[j] == null) {
  278. statement.setNull(i+1, columnType);
  279. continue;
  280. }
  281. switch(columnType) {
  282. case Types.CHAR:
  283. case Types.VARCHAR:
  284. case Types.NVARCHAR:
  285. case Types.LONGVARCHAR:
  286. case Types.LONGNVARCHAR:
  287. statement.setString(i+1, selectionArgs[j]);
  288. break;
  289. case Types.SMALLINT:
  290. statement.setShort(i+1, Short.parseShort(selectionArgs[j]));
  291. break;
  292. case Types.INTEGER:
  293. statement.setInt(i+1, Integer.parseInt(selectionArgs[j]));
  294. break;
  295. case Types.BIGINT:
  296. statement.setLong(i+1, Integer.parseInt(selectionArgs[j]));
  297. break;
  298. case Types.FLOAT:
  299. statement.setFloat(i+1, Float.parseFloat(selectionArgs[j]));
  300. break;
  301. case Types.DOUBLE:
  302. statement.setDouble(i+1, Double.parseDouble(selectionArgs[j]));
  303. break;
  304. default:
  305. throw new RuntimeException("unsupported type " + columnType);
  306. }
  307. }
  308. }
  309. return statement.executeUpdate();
  310. } catch (Exception e) {
  311. throw new RuntimeException(e);
  312. }
  313. }
  314. private static String getTableNameFromUri(String uri) {
  315. String path[] = uri.split("/");
  316. if(path.length>3)
  317. return path[3];
  318. else
  319. return path[path.length-1];
  320. }
  321. private static String getIdFromUri(String uri) {
  322. String path[] = uri.split("/");
  323. return path[4];
  324. }
  325. public Connection getConnection() throws SQLException {
  326. Connection connection = DriverManager.getConnection("jdbc:sqlite:" + dbname);
  327. return connection;
  328. }
  329. @Override
  330. public void batchInsert(String uri,
  331. ContentValues[] operations) throws Exception {
  332. for(ContentValues cv : operations) {
  333. insert(uri, cv);
  334. }
  335. }
  336. }