This creates SQL statements that correspond to a table's schema. These statements are used to bootstrap sqlite. */
| 1858 | /* This creates SQL statements that correspond to a table's schema. These |
| 1859 | statements are used to bootstrap sqlite. */ |
| 1860 | static int create_sqlmaster_record(struct dbtable *tbl, void *tran) |
| 1861 | { |
| 1862 | int field; |
| 1863 | char namebuf[128]; |
| 1864 | char *tablename = tbl->sqlaliasname ? tbl->sqlaliasname : tbl->tablename; |
| 1865 | |
| 1866 | struct schema *schema = tbl->schema; |
| 1867 | if (schema == NULL) { |
| 1868 | logmsg(LOGMSG_ERROR, "No .ONDISK tag for table %s.\n", tablename); |
| 1869 | return -1; |
| 1870 | } |
| 1871 | |
| 1872 | if (is_sqlite_stat(tablename)) { |
| 1873 | create_sqlite_stat_sqlmaster_record(tbl); |
| 1874 | return 0; |
| 1875 | } |
| 1876 | |
| 1877 | strbuf *sql = strbuf_new(); |
| 1878 | strbuf_clear(sql); |
| 1879 | strbuf_appendf(sql, "create table \"%s\"(", tablename); |
| 1880 | |
| 1881 | /* Fields */ |
| 1882 | for (field = 0; field < schema->nmembers; field++) { |
| 1883 | char *type = sqltype(&schema->member[field], namebuf, sizeof(namebuf)); |
| 1884 | if (type == NULL) { |
| 1885 | logmsg(LOGMSG_ERROR, |
| 1886 | "Unsupported type in schema: column '%s' [%d] " |
| 1887 | "table %s\n", |
| 1888 | schema->member[field].name, field, tablename); |
| 1889 | strbuf_free(sql); |
| 1890 | return -1; |
| 1891 | } |
| 1892 | strbuf_appendf(sql, "\"%s\" %s", schema->member[field].name, type); |
| 1893 | /* add defaults for write sql */ |
| 1894 | if (schema->member[field].in_default) { |
| 1895 | strbuf_append(sql, " DEFAULT"); |
| 1896 | char *dstr = sql_field_default_trans(&schema->member[field], 0); |
| 1897 | |
| 1898 | if (dstr) { |
| 1899 | strbuf_appendf(sql, " %s", dstr); |
| 1900 | sqlite3_free(dstr); |
| 1901 | } else { |
| 1902 | logmsg(LOGMSG_ERROR, |
| 1903 | "Failed to convert default value column '%s' table " |
| 1904 | "%s type %d\n", |
| 1905 | schema->member[field].name, tablename, |
| 1906 | schema->member[field].type); |
| 1907 | strbuf_free(sql); |
| 1908 | return -1; |
| 1909 | } |
| 1910 | } |
| 1911 | |
| 1912 | if (field != schema->nmembers - 1) |
| 1913 | strbuf_append(sql, ", "); |
| 1914 | } |
| 1915 | |
| 1916 | /* CHECK constraints */ |
| 1917 | for (int i = 0; i < tbl->n_check_constraints; i++) { |
no test coverage detected