github.com/nakagami/firebirdsql@v0.9.10/rows.go (about)

     1  /*******************************************************************************
     2  The MIT License (MIT)
     3  
     4  Copyright (c) 2013-2019 Hajime Nakagami
     5  
     6  Permission is hereby granted, free of charge, to any person obtaining a copy of
     7  this software and associated documentation files (the "Software"), to deal in
     8  the Software without restriction, including without limitation the rights to
     9  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    10  the Software, and to permit persons to whom the Software is furnished to do so,
    11  subject to the following conditions:
    12  
    13  The above copyright notice and this permission notice shall be included in all
    14  copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    18  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    19  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    20  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    21  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    22  *******************************************************************************/
    23  
    24  package firebirdsql
    25  
    26  import (
    27  	"bytes"
    28  	"container/list"
    29  	"context"
    30  	"database/sql/driver"
    31  	"io"
    32  	"reflect"
    33  	"strings"
    34  )
    35  
    36  type firebirdsqlRows struct {
    37  	ctx             context.Context
    38  	stmt            *firebirdsqlStmt
    39  	currentChunkRow *list.Element
    40  	moreData        bool
    41  	result          []driver.Value
    42  }
    43  
    44  func newFirebirdsqlRows(ctx context.Context, stmt *firebirdsqlStmt, result []driver.Value) *firebirdsqlRows {
    45  	rows := new(firebirdsqlRows)
    46  	rows.ctx = ctx
    47  	rows.stmt = stmt
    48  	rows.result = result
    49  	if stmt.stmtType == isc_info_sql_stmt_select {
    50  		rows.moreData = true
    51  	}
    52  	return rows
    53  }
    54  
    55  func (rows *firebirdsqlRows) Columns() []string {
    56  	columns := make([]string, len(rows.stmt.xsqlda))
    57  	for i, x := range rows.stmt.xsqlda {
    58  		columns[i] = x.aliasname
    59  		if rows.stmt.fc.columnNameToLower {
    60  			columns[i] = strings.ToLower(columns[i])
    61  		}
    62  	}
    63  	return columns
    64  }
    65  
    66  func (rows *firebirdsqlRows) Close() (er error) {
    67  	rows.stmt.Close()
    68  
    69  	return
    70  }
    71  
    72  func (rows *firebirdsqlRows) Next(dest []driver.Value) (err error) {
    73  	if rows.ctx.Err() != nil {
    74  		rows.stmt.fc.wp.opCancel(fb_cancel_raise)
    75  		return rows.ctx.Err()
    76  	}
    77  
    78  	if rows.stmt.stmtType == isc_info_sql_stmt_exec_procedure {
    79  		if rows.result != nil {
    80  			for i, v := range rows.result {
    81  				dest[i] = v
    82  			}
    83  			rows.result = nil
    84  		} else {
    85  			err = io.EOF
    86  		}
    87  
    88  		return
    89  	}
    90  
    91  	if rows.currentChunkRow != nil {
    92  		rows.currentChunkRow = rows.currentChunkRow.Next()
    93  	}
    94  
    95  	if rows.currentChunkRow == nil && rows.moreData == true {
    96  		// Get one chunk
    97  		var chunk *list.List
    98  		err = rows.stmt.fc.wp.opFetch(rows.stmt.stmtHandle, rows.stmt.blr)
    99  		if err != nil {
   100  			return err
   101  		}
   102  		chunk, rows.moreData, err = rows.stmt.fc.wp.opFetchResponse(rows.stmt.stmtHandle, rows.stmt.fc.tx.transHandle, rows.stmt.xsqlda)
   103  
   104  		if err == nil {
   105  			rows.currentChunkRow = chunk.Front()
   106  		} else {
   107  			return
   108  		}
   109  	}
   110  
   111  	if rows.currentChunkRow == nil {
   112  		err = io.EOF
   113  		return
   114  	}
   115  	row, _ := rows.currentChunkRow.Value.([]driver.Value)
   116  	for i, v := range row {
   117  		if rows.stmt.xsqlda[i].sqltype == SQL_TYPE_BLOB && v != nil {
   118  			blobId := v.([]byte)
   119  			var blob []byte
   120  			blob, err = rows.stmt.fc.wp.getBlobSegments(blobId, rows.stmt.fc.tx.transHandle)
   121  			if rows.stmt.xsqlda[i].sqlsubtype == 1 {
   122  				dest[i] = bytes.NewBuffer(blob).String()
   123  			} else {
   124  				dest[i] = blob
   125  			}
   126  
   127  		} else {
   128  			dest[i] = v
   129  		}
   130  	}
   131  
   132  	return
   133  }
   134  
   135  func (rows *firebirdsqlRows) ColumnTypeDatabaseTypeName(index int) string {
   136  	return rows.stmt.xsqlda[index].typename()
   137  }
   138  
   139  func (rows *firebirdsqlRows) ColumnTypeLength(index int) (length int64, ok bool) {
   140  	return int64(rows.stmt.xsqlda[index].displayLength()), true
   141  }
   142  
   143  func (rows *firebirdsqlRows) ColumnTypeNullable(index int) (nullable bool, ok bool) {
   144  	return rows.stmt.xsqlda[index].null_ok, true
   145  }
   146  
   147  func (rows *firebirdsqlRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
   148  	return int64(rows.stmt.xsqlda[index].displayLength()), int64(rows.stmt.xsqlda[index].sqlscale), rows.stmt.xsqlda[index].hasPrecisionScale()
   149  }
   150  
   151  func (rows *firebirdsqlRows) ColumnTypeScanType(index int) reflect.Type {
   152  	return rows.stmt.xsqlda[index].scantype()
   153  }