-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresult.go
1150 lines (1002 loc) · 35.7 KB
/
result.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
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
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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// //// SQLite Cloud
// //////////// ///
// /// /// /// Product : SQLite Cloud GO SDK
// /// /// /// Version : 1.2.2
// // /// /// /// Date : 2021/10/14
// /// /// /// /// Author : Andreas Pfeil
// /// /// /// ///
// /// ////////// /// /// Description : GO Methods related to the
// //// /// /// Result class.
// //// ////////// ///
// //// ////
// //// /////
// /// Copyright : 2021 by SQLite Cloud Inc.
//
// -----------------------------------------------------------------------TAB=2
package sqlitecloud
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
"golang.org/x/term"
)
var rowsetChunkEndPatterns = []([]byte){[]byte("5 0 0 0"), []byte("6 0 0 0 "), []byte("8 0 0 0 0 ")}
const OUTFORMAT_LIST = 0
const OUTFORMAT_CSV = 1
const OUTFORMAT_QUOTE = 2
const OUTFORMAT_TABS = 3
const OUTFORMAT_LINE = 4
const OUTFORMAT_JSON = 5
const OUTFORMAT_HTML = 6
const OUTFORMAT_MARKDOWN = 7
const OUTFORMAT_TABLE = 8
const OUTFORMAT_BOX = 9
const OUTFORMAT_XML = 10
const ROWSET_TYPE_BASIC = 1
const ROWSET_TYPE_METADATA_v1 = 2
const ROWSET_TYPE_HEADER_ONLY = 3
const ROWSET_TYPE_DATA_ONLY = 4
// The Result is either a Literal or a RowSet
type Result struct {
uncompressedChuckSizeSum uint64
value Value
rows []ResultRow
// columns metadata
Name []string
Width []uint64
DeclType []string
DbName []string
TblName []string
OrigName []string
Notnull []int32
PriKey []int32
Autoinc []int32
MaxHeaderWidth uint64
}
var OKResult = Result{
value: Value{Type: '+', Buffer: []byte("OK")}, // This is an unset Value
rows: nil,
MaxHeaderWidth: 0,
uncompressedChuckSizeSum: 0,
}
func (this *Result) Rows() []ResultRow { return this.rows }
// ResultSet Methods (100% GO)
// GetType returns the type of this query result as an integer (see: RESULT_ constants).
func (this *Result) GetType() byte { return this.value.GetType() }
// IsOK returns true if this query result if of type "RESULT_OK", false otherwise.
func (this *Result) IsOK() bool { return this.value.IsOK() }
// GetNumberOfRows returns the number of rows in this query result
func (this *Result) GetNumberOfRows() uint64 {
switch {
case !this.IsRowSet():
return 0
default:
return uint64(len(this.rows))
}
}
// GetNumberOfColumns returns the number of columns in this query result
func (this *Result) GetNumberOfColumns() uint64 {
switch {
case !this.IsRowSet():
return 0
default:
return uint64(len(this.Width))
}
}
// Dump outputs this query result to the screen.
// Warning: No line truncation is used. If you want to truncation the output to a certain width, use: Result.DumpToScreen( width )
func (this *Result) Dump() {
w := 0
if width, _, err := term.GetSize(0); err == nil {
w = width
}
this.DumpToScreen(uint(w))
}
// ToJSON returns a JSON representation of this query result.
func (this *Result) ToJSON() string {
buf := new(bytes.Buffer)
_, _ = this.DumpToWriter(bufio.NewWriter(buf), OUTFORMAT_JSON, false, "<AUTO>", "NULL", "", 0, false)
return buf.String()
}
// Additional ResultSet Methods (100% GO)
// GetMaxColumnLength returns the number of runes of the value in the specified column with the maximum length in this query result.
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
func (this *Result) GetMaxColumnWidth(Column uint64) (uint64, error) {
switch {
case !this.IsRowSet():
return 0, errors.New("Not a RowSet")
case Column >= this.GetNumberOfColumns():
return 0, errors.New("Column Index out of bounds")
default:
return this.Width[Column], nil
}
}
// GetNameWidth returns the number of runes of the column name in the specified column.
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
func (this *Result) GetNameLength(Column uint64) (uint64, error) {
switch {
case !this.IsRowSet():
return 0, errors.New("Not a RowSet")
case Column >= this.GetNumberOfColumns():
return 0, errors.New("Column Index out of bounds")
default:
return uint64(len(this.Name[Column])), nil
}
}
// GetMaxNameWidth returns the number of runes of the longest column name.
func (this *Result) GetMaxNameWidth() uint64 { return this.MaxHeaderWidth }
// Additional Data Access Functions (100% GO)
// IsError returns true if this query result is of type "RESULT_ERROR", false otherwise.
func (this *Result) IsError() bool { return this.value.IsError() }
// IsNull returns true if this query result is of type "RESULT_NULL", false otherwise.
func (this *Result) IsNULL() bool { return this.value.IsNULL() }
// IsJson returns true if this query result is of type "RESULT_JSON", false otherwise.
func (this *Result) IsJSON() bool { return this.value.IsJSON() }
// IsString returns true if this query result is of type "RESULT_STRING", false otherwise.
func (this *Result) IsString() bool { return this.value.IsString() }
// IsInteger returns true if this query result is of type "RESULT_INTEGER", false otherwise.
func (this *Result) IsInteger() bool { return this.value.IsInteger() }
// IsFloat returns true if this query result is of type "RESULT_FLOAT", false otherwise.
func (this *Result) IsFloat() bool { return this.value.IsFloat() }
// IsPSUB returns true if this query result is of type "RESULT_XXXXXXX", false otherwise.
func (this *Result) IsPSUB() bool { return this.value.IsPSUB() }
// IsCommand returns true if this query result is of type "RESULT_XXXXXXX", false otherwise.
func (this *Result) IsCommand() bool { return this.value.IsCommand() }
// IsReconnect returns true if this query result is of type "RESULT_XXXXXXX", false otherwise.
func (this *Result) IsReconnect() bool { return this.value.IsReconnect() }
func (this *Result) IsBLOB() bool { return this.value.IsBLOB() }
func (this *Result) IsArray() bool { return this.value.IsArray() }
// IsText returns true if this query result is of type "RESULT_JSON", "RESULT_STRING", "RESULT_INTEGER" or "RESULT_FLOAT", false otherwise.
func (this *Result) IsText() bool { return this.value.IsText() }
// IsRowSet returns true if this query result is of type "RESULT_ROWSET", false otherwise.
func (this *Result) IsRowSet() bool {
switch {
case !this.value.IsRowSet() && !this.value.IsArray():
return false
case this.rows == nil:
return false
case this.Name == nil:
return false
case this.Width == nil:
return false
case this.MaxHeaderWidth == 0:
return false
default:
return true
}
}
func (this *Result) IsLiteral() bool { return !this.IsRowSet() }
// ResultSet Buffer/Scalar Methods
// GetUncompressedChuckSizeSum returns the
func (this *Result) GetUncompressedChuckSizeSum() uint64 { return this.uncompressedChuckSizeSum }
// GetBufferLength returns the length of the buffer of this query result.
func (this *Result) GetBufferLength() (uint64, error) {
switch {
case this.IsRowSet():
return 0, errors.New("Not a scalar value")
default:
return this.value.GetLength(), nil
}
}
// GetBuffer returns the buffer of this query result as string.
func (this *Result) GetBuffer() []byte { return this.value.GetBuffer() }
func (this *Result) GetString() (string, error) {
switch {
case this.IsRowSet():
return "", errors.New("Not a literal")
default:
return this.value.GetString(), nil
}
}
func (this *Result) GetString_() string {
value, _ := this.GetString()
return value
}
func (this *Result) GetJSON() (object interface{}, err error) {
switch {
case !this.IsJSON():
return nil, errors.New("Not a JSON object")
default:
err = json.Unmarshal(this.value.GetBuffer(), object)
return
}
}
func (this *Result) GetJSON_() (object interface{}) {
value, _ := this.GetJSON()
return value
}
func (this *Result) GetInt32() (int32, error) {
switch {
case !this.IsInteger():
return 0, errors.New("Not an integer value")
default:
return this.value.GetInt32()
}
}
func (this *Result) GetInt32_() int32 {
value, _ := this.GetInt32()
return value
}
func (this *Result) GetInt64() (int64, error) {
switch {
case !this.IsInteger():
return 0, errors.New("Not an integer value")
default:
return this.value.GetInt64()
}
}
func (this *Result) GetInt64_() int64 {
value, _ := this.GetInt64()
return value
}
func (this *Result) GetFloat32() (float32, error) {
switch {
case !this.IsFloat():
return 0, errors.New("Not a float value")
default:
return this.value.GetFloat32()
}
}
func (this *Result) GetFloat32_() float32 {
value, _ := this.GetFloat32()
return value
}
func (this *Result) GetFloat64() (float64, error) {
switch {
case !this.IsFloat():
return 0, errors.New("Not a float value")
default:
return this.value.GetFloat64()
}
}
func (this *Result) GetFloat64_() float64 {
value, _ := this.GetFloat64()
return value
}
// GetError returns the ErrorCode, ExtErrorCode, ErrorOffset, ErrorMessage
// and the error object of the receiver
func (this *Result) GetError() (int, int, int, string, error) {
switch {
case !this.IsError():
return 0, NO_EXTCODE, NO_OFFCODE, "", errors.New("Not an error")
default:
return this.value.GetError()
}
}
func (this *Result) GetError_() (int, string) {
code, _, _, message, _ := this.GetError()
return code, message
}
func (this *Result) GetErrorAsString() string {
switch code, extcode, offset, message, err := this.GetError(); {
case err != nil:
return fmt.Sprintf("INTERNAL ERROR: %s", err.Error())
default:
if extcode == NO_EXTCODE && offset == NO_OFFCODE {
return fmt.Sprintf("ERROR: %s (%d)", message, code)
} else {
return fmt.Sprintf("ERROR: %s (%d:%d:%d)", message, code, extcode, offset)
}
}
}
// Free frees all memory allocated by this query result.
func (this *Result) Free() {
this.value = Value{Type: 0, Buffer: nil} // GC
this.rows = []ResultRow{} // GC
this.Name = []string{} // GC
this.Width = []uint64{} // GC
this.DeclType = []string{} // GC
this.DbName = []string{}
this.TblName = []string{}
this.OrigName = []string{}
this.Notnull = []int32{}
this.PriKey = []int32{}
this.Autoinc = []int32{}
this.MaxHeaderWidth = 0
}
// GetName returns the column name in column Column of this query result.
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
func (this *Result) GetName(Column uint64) (string, error) {
switch {
case Column >= this.GetNumberOfColumns():
return "", errors.New("Column Index out of bounds")
default:
return this.Name[Column], nil
}
}
func (this *Result) GetName_(Column uint64) string {
value, _ := this.GetName(Column)
return value
}
// DumpToScreen outputs this query result to the screen.
// The output is truncated at a maximum line width of MaxLineLength runes (compare: Result.Dump())
func (this *Result) DumpToScreen(MaxLineLength uint) {
_, _ = this.DumpToWriter(bufio.NewWriter(os.Stdout), OUTFORMAT_BOX, false, "│", "NULL", "\r\n", MaxLineLength, false)
}
////// Row Methods (100% GO)
// GetRow returns a pointer to the row Row of this query result.
// The Row index is an unsigned int in the range of 0...GetNumberOfRows() - 1.
// If the index row can not be found, nil is returned instead.
func (this *Result) GetRow(Row uint64) (*ResultRow, error) {
switch {
case !this.IsRowSet():
return nil, errors.New("Not a Rowset")
case Row >= this.GetNumberOfRows():
return nil, errors.New("Row index is out of bounds")
default:
return &this.rows[Row], nil
}
}
// GetFirstRow returns the first row of this query result.
// If this query result has no row's, nil is returned instead.
func (this *Result) GetFirstRow() (*ResultRow, error) { return this.GetRow(0) }
// GetLastRow returns the first row of this query result.
// If this query result has no row's, nil is returned instead.
func (this *Result) GetLastRow() (*ResultRow, error) { return this.GetRow(this.GetNumberOfRows() - 1) }
// Additional Row Methods <- sollte es eigentlich nicht geben!!!!!
func (this *Result) GetValue(Row uint64, Column uint64) (*Value, error) {
switch row, err := this.GetRow(Row); {
case err != nil:
return nil, err
default:
return row.GetValue(Column)
}
}
// GetValueType returns the type of the value in row Row and column Column of this query result.
// The Row index is an unsigned int in the range of 0...GetNumberOfRows() - 1.
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
// Possible return types are: VALUE_INTEGER, VALUE_FLOAT, VALUE_TEXT, VALUE_BLOB, VALUE_NULL
func (this *Result) GetValueType(Row uint64, Column uint64) (byte, error) {
switch value, err := this.GetValue(Row, Column); {
case err != nil:
return '_', err
default:
return value.GetType(), nil
}
}
func (this *Result) GetValueType_(Row uint64, Column uint64) byte {
value, _ := this.GetValueType(Row, Column)
return value
}
// GetStringValue returns the contents in row Row and column Column of this query result as string.
// The Row index is an unsigned int in the range of 0...GetNumberOfRows() - 1.
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
func (this *Result) GetStringValue(Row uint64, Column uint64) (string, error) {
switch value, err := this.GetValue(Row, Column); {
case err != nil:
return "", err
default:
return value.GetString(), nil
}
}
func (this *Result) GetStringValue_(Row uint64, Column uint64) string {
value, _ := this.GetStringValue(Row, Column)
return value
}
// GetInt32Value returns the contents in row Row and column Column of this query result as int32.
// The Row index is an unsigned int in the range of 0...GetNumberOfRows() - 1.
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
func (this *Result) GetInt32Value(Row uint64, Column uint64) (int32, error) {
switch value, err := this.GetValue(Row, Column); {
case err != nil:
return 0, err
default:
return value.GetInt32()
}
}
func (this *Result) GetInt32Value_(Row uint64, Column uint64) int32 {
value, _ := this.GetInt32Value(Row, Column)
return value
}
// GetInt64Value returns the contents in row Row and column Column of this query result as int64.
// The Row index is an unsigned int in the range of 0...GetNumberOfRows() - 1.
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
func (this *Result) GetInt64Value(Row uint64, Column uint64) (int64, error) {
switch value, err := this.GetValue(Row, Column); {
case err != nil:
return 0, err
default:
return value.GetInt64()
}
}
func (this *Result) GetInt64Value_(Row uint64, Column uint64) int64 {
value, _ := this.GetInt64Value(Row, Column)
return value
}
// GetFloat32Value returns the contents in row Row and column Column of this query result as float32.
// The Row index is an unsigned int in the range of 0...GetNumberOfRows() - 1.
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
func (this *Result) GetFloat32Value(Row uint64, Column uint64) (float32, error) {
switch value, err := this.GetValue(Row, Column); {
case err != nil:
return 0, err
default:
return value.GetFloat32()
}
}
func (this *Result) GetFloat32Value_(Row uint64, Column uint64) float32 {
value, _ := this.GetFloat32Value(Row, Column)
return value
}
// GetFloat64Value returns the contents in row Row and column Column of this query result as float64.
// The Row index is an unsigned int in the range of 0...GetNumberOfRows() - 1.
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
func (this *Result) GetFloat64Value(Row uint64, Column uint64) (float64, error) {
switch value, err := this.GetValue(Row, Column); {
case err != nil:
return 0, err
default:
return value.GetFloat64()
}
}
func (this *Result) GetFloat64Value_(Row uint64, Column uint64) float64 {
value, _ := this.GetFloat64Value(Row, Column)
return value
}
// GetSQLDateTime parses this query result value in Row and Column as an SQL-DateTime and returns its value.
// The Row index is an unsigned int in the range of 0...GetNumberOfRows() - 1.
// The Column index is an unsigned int in the range of 0...GetNumberOfColumns() - 1.
func (this *Result) GetSQLDateTime(Row uint64, Column uint64) (time.Time, error) {
switch value, err := this.GetValue(Row, Column); {
case err != nil:
return time.Unix(0, 0), err
default:
return value.GetSQLDateTime()
}
}
func (this *Result) GetSQLDateTime_(Row uint64, Column uint64) time.Time {
value, _ := this.GetSQLDateTime(Row, Column)
return value
}
////////////////////////////
func trimStringToMaxLength(Buffer string, MaxLineLength uint) string {
switch {
case MaxLineLength == 0:
return Buffer
case MaxLineLength >= uint(len([]rune(Buffer))):
return Buffer
default:
return fmt.Sprintf(fmt.Sprintf("%%.%ds…", MaxLineLength-1), Buffer)
}
}
func renderCenteredString(Buffer string, Width int) string {
return fmt.Sprintf("%[1]*s", -Width, fmt.Sprintf("%[1]*s", (Width+len(Buffer))/2, Buffer))
}
func (this *Result) renderHorizontalTableLine(Left string, Fill string, Separator string, Right string) string {
outBuffer := ""
for _, columnWidth := range this.Width {
outBuffer = fmt.Sprintf("%s%s%s", outBuffer, strings.Repeat(Fill, int(columnWidth+2)), Separator)
}
return fmt.Sprintf("%s%s%s", Left, strings.TrimRight(outBuffer, Separator), Right)
}
func (this *Result) renderTableColumnNames(Left string, Separator string, Right string) string {
outBuffer := ""
for forThisColumn, columnWidth := range this.Width {
columnName, _ := this.GetName(uint64(forThisColumn))
outBuffer = fmt.Sprintf("%s%s%s", outBuffer, renderCenteredString(columnName, int(columnWidth+2)), Separator)
}
return fmt.Sprintf("%s%s%s", Left, strings.TrimRight(outBuffer, Separator), Right)
}
func (this *Result) renderTableHeader(Format int, Separator string, NewLine string, MaxLineLength uint) string {
switch Format {
case OUTFORMAT_JSON:
return fmt.Sprintf("[%s", NewLine)
case OUTFORMAT_MARKDOWN:
return trimStringToMaxLength(this.renderTableColumnNames(Separator, Separator, Separator), MaxLineLength) + NewLine +
trimStringToMaxLength(this.renderHorizontalTableLine(Separator, "-", Separator, Separator), MaxLineLength) + NewLine
case OUTFORMAT_TABLE:
tableLine := trimStringToMaxLength(this.renderHorizontalTableLine("+", "-", "+", "+"), MaxLineLength) + NewLine
return tableLine +
trimStringToMaxLength(this.renderTableColumnNames(Separator, Separator, Separator), MaxLineLength) + NewLine +
tableLine
case OUTFORMAT_BOX:
return trimStringToMaxLength(this.renderHorizontalTableLine("┌", "─", "┬", "┐"), MaxLineLength) + NewLine +
trimStringToMaxLength(this.renderTableColumnNames(Separator, Separator, Separator), MaxLineLength) + NewLine +
trimStringToMaxLength(this.renderHorizontalTableLine("├", "─", "┼", "┤"), MaxLineLength) + NewLine
case OUTFORMAT_XML:
return trimStringToMaxLength("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>", MaxLineLength) + NewLine +
trimStringToMaxLength(fmt.Sprintf("<resultset statement=\"%s\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">", Separator), MaxLineLength) + NewLine
default:
return "" // No header
}
}
func (this *Result) renderTableFooter(Format int, NewLine string, MaxLineLength uint) string {
switch Format {
case OUTFORMAT_JSON:
return trimStringToMaxLength("]", MaxLineLength) + NewLine
case OUTFORMAT_TABLE:
return trimStringToMaxLength(this.renderHorizontalTableLine("+", "-", "+", "+"), MaxLineLength) + NewLine
case OUTFORMAT_BOX:
return trimStringToMaxLength(this.renderHorizontalTableLine("└", "─", "┴", "┘"), MaxLineLength) + NewLine
case OUTFORMAT_XML:
return trimStringToMaxLength("</resultset>", MaxLineLength) + NewLine
default:
return "" // No footer
}
}
// DumpToWriter renders this query result into the buffer of an io.Writer.
// The output Format can be specified and must be one of the following values: OUTFORMAT_LIST, OUTFORMAT_CSV, OUTFORMAT_QUOTE, OUTFORMAT_TABS, OUTFORMAT_LINE, OUTFORMAT_JSON, OUTFORMAT_HTML, OUTFORMAT_MARKDOWN, OUTFORMAT_TABLE, OUTFORMAT_BOX
// The Separator argument specifies the column separating string (default: '|').
// All lines are truncated at MaxLineLeength. A MaxLineLangth of '0' means no truncation.
// If this query result is of type RESULT_OK and SuppressOK is set to false, an "OK" string is written to the buffer, otherwise nothing is written to the buffer.
func (this *Result) DumpToWriter(Out *bufio.Writer, Format int, NoHeader bool, Separator string, NullValue string, NewLine string, MaxLineLength uint, SuppressOK bool) (int, error) {
defer Out.Flush()
if sep, err := GetDefaultSeparatorForOutputFormat(Format); err != nil {
return 0, err
} else if strings.ToUpper(strings.TrimSpace(Separator)) == "<AUTO>" {
Separator = sep
}
if strings.TrimSpace(NullValue) == "" {
NullValue = "NULL"
}
// fmt.Printf( "Type = %d\r\n", this.Type )
switch {
case this.IsOK():
if SuppressOK {
return 0, nil
} else {
return io.WriteString(Out, fmt.Sprintf("OK%s", NewLine))
}
case this.IsNULL():
return io.WriteString(Out, fmt.Sprintf("%s%s", NullValue, NewLine))
case this.IsError():
return io.WriteString(Out, fmt.Sprintf("%s%s", this.GetErrorAsString(), NewLine))
case this.IsString(), this.IsInteger(), this.IsFloat(), this.IsJSON():
return io.WriteString(Out, string(this.GetBuffer())+NewLine)
case this.IsArray():
fallthrough
case this.IsRowSet():
var totalOutputLength int = 0
if !NoHeader { // Render Table Header incl. new line
if len, err := io.WriteString(Out, this.renderTableHeader(Format, Separator, NewLine, MaxLineLength)); err == nil {
totalOutputLength += len
} else {
return len + totalOutputLength, err
}
}
// Render Table Body incl. new line
for row, err := this.GetFirstRow(); err == nil && row != nil; row = row.Next() {
if len, err := row.DumpToWriter(Out, Format, Separator, NullValue, NewLine, MaxLineLength); err == nil {
totalOutputLength += len
} else {
return len + totalOutputLength, err
}
if row.Next() != nil {
switch Format {
case OUTFORMAT_JSON:
if len, err := io.WriteString(Out, Separator); err == nil {
totalOutputLength += len
} else {
return len + totalOutputLength, err
}
}
}
}
if !NoHeader { // Render Table Footer
if len, err := io.WriteString(Out, this.renderTableFooter(Format, NewLine, MaxLineLength)); err == nil {
totalOutputLength += len
} else {
return len + totalOutputLength, err
}
}
if err := Out.Flush(); err != nil {
return totalOutputLength, err
}
return totalOutputLength, nil
default:
return 0, errors.New("Unknown Output Format")
}
}
func GetOutputFormatFromString(Format string) (int, error) {
switch strings.ToUpper(strings.TrimSpace(Format)) {
case "LIST":
return OUTFORMAT_LIST, nil
case "CSV":
return OUTFORMAT_CSV, nil
case "QUOTE":
return OUTFORMAT_QUOTE, nil
case "TABS":
return OUTFORMAT_TABS, nil
case "LINE":
return OUTFORMAT_LINE, nil
case "JSON":
return OUTFORMAT_JSON, nil
case "HTML":
return OUTFORMAT_HTML, nil
case "MARKDOWN":
return OUTFORMAT_MARKDOWN, nil
case "TABLE":
return OUTFORMAT_TABLE, nil
case "BOX":
return OUTFORMAT_BOX, nil
case "XML":
return OUTFORMAT_XML, nil
case "":
return -1, errors.New("Missing output format")
default:
return -1, errors.New("Unknown output format")
}
}
func GetDefaultSeparatorForOutputFormat(Format int) (string, error) {
switch Format {
case OUTFORMAT_LIST, OUTFORMAT_MARKDOWN, OUTFORMAT_TABLE:
return "|", nil
case OUTFORMAT_CSV, OUTFORMAT_QUOTE, OUTFORMAT_JSON:
return ",", nil
case OUTFORMAT_TABS:
return "\t", nil
case OUTFORMAT_LINE:
return "=", nil
case OUTFORMAT_HTML, OUTFORMAT_XML:
return "", nil
case OUTFORMAT_BOX:
return "│", nil
default:
return "", errors.New("Unknown output format")
}
}
// ////
// is called from connection.Select
func (this *SQCloud) readResult() (*Result, error) {
ErrorResult := Result{
value: Value{Type: '-', Buffer: []byte("100000 Unknown internal error")}, // This is an unset Value
rows: nil,
Name: nil,
Width: nil,
MaxHeaderWidth: 0,
uncompressedChuckSizeSum: 0,
}
result := ErrorResult
var rowIndex uint64 = 0
for { // loop through all chunks
if chunk, err := this.readNextRawChunk(); err != nil {
ErrorResult.uncompressedChuckSizeSum = chunk.LEN
ErrorResult.value.Buffer = []byte(fmt.Sprintf("100001 Internal Error: SQCloud.readNextRawChunk (%s)", err.Error()))
return &ErrorResult, err
} else {
if err := chunk.Uncompress(); err != nil {
ErrorResult.uncompressedChuckSizeSum = chunk.LEN
ErrorResult.value.Buffer = []byte(fmt.Sprintf("100002 Internal Error: Chunk.Uncompress (%s)", err.Error()))
return &ErrorResult, err
} else {
result.uncompressedChuckSizeSum += chunk.LEN
switch Type := chunk.GetType(); Type {
case CMD_COMPRESSED:
return nil, errors.New("Nested compression")
// Values
case CMD_NULL:
fallthrough // NULL
case CMD_INT, CMD_FLOAT:
fallthrough // INT, FLOAT
case CMD_STRING, CMD_ZEROSTRING, CMD_BLOB, CMD_ERROR, CMD_JSON:
fallthrough // String, C-String, BLOB, Error-String, JSON-String
case CMD_PUBSUB:
fallthrough // PSUB
case CMD_COMMAND:
fallthrough // Command
case CMD_RECONNECT: // Reconnect
result.value.Type = Type
switch bytesRead, err := result.value.readBufferAt(chunk, 1); {
case err != nil:
return nil, err
case bytesRead == 0:
return nil, errors.New("No Data")
case Type == CMD_PUBSUB:
// PSUB
pauth := result.GetString_()
if this.psub == nil {
tokens := append(strings.SplitN(pauth, " ", 3), []string{"", "", ""}...) // make sure that there are enough elements for max index 2
// this.psubc = make(chan string)
this.psub = &SQCloud{
SQCloudConfig: SQCloudConfig{
Host: this.Host,
Port: this.Port,
Username: "",
Password: "",
Database: "",
Timeout: 0,
},
sock: nil,
psub: nil,
cert: this.cert,
uuid: tokens[1],
secret: tokens[2],
ErrorCode: 0,
ErrorMessage: "",
}
if err := this.psub.reconnect(); err != nil {
_ = this.psub.Close()
this.psub = nil
return nil, err
}
if _, err := this.psub.sendString(pauth); err != nil {
_ = this.psub.Close()
this.psub = nil
return nil, err
}
if result2, err2 := this.psub.readResult(); result2 != nil {
defer result2.Free()
if err2 != nil {
_ = this.psub.Close()
this.psub = nil
return nil, err2
}
if !result2.IsString() || result2.GetString_() != "OK" {
_ = this.psub.Close()
this.psub = nil
return nil, errors.New("unexpected result")
}
go func() {
for {
result, err := this.psub.readResult()
if result != nil {
defer result.Free()
}
if err != nil {
return
}
if result == nil {
return
}
if !result.IsJSON() {
return
}
this.Callback(this, result.GetString_())
// select {
// case this.psubc <- result.GetString_():
// default:
// // no message sent
// }
}
}()
}
}
result = OKResult
result.uncompressedChuckSizeSum = chunk.LEN
fallthrough
default:
return &result, nil
}
// Array
case CMD_ARRAY:
var offset uint64 = 1 // skip the first type byte
var N uint64
var bytesRead uint64
if _, _, bytesRead, err = chunk.readUInt64At(offset); err != nil {
return nil, err
}
offset += bytesRead
if N, _, bytesRead, err = chunk.readUInt64At(offset); err != nil {
return nil, err
} // 0..N-values
offset += bytesRead
result.rows = make([]ResultRow, int(N))
result.Name = []string{"ARRAY"}
result.Width = []uint64{5}
result.MaxHeaderWidth = 0
for row := uint64(0); row < N; row++ {
result.rows[row].result = &result
result.rows[row].Index = row
result.rows[row].Columns = make([]Value, 1)
switch result.rows[row].Columns[0], bytesRead, err = chunk.readValueAt(offset); {
case err != nil:
return nil, err
default:
columnLength := result.rows[row].Columns[0].GetLength()
if result.Width[0] < columnLength {
result.Width[0] = columnLength
}
if result.MaxHeaderWidth < columnLength {
result.MaxHeaderWidth = columnLength
}
offset += bytesRead
}
}
result.value.Type = '='
result.value.Buffer = nil
return &result, nil
// RowSet
case CMD_ROWSET_CHUNK, CMD_ROWSET:
// RowSet: *LEN 0:VERSION ROWS COLS DATA
// RowSet Chunk: /LEN IDX:VERSION ROWS COLS DATA
var offset uint64 = 1 // skip the first type byte
var bytesRead uint64
var LEN uint64
var IDX uint64
var VERSION uint64
var NROWS uint64
var NCOLS uint64
// Detect end of Rowset Chunk directly without parsing...
if Type == CMD_ROWSET_CHUNK {
for _, pattern := range rowsetChunkEndPatterns {
if chunk.RAW[offset] == pattern[0] && bytes.Equal(chunk.RAW[offset:offset+uint64(len(pattern))], pattern) {
return &result, nil
}
}
}
if LEN, _, bytesRead, err = chunk.readUInt64At(offset); err != nil {
return nil, err
}
offset += bytesRead
if IDX, VERSION, bytesRead, err = chunk.readUInt64At(offset); err != nil {
return nil, err
}
offset += bytesRead
if NROWS, _, bytesRead, err = chunk.readUInt64At(offset); err != nil {
return nil, err
} // 0..rows-1
offset += bytesRead
if NCOLS, _, bytesRead, err = chunk.readUInt64At(offset); err != nil {
return nil, err
} // 0..columns-1
offset += bytesRead
LEN += offset // check for overreading...
if Type == CMD_ROWSET_CHUNK && NROWS == 0 && NCOLS == 0 {
return &result, nil
}
// single chunk or first chunk of multiple chunks
if IDX == 0 || IDX == 1 {
result.rows = []ResultRow{}
result.MaxHeaderWidth = 0
if VERSION != ROWSET_TYPE_DATA_ONLY {
// header is guarantee to contain column names
result.Name = make([]string, int(NCOLS))
result.Width = make([]uint64, int(NCOLS))
for column := uint64(0); column < NCOLS; column++ { // Read in the column names, use the result.value as scratch variable
switch val, bytesRead, err := chunk.readValueAt(offset); {
case err != nil:
return nil, err
case !val.IsString():
return nil, errors.New("Invalid Column name")
default:
result.Name[column] = val.GetString()
result.Width[column] = val.GetLength()
if result.MaxHeaderWidth < result.Width[column] {
result.MaxHeaderWidth = result.Width[column]
}
offset += bytesRead
}
}
// check if additional metadata is contained
if VERSION == ROWSET_TYPE_METADATA_v1 {