Fossil

Check-in [b01eb58b]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added query_col_int and query_col_double. Renamed query_column_xxx to query_col_xxx.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | th1-query-api
Files: files | file ages | folders
SHA1: b01eb58bcaadfd79ba6e1732c7f7003b674dc406
User & Date: stephan 2012-07-14 00:56:23
Context
2012-07-14
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
00:20
added th1 query API. check-in: c3b10e12 user: stephan tags: th1-query-api
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/th_main.c.

515
516
517
518
519
520
521

































522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
...
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574

575
576
577
578
579
580
581








































582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
...
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
...
660
661
662
663
664
665
666
667
668



669
670
671
672
673
674
675
676
677
678
679
  return TH_OK;
}

static void queryReportDbErr( Th_Interp * interp, int rc ){
  char const * msg = sqlite3_errmsg( g.db );
  Th_ErrorMessage(interp, "db error:", msg, -1);
}


































static int queryStepCmd(
  Th_Interp *interp,
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  int rc = 0;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "query_step StmtHandle");
  }
  pStmt = queryStmtHandle(interp, argv[1], argl[1], &rc);
  if( rc < 1 ){
    return TH_ERROR;
  }
  rc = sqlite3_step( pStmt );
  switch(rc){
    case SQLITE_ROW:
      rc = 1;
      break;
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  char const * val;
  int index;
  int rc = 0;
  int valLen;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_column_string StmtHandle Index");
  }
  pStmt = queryStmtHandle(interp, argv[1], argl[1], &rc);
  if( rc < 1 ){
    return TH_ERROR;
  }
  if( 0 != Th_ToInt( interp, argv[2], argl[2], &index ) ){

    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 queryColCountCmd(
  Th_Interp *interp,
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  int rc;
  sqlite3_stmt * pStmt = NULL;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "query_column_count StmtHandle");
  }
  pStmt = queryStmtHandle(interp, argv[1], argl[1], NULL);
  if( NULL == pStmt ){
    return TH_ERROR;
  }
  rc = sqlite3_column_count( pStmt );
  Th_SetResultInt( interp, rc );
................................................................................
){
  sqlite3_stmt * pStmt = NULL;
  char const * val;
  int index;
  int rc = 0;
  int valLen;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_column_name StmtHandle Index");
  }
  pStmt = queryStmtHandle(interp, argv[1], argl[1], &rc);
  if( rc < 1 ){
    return TH_ERROR;
  }
  if( 0 != Th_ToInt( interp, argv[2], argl[2], &index ) ){
    return TH_ERROR;
................................................................................
    {"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_column_count",   queryColCountCmd,  0},
    {"query_column_name",    queryColNameCmd,   0},



    {"query_finalize",       queryFinalizeCmd,  0},
    {"query_prepare",        queryPrepareCmd,   0},
    {"query_step",           queryStepCmd,      0},
    {"query_column_string",  queryColStringCmd, 0},
#endif
    {"wiki",          wikiCmd,              0},
    {"repository",    repositoryCmd,        0},
    {0, 0, 0}
  };
  if( g.interp==0 ){
    int i;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>













|
<







 







<


|

<
<
<
<
<
>







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>












|







 







|







 







|
|
>
>
>
|
|
|
<







515
516
517
518
519
520
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568

569
570
571
572
573
574
575
...
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
...
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
...
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741

742
743
744
745
746
747
748
  return TH_OK;
}

static void queryReportDbErr( Th_Interp * interp, int rc ){
  char const * msg = sqlite3_errmsg( g.db );
  Th_ErrorMessage(interp, "db error:", msg, -1);
}

static int queryStmtIndexArgs(
  Th_Interp * interp,
  int argc,
  char const ** argv,
  int *argl,
  sqlite3_stmt ** pStmt,
  int * pIndex ){
  int index = 0;
  sqlite3_stmt * stmt;
  if( !pIndex ){
    if(argc<2){
      return Th_WrongNumArgs(interp, "StmtHandle");
    }
  }else{
    if( argc<3 ){
      return Th_WrongNumArgs(interp, "StmtHandle, Index");
    }
    if( 0 != Th_ToInt( interp, argv[2], argl[2], &index ) ){
      return TH_ERROR;
    }
  }
  stmt = queryStmtHandle(interp, argv[1], argl[1], NULL);
  if( NULL == stmt ){
    return TH_ERROR;
  }else{
    *pStmt = stmt;
    if( pIndex ){
      *pIndex = index;
    }
    return 0;
  }
}

static int queryStepCmd(
  Th_Interp *interp,
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  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;
................................................................................
  int argc, 
  const char **argv, 
  int *argl
){
  sqlite3_stmt * pStmt = NULL;
  char const * val;
  int index;

  int valLen;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_col_string StmtHandle Index");
  }





  if(0 != queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index)){
    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 = 0;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_col_int StmtHandle Index");
  }
  if(0 != queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index)){
    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");
  }
  if(0 != queryStmtIndexArgs(interp, argc, argv, argl, &pStmt, &index)){
    return TH_ERROR;
  }
  Th_SetResultDouble( interp, sqlite3_column_double( pStmt, index ) );
  return TH_OK;
}


static int queryColCountCmd(
  Th_Interp *interp,
  void *p, 
  int argc, 
  const char **argv, 
  int *argl
){
  int rc;
  sqlite3_stmt * pStmt = NULL;
  if( argc!=2 ){
    return Th_WrongNumArgs(interp, "query_col_count StmtHandle");
  }
  pStmt = queryStmtHandle(interp, argv[1], argl[1], NULL);
  if( NULL == pStmt ){
    return TH_ERROR;
  }
  rc = sqlite3_column_count( pStmt );
  Th_SetResultInt( interp, rc );
................................................................................
){
  sqlite3_stmt * pStmt = NULL;
  char const * val;
  int index;
  int rc = 0;
  int valLen;
  if( argc!=3 ){
    return Th_WrongNumArgs(interp, "query_col_name StmtHandle Index");
  }
  pStmt = queryStmtHandle(interp, argv[1], argl[1], &rc);
  if( rc < 1 ){
    return TH_ERROR;
  }
  if( 0 != Th_ToInt( interp, argv[2], argl[2], &index ) ){
    return TH_ERROR;
................................................................................
    {"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},
    {"query_col_int",     queryColIntCmd,    0},
    {"query_col_double",  queryColDoubleCmd, 0},
    {"query_finalize",    queryFinalizeCmd,  0},
    {"query_prepare",     queryPrepareCmd,   0},
    {"query_step",        queryStepCmd,      0},

#endif
    {"wiki",          wikiCmd,              0},
    {"repository",    repositoryCmd,        0},
    {0, 0, 0}
  };
  if( g.interp==0 ){
    int i;

Changes to test/th1-query-api-1.th1.



1
2
3
4
5
6
7
..
19
20
21
22
23
24
25
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81




82
83
84
85

86
87
88
89



90
91
92
93
94


<th1>
proc bar {} {
puts "json ?= [hasfeature json]\n"
puts "tcl ?= [hasfeature tcl]\n"
puts "ssl ?= [hasfeature ssl]\n"
puts [lindex {a b c} 1] "\n"
proc foo {a {b steve}} {
................................................................................
proc xyz {} {
        return 42
}
set a [xyz]
puts "a=${a}" ! \n

set stmt [query_prepare {SELECT login, cap FROM user}]
set colCount [query_column_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 "val =" [query_column_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_column_name $stmt $i]
}

for {set row 0} {0 < [query_step $stmt]} {incr row} {
    for {set i 0} {$i < $colCount} {incr i} {
        if {$i > 0} {
            puts $sep
        } else {
            puts "#$row: $sep"
        }
        puts $colNames($i) = [query_column_string $stmt $i]
    }
    puts "\n"
}
unset row

query_finalize $stmt
#query_finalize $stmt2


proc query_step_each {{stmt} {callback}} {
    set colNames(0) 0
    set colCount [query_column_count $stmt]
    for {set i 0} {$i < $colCount} {incr i} {
        set colNames($i) [query_column_name $stmt $i]
    }
    upvar cb $callback
    for {set row 0} {0 < [query_step $stmt]} {incr row} {
        #puts "Calling callback: $stmt $colCount colNames\n"
        $callback $stmt $colCount
    }
}


set stmt [query_prepare {SELECT login FROM user}]
set rc 0
catch {
    proc my_each {stmt colCount} {




        for {set i 0} {$i < $colCount} {incr i} {
            if {$i > 0} { puts $sep }
            puts [query_column_string $stmt $i]
        }

        puts "\n"
#        error "hi!"
    }
    query_step_each $stmt my_each



} rc
query_finalize $stmt
puts rc = $rc

</th1>
>
>







 







|




|












|









|











|

|









|



>
>
>
>
|
|
<
<
>




>
>
>





1
2
3
4
5
6
7
8
9
..
21
22
23
24
25
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
This is not a formal test suite, but a tinkering ground.
Run it through "fossil test-th-render THIS_FILE".
<th1>
proc bar {} {
puts "json ?= [hasfeature json]\n"
puts "tcl ?= [hasfeature tcl]\n"
puts "ssl ?= [hasfeature ssl]\n"
puts [lindex {a b c} 1] "\n"
proc foo {a {b steve}} {
................................................................................
proc xyz {} {
        return 42
}
set a [xyz]
puts "a=${a}" ! \n

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 "val =" [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]
}

for {set row 0} {0 < [query_step $stmt]} {incr row} {
    for {set i 0} {$i < $colCount} {incr i} {
        if {$i > 0} {
            puts $sep
        } else {
            puts "#$row: $sep"
        }
        puts $colNames($i) = [query_col_string $stmt $i]
    }
    puts "\n"
}
unset row

query_finalize $stmt
#query_finalize $stmt2


proc query_step_each {{stmt} {callback}} {
    set colNames(0) 0
    set colCount [query_col_count $stmt]
    for {set i 0} {$i < $colCount} {incr i} {
        set colNames($i) [query_col_name $stmt $i]
    }
    upvar cb $callback
    for {set row 0} {0 < [query_step $stmt]} {incr row} {
        #puts "Calling callback: $stmt $colCount colNames\n"
        $callback $stmt $colCount
    }
}


set stmt [query_prepare {SELECT uid, login FROM user}]
set rc 0
catch {
    proc my_each {stmt colCount} {
        upvar 2 sep sep
        puts [query_col_int $stmt 0] $sep
        puts [query_col_double $stmt 0] $sep
        puts [query_col_string $stmt 1]
#        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

</th1>