test-sqlite3.nut 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. local db = SQLite3(":memory:");
  2. print(db);
  3. class MultiplyDecimal3 {
  4. first = null;
  5. second = null;
  6. thirdy = null;
  7. constructor(){
  8. first = Decimal(0);
  9. second = Decimal(0);
  10. thirdy = Decimal(0);
  11. }
  12. function reset(){
  13. first.set(0);
  14. second.set(0);
  15. thirdy.set(0);
  16. }
  17. function doIt(){
  18. return (first * second * thirdy).tostring();
  19. }
  20. }
  21. local function multiply3_MultiplyDecimal3(ctx,a,b,c){
  22. //print(ctx.user_data());
  23. //local m3 = Multiply3();
  24. local m3 = ctx.user_data();
  25. m3.reset();
  26. m3.first.set(a);
  27. m3.second.set(b);
  28. m3.thirdy.set(c);
  29. ctx.result_text(m3.doIt());
  30. //ctx.result_double(a*b*c);
  31. }
  32. class Multiply3 {
  33. first = null;
  34. second = null;
  35. thirdy = null;
  36. constructor(){
  37. reset();
  38. }
  39. function reset(){
  40. first = second = thirdy = 0.0;
  41. }
  42. function doIt(){
  43. return first * second * thirdy;
  44. }
  45. }
  46. local function multiply3_Multiply3(ctx,a,b,c){
  47. //print(ctx.user_data());
  48. //local m3 = Multiply3();
  49. local m3 = ctx.user_data();
  50. m3.reset();
  51. m3.first = a;
  52. m3.second = b;
  53. m3.thirdy = c;
  54. ctx.result_double(m3.doIt());
  55. //ctx.result_double(a*b*c);
  56. }
  57. local function multiply3(ctx,a,b,c){
  58. //print(ctx.user_data());
  59. ctx.result_double(a*b*c);
  60. }
  61. local function sq_multiply3(a,b,c){
  62. //print(ctx.user_data());
  63. return (a*b*c);
  64. }
  65. local function showall(ctx,...){
  66. for(local i = 0; i< vargv.len(); i++)
  67. {
  68. ::print("varparam "+i+" = "+vargv[i]+"\n");
  69. }
  70. //print(ctx.user_data());
  71. ctx.result_null();
  72. }
  73. db.create_function("showall",-1, showall);
  74. db.exec_get_one("select showall(1,'two', 3, 'four')");
  75. db.exec_dml("create table tr(id integer primary key, name text);");
  76. db.exec_dml("create trigger tr_trigger before insert on tr begin select showall(new.id, new.name); end;");
  77. db.exec_dml("insert into tr(id,name) values(1, 'dad'), (2, 'car');");
  78. //db.exec_get_one("select showall(*) from tr");
  79. //db.create_function("multiply3",3, multiply3_MultiplyDecimal3, MultiplyDecimal3());
  80. //db.create_function("multiply3",3, multiply3_Multiply3, Multiply3());
  81. db.create_function("multiply3",3, multiply3);
  82. local sql = "select 1.2*2.5*3.6;";
  83. local sql_squilu = "select multiply3(1.2,2.5,3.6);";
  84. local stmt = db.prepare(sql);
  85. local stmt_squilu = db.prepare(sql_squilu);
  86. print(db.exec_get_one(sql_squilu));
  87. print(db.exec_get_one(sql));
  88. local count = 100000;
  89. local now = os.clock();
  90. print("top", getstacktop(), dostring("return 1.2*2.5*3.6;", true));
  91. for(local i=0; i<count; ++i){
  92. local val = dostring("return 1.2*2.5*3.6;", true);
  93. }
  94. print("top", getstacktop());
  95. print("SquiLu function eval took:", os.clock() -now);
  96. now = os.clock();
  97. for(local i=0; i<count; ++i){
  98. local val = db.exec_get_one(sql_squilu);
  99. }
  100. print("SquiLu sqlite function took:", os.clock() -now);
  101. now = os.clock();
  102. for(local i=0; i<count; ++i){
  103. local val = db.exec_get_one(sql);
  104. }
  105. print("SQL function took:", os.clock() -now);
  106. stmt_squilu.reset();
  107. stmt_squilu.step()
  108. print(stmt_squilu.col(0));
  109. now = os.clock();
  110. for(local i=0; i<count; ++i){
  111. local val = sq_multiply3(1.2,2.5,3.6);
  112. }
  113. print("SquiLu function took:", os.clock() -now);
  114. now = os.clock();
  115. for(local i=0; i<count; ++i){
  116. local val = (1.2*2.5*3.6);
  117. }
  118. print("SquiLu direct calc took:", os.clock() -now);
  119. now = os.clock();
  120. for(local i=0; i<count; ++i){
  121. stmt_squilu.reset();
  122. stmt_squilu.step()
  123. local val = stmt_squilu.col(0);
  124. }
  125. print("SquiLu SQL prepared function took:", os.clock() -now);
  126. now = os.clock();
  127. for(local i=0; i<count; ++i){
  128. stmt.reset();
  129. stmt.step()
  130. local val = stmt.col(0);
  131. }
  132. print("SQL prepared function took:", os.clock() -now);
  133. stmt.finalize();
  134. stmt_squilu.finalize();
  135. db.exec_dml("create table if not exists test_slice(value text)");
  136. db.exec_dml("insert into test_slice values('value text')");
  137. stmt = db.prepare("select * from test_slice");
  138. if(stmt.next_row()) print("col_slice", stmt.col_slice(0, 2, 5));
  139. stmt.finalize();
  140. db.close();