Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Added th1 query_bind functions. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | th1-query-api |
Files: | files | file ages | folders |
SHA1: |
e30002440a9280e8fb560487affd6214 |
User & Date: | stephan 2012-07-14 02:13:09.107 |
Context
2012-07-14
| ||
09:14 | find_option() now accepts --long=VAL and --short=VAL forms, in addition to the conventional --long VAL and -short VAL. Long-form has had this feature a while (apparently) but it has not been documented AFAIK. ... (check-in: aa3ea63c user: stephan tags: th1-query-api) | |
02:13 | Added th1 query_bind functions. ... (check-in: e3000244 user: stephan tags: th1-query-api) | |
00:56 | Added query_col_int and query_col_double. Renamed query_column_xxx to query_col_xxx. ... (check-in: b01eb58b user: stephan tags: th1-query-api) | |
Changes
Changes to src/th_main.c.
︙ | ︙ | |||
470 471 472 473 474 475 476 477 478 479 480 481 482 483 | } rc = Th_AddStmt( interp, pStmt ); assert( rc >= 0 && "AddStmt failed."); Th_SetResultInt( interp, rc ); return TH_OK; } static sqlite3_stmt * queryStmtHandle(Th_Interp *interp, char const * arg, int argLen, int * stmtId ){ int rc = 0; sqlite3_stmt * pStmt = NULL; if( 0 == Th_ToInt( interp, arg, argLen, &rc ) ){ if(stmtId){ *stmtId = rc; } | > > > > > > > > > > | 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | } rc = Th_AddStmt( interp, pStmt ); assert( rc >= 0 && "AddStmt failed."); Th_SetResultInt( interp, rc ); return TH_OK; } /* ** Tries to convert arg, which must be argLen bytes long, to a ** statement handle id and, in turn, to a sqlite3_stmt. On success ** (the argument references a prepared statement) it returns the ** handle and stmtId (if not NULL) is assigned to the integer value of ** arg. On error NULL is returned and stmtId might be modified (if not ** NULL). If stmtId is unmodified after an error then it is not a ** number, else it is a number but does not reference an opened ** statement. */ static sqlite3_stmt * queryStmtHandle(Th_Interp *interp, char const * arg, int argLen, int * stmtId ){ int rc = 0; sqlite3_stmt * pStmt = NULL; if( 0 == Th_ToInt( interp, arg, argLen, &rc ) ){ if(stmtId){ *stmtId = rc; } |
︙ | ︙ | |||
511 512 513 514 515 516 517 | } assert( NULL != pStmt ); rc = Th_FinalizeStmt( interp, rc ); Th_SetResultInt( interp, rc ); return TH_OK; } | | > > > > > > > > > > > > > > | 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | } assert( NULL != pStmt ); rc = Th_FinalizeStmt( interp, rc ); Th_SetResultInt( interp, rc ); return TH_OK; } static int queryReportDbErr( Th_Interp * interp ){ char const * msg = sqlite3_errmsg( g.db ); Th_ErrorMessage(interp, "db error:", msg, -1); return TH_ERROR; } /* ** Internal helper for fetching statement handle and index parameters. ** The first 4 args should be the args passed to the TH1 callback. ** pStmt must be a pointer to a NULL pointer. pIndex may be NULL or ** a pointer to store the statement index argument in. If pIndex is ** NULL then argc is asserted to be at least 2, else it must be at ** least 3. ** ** On success it returns 0, sets *pStmt to the referenced statement ** handle, and pIndex (if not NULL) to the integer value of argv[2] ** argument. On error it reports the error via TH, returns non-0, and ** modifies neither pStmt not pIndex. */ static int queryStmtIndexArgs( Th_Interp * interp, int argc, char const ** argv, int *argl, sqlite3_stmt ** pStmt, int * pIndex ){ |
︙ | ︙ | |||
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | int rc = 0; if( argc!=2 ){ return Th_WrongNumArgs(interp, "query_step StmtHandle"); } if(0 != queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, NULL)){ return TH_ERROR; } rc = sqlite3_step( pStmt ); switch(rc){ case SQLITE_ROW: rc = 1; break; case SQLITE_DONE: rc = 0; break; default: | > | < | | > | | > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 | int rc = 0; if( argc!=2 ){ return Th_WrongNumArgs(interp, "query_step StmtHandle"); } if(0 != queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, NULL)){ return TH_ERROR; } assert(NULL != pStmt); rc = sqlite3_step( pStmt ); switch(rc){ case SQLITE_ROW: rc = 1; break; case SQLITE_DONE: rc = 0; break; default: return queryReportDbErr( interp ); } Th_SetResultInt( interp, rc ); return TH_OK; } static int queryColStringCmd( Th_Interp *interp, void *p, int argc, const char **argv, int *argl ){ sqlite3_stmt * pStmt = NULL; char const * val; int index = -1; int valLen; if( argc!=3 ){ return Th_WrongNumArgs(interp, "query_col_string StmtHandle Index"); } queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index); if(index < 0){ return TH_ERROR; } val = sqlite3_column_text( pStmt, index ); valLen = val ? sqlite3_column_bytes( pStmt, index ) : 0; Th_SetResult( interp, val, valLen ); return TH_OK; } static int queryColIntCmd( Th_Interp *interp, void *p, int argc, const char **argv, int *argl ){ sqlite3_stmt * pStmt = NULL; int rc = 0; int index = -1; if( argc!=3 ){ return Th_WrongNumArgs(interp, "query_col_int StmtHandle Index"); } queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index); if(index < 0){ return TH_ERROR; } Th_SetResultInt( interp, sqlite3_column_int( pStmt, index ) ); return TH_OK; } static int queryColDoubleCmd( Th_Interp *interp, void *p, int argc, const char **argv, int *argl ){ sqlite3_stmt * pStmt = NULL; double rc = 0; int index = -1; if( argc!=3 ){ return Th_WrongNumArgs(interp, "query_col_double StmtHandle Index"); } queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index); if(index < 0){ return TH_ERROR; } Th_SetResultDouble( interp, sqlite3_column_double( pStmt, index ) ); return TH_OK; } static int queryColIsNullCmd( Th_Interp *interp, void *p, int argc, const char **argv, int *argl ){ sqlite3_stmt * pStmt = NULL; double rc = 0; int index = -1; if( argc!=3 ){ return Th_WrongNumArgs(interp, "query_col_is_null StmtHandle Index"); } queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index); if(index < 0){ return TH_ERROR; } Th_SetResultInt( interp, SQLITE_NULL==sqlite3_column_type( pStmt, index ) ); return TH_OK; } static int queryColTypeCmd( Th_Interp *interp, void *p, int argc, const char **argv, int *argl ){ sqlite3_stmt * pStmt = NULL; double rc = 0; int index = -1; if( argc!=3 ){ return Th_WrongNumArgs(interp, "query_col_type StmtHandle Index"); } queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index); if(index < 0){ return TH_ERROR; } Th_SetResultInt( interp, sqlite3_column_type( pStmt, index ) ); return TH_OK; } static int queryColCountCmd( Th_Interp *interp, void *p, int argc, const char **argv, int *argl |
︙ | ︙ | |||
696 697 698 699 700 701 702 703 704 705 706 707 708 709 | Th_ErrorMessage(interp, "Column index out of bounds(?):", argv[2], -1); return TH_ERROR; }else{ Th_SetResult( interp, val, strlen( val ) ); return TH_OK; } } #endif /* end TH_USE_SQLITE */ /* ** Make sure the interpreter has been initialized. Initialize it if ** it has not been already. | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 | Th_ErrorMessage(interp, "Column index out of bounds(?):", argv[2], -1); return TH_ERROR; }else{ Th_SetResult( interp, val, strlen( val ) ); return TH_OK; } } static int queryBindNullCmd( Th_Interp *interp, void *p, int argc, const char **argv, int *argl ){ sqlite3_stmt * pStmt = NULL; int rc; int index = 0; if( argc!=3 ){ return Th_WrongNumArgs(interp, "query_bind_null StmtHandle Index"); } queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index); if(index < 1){ return TH_ERROR; } rc = sqlite3_bind_null( pStmt, index ); if(rc){ return queryReportDbErr( interp ); } Th_SetResultInt( interp, 0 ); return TH_OK; } static int queryBindStringCmd( Th_Interp *interp, void *p, int argc, const char **argv, int *argl ){ sqlite3_stmt * pStmt = NULL; int rc; int index = 0; if( argc!=4 ){ return Th_WrongNumArgs(interp, "query_bind_string StmtHandle Index val"); } queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index); if(index < 1){ return TH_ERROR; } rc = sqlite3_bind_text( pStmt, index, argv[3], argl[3], SQLITE_TRANSIENT ); if(rc){ return queryReportDbErr( interp ); } Th_SetResultInt( interp, 0 ); return TH_OK; } static int queryBindIntCmd( Th_Interp *interp, void *p, int argc, const char **argv, int *argl ){ sqlite3_stmt * pStmt = NULL; int rc; int val; int index = 0; if( argc!=4 ){ return Th_WrongNumArgs(interp, "query_bind_int StmtHandle Index val"); } queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index); if(index < 1){ return TH_ERROR; } if( 0 != Th_ToInt( interp, argv[3], argl[3], &val ) ){ return TH_ERROR; } rc = sqlite3_bind_int( pStmt, index, val ); if(rc){ return queryReportDbErr( interp ); } Th_SetResultInt( interp, 0 ); return TH_OK; } static int queryBindDoubleCmd( Th_Interp *interp, void *p, int argc, const char **argv, int *argl ){ sqlite3_stmt * pStmt = NULL; int rc; double val; int index = 0; if( argc!=4 ){ return Th_WrongNumArgs(interp, "query_bind_double StmtHandle Index val"); } queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index); if(index < 1){ return TH_ERROR; } if( 0 != Th_ToDouble( interp, argv[3], argl[3], &val ) ){ return TH_ERROR; } rc = sqlite3_bind_double( pStmt, index, val ); if(rc){ return queryReportDbErr( interp ); } Th_SetResultInt( interp, 0 ); return TH_OK; } #endif /* end TH_USE_SQLITE */ /* ** Make sure the interpreter has been initialized. Initialize it if ** it has not been already. |
︙ | ︙ | |||
726 727 728 729 730 731 732 733 734 735 736 | {"hascap", hascapCmd, 0}, {"hasfeature", hasfeatureCmd, 0}, {"htmlize", htmlizeCmd, 0}, {"date", dateCmd, 0}, {"html", putsCmd, &puts_Html}, {"puts", putsCmd, &puts_Normal}, {"putsl", putsCmd, &puts_Ext}, #ifdef TH_USE_SQLITE {"query_col_count", queryColCountCmd, 0}, {"query_col_name", queryColNameCmd, 0}, {"query_col_string", queryColStringCmd, 0}, | > > > > > > > > > | < < < > > > > > > > > > > > > > > > > > > > | 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 | {"hascap", hascapCmd, 0}, {"hasfeature", hasfeatureCmd, 0}, {"htmlize", htmlizeCmd, 0}, {"date", dateCmd, 0}, {"html", putsCmd, &puts_Html}, {"puts", putsCmd, &puts_Normal}, {"putsl", putsCmd, &puts_Ext}, {"wiki", wikiCmd, 0}, {"repository", repositoryCmd, 0}, #ifdef TH_USE_SQLITE {"query_bind_int", queryBindIntCmd, 0}, {"query_bind_double", queryBindDoubleCmd,0}, {"query_bind_null", queryBindNullCmd, 0}, {"query_bind_string", queryBindStringCmd,0}, {"query_col_count", queryColCountCmd, 0}, {"query_col_double", queryColDoubleCmd, 0}, {"query_col_int", queryColIntCmd, 0}, {"query_col_is_null", queryColIsNullCmd, 0}, {"query_col_name", queryColNameCmd, 0}, {"query_col_string", queryColStringCmd, 0}, {"query_col_type", queryColTypeCmd, 0}, {"query_finalize", queryFinalizeCmd, 0}, {"query_prepare", queryPrepareCmd, 0}, {"query_step", queryStepCmd, 0}, #endif {0, 0, 0} }; if( g.interp==0 ){ int i; g.interp = Th_CreateInterp(&vtab); th_register_language(g.interp); /* Basic scripting commands. */ #ifdef FOSSIL_ENABLE_TCL if( getenv("TH1_ENABLE_TCL")!=0 || db_get_boolean("tcl", 0) ){ th_register_tcl(g.interp, &g.tcl); /* Tcl integration commands. */ } #endif for(i=0; i<sizeof(aCommand)/sizeof(aCommand[0]); i++){ if ( !aCommand[i].zName || !aCommand[i].xProc ) continue; Th_CreateCommand(g.interp, aCommand[i].zName, aCommand[i].xProc, aCommand[i].pContext, 0); } #ifdef TH_USE_SQLITE { enum { BufLen = 100 }; char buf[BufLen]; int i; #define SET(K) i = snprintf(buf, BufLen, "%d", K); \ Th_SetVar( g.interp, #K, strlen(#K), buf, i ); SET(SQLITE_BLOB); SET(SQLITE_DONE); SET(SQLITE_ERROR); SET(SQLITE_FLOAT); SET(SQLITE_INTEGER); SET(SQLITE_NULL); SET(SQLITE_OK); SET(SQLITE_ROW); SET(SQLITE_TEXT); #undef SET } #endif } } /* ** Store a string value in a variable in the interpreter. */ void Th_Store(const char *zName, const char *zValue){ |
︙ | ︙ |
Changes to test/th1-query-api-1.th1.
︙ | ︙ | |||
26 27 28 29 30 31 32 | set stmt [query_prepare {SELECT login, cap FROM user}] set colCount [query_col_count $stmt] puts "query column count: ${colCount}\n" #set stmt2 [query_prepare {SELECT cap, login FROM user}] #puts "stmt=${stmt} stmt2=${stmt2}\n" #putsl "step =" [query_step $stmt] | | > | 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | set stmt [query_prepare {SELECT login, cap FROM user}] set colCount [query_col_count $stmt] puts "query column count: ${colCount}\n" #set stmt2 [query_prepare {SELECT cap, login FROM user}] #puts "stmt=${stmt} stmt2=${stmt2}\n" #putsl "step =" [query_step $stmt] #putsl "cap =" [query_col_string $stmt 1] proc noop {} {} proc incr {name {step 1}} { upvar $name x set x [expr $x+$step] } set sep " " set i 0 set colNames(0) 0 for {set i 0} {$i < $colCount} {incr i} { set colNames($i) [query_col_name $stmt $i] puts "colNames($i)=" $colNames($i) "\n" } for {set row 0} {0 < [query_step $stmt]} {incr row} { for {set i 0} {$i < $colCount} {incr i} { if {$i > 0} { puts $sep } else { |
︙ | ︙ | |||
72 73 74 75 76 77 78 | upvar cb $callback for {set row 0} {0 < [query_step $stmt]} {incr row} { #puts "Calling callback: $stmt $colCount colNames\n" $callback $stmt $colCount } } | | > > | > > > > > > > | | > > | > > > > > > > > > | 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | upvar cb $callback for {set row 0} {0 < [query_step $stmt]} {incr row} { #puts "Calling callback: $stmt $colCount colNames\n" $callback $stmt $colCount } } set sql {SELECT uid, login FROM user WHERE uid!=?} #set sql {SELECT uid, login FROM user WHERE login=?} #set sql {SELECT tagid, value, null FROM tagxref WHERE value IS ? LIMIT 3} set stmt [query_prepare $sql] puts "stmt ID=" $stmt "\n" query_bind_int $stmt 1 3 #set stmt [query_prepare $sql] #query_bind_string $stmt 1 stephan #set stmt [query_prepare $sql] #query_bind_null $stmt 1 set rc 0 puts "USER LIST:\n" catch { proc my_each {stmt colCount} { upvar 2 sep sep puts [query_col_int $stmt 0] " (type=" [query_col_type $stmt 0] ")" $sep puts [query_col_double $stmt 0] $sep puts [query_col_string $stmt 1] " (type=" [query_col_type $stmt 1] ")" $sep puts "isnull 0 ?= " [query_col_is_null $stmt 0] $sep puts "isnull 2 ?= " [query_col_is_null $stmt 2] # for {set i 0} {$i < $colCount} {incr i} { # if {$i > 0} { puts $sep } # } puts "\n" # error "hi!" } query_step_each $stmt my_each # query_step_each $stmt { # proc each {stmt cc} { puts hi "\n" } # } } rc query_finalize $stmt puts rc = $rc "\n" set consts [list SQLITE_BLOB SQLITE_DONE SQLITE_ERROR SQLITE_FLOAT SQLITE_INTEGER SQLITE_NULL SQLITE_OK SQLITE_ROW SQLITE_TEXT] #set consts $SQLITE_CONSTANTS puts consts = $consts "\n" for {set i 0} {$i < [llength $consts]} {incr i} { set x [lindex $consts $i] puts \$$x = [expr \$$x] "\n" } puts "If you got this far, you win!\n" </th1> |