@@ -962,25 +962,36 @@ func EscapeValue(val DataValue) string {
962
962
}
963
963
}
964
964
965
- // Figure out the primary key columns of a table.
965
+ // Figure out the primary key columns and the other columns of a table.
966
966
// The schema and table parameters specify the schema and table names to use.
967
- func GetPrimaryKeyColumns (sdb * sqlite.Conn , schema string , table string ) (pks []string , err error ) {
967
+ // This function returns two arrays: One containing the list of primary key columns in the same order as they
968
+ // are used in the primary key. The other array contains a list of all the other, non-primary key columns.
969
+ // Generated columns are ignored completely. If the primary key exists only implicitly, i.e. it's the rowid
970
+ // column, the implicit_pk flag is set to true.
971
+ func GetPrimaryKeyAndOtherColumns (sdb * sqlite.Conn , schema string , table string ) (pks []string , implicit_pk bool , other []string , err error ) {
968
972
// Prepare query
969
973
var stmt * sqlite.Stmt
970
974
stmt , err = sdb .Prepare ("PRAGMA " + EscapeId (schema ) + ".table_info(" + EscapeId (table ) + ")" )
971
975
if err != nil {
972
- log .Printf ("Error when preparing statement in GetPrimaryKey (): %s\n " , err )
973
- return nil , err
976
+ log .Printf ("Error when preparing statement in GetPrimaryKeyAndOtherColumns (): %s\n " , err )
977
+ return nil , false , nil , err
974
978
}
975
979
defer stmt .Finalize ()
976
980
977
- // Execute query and retrieve all primary key columns
981
+ // Execute query and retrieve all columns
978
982
primaryKeyColumns := make (map [int ]string )
979
983
var hasColumnRowid , hasColumn_Rowid_ , hasColumnOid bool
980
984
err = stmt .Select (func (s * sqlite.Stmt ) error {
985
+ // Get name and primary key order
981
986
columnName , _ := s .ScanText (1 )
982
987
pkOrder , _ , _ := s .ScanInt (5 )
983
- if pkOrder > 0 {
988
+
989
+ // Is this column part of the primary key?
990
+ if pkOrder == 0 {
991
+ // It's not
992
+ other = append (other , columnName )
993
+ } else {
994
+ // It is
984
995
primaryKeyColumns [pkOrder ] = columnName
985
996
}
986
997
@@ -995,15 +1006,17 @@ func GetPrimaryKeyColumns(sdb *sqlite.Conn, schema string, table string) (pks []
995
1006
return nil
996
1007
})
997
1008
if err != nil {
998
- log .Printf ("Error when retrieving rows in GetPrimaryKey (): %s\n " , err )
999
- return nil , err
1009
+ log .Printf ("Error when retrieving rows in GetPrimaryKeyAndOtherColumns (): %s\n " , err )
1010
+ return nil , false , nil , err
1000
1011
}
1001
1012
1002
1013
// Did we get any primary key columns? If not, this table has only an implicit primary key which
1003
1014
// is accessible by the name rowid, _rowid_, or oid
1004
1015
if len (primaryKeyColumns ) > 0 {
1005
1016
// Explicit primary key
1006
1017
1018
+ implicit_pk = false
1019
+
1007
1020
// Sort the columns by their order in the PK
1008
1021
keys := make ([]int , 0 , len (primaryKeyColumns ))
1009
1022
for k := range primaryKeyColumns {
@@ -1017,6 +1030,9 @@ func GetPrimaryKeyColumns(sdb *sqlite.Conn, schema string, table string) (pks []
1017
1030
}
1018
1031
} else {
1019
1032
// Implicit primary key
1033
+
1034
+ implicit_pk = true
1035
+
1020
1036
if ! hasColumnRowid {
1021
1037
pks = append (pks , "rowid" )
1022
1038
} else if ! hasColumn_Rowid_ {
@@ -1025,8 +1041,9 @@ func GetPrimaryKeyColumns(sdb *sqlite.Conn, schema string, table string) (pks []
1025
1041
pks = append (pks , "oid" )
1026
1042
} else {
1027
1043
log .Printf ("Unreachable rowid column in GetPrimaryKey()\n " )
1028
- return nil , errors .New ("Unreachable rowid column" )
1044
+ return nil , false , nil , errors .New ("Unreachable rowid column" )
1029
1045
}
1030
1046
}
1031
- return pks , nil
1047
+
1048
+ return pks , implicit_pk , other , nil
1032
1049
}
0 commit comments