Changeset 10737 for trunk/psLib/src/db/psDB.c
- Timestamp:
- Dec 14, 2006, 1:03:40 PM (19 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/db/psDB.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/db/psDB.c
r10708 r10737 12 12 * @author Joshua Hoblitt 13 13 * 14 * @version $Revision: 1.12 2$ $Name: not supported by cvs2svn $15 * @date $Date: 2006-12-14 05:13:58$14 * @version $Revision: 1.123 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2006-12-14 23:03:40 $ 16 16 * 17 17 * Copyright (C) 2005-2006 Joshua Hoblitt, University of Hawaii … … 1864 1864 } 1865 1865 1866 typedef enum { 1867 PS_DB_OP_EQ, 1868 PS_DB_OP_LT, 1869 PS_DB_OP_GT, 1870 PS_DB_OP_LE, 1871 PS_DB_OP_GE, 1872 } psDBOpValue; 1873 1874 # define PS_DB_FLT_PAD (FLT_EPSILON * 10) 1875 # define PS_DB_DBL_PAD (DBL_EPSILON * 10) 1876 1866 1877 static psString psDBGenerateConditionalSQL(const psMetadataItem *item, const char *tableName) 1867 1878 { … … 1890 1901 1891 1902 // default to exact match ('=') 1892 char *op = "="; 1903 char *opStr = "="; 1904 psDBOpValue op = PS_DB_OP_EQ; 1893 1905 if (item->comment) { 1894 1906 // arbitrary semantic, precedence is: >=, <=, >, <, = (default) 1895 1907 if (strstr(item->comment, ">=")) { 1896 op = ">="; 1908 opStr = ">="; 1909 op = PS_DB_OP_GE; 1897 1910 } else if (strstr(item->comment, "<=")) { 1898 op = "<="; 1911 opStr = "<="; 1912 op = PS_DB_OP_LE; 1899 1913 } else if (strstr(item->comment, ">")) { 1900 op = ">"; 1914 opStr = ">"; 1915 op = PS_DB_OP_GT; 1901 1916 } else if (strstr(item->comment, "<")) { 1902 op = "<"; 1903 } 1904 } 1917 opStr = "<"; 1918 op = PS_DB_OP_LT; 1919 } 1920 } 1921 1922 // XXX why are >, < searches not supported here???? 1923 // fix this immediately!! 1924 // for datetime comparisons, we need to use single-quotes around the string value: 1925 // where dateobs < '2002-09-06' and dateobs > '2002-09-05,15:40:22' 1905 1926 1906 1927 switch (item->type) { 1907 1928 case PS_DATA_S32: 1908 psStringAppend(&query, "%s %s %d", itemName, op, (int)(item->data.S32)); 1929 // the raw opStr is good enough in the query 1930 psStringAppend(&query, "%s %s %d", itemName, opStr, (int)(item->data.S32)); 1909 1931 break; 1910 1932 case PS_DATA_F32: 1911 psStringAppend(&query, "(ABS(%s - %.8f) < %.8f)", itemName, (float)(item->data.F32), FLT_EPSILON * 10); 1933 // need to handle floating-point round-off issues (use a padding of 10*FLT_EPSILON) 1934 switch (op) { 1935 case PS_DB_OP_EQ: 1936 psStringAppend(&query, "(ABS(%s - %.8f) < %.8f)", itemName, (float)(item->data.F32), PS_DB_FLT_PAD); 1937 break; 1938 case PS_DB_OP_LE: 1939 case PS_DB_OP_LT: 1940 // A < B becomes A < B + epsilon 1941 psStringAppend(&query, "(%s < %.8f + %.8f)", itemName, (float)(item->data.F32), PS_DB_FLT_PAD); 1942 break; 1943 case PS_DB_OP_GE: 1944 case PS_DB_OP_GT: 1945 // A > B becomes A > B - epsilon 1946 psStringAppend(&query, "(%s > %.8f - %.8f)", itemName, (float)(item->data.F32), PS_DB_FLT_PAD); 1947 break; 1948 } 1912 1949 break; 1913 1950 case PS_DATA_F64: 1914 psStringAppend(&query, "(ABS(%s - %.17f) < %.17f)", itemName, (double)(item->data.F64), DBL_EPSILON * 10); 1951 // need to handle floating-point round-off issues (use a padding of 10*FLT_EPSILON) 1952 switch (op) { 1953 case PS_DB_OP_EQ: 1954 psStringAppend(&query, "(ABS(%s - %.17f) < %.17f)", itemName, (float)(item->data.F64), PS_DB_DBL_PAD); 1955 break; 1956 case PS_DB_OP_LE: 1957 case PS_DB_OP_LT: 1958 // A < B becomes A < B + epsilon 1959 psStringAppend(&query, "(%s < %.17f + %.17f)", itemName, (float)(item->data.F64), PS_DB_DBL_PAD); 1960 break; 1961 case PS_DB_OP_GE: 1962 case PS_DB_OP_GT: 1963 // A > B becomes A > B - epsilon 1964 psStringAppend(&query, "(%s > %.17f - %.17f)", itemName, (float)(item->data.F64), PS_DB_DBL_PAD); 1965 break; 1966 } 1915 1967 break; 1916 1968 case PS_DATA_BOOL: 1917 psStringAppend(&query, "%s = %d", itemName, (int)(item->data.B)); 1969 switch (op) { 1970 case PS_DB_OP_EQ: 1971 psStringAppend(&query, "%s = %d", itemName, (int)(item->data.B)); 1972 break; 1973 default: 1974 psError(PS_ERR_UNKNOWN, true, "NULL psBool can't be compared with any operator other than '=='"); 1975 psFree(itemName); 1976 return NULL; 1977 } 1918 1978 break; 1919 1979 case PS_DATA_STRING: 1980 // XXX EAM : probably need to add some regex-like operations here 1920 1981 // + column name + _ + like + _ + ' + value + ' 1921 1982 // check for NULL and empty ("") strings … … 1937 1998 if (item->data.V) { 1938 1999 psString timeStr = psTimeToISO(item->data.V); 1939 psStringAppend(&query, "%s %s '%s'", itemName, op , timeStr);2000 psStringAppend(&query, "%s %s '%s'", itemName, opStr, timeStr); 1940 2001 psFree(timeStr); 1941 } else if (strstr(op , "=")) {2002 } else if (strstr(opStr, "=")) { 1942 2003 psStringAppend(&query, "%s IS NULL", itemName); 1943 2004 } else { 1944 psError(PS_ERR_UNKNOWN, true, " NULL psTime can't be compared with any operator other than '=='");2005 psError(PS_ERR_UNKNOWN, true, "psTime comparison value is NULL: A NULL psTime value can't be compared with any operator other than '=='"); 1945 2006 psFree(itemName); 1946 2007 return NULL;
Note:
See TracChangeset
for help on using the changeset viewer.
