Esempio n. 1
0
func (dc *sqlite3Dialect) SchemaColumnTypeSql(col *base.Column) string {

	sql, ok := columnTypes[col.Type]
	if !ok {
		return dc.QuoteStr(col.Name) + col.Type
		//, errors.New("Unsupported column type `" + col.Type + "`")
	}

	switch col.Type {
	case "string":
		sql = fmt.Sprintf(sql, col.Length)
	case "float64-decimal":
		lens := strings.Split(col.Length, ",")
		if lens[0] == "" {
			lens[0] = "10"
		}
		if len(lens) < 2 {
			lens = append(lens, "2")
		}
		sql = fmt.Sprintf(sql, lens[0], lens[1])
	}

	if base.ArrayContain("notnull", col.Extra) {
		sql += " NOT NULL"
	} else {
		sql += " NULL"
	}

	if base.ArrayContain("PRIMARY", col.Extra) {

		if col.IncrAble {
			sql += " PRIMARY KEY AUTOINCREMENT"
		} else {
			sql += " PRIMARY KEY"
		}
	}

	// INTEGER

	if col.Default != "" {
		if col.Default == "null" {
			sql += " DEFAULT NULL"
		} else {
			sql += " DEFAULT '" + col.Default + "'"
		}
	}

	return dc.QuoteStr(col.Name) + " " + sql
}
Esempio n. 2
0
func (dc *sqlite3Dialect) SchemaTableQuery(dbName string) ([]*base.Table, error) {

	tables := []*base.Table{}

	q := "SELECT name FROM sqlite_master WHERE type='table'"

	rows, err := dc.Base().Conn.Query(q)
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	for rows.Next() {

		var name string
		if err = rows.Scan(&name); err != nil {
			return nil, err
		}

		if name == "sqlite_sequence" {
			continue
		}

		idxs, _ := dc.SchemaIndexQuery(dbName, name)

		cols, _ := dc.SchemaColumnQuery(dbName, name)
		// Patch for sqlite3
		for _, v := range cols {
			if base.ArrayContain("PRIMARY", v.Extra) {
				idxs = append(idxs, base.NewIndex("PRIMARY", base.IndexTypePrimaryKey).AddColumn(v.Name))
				break
			}
		}

		tables = append(tables, &base.Table{
			Name:    name,
			Columns: cols,
			Indexes: idxs,
		})
	}

	return tables, nil
}