Guest User

Untitled

a guest
Mar 11th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. importClass(Packages.java.sql.DriverManager);
  2. importClass(Packages.java.sql.ResultSet);
  3.  
  4. // Let the classloader see it.
  5. new Packages.com.mysql.jdbc.Driver();
  6.  
  7. var DB_BASE='kingdom';
  8. var DB_USER='root';
  9. var DB_PASS='';
  10. var DB_HOST='localhost';
  11.  
  12. function dbConnection(mode){
  13. mode=mode||'development';
  14. var conn=DriverManager.
  15. getConnection('jdbc:mysql://'+DB_HOST+'/'+DB_BASE+'_'+mode,
  16. DB_USER,
  17. DB_PASS);
  18. return conn;
  19. }
  20.  
  21. var CONN=dbConnection('development');
  22.  
  23. function dbQuery(str,updatable){
  24. if(updatable){
  25. return CONN.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
  26. ResultSet.CONCUR_UPDATABLE).executeQuery(str);
  27. }else{
  28. return CONN.createStatement().executeQuery(str);
  29. }
  30. }
  31.  
  32. function dbMap(str,fn){
  33. var rs=dbQuery(str,true);
  34. var a=[];
  35. var thunk=dbBuildThunk(rs);
  36.  
  37. while(rs.next()){
  38. a.push(fn(thunk));
  39. if(updatedRow){
  40. updatedRow=false;
  41. rs.updateRow();
  42. }
  43. }
  44.  
  45. return a;
  46. }
  47.  
  48. function dbBuildThunk(rs){
  49. var meta=rs.getMetaData();
  50. var cols={};
  51.  
  52. for(var k=1;k<=meta.getColumnCount();k++){
  53. cols[meta.getColumnName(k)]=k;
  54. }
  55.  
  56. var updatedRow=false;
  57. return function(col,newVal){
  58. if(typeof col === 'string'){
  59. col=cols[col];
  60. }
  61.  
  62. if(newVal===undefined){
  63. return rs.getObject(col);
  64. }else{
  65. updatedRow=true;
  66. rs.updateObject(col,newVal);
  67. }
  68. };
  69. }
  70.  
  71. function dbInsert(table,populate){
  72. var rs=dbQuery("select * from "+table+" where false",true);
  73. rs.moveToInsertRow();
  74. if(typeof populate === 'function'){
  75. populate(dbBuildThunk(rs));
  76. }
  77. rs.insertRow();
  78. }
Add Comment
Please, Sign In to add comment