home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MDBS.ZIP / SQLAUTHO.CLS < prev    next >
Text File  |  1990-02-21  |  12KB  |  390 lines

  1. define(#SQLAUTHORS_AUTHORIDTEXT, 9009);
  2. define(#SQLAUTHORS_AUTHORID, 9003);
  3. define(#SQLAUTHORS_FIRSTNAMETEXT, 9011);
  4. define(#SQLAUTHORS_FIRSTNAME, 9005);
  5. define(#SQLAUTHORS_LASTNAMETEXT, 9010);
  6. define(#SQLAUTHORS_LASTNAME, 9004);
  7. define(#SQLAUTHORS_ADDRESSTEXT, 9012);
  8. define(#SQLAUTHORS_ADDRESS, 9006);
  9. define(#SQLAUTHORS_CITYTEXT, 9013);
  10. define(#SQLAUTHORS_CITY, 9015);
  11. define(#SQLAUTHORS_STATETEXT, 9014);
  12. define(#SQLAUTHORS_STATE, 9016);
  13. define(#SQLAUTHORS_ZIPTEXT, 9017);
  14. define(#SQLAUTHORS_ZIPCODE, 9018);
  15. define(#SQLAUTHORS_TELEPHONETEXT, 9019);$
  16. define(#SQLAUTHORS_PHONE, 9020);
  17. define(#SQLAUTHORS_NEW, 9024);
  18. define(#SQLAUTHORS_FIND, 9021);
  19. define(#SQLAUTHORS_NEXT, 9023);
  20. define(#SQLAUTHORS_UPDATE, 9025);
  21. define(#SQLAUTHORS_QUIT, 9022);$
  22. $
  23.  
  24.  
  25. subclass(SQLForms,
  26.      #SQLAuthors,
  27.          #(updateSession,
  28.            updateResultBuffer,
  29.            updateCommandBuffer),
  30.          true, true, false);
  31. $
  32.  
  33.  
  34. /* Create the form */
  35. method SQLAuthors::initFormSQLAuthors1(self, hPS)
  36. {
  37.     local temp;
  38.  
  39.     temp = new(TextControl,
  40.         asPels(new(Rect, 295, 2657, 1225, 2879), hPS),
  41.         self, SQLAUTHORS_AUTHORIDTEXT, "Author ID",0x10000001);
  42.     addObject(self, temp);
  43.  
  44.     temp = new(EntryField,
  45.         asPels(new(Rect, 1417, 2657, 6998, 2982), hPS),
  46.         self, SQLAUTHORS_AUTHORID, "",0x10010008);
  47.     addObject(self, temp);
  48.     addEventPair(self, #(SQLAUTHORS_AUTHORID, #dummyHandler));
  49.  
  50.     temp = new(TextControl,
  51.         asPels(new(Rect, 3942, 2185, 5123, 2421), hPS),
  52.         self, SQLAUTHORS_FIRSTNAMETEXT, "First Name",0x10000001);
  53.     addObject(self, temp);
  54.  
  55.     temp = new(EntryField,
  56.         asPels(new(Rect, 5300, 2185, 7013, 2509), hPS),
  57.         self, SQLAUTHORS_FIRSTNAME, "",0x10010008);
  58.     addObject(self, temp);
  59.     addEventPair(self, #(SQLAUTHORS_FIRSTNAME, #dummyHandler));
  60.  
  61.     temp = new(TextControl,
  62.         asPels(new(Rect, 295, 2199, 1387, 2436), hPS),
  63.         self, SQLAUTHORS_LASTNAMETEXT, "Last Name",0x10000001);
  64.     addObject(self, temp);
  65.  
  66. }
  67.  
  68. /* Create the form */
  69. method SQLAuthors::initFormSQLAuthors2(self, hPS)
  70. {
  71.     local temp;
  72.  
  73.     temp = new(EntryField,
  74.         asPels(new(Rect, 1565, 2185, 3691, 2509), hPS),
  75.         self, SQLAUTHORS_LASTNAME, "",0x10010008);
  76.     addObject(self, temp);
  77.     addEventPair(self, #(SQLAUTHORS_LASTNAME, #dummyHandler));
  78.  
  79.     temp = new(TextControl,
  80.         asPels(new(Rect, 295, 1756, 1107, 1993), hPS),
  81.         self, SQLAUTHORS_ADDRESSTEXT, "Address",0x10000001);
  82.     addObject(self, temp);
  83.  
  84.     temp = new(EntryField,
  85.         asPels(new(Rect, 1284, 1742, 7057, 2067), hPS),
  86.         self, SQLAUTHORS_ADDRESS, "",0x10010008);
  87.     addObject(self, temp);
  88.     addEventPair(self, #(SQLAUTHORS_ADDRESS, #dummyHandler));
  89.  
  90.     temp = new(TextControl,
  91.         asPels(new(Rect, 295, 1314, 738, 1550), hPS),
  92.         self, SQLAUTHORS_CITYTEXT, "City",0x10000001);
  93.     addObject(self, temp);
  94.  
  95.     temp = new(EntryField,
  96.         asPels(new(Rect, 930, 1314, 2406, 1638), hPS),
  97.         self, SQLAUTHORS_CITY, "",0x10010008);
  98.     addObject(self, temp);
  99.     addEventPair(self, #(SQLAUTHORS_CITY, #dummyHandler));
  100.  
  101. }
  102.  
  103. /* Create the form */
  104. method SQLAuthors::initFormSQLAuthors3(self, hPS)
  105. {
  106.     local temp;
  107.  
  108.     temp = new(TextControl,
  109.         asPels(new(Rect, 2687, 1314, 3248, 1550), hPS),
  110.         self, SQLAUTHORS_STATETEXT, "State",0x10000001);
  111.     addObject(self, temp);
  112.  
  113.     temp = new(EntryField,
  114.         asPels(new(Rect, 3425, 1314, 4887, 1638), hPS),
  115.         self, SQLAUTHORS_STATE, "",0x10010008);
  116.     addObject(self, temp);
  117.     addEventPair(self, #(SQLAUTHORS_STATE, #dummyHandler));
  118.  
  119.     temp = new(TextControl,
  120.         asPels(new(Rect, 5138, 1314, 5448, 1550), hPS),
  121.         self, SQLAUTHORS_ZIPTEXT, "Zip",0x10000001);
  122.     addObject(self, temp);
  123.  
  124.     temp = new(EntryField,
  125.         asPels(new(Rect, 5551, 1314, 7013, 1638), hPS),
  126.         self, SQLAUTHORS_ZIPCODE, "",0x10010008);
  127.     addObject(self, temp);
  128.     addEventPair(self, #(SQLAUTHORS_ZIPCODE, #dummyHandler));
  129.  
  130.     temp = new(TextControl,
  131.         asPels(new(Rect, 295, 871, 1343, 1107), hPS),
  132.         self, SQLAUTHORS_TELEPHONETEXT, "Telephone",0x10000001);
  133.     addObject(self, temp);
  134.  
  135. }
  136.  
  137. /* Create the form */
  138. method SQLAuthors::initFormSQLAuthors4(self, hPS)
  139. {
  140.     local temp;
  141.  
  142.     temp = new(EntryField,
  143.         asPels(new(Rect, 1491, 871, 4636, 1195), hPS),
  144.         self, SQLAUTHORS_PHONE, "",0x10030008);
  145.     addObject(self, temp);
  146.     addEventPair(self, #(SQLAUTHORS_PHONE, #dummyHandler));
  147.  
  148.     temp = new(PushButton,
  149.         asPels(new(Rect, 324, 177, 1373, 546), hPS),
  150.         self, SQLAUTHORS_NEW, "New",0x10000000);
  151.     addObject(self, temp);
  152.     addEventPair(self, #(SQLAUTHORS_NEW, #newEntry));
  153.  
  154.     temp = new(PushButton,
  155.         asPels(new(Rect, 1697, 177, 2746, 546), hPS),
  156.         self, SQLAUTHORS_FIND, "Find",0x10010400);
  157.     addObject(self, temp);
  158.     addEventPair(self, #(SQLAUTHORS_FIND, #find));
  159.  
  160.     temp = new(PushButton,
  161.         asPels(new(Rect, 3070, 177, 4119, 546), hPS),
  162.         self, SQLAUTHORS_NEXT, "Next",0x10000000);
  163.     addObject(self, temp);
  164.     addEventPair(self, #(SQLAUTHORS_NEXT, #next));
  165.  
  166.     temp = new(PushButton,
  167.         asPels(new(Rect, 4444, 177, 5492, 546), hPS),
  168.         self, SQLAUTHORS_UPDATE, "Update",0x10000000);
  169.     addObject(self, temp);
  170.     addEventPair(self, #(SQLAUTHORS_UPDATE, #updateDB));
  171.  
  172. }
  173.  
  174. /* Create the form */
  175. method SQLAuthors::initFormSQLAuthors5(self, hPS)
  176. {
  177.     local temp;
  178.  
  179.     temp = new(PushButton,
  180.         asPels(new(Rect, 5817, 177, 6865, 546), hPS),
  181.         self, SQLAUTHORS_QUIT, "Quit",0x10000000);
  182.     addObject(self, temp);
  183.     addEventPair(self, #(SQLAUTHORS_QUIT, #quit));
  184.  
  185. }
  186.  
  187. /* Handle message from control. */
  188. method SQLAuthors::dummyHandler(self, mp1, mp2)
  189. {
  190.     return nil;
  191. }
  192.  
  193. /* find the next author with the same last name as the last author. */
  194. method SQLAuthors::find(self, mp1, mp2)
  195. {
  196.         cancel(commandBuffer);
  197.         addCommand(commandBuffer, "select * from authors ");
  198.         addCommand(commandBuffer, "where au_lname like \"");
  199.     addCommand(commandBuffer, itemTextOf(self, SQLAUTHORS_LASTNAME));
  200.         addCommand(commandBuffer, "%\"");
  201.         sendCommand(commandBuffer, true);
  202.  
  203.         if (nextQuery(resultBuffer) == FAIL)
  204.                 destroy(self);
  205.  
  206.         if (nextRow(resultBuffer) != NO_MORE_ROWS) {
  207.         enableItem(self, SQLAUTHORS_NEXT);
  208.                 next(self, nil, nil);
  209.         } else
  210.         disableItem(self, SQLAUTHORS_NEXT);
  211.         return nil;
  212. }
  213.  
  214. /* Blank the form */
  215. method SQLAuthors::newEntry(self, mp1, mp2)
  216. {
  217.         cancelQuery(resultBuffer);
  218.         cancel(commandBuffer);
  219.     disableItem(self, SQLAUTHORS_NEXT);
  220.  
  221.     setItemText(self, SQLAUTHORS_AUTHORID, EmptyString);
  222.     setItemText(self, SQLAUTHORS_LASTNAME, EmptyString);
  223.     setItemText(self, SQLAUTHORS_FIRSTNAME, EmptyString);
  224.     setItemText(self, SQLAUTHORS_PHONE, EmptyString);
  225.     setItemText(self, SQLAUTHORS_ADDRESS, EmptyString);
  226.     setItemText(self, SQLAUTHORS_CITY, EmptyString);
  227.     setItemText(self, SQLAUTHORS_STATE, EmptyString);
  228.     setItemText(self, SQLAUTHORS_ZIPCODE, EmptyString);
  229.         return nil;             
  230. }
  231.  
  232. /* get the next row from the result buffer and display it. */
  233. method SQLAuthors::next(self, mp1, mp2)
  234. {
  235.         fill(self);
  236.         if (nextRow(resultBuffer) != NO_MORE_ROWS) {
  237.         enableItem(self, SQLAUTHORS_NEXT);
  238.         } else {
  239.         disableItem(self, SQLAUTHORS_NEXT);
  240.         }
  241.         return nil;
  242. }
  243.  
  244. /* Handle message from control. */
  245. method SQLAuthors::quit(self, mp1, mp2)
  246. {
  247.         destroy(self);
  248.         return nil;
  249. }
  250.  
  251. /* Handle message from control. */
  252. method SQLAuthors::updateDB(self, mp1, mp2)
  253. {
  254.         addCommand(updateCommandBuffer, "update authors set address = '");
  255.     addCommand(updateCommandBuffer, itemTextOf(self, SQLAUTHORS_ADDRESS));
  256.         addCommand(updateCommandBuffer, "', city = '");
  257.     addCommand(updateCommandBuffer, itemTextOf(self, SQLAUTHORS_CITY));
  258.         addCommand(updateCommandBuffer, "', state = '");
  259.     addCommand(updateCommandBuffer, itemTextOf(self, SQLAUTHORS_STATE));
  260.         addCommand(updateCommandBuffer, "', zip = '");
  261.     addCommand(updateCommandBuffer, trim(itemTextOf(self, SQLAUTHORS_ZIPCODE)));
  262.         addCommand(updateCommandBuffer, "', au_lname = '");
  263.     addCommand(updateCommandBuffer, itemTextOf(self, SQLAUTHORS_LASTNAME));
  264.         addCommand(updateCommandBuffer, "', au_fname = '");
  265.     addCommand(updateCommandBuffer, itemTextOf(self, SQLAUTHORS_FIRSTNAME));
  266.         addCommand(updateCommandBuffer, "', phone = '");
  267.     addCommand(updateCommandBuffer, itemTextOf(self, SQLAUTHORS_PHONE));
  268.         addCommand(updateCommandBuffer, "' where au_id = '");
  269.     addCommand(updateCommandBuffer, itemTextOf(self, SQLAUTHORS_AUTHORID));
  270.         addCommand(updateCommandBuffer, "'");
  271.         sendCommand(updateCommandBuffer, true);
  272.         nextQuery(updateResultBuffer);
  273.         return nil;
  274. }
  275.  
  276. /* Initialize the form */
  277. method SQLAuthors::init(self, parent)
  278. {
  279.  
  280.     return init(super, parent, basicNew(Rect), "SQLAuthors SQL Server Example", 0x80000080, 0x1101, 0xffffff);
  281.  
  282. }
  283.  
  284. /* Initialize the form by creating the objects on it. */
  285. method SQLAuthors::initializeForm(self)
  286. {
  287.     local hPS, finalShape;
  288.  
  289.     hPS = getPS(self);
  290.     setPS(0@0, PU_HIENGLISH, hPS);
  291.  
  292.     finalShape = asPels(new(Rect, 1247, 1882, 8511, 5484), hPS);
  293.  
  294.     createFattr(fattrManager,"System Proportional",0,0,120,-1);
  295.     initFormSQLAuthors1(self, hPS);
  296.     initFormSQLAuthors2(self, hPS);
  297.     initFormSQLAuthors3(self, hPS);
  298.     initFormSQLAuthors4(self, hPS);
  299.     initFormSQLAuthors5(self, hPS);
  300.     deleteAllFattrs(fattrManager);
  301.     releasePS(self, hPS);
  302.     initializeForm(super);
  303.     return finalShape;
  304. }
  305.  
  306. /* Answer whether the form is modal. */
  307. method SQLAuthors::isModal(self)
  308. {
  309.     return false;
  310. }
  311. /* set the default database name and user name. */
  312. method SQLAuthors::userInit(self)
  313. {
  314.         databaseName = "pubs";
  315.         userName = "sa";
  316.     disableItem(self, SQLAUTHORS_NEXT);
  317.         return false;
  318. }
  319. /* Set the fields of the form from the current row from the database. */
  320. method SQLAuthors::fill(self)
  321. {
  322.     setItemText(self, SQLAUTHORS_AUTHORID, getColumn(resultBuffer, "au_id"));
  323.     setItemText(self, SQLAUTHORS_LASTNAME, getColumn(resultBuffer, "au_lname"));
  324.     setItemText(self, SQLAUTHORS_FIRSTNAME, getColumn(resultBuffer, "au_fname"));
  325.     setItemText(self, SQLAUTHORS_PHONE, getColumn(resultBuffer, "phone"));
  326.     setItemText(self, SQLAUTHORS_ADDRESS, getColumn(resultBuffer, "address"));
  327.     setItemText(self, SQLAUTHORS_CITY, getColumn(resultBuffer, "city"));
  328.     setItemText(self, SQLAUTHORS_STATE, getColumn(resultBuffer, "state"));
  329.     setItemText(self, SQLAUTHORS_ZIPCODE, getColumn(resultBuffer, "zip"));
  330.         return nil;             
  331. }
  332. /* I need two sessions, so I will override this message. */
  333. method SQLAuthors::init2(self)
  334. {
  335.         local sPtr;
  336.  
  337.         sPtr = setPointer(systemPointer(SPTR_WAIT));
  338.  
  339.         init2(super);
  340.         sPtr = setPointer(systemPointer(SPTR_WAIT));
  341.  
  342.         updateSession = new(SQLSession, loginRecord, serverName);
  343.  
  344.         if (isNil(updateSession)) {
  345.                 messageBox(self,
  346.                            "Could not login in to SQL Server.",
  347.                            "Object/1",
  348.                            MB_ICONHAND | MB_OK);
  349.                 if (canceledLogin(self)) {
  350.                         destroy(self);
  351.                         return nil;
  352.                 } else {
  353.                         update(DeskTop);
  354.                         return self;
  355.                 }
  356.         }
  357.  
  358.         sqlErrorMessages = new(OrderedCollection, 0);
  359.         setErrorMessageHandler(updateSession,self);
  360.         setErrorReportHandler(updateSession, self, #displayError);
  361.  
  362.         if (notNil(databaseName))
  363.                 use(updateSession, databaseName);
  364.  
  365.         updateResultBuffer = resultBufferOf(updateSession);
  366.         updateCommandBuffer = commandBufferOf(updateSession);
  367.         setPointer(sPtr);
  368.         return self;
  369. }
  370. /* Make sure the database gets closed. */
  371. method SQLAuthors::destroy(self)
  372. {
  373.         if (notNil(updateSession)) {
  374.                 close(updateSession);
  375.         }
  376.         updateCommandBuffer = nil;
  377.         updateResultBuffer = nil;
  378.         updateSession = nil;
  379.         return destroy(super);
  380. }
  381.  
  382. /*
  383. ** Answer the name of the demo application
  384. ** to be used by the demo start list box.
  385. */
  386. method SQLAuthorsClass::demoNameOf(self)
  387. {
  388.     return "Authors (SQL Server)";
  389.