sqlp.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* SQL Parser */
  2. /* KEYWORD OPS */
  3. /* SQL COMMANDS */
  4. #define SQLP_CREATE 1
  5. #define SQLP_DROP 2
  6. #define SQLP_INSERT 3
  7. #define SQLP_SELECT 4
  8. #define SQLP_UPDATE 5
  9. #define SQLP_DELETE 6
  10. #define SQLP_ADD_COLUMN 7
  11. #define SQLP_DROP_COLUMN 8
  12. /* SQL OPERATORS */
  13. /* Arithmetical */
  14. #define SQLP_ADD 1 /* + */
  15. #define SQLP_SUBTR 2 /* - */
  16. #define SQLP_MLTP 3 /* * */
  17. #define SQLP_DIV 4 /* / */
  18. /* Comparison */
  19. #define SQLP_EQ 11 /* = */
  20. #define SQLP_LT 12 /* < */
  21. #define SQLP_LE 13 /* <= */
  22. #define SQLP_GT 14 /* > */
  23. #define SQLP_GE 15 /* >= */
  24. #define SQLP_NE 16 /* <> */
  25. #define SQLP_MTCH 17 /* ~ */
  26. #define SQLP_ISNULL 18 /* IS NULL */
  27. #define SQLP_NOTNULL 19 /* IS NULL */
  28. /* Logical */
  29. #define SQLP_AND 21
  30. #define SQLP_OR 22
  31. #define SQLP_NOT 23
  32. /* SQL VALUE TYPES, NOT COLUMN TYPES */
  33. #define SQLP_NULL 1 /* value NULL -> unknown type */
  34. #define SQLP_S 2 /* string */
  35. #define SQLP_I 3 /* integer */
  36. #define SQLP_D 4 /* float */
  37. #define SQLP_BOOL 5 /* used only for type of expression */
  38. #define SQLP_EXPR 6 /* expression XXX */
  39. /* SQL COLUMN TYPES */
  40. #define SQLP_VARCHAR 1
  41. #define SQLP_INTEGER 2
  42. #define SQLP_DOUBLE 3
  43. #define SQLP_DATE 4
  44. #define SQLP_TIME 5
  45. #define SQLP_MAX_TABLE 200
  46. #define SQLP_MAX_ERR 500
  47. /* Condition node */
  48. #define SQLP_NODE_COLUMN 1
  49. #define SQLP_NODE_VALUE 2
  50. #define SQLP_NODE_EXPRESSION 3
  51. /* Order direction */
  52. #define SORT_ASC 1
  53. #define SORT_DESC 2
  54. typedef struct
  55. {
  56. int type; /* SQLP_S, SQLP_I, SQLP_D, SQLP_NULL, SQL_EXPR */
  57. char *s; /* pointer to string or NULL */
  58. int i;
  59. double d;
  60. struct sqlpnode *expr;
  61. } SQLPVALUE;
  62. typedef struct sqlpnode
  63. {
  64. int node_type; /* Node type: SQLP_NODE_COLUMN, SQLP_NODE_VALUE, SQLP_NODE_EXPRESSION */
  65. int oper; /* Operator code */
  66. struct sqlpnode *left; /* left argument, sometimes NULL */
  67. struct sqlpnode *right; /* right argument, sometimes NULL */
  68. char *column_name;
  69. SQLPVALUE value;
  70. } SQLPNODE;
  71. typedef struct
  72. {
  73. char *stmt; /* input statement string */
  74. char *cur; /* cursor for parser */
  75. char errmsg[SQLP_MAX_ERR + 1];
  76. int command;
  77. char table[SQLP_MAX_TABLE + 1];
  78. SQLPVALUE *Col; /* column names */
  79. int *ColType;
  80. int *ColWidth; /* length */
  81. int *ColDecim; /* decimals */
  82. int aCol; /* allocated */
  83. int nCol; /* number of columns */
  84. SQLPVALUE *Val; /* values */
  85. int aVal;
  86. int nVal;
  87. SQLPNODE *upperNodeptr;
  88. char *orderCol; /* column name which should be used for sorting (ORDER BY) or NULL (no sorting) */
  89. int orderDir; /* direction of ordering (ASC or DESC) */
  90. } SQLPSTMT;
  91. int my_yyinput(char *buf, int max_size);
  92. void yyerror(char *s);
  93. int yyparse();
  94. int yywrap();
  95. int sqpSaveStr(SQLPVALUE * st, char *c);
  96. void sqpInitValue(SQLPVALUE * val);
  97. void sqpCopyValue(SQLPVALUE * from, SQLPVALUE * to);
  98. SQLPSTMT *sqpInitStmt(void);
  99. int sqpFreeStmt(SQLPSTMT * st);
  100. int sqpPrintStmt(SQLPSTMT * st);
  101. int sqpAllocCol(SQLPSTMT * st, int n);
  102. int sqpAllocVal(SQLPSTMT * st, int n);
  103. int sqpAllocCom(SQLPSTMT * st, int n);
  104. int sqpInitParser(SQLPSTMT * st);
  105. void sqpCommand(int command);
  106. void sqpTable(char *table);
  107. void sqpColumn(char *column);
  108. void sqpColumnDef(char *column, int type, int width, int decimals);
  109. void sqpValue(char *strval, int intval, double dblval, int type);
  110. void sqpAssignment(char *column, char *strval, int intval, double dblval,
  111. SQLPNODE * expr, int type);
  112. void sqpOrderColumn(char *col, int dir);
  113. int sqpOperatorCode(char *);
  114. char *sqpOperatorName(int);
  115. SQLPNODE *sqpNewNode(void);
  116. SQLPNODE *sqpNewExpressionNode(int oper, SQLPNODE * left, SQLPNODE * right);
  117. SQLPNODE *sqpNewColumnNode(char *name);
  118. SQLPNODE *sqpNewValueNode(char *strval, int intval, double dblval, int type);
  119. void sqpFreeNode(SQLPNODE *);
  120. extern SQLPSTMT *sqlpStmt;