Beispiel #1
0
func (s *source) tableColumns(tableName string) ([]string, error) {

	// Making sure this table is allocated.
	tableSchema := s.schema.Table(tableName)

	if len(tableSchema.Columns) > 0 {
		return tableSchema.Columns, nil
	}

	q := fmt.Sprintf(`PRAGMA TABLE_INFO('%s')`, tableName)

	rows, err := s.doRawQuery(q)

	if s.columns == nil {
		s.columns = make(map[string][]columnSchemaT)
	}

	columns := []columnSchemaT{}

	if err = sqlutil.FetchRows(rows, &columns); err != nil {
		return nil, err
	}

	s.columns[tableName] = columns

	s.schema.TableInfo[tableName].Columns = make([]string, 0, len(s.columns))

	for i := range s.columns[tableName] {
		s.schema.TableInfo[tableName].Columns = append(s.schema.TableInfo[tableName].Columns, s.columns[tableName][i].Name)
	}

	return s.schema.TableInfo[tableName].Columns, nil
}
Beispiel #2
0
Datei: dh.go Projekt: worg/go
func (self *Dh) Count(query string) int64 {
	var errs api.Err
	drv := self.SqlDriver()
	rows, err := drv.Query(query)
	if err != nil {
		errs.Push(api.Msg{
			Field: "Database select All  error: ",
			Error: err.Error(),
		})
		return 0
	}

	type Total struct {
		Count int64 `db:"count"`
	}

	total := []Total{}
	// Mapping to an array.
	if err = sqlutil.FetchRows(rows, &total); err != nil {
		errs.Push(api.Msg{
			Field: "Database select All  error: ",
			Error: err.Error(),
		})
	}

	defer rows.Close()
	return total[0].Count
}
Beispiel #3
0
// Returns a collection instance by name.
func (self *source) Collection(names ...string) (db.Collection, error) {
	var rows *sql.Rows
	var err error

	if len(names) == 0 {
		return nil, db.ErrMissingCollectionName
	}

	col := &table{
		source: self,
		names:  names,
	}

	columns_t := []columnSchema_t{}

	for _, name := range names {
		chunks := strings.SplitN(name, " ", 2)

		if len(chunks) > 0 {

			name = chunks[0]

			if err := self.tableExists(name); err != nil {
				return nil, err
			}

			// Getting columns
			rows, err = self.doQuery(sqlgen.Statement{
				Type:  sqlgen.SqlSelect,
				Table: sqlgen.Table{`information_schema.columns`},
				Columns: sqlgen.Columns{
					{`column_name`},
					{`data_type`},
				},
				Where: sqlgen.Where{
					sqlgen.ColumnValue{sqlgen.Column{`table_catalog`}, `=`, sqlPlaceholder},
					sqlgen.ColumnValue{sqlgen.Column{`table_name`}, `=`, sqlPlaceholder},
				},
			}, self.config.Database, name)

			if err != nil {
				return nil, err
			}

			if err = sqlutil.FetchRows(rows, &columns_t); err != nil {
				return nil, err
			}

			col.Columns = make([]string, 0, len(columns_t))

			for _, column := range columns_t {
				col.Columns = append(col.Columns, column.Name)
			}
		}
	}

	return col, nil
}
Beispiel #4
0
func (s *source) tableColumns(tableName string) ([]string, error) {

	// Making sure this table is allocated.
	tableSchema := s.schema.Table(tableName)

	if len(tableSchema.Columns) > 0 {
		return tableSchema.Columns, nil
	}

	stmt := sqlgen.Statement{
		Type: sqlgen.SqlSelect,
		Table: sqlgen.Table{
			`information_schema.columns`,
		},
		Columns: sqlgen.Columns{
			{`column_name`},
			{`data_type`},
		},
		Where: sqlgen.Where{
			sqlgen.ColumnValue{
				sqlgen.Column{`table_catalog`},
				`=`,
				sqlPlaceholder,
			},
			sqlgen.ColumnValue{
				sqlgen.Column{`table_name`},
				`=`,
				sqlPlaceholder,
			},
		},
	}

	var rows *sql.Rows
	var err error

	if rows, err = s.doQuery(stmt, s.schema.Name, tableName); err != nil {
		return nil, err
	}

	tableFields := []columnSchemaT{}

	if err = sqlutil.FetchRows(rows, &tableFields); err != nil {
		return nil, err
	}

	s.schema.TableInfo[tableName].Columns = make([]string, 0, len(tableFields))

	for i := range tableFields {
		s.schema.TableInfo[tableName].Columns = append(s.schema.TableInfo[tableName].Columns, tableFields[i].Name)
	}

	return s.schema.TableInfo[tableName].Columns, nil
}
func TestRawQuery(t *testing.T) {
	var sess db.Database
	var rows *sql.Rows
	var err error
	var drv *sql.DB

	type publicationType struct {
		ID       int64  `db:"id,omitempty"`
		Title    string `db:"title"`
		AuthorID int64  `db:"author_id"`
	}

	if sess, err = db.Open(Adapter, settings); err != nil {
		t.Fatal(err)
	}

	defer sess.Close()

	drv = sess.Driver().(*sql.DB)

	rows, err = drv.Query(`
		SELECT
			p.id,
			p.title AS publication_title,
			a.name AS artist_name
		FROM
			artist AS a,
			publication AS p
		WHERE
			a.id = p.author_id
	`)

	if err != nil {
		t.Fatal(err)
	}

	var all []publicationType

	if err = sqlutil.FetchRows(rows, &all); err != nil {
		t.Fatal(err)
	}

	if len(all) != 9 {
		t.Fatalf("Expecting some rows.")
	}
}
Beispiel #6
0
// Returns a collection instance by name.
func (self *source) Collection(names ...string) (db.Collection, error) {

	if len(names) == 0 {
		return nil, db.ErrMissingCollectionName
	}

	col := &table{
		source: self,
		names:  names,
	}

	var columns_t []columnSchema_t

	for _, name := range names {
		chunks := strings.SplitN(name, " ", 2)

		if len(chunks) > 0 {

			name = chunks[0]

			if err := self.tableExists(name); err != nil {
				return nil, err
			}

			rows, err := self.doRawQuery(fmt.Sprintf(`PRAGMA TABLE_INFO('%s')`, name))

			if err != nil {
				return nil, err
			}

			if err = sqlutil.FetchRows(rows, &columns_t); err != nil {
				return nil, err
			}

			col.Columns = make([]string, 0, len(columns_t))

			for _, column := range columns_t {
				col.Columns = append(col.Columns, strings.ToLower(column.Name))
			}
		}
	}

	return col, nil
}
Beispiel #7
0
// Dumps all results into a pointer to an slice of structs or maps.
func (r *result) All(dst interface{}) error {
	var err error

	if r.cursor != nil {
		return db.ErrQueryIsPending
	}

	// Current cursor.
	err = r.setCursor()

	if err != nil {
		return err
	}

	defer r.Close()

	// Fetching all results within the cursor.
	err = sqlutil.FetchRows(r.cursor, dst)

	return err
}
Beispiel #8
0
// Returns a collection instance by name.
func (self *source) Collection(names ...string) (db.Collection, error) {

	if len(names) == 0 {
		return nil, db.ErrMissingCollectionName
	}

	col := &table{
		source: self,
		names:  names,
	}

	columns_t := []columnSchema_t{}

	for _, name := range names {
		chunks := strings.SplitN(name, " ", 2)

		if len(chunks) > 0 {

			name = chunks[0]

			if err := self.tableExists(name); err != nil {
				return nil, err
			}

			rows, err := self.doQuery(sqlgen.Statement{
				Type:  sqlgen.SqlSelect,
				Table: sqlgen.Table{`__Column`},
				Columns: sqlgen.Columns{
					{`Name`},
					{`Type`},
				},
				Where: sqlgen.Where{
					sqlgen.ColumnValue{sqlgen.Column{`TableName`}, `==`, sqlPlaceholder},
				},
			}, name)

			if err != nil {
				return nil, err
			}

			if err = sqlutil.FetchRows(rows, &columns_t); err != nil {
				return nil, err
			}

			col.Columns = make([]string, len(columns_t))
			col.columnTypes = make(map[string]reflect.Kind)

			for i, column := range columns_t {

				column.DataType = strings.ToLower(column.DataType)

				col.Columns[i] = column.ColumnName

				// Default properties.
				dtype := column.DataType
				ctype := reflect.String

				// Guessing datatypes.
				switch dtype {
				case `int`:
					ctype = reflect.Int
				case `int8`:
					ctype = reflect.Int8
				case `int16`:
					ctype = reflect.Int16
				case `int32`, `rune`:
					ctype = reflect.Int32
				case `int64`:
					ctype = reflect.Int64
				case `uint`:
					ctype = reflect.Uint
				case `uint8`:
					ctype = reflect.Uint8
				case `uint16`:
					ctype = reflect.Uint16
				case `uint32`:
					ctype = reflect.Uint32
				case `uint64`:
					ctype = reflect.Uint64
				case `float64`:
					ctype = reflect.Float64
				case `float32`:
					ctype = reflect.Float32
				case `time`:
					ctype = timeType
				default:
					ctype = reflect.String
				}

				col.columnTypes[column.ColumnName] = ctype
			}

		}
	}

	return col, nil
}