github.com/pavlo67/common@v0.5.3/common/sqllib/sqllib_sqlite/queries.go (about)

     1  package sqllib_sqlite
     2  
     3  //var reIndexLimit = regexp.MustCompile(`\(\d+\)`)
     4  
     5  //func (_ SQLite) CreateSQL(table config.SQLTable) (string, error) {
     6  //
     7  //	//reText := regexp.MustCompile("text")
     8  //	//reTime := regexp.MustCompile("(datetime|timestamp)")
     9  //
    10  //	sqlQuery := "create table `" + table.Name + "` ( "
    11  //	firsFieldAdded := false
    12  //
    13  //	for _, f := range table.Fields {
    14  //		if firsFieldAdded {
    15  //			sqlQuery += ", \n"
    16  //		}
    17  //		sqlQuery += "`" + f.Name + "` " + f.Type + " "
    18  //		if !f.Null {
    19  //			sqlQuery += " NOT NULL "
    20  //		} else {
    21  //			sqlQuery += " NULL "
    22  //		}
    23  //
    24  //		if f.Default != "" {
    25  //			sqlQuery += " DEFAULT '" + f.Default + "' "
    26  //		}
    27  //
    28  //		if f.Extra != "" {
    29  //			sqlQuery += f.Extra + " "
    30  //		}
    31  //		firsFieldAdded = true
    32  //	}
    33  //	for _, i := range table.Indexes {
    34  //		sqlQuery += ", \n"
    35  //		if strings.ToUpper(i.Type) == "PRIMARY" {
    36  //			sqlQuery += "PRIMARY KEY  ("
    37  //			liF := 0
    38  //			for _, f := range i.Fields {
    39  //				if liF > 0 {
    40  //					sqlQuery += ", "
    41  //				}
    42  //				sqlQuery += "`" + f + "`"
    43  //				liF++
    44  //			}
    45  //			sqlQuery += ")"
    46  //		}
    47  //		if strings.ToUpper(i.Type) == "UNIQUE" || i.Type == "" {
    48  //			if i.Type == strings.ToUpper("UNIQUE") {
    49  //				sqlQuery += "UNIQUE KEY `" + i.Name + "` ("
    50  //			} else {
    51  //				sqlQuery += "KEY `" + i.Name + "` USING BTREE ("
    52  //			}
    53  //			liF := 0
    54  //			for _, f := range i.Fields {
    55  //				if liF > 0 {
    56  //					sqlQuery += ", "
    57  //				}
    58  //				if reIndexLimit.MatchString(f) {
    59  //					sqlQuery += f
    60  //				} else {
    61  //					sqlQuery += "`" + f + "`"
    62  //				}
    63  //				liF++
    64  //			}
    65  //			sqlQuery += ")"
    66  //		}
    67  //	}
    68  //	sqlQuery += ");"
    69  //
    70  //	return sqlQuery, nil
    71  //}
    72  //
    73  //func (_ SQLite) TableExistsSQL(tableName string) (string, error) {
    74  //	tableName = strings.TrimSpace(tableName)
    75  //	if tableName == "" {
    76  //		return "", errors.New("empty table name")
    77  //	}
    78  //
    79  //	return "SELECT name FROM sqlite_master WHERE type ='table' AND name = '" + tableName + "'", nil
    80  //
    81  //	// AND name NOT LIKE 'sqlite_%'
    82  //}