A C++ DAL / ORM code generation framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
11 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. WORM - DAL / ORM code generation framework
  2. Copyright (C) 2011 Erik Winn (sidewalksoftware at gmail dot com)
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. 2013: Version 0.2.1
  8. WORM is a Data Abstraction Layer for access to database servers and an
  9. Object Relational Mapping system. It consists of libraries providing a
  10. generic API for database access and database metadata mapping of schema
  11. structure. On top of this there is a tool to generate C++ class files for a
  12. given arbitrary database schema - "wormgen". wormgen accepts a few
  13. arguments setting the database to use and optionally username, password,
  14. output directory, etc. - "wormgen -h" shows usage. See "USING WORM"
  15. below for more information.
  16. Why "Yet Another ORM" - simple: I could not find a C++ ORM class
  17. generator for linux that would take the schema from an existing
  18. database and generate the ORM layer. Everything I found did things
  19. like force one to create an XML description of the schema from which
  20. both SQL and C++ would be generated. Or do the database in C++
  21. classes first (with some special syntax or #pragmas) .. I like SQL - it is
  22. a good language for design, I frequently do the schema (aka "Model")
  23. first when I am designing and then derive the architecture from that.
  24. I didn't want to be forced to do that in OO - just a personal design
  25. preference (aka "Model Driven Architecture"). Erm, plus I happened
  26. to _have_ an existing database and wanted to port an app from PHP
  27. to C++ .. why do what you could do manually in 3 days by hand
  28. when you can spend a month automating it? Really - which is going
  29. to be more fun, huh?
  30. Hence, libworm and wormgen.
  31. At this writing the framework only supports SQLite3 and MySQL
  32. databases. and the default output supports Wt's Dbo library.
  33. (Web Toolkit http://webtoolkit.eu) It generates header files that
  34. (with a little adjustment sometimes) will work with Wt::Dbo for
  35. existing database schemata. There are also example templates that
  36. generate *.h/*.cpp pairs containing CRUD methods for Qt and
  37. generic C++ with SQL (See below under "Templates")
  38. The output is configurable using the WORM template system which uses
  39. google's ctemplate for output templating.
  40. Additionally the libraries provide a general API for basic database access
  41. including query execution with structured result sets and runtime metadata
  42. including column information such as name, data type, defaults, extras, etc.
  43. NOTE: This API is not fully stable! I expect there to be some changes and
  44. do not yet recommend building off the API for anything serious. wormgen
  45. is functional (if simple) and changes there should not cause problems but
  46. the libraries are still under development. There may be modifications
  47. and these should be expected until this message is removed.
  48. You have been warned :).
  49. NOTE: THIS IS UNTESTED ON WINDOWS or MAC AND NOT LIKELY
  50. TO WORK CURRENTLY..
  51. linux or Unices should be ok.
  52. Requirements:
  53. * cmake, make, gcc, libstdc++, stl
  54. * SQlite3 libraries - you may comment out the sqlite driver in
  55. src/CMakeLists.txt if you don't want SQlite3 support
  56. * libmysqlclient - same applies, you may comment this out
  57. * ctemplate 1.0; libctemplate (currently the no threads version - this can
  58. be changed in the cmake files)
  59. Build Instructions:
  60. You can either clone the repository with git:
  61. git clone https://github.com/erikwinn/worm.git
  62. - which will create a directory named "worm" in your current working
  63. directory.
  64. Or, download a tar.gz and do(the number corresponds to the
  65. current version):
  66. tar xzvf erikwinn-worm-79a2021.tar.gz
  67. mv erikwinn-worm-79a2021 worm
  68. cd worm
  69. cd ./build && cmake ../
  70. make
  71. This will build the libraries and wormgen under build/src. By default
  72. the static library is built - you can uncomment the shared library in
  73. src/CMakeLists.txt and comment out the static to change this
  74. (todo: add a configure script ..)
  75. libraries: libworm.so libworm.a
  76. binary: wormgen
  77. Installation(optional):
  78. You must be root to install.
  79. make install (OR) sudo make install
  80. ldconfig
  81. * This will install the libraries to /usr/local/lib/libworm.* and the
  82. * binary to /usr/local/bin/wormgen. Code generation templates will
  83. * be installed to /usr/local/share/worm/
  84. NOTE: You can also use wormgen without installing by doing something
  85. like this if it was built with the shared libraries:
  86. (use a full path to work outside this directory):
  87. export LD_LIBRARY_PATH=./build/src
  88. ./build/src/wormgen
  89. Uninstall:
  90. rm /usr/local/bin/wormgen
  91. rm /usr/local/lib/libworm*
  92. rm -rf /usr/local/share/worm
  93. Alternately you can do:
  94. cat install_manifest.txt | xargs rm
  95. *************** USING WORM *******************
  96. wormgen Usage:
  97. wormgen -h gives help on usage.
  98. The database argument is the only required argument - you must
  99. have a database already set up and access to it. By default it assumes
  100. the username is root with no password - for an SQlite3 database pass
  101. the filename as the argument for -d database; eg.:
  102. wormgen -d ./examples/blog.db
  103. This will generate files in the current directory - to specify a different
  104. output directory use:
  105. wormgen -o /path/to/output
  106. Other commandline arguments include:
  107. -D driver ([default]mysql or sqlite)
  108. -t template directory (default ./src/orm/templates)
  109. -u username
  110. -p password
  111. -h help - use this to see all the arguments ..
  112. Output:
  113. The output can be configured by editing or creating new template files
  114. in the template directory - any files in this directory are assumed to be
  115. templates to use. These are the supported template types:
  116. base class header - generates FooBase.h:
  117. ClassDeclarationBase, //class_declaration_base.tpl
  118. base class source - generates FooBase.cpp:
  119. ClassDefinitionBase, //class_definition_base.tpl
  120. class header - generates Foo.h:
  121. ClassDeclaration, //class_declaration.tpl
  122. class source - generates Foo.cpp:
  123. ClassDefinition, //class_definition.tpl
  124. These are not yet implemented but will be soon (read someday..):
  125. EditViewDeclarationBase, //edit_view_declaration_base.tpl
  126. EditViewDeclaration, //edit_view_declaration.tpl
  127. EditViewDefinitionBase, //edit_view_definition_base.tpl
  128. EditViewDefinition, //edit_view_definition.tpl
  129. ListViewDeclarationBase, //list_view_declaration_base.tpl
  130. ListViewDeclaration, //list_view_declaration.tpl
  131. ListViewDefinitionBase, //list_view_definition_base.tpl
  132. ListViewDefinition //list_view_definition.tpl
  133. Database and class naming conventions:
  134. A table in the database should be named in lowercase with underscores
  135. for best results (Note: this is optional but will avoid naming conflicts
  136. - wormgen will still generate files for camel case naming schema).
  137. Eg: some_persons will be transformed to a class SomePerson. Columns
  138. like first_name will transform to firstName. Underscores for private
  139. variables, accessors, etc can be established in the templates. Table names
  140. should be plural and will be transformed to singular class names -
  141. eg. users table becomes class User. Wormgen tries to be clever about
  142. irregulars (eg. fly -> flies) - but isn't yet brilliant and only speaks english,
  143. so ..
  144. *************** USING WORM TEMPLATES *******************
  145. Templates:
  146. (examples are under <worm_directory_where_ever>/src/orm/templates.)
  147. The templates use ctemplate and currently support the following tags:
  148. ******* Top Level ********
  149. (These may be used at any level)
  150. "CLASS_NAME"
  151. "TABLE_NAME"
  152. ****** Sections (lists) *******
  153. "BELONGS_TO" = appears when a class has a many to one relationship with another class
  154. "COLUMNS" = contains a list of COLUMN dictionaries
  155. "HAS_MANY" = appears when a class has a one to many relationship with another class
  156. "INCLUDES" = #include <string>, #include "footableclass.h", etc. (hopefully correctly ..)
  157. "FORWARD_DECLARATIONS" = forward declarations when there are pointers in a header
  158. "PK_SECTION" = appears when a table has an identifiable primary key. Note: compound keys are not handled - wormgen will simply generate multiple versions of the section and you will have to select/edit as needed
  159. "UNSUPPORTED" = appears when an unsupported variable type is encountered (eg. BLOB)
  160. ******* List Items ***********
  161. In COLUMNS:
  162. "CURRENT_COLUMN_NUMBER" = the current column number (0...COLUMN_COUNT - 1)
  163. "COLUMN_COUNT" = number of colums found in table. This also holds CURRENT_COLUMN_NUMBER + 1 during COLUMNS loops
  164. "COLUMN_NAME" = eg. "user_name"
  165. "COLUMNS_separator" = Actually a section - strings inside will be repeated for every column except the last.
  166. "DATATYPE" = int, char, short, std::string, etc.
  167. "FOREIGNKEY_CLASSNAME" = eg. Session (foreign key in users table) -- one to many; HAS_MANY section
  168. "FOREIGNKEY_CLASS_PLURAL" = eg. Sessions
  169. "REFERENCED_CLASSNAME" = eg. UserType (foreign key to user in user_type table) -- many to one; BELONGS_TO section
  170. "REFERENCED_TABLENAME" = eg. "user_type" -- many to one; BELONGS_TO section
  171. "REFERENCED_VARIABLE_NAME" = eg. "userTypeId" - the foreign key variable -- many to one; BELONGS_TO section
  172. "VARIABLE_NAME" = eg. userName
  173. "VARIABLE_GETTOR" = eg. getUserName
  174. "VARIABLE_SETTOR" = eg setUserName
  175. "UNSIGNED" = Actually a section, appears if variable is unsigned ..
  176. In INCLUDES:
  177. "INCLUDE" = eg. "#include usertype.h"
  178. In PK_SECTION:
  179. "PK_VARIABLE_NAME" = the class member that represents the primary key column, eg. userName or userId
  180. "PK_COLUMN_NAME" = the database name the primary key column, eg. user_name or user_id
  181. Note that multi column keys will generate duplicate sections for each key - edit/select as needed in the generated classes (or don't use multi column keys)
  182. There are several example templates under:
  183. <worm_directory_where_ever>/src/orm/templates.
  184. These show some tag usage possibilities - Note however that the templates
  185. probably won't work "out of the box". They will do most of the heavy lifting
  186. of ORM boilerplate but the generated classes will likely need at least some
  187. tweaking to work, YMMV. However, since the output _is_ templated it is of
  188. course possible to tweak the templates themselves or create new ones - maybe
  189. even a perfect "out of the box" example (which would be a welcome
  190. contribution).
  191. The out put is not restricted to C++ (except that currently wormgen will
  192. create std::string variables for VARCHAR - this may change to be more
  193. flexible in future).
  194. The current default template generates Wt::Dbo object headers.
  195. A more thorough introduction to ctemplate syntax is available at the
  196. ctemplate website ( http://ctemplate.googlecode.com ).
  197. *************** USING WORM LIBRARY (libworm) *******************
  198. Library Usage and Documentation:
  199. There are some small example programs under examples/ - I recommend
  200. starting with these. Also, the code is heavily commented - and there are
  201. copious amounts of HTML formatted documentation under doc/html.
  202. Although wormgen itself is now pretty stable the library is still mostly a
  203. Pull model and needs more work to be useful in a general sense - still you
  204. may find use for it. Documentation for library usage will probably appear
  205. sometime after the library itself is more complete.
  206. peace,
  207. -e