From 79fb10a9c6a31d2ea67e42d07f2c21e07853ea96 Mon Sep 17 00:00:00 2001 From: helge Date: Mon, 11 Apr 2005 18:22:11 +0000 Subject: [PATCH] started MySQL4 adaptor based on SQLite one git-svn-id: http://svn.opengroupware.org/SOPE/trunk@716 e4a50df8-12e2-0310-a44c-efbce7f8a7e3 --- sope-gdl1/MySQL4/COPYING.LIB | 481 ++++++++++++++ sope-gdl1/MySQL4/ChangeLog | 3 + sope-gdl1/MySQL4/EOAttribute+SQLite.h | 45 ++ sope-gdl1/MySQL4/EOAttribute+SQLite.m | 205 ++++++ sope-gdl1/MySQL4/GNUmakefile | 69 ++ sope-gdl1/MySQL4/GNUmakefile.preamble | 46 ++ sope-gdl1/MySQL4/NSCalendarDate+SQLiteVal.m | 221 +++++++ sope-gdl1/MySQL4/NSData+SQLiteVal.m | 92 +++ sope-gdl1/MySQL4/NSNumber+SQLiteVal.m | 103 +++ sope-gdl1/MySQL4/NSString+SQLite.h | 40 ++ sope-gdl1/MySQL4/NSString+SQLite.m | 162 +++++ sope-gdl1/MySQL4/NSString+SQLiteVal.m | 113 ++++ sope-gdl1/MySQL4/README | 103 +++ sope-gdl1/MySQL4/SQLiteAdaptor.h | 75 +++ sope-gdl1/MySQL4/SQLiteAdaptor.m | 169 +++++ sope-gdl1/MySQL4/SQLiteChannel+Model.h | 41 ++ sope-gdl1/MySQL4/SQLiteChannel+Model.m | 298 +++++++++ sope-gdl1/MySQL4/SQLiteChannel.h | 94 +++ sope-gdl1/MySQL4/SQLiteChannel.m | 677 ++++++++++++++++++++ sope-gdl1/MySQL4/SQLiteContext.h | 39 ++ sope-gdl1/MySQL4/SQLiteContext.m | 84 +++ sope-gdl1/MySQL4/SQLiteException.h | 54 ++ sope-gdl1/MySQL4/SQLiteException.m | 62 ++ sope-gdl1/MySQL4/SQLiteExpression.h | 55 ++ sope-gdl1/MySQL4/SQLiteExpression.m | 92 +++ sope-gdl1/MySQL4/SQLiteValues.h | 83 +++ sope-gdl1/MySQL4/SQLiteValues.m | 141 ++++ sope-gdl1/MySQL4/Version | 3 + sope-gdl1/MySQL4/common.h | 40 ++ sope-gdl1/MySQL4/condict.plist | 3 + sope-gdl1/MySQL4/fhs.make | 25 + sope-gdl1/MySQL4/gdltest.m | 220 +++++++ sope-gdl1/MySQL4/test.eomodel | 26 + 33 files changed, 3964 insertions(+) create mode 100644 sope-gdl1/MySQL4/COPYING.LIB create mode 100644 sope-gdl1/MySQL4/ChangeLog create mode 100644 sope-gdl1/MySQL4/EOAttribute+SQLite.h create mode 100644 sope-gdl1/MySQL4/EOAttribute+SQLite.m create mode 100644 sope-gdl1/MySQL4/GNUmakefile create mode 100644 sope-gdl1/MySQL4/GNUmakefile.preamble create mode 100644 sope-gdl1/MySQL4/NSCalendarDate+SQLiteVal.m create mode 100644 sope-gdl1/MySQL4/NSData+SQLiteVal.m create mode 100644 sope-gdl1/MySQL4/NSNumber+SQLiteVal.m create mode 100644 sope-gdl1/MySQL4/NSString+SQLite.h create mode 100644 sope-gdl1/MySQL4/NSString+SQLite.m create mode 100644 sope-gdl1/MySQL4/NSString+SQLiteVal.m create mode 100644 sope-gdl1/MySQL4/README create mode 100644 sope-gdl1/MySQL4/SQLiteAdaptor.h create mode 100644 sope-gdl1/MySQL4/SQLiteAdaptor.m create mode 100644 sope-gdl1/MySQL4/SQLiteChannel+Model.h create mode 100644 sope-gdl1/MySQL4/SQLiteChannel+Model.m create mode 100644 sope-gdl1/MySQL4/SQLiteChannel.h create mode 100644 sope-gdl1/MySQL4/SQLiteChannel.m create mode 100644 sope-gdl1/MySQL4/SQLiteContext.h create mode 100644 sope-gdl1/MySQL4/SQLiteContext.m create mode 100644 sope-gdl1/MySQL4/SQLiteException.h create mode 100644 sope-gdl1/MySQL4/SQLiteException.m create mode 100644 sope-gdl1/MySQL4/SQLiteExpression.h create mode 100644 sope-gdl1/MySQL4/SQLiteExpression.m create mode 100644 sope-gdl1/MySQL4/SQLiteValues.h create mode 100644 sope-gdl1/MySQL4/SQLiteValues.m create mode 100644 sope-gdl1/MySQL4/Version create mode 100644 sope-gdl1/MySQL4/common.h create mode 100644 sope-gdl1/MySQL4/condict.plist create mode 100644 sope-gdl1/MySQL4/fhs.make create mode 100644 sope-gdl1/MySQL4/gdltest.m create mode 100644 sope-gdl1/MySQL4/test.eomodel diff --git a/sope-gdl1/MySQL4/COPYING.LIB b/sope-gdl1/MySQL4/COPYING.LIB new file mode 100644 index 00000000..eb685a5e --- /dev/null +++ b/sope-gdl1/MySQL4/COPYING.LIB @@ -0,0 +1,481 @@ + GNU LIBRARY GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1991 Free Software Foundation, Inc. + 675 Mass Ave, Cambridge, MA 02139, USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the library GPL. It is + numbered 2 because it goes with version 2 of the ordinary GPL.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Library General Public License, applies to some +specially designated Free Software Foundation software, and to any +other libraries whose authors decide to use it. You can use it for +your libraries, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if +you distribute copies of the library, or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link a program with the library, you must provide +complete object files to the recipients so that they can relink them +with the library, after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + Our method of protecting your rights has two steps: (1) copyright +the library, and (2) offer you this license which gives you legal +permission to copy, distribute and/or modify the library. + + Also, for each distributor's protection, we want to make certain +that everyone understands that there is no warranty for this free +library. If the library is modified by someone else and passed on, we +want its recipients to know that what they have is not the original +version, so that any problems introduced by others will not reflect on +the original authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that companies distributing free +software will individually obtain patent licenses, thus in effect +transforming the program into proprietary software. To prevent this, +we have made it clear that any patent must be licensed for everyone's +free use or not licensed at all. + + Most GNU software, including some libraries, is covered by the ordinary +GNU General Public License, which was designed for utility programs. This +license, the GNU Library General Public License, applies to certain +designated libraries. This license is quite different from the ordinary +one; be sure to read it in full, and don't assume that anything in it is +the same as in the ordinary license. + + The reason we have a separate public license for some libraries is that +they blur the distinction we usually make between modifying or adding to a +program and simply using it. Linking a program with a library, without +changing the library, is in some sense simply using the library, and is +analogous to running a utility program or application program. However, in +a textual and legal sense, the linked executable is a combined work, a +derivative of the original library, and the ordinary General Public License +treats it as such. + + Because of this blurred distinction, using the ordinary General +Public License for libraries did not effectively promote software +sharing, because most developers did not use the libraries. We +concluded that weaker conditions might promote sharing better. + + However, unrestricted linking of non-free programs would deprive the +users of those programs of all benefit from the free status of the +libraries themselves. This Library General Public License is intended to +permit developers of non-free programs to use free libraries, while +preserving your freedom as a user of such programs to change the free +libraries that are incorporated in them. (We have not seen how to achieve +this as regards changes in header files, but we have achieved it as regards +changes in the actual functions of the Library.) The hope is that this +will lead to faster development of free libraries. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, while the latter only +works together with the library. + + Note that it is possible for a library to be covered by the ordinary +General Public License rather than by this special one. + + GNU LIBRARY GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library which +contains a notice placed by the copyright holder or other authorized +party saying it may be distributed under the terms of this Library +General Public License (also called "this License"). Each licensee is +addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also compile or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + c) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + d) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the source code distributed need not include anything that is normally +distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Library General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + Appendix: How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/sope-gdl1/MySQL4/ChangeLog b/sope-gdl1/MySQL4/ChangeLog new file mode 100644 index 00000000..a85905be --- /dev/null +++ b/sope-gdl1/MySQL4/ChangeLog @@ -0,0 +1,3 @@ +2005-04-11 Helge Hess + + * started MySQL4 adaptor based on SQLite one (v4.5.1) diff --git a/sope-gdl1/MySQL4/EOAttribute+SQLite.h b/sope-gdl1/MySQL4/EOAttribute+SQLite.h new file mode 100644 index 00000000..fb3f45ea --- /dev/null +++ b/sope-gdl1/MySQL4/EOAttribute+SQLite.h @@ -0,0 +1,45 @@ +/* + EOAttribute+SQLite.h + + Copyright (C) 2003-2004 Helge Hess + + Author: Helge Hess (helge.hess@opengroupware.org) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef ___SQLite_EOAttribute_H___ +#define ___SQLite_EOAttribute_H___ + +#import +#include + +@class NSString; + +@interface EOAttribute(SQLiteAttributeAdditions) + +- (void)loadValueClassAndTypeUsingSQLiteType:(int)_type + size:(int)_size + modification:(int)_modification + binary:(BOOL)_isBinary; + +- (void)loadValueClassForExternalSQLiteType:(NSString *)_type; + +@end + +#endif /* ___SQLite_EOAttribute_H___ */ diff --git a/sope-gdl1/MySQL4/EOAttribute+SQLite.m b/sope-gdl1/MySQL4/EOAttribute+SQLite.m new file mode 100644 index 00000000..ff328c5b --- /dev/null +++ b/sope-gdl1/MySQL4/EOAttribute+SQLite.m @@ -0,0 +1,205 @@ +/* + EOAttribute+SQLite.m + + Copyright (C) 1999 MDlink online service center GmbH and Helge Hess + + Author: Helge Hess (helge@mdlink.de) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "common.h" +#import "EOAttribute+SQLite.h" + +static NSString *SQLITE3_DATETIME_FORMAT = @"%b %d %Y %I:%M:%S:000%p"; +static NSString *SQLITE3_TIMESTAMP_FORMAT = @"%Y-%m-%d %H:%M:%S%z"; + +@implementation EOAttribute(SQLiteAttributeAdditions) + +- (void)loadValueClassAndTypeUsingSQLiteType:(int)_type + size:(int)_size + modification:(int)_modification + binary:(BOOL)_isBinary +{ + /* This method makes no sense with SQLite? */ + + if (_isBinary) + [self setValueClassName:@"NSData"]; + +#if 0 + switch (_type) { + case BOOLOID: + [self setExternalType:@"bool"]; + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"i"]; + return; + case NAMEOID: + [self setExternalType:@"name"]; + [self setValueClassName:@"NSString"]; + return; + case TEXTOID: + [self setExternalType:@"textoid"]; + [self setValueClassName:@"NSString"]; + return; + case INT2OID: + [self setExternalType:@"int2"]; + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"d"]; + return; + case INT4OID: + [self setExternalType:@"int4"]; + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"d"]; + return; + case INT8OID: + [self setExternalType:@"int8"]; + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"d"]; + return; + + case CHAROID: + [self setExternalType:@"char"]; + [self setValueClassName:@"NSString"]; + break; + case VARCHAROID: + [self setExternalType:@"varchar"]; + [self setValueClassName:@"NSString"]; + break; + case NUMERICOID: + [self setExternalType:@"numeric"]; + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"d"]; + break; + case FLOAT4OID: + [self setExternalType:@"float4"]; + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"f"]; + break; + case FLOAT8OID: + [self setExternalType:@"float8"]; + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"f"]; + break; + + case DATEOID: + [self setExternalType:@"datetime"]; + [self setValueClassName:@"NSCalendarDate"]; + [self setCalendarFormat:SQLITE3_DATETIME_FORMAT]; + break; + case TIMEOID: + [self setExternalType:@"time"]; + [self setValueClassName:@"NSCalendarDate"]; + [self setCalendarFormat:SQLITE3_DATETIME_FORMAT]; + break; + case TIMESTAMPOID: + [self setExternalType:@"timestamp"]; + [self setValueClassName:@"NSCalendarDate"]; + [self setCalendarFormat:SQLITE3_DATETIME_FORMAT]; + break; + case TIMESTAMPTZOID: + [self setExternalType:@"timestamptz"]; + [self setValueClassName:@"NSCalendarDate"]; + [self setCalendarFormat:SQLITE3_DATETIME_FORMAT]; + break; + case BITOID: + [self setExternalType:@"bit"]; + break; + default: + NSLog(@"What is SQLITE3 Oid %i ???", _type); + break; + } +#endif +} + +- (void)loadValueClassForExternalSQLiteType:(NSString *)_type { + if ([_type isEqualToString:@"bool"]) { + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"i"]; + } + else if ([_type isEqualToString:@"int2"]) { + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"i"]; + } + else if ([_type isEqualToString:@"int4"]) { + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"i"]; + } + else if ([_type isEqualToString:@"float4"]) { + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"f"]; + } + else if ([_type isEqualToString:@"float8"]) { + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"d"]; + } + else if ([_type isEqualToString:@"decimal"]) { + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"d"]; + } + else if ([_type isEqualToString:@"numeric"]) { + [self setValueClassName:@"NSNumber"]; + [self setValueType:@"d"]; + } + else if ([_type isEqualToString:@"name"]) { + [self setExternalType:@"name"]; + [self setValueClassName:@"NSString"]; + } + else if ([_type isEqualToString:@"varchar"]) { + [self setExternalType:@"varchar"]; + [self setValueClassName:@"NSString"]; + } + else if ([_type isEqualToString:@"char"]) { + [self setExternalType:@"char"]; + [self setValueClassName:@"NSString"]; + } + else if ([_type isEqualToString:@"timestamp"]) { + [self setValueClassName:@"NSCalendarDate"]; + [self setCalendarFormat:SQLITE3_TIMESTAMP_FORMAT]; + } + else if ([_type isEqualToString:@"timestamptz"]) { + [self setValueClassName:@"NSCalendarDate"]; + [self setCalendarFormat:SQLITE3_TIMESTAMP_FORMAT]; + } + else if ([_type isEqualToString:@"datetime"]) { + [self setValueClassName:@"NSCalendarDate"]; + [self setCalendarFormat:SQLITE3_DATETIME_FORMAT]; + } + else if ([_type isEqualToString:@"date"]) { + [self setValueClassName:@"NSString"]; + } + else if ([_type isEqualToString:@"time"]) { + [self setValueClassName:@"NSString"]; + } + else if ([_type isEqualToString:@"text"]) { + [self setValueClassName:@"NSString"]; + } + else { + NSLog(@"invalid argument %@", _type); + + [NSException raise:@"InvalidArgumentException" + format:@"invalid SQLite type %@ passed to %s", + _type, __PRETTY_FUNCTION__]; + } +} + +@end /* EOAttribute(SQLite) */ + +void __link_EOAttributeSQLite() { + // used to force linking of object file + __link_EOAttributeSQLite(); +} diff --git a/sope-gdl1/MySQL4/GNUmakefile b/sope-gdl1/MySQL4/GNUmakefile new file mode 100644 index 00000000..8340bd87 --- /dev/null +++ b/sope-gdl1/MySQL4/GNUmakefile @@ -0,0 +1,69 @@ +# +# GNUmakefile +# +# Copyright (C) 2005 Helge Hess +# +# Author: Helge Hess (helge.hess@opengroupware.org) +# +# This file is part of the MySQL4 Adaptor Library +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public +# License along with this library; see the file COPYING.LIB. +# If not, write to the Free Software Foundation, +# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +-include ../../config.make +include $(GNUSTEP_MAKEFILES)/common.make +include ../Version +include ./Version + +BUNDLE_NAME = MySQL4 + +MySQL4_OBJC_FILES = \ + MySQL4Expression.m \ + MySQL4Adaptor.m \ + MySQL4Context.m \ + MySQL4Channel.m \ + MySQL4Channel+Model.m \ + MySQL4Exception.m \ + MySQL4Values.m \ + NSString+MySQL4.m \ + EOAttribute+MySQL4.m \ + NSString+MySQL4Val.m \ + NSData+MySQL4Val.m \ + NSCalendarDate+MySQL4Val.m \ + NSNumber+MySQL4Val.m \ + +MySQL4_PRINCIPAL_CLASS = MySQL4Adaptor + +BUNDLE_INSTALL = MySQL4 +BUNDLE_INSTALL_DIR = $(GNUSTEP_INSTALLATION_DIR)/Library/GDLAdaptors-$(MAJOR_VERSION).$(MINOR_VERSION)/ + +# Use .gdladaptor as the bundle extension +BUNDLE_EXTENSION = .gdladaptor + +MySQL4_RESOURCE_FILES += Version + +# tool + +TOOL_NAME = gdltest + +gdltest_OBJC_FILES = gdltest.m + +-include GNUmakefile.preamble +include $(GNUSTEP_MAKEFILES)/bundle.make +ifeq ($(test),yes) +include $(GNUSTEP_MAKEFILES)/tool.make +endif +-include GNUmakefile.postamble +include fhs.make diff --git a/sope-gdl1/MySQL4/GNUmakefile.preamble b/sope-gdl1/MySQL4/GNUmakefile.preamble new file mode 100644 index 00000000..d700c159 --- /dev/null +++ b/sope-gdl1/MySQL4/GNUmakefile.preamble @@ -0,0 +1,46 @@ +# +# GNUmakefile +# +# Copyright (C) 2003-2005 Helge Hess +# +# Author: Helge Hess (helge.hess@opengroupware.org) +# +# This file is part of the SQLite3 Adaptor Library +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Library General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Library General Public License for more details. +# +# You should have received a copy of the GNU Library General Public +# License along with this library; see the file COPYING.LIB. +# If not, write to the Free Software Foundation, +# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +SQLite3_BUNDLE_LIBS += \ + -lGDLAccess \ + -lEOControl \ + -lsqlite3 + +gdltest_TOOL_LIBS += \ + -lGDLAccess \ + -lNGExtensions + +# set compile flags and go + +ADDITIONAL_INCLUDE_DIRS += \ + -I.. -I../.. \ + -I/usr/local/include \ + -I/usr/include \ + +ADDITIONAL_LIB_DIRS += \ + -L../GDLAccess/$(GNUSTEP_OBJ_DIR) + +SYSTEM_LIB_DIR += \ + -L/usr/local/lib \ + -L/usr/lib diff --git a/sope-gdl1/MySQL4/NSCalendarDate+SQLiteVal.m b/sope-gdl1/MySQL4/NSCalendarDate+SQLiteVal.m new file mode 100644 index 00000000..b1b8c1a0 --- /dev/null +++ b/sope-gdl1/MySQL4/NSCalendarDate+SQLiteVal.m @@ -0,0 +1,221 @@ +/* + NSCalendarDate+SQLiteVal.m + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + + +#import +#include "SQLiteChannel.h" +#include "common.h" + +#if COCOA_Foundation_LIBRARY || NeXT_Foundation_LIBRARY +@interface NSCalendarDate(UsedPrivates) +- (id)initWithTimeIntervalSince1970:(NSTimeInterval)_tv; +@end +#endif + +static NSString *SQLITE3_DATETIME_FORMAT = @"%b %d %Y %I:%M:%S:000%p"; + +@implementation NSCalendarDate(SQLiteValues) + +/* + Format: '2001-07-26 14:00:00+02' + '2001-07-26 14:00:00+09:30' + 0123456789012345678901234 + + Matthew: "07/25/2003 06:00:00 CDT". +*/ + +static Class NSCalDateClass = Nil; +static NSTimeZone *DefServerTimezone = nil; +static NSTimeZone *gmt = nil; +static NSTimeZone *gmt01 = nil; +static NSTimeZone *gmt02 = nil; + +- (id)initWithSQLiteData:(const void *)_value length:(int)_length { + static unsigned char buf[28]; // reused buffer, THREAD + const char *_cstr = _value; + unsigned char *p; + NSTimeZone *attrTZ; + NSCalendarDate *date; + int year, month, day, hour, min, sec, tzOffset; + + if (_length == 0) + return nil; + + if (_length != 22 && _length != 25) { + NSLog(@"ERROR(%s): unexpected date string '%s', returning now" + @" (expected format: '2001-07-26 14:00:00+02')", + __PRETTY_FUNCTION__, _cstr); + return [NSCalendarDate date]; + } + strncpy(buf, _cstr, 25); + buf[25] = '\0'; + + /* perform on reverse, so that we don't overwrite with null-terminators */ + + if (_length == 22) { + p = &(buf[19]); + tzOffset = atoi(p) * 60; + } + else if (_length >= 25) { + int mins; + p = &(buf[23]); + mins = atoi(p); + buf[22] = '\0'; // the ':' + p = &(buf[19]); + tzOffset = atoi(p) * 60; + tzOffset = tzOffset > 0 ? (tzOffset + mins) : (tzOffset - mins); + } + + p = &(buf[17]); buf[19] = '\0'; sec = atoi(p); + p = &(buf[14]); buf[16] = '\0'; min = atoi(p); + p = &(buf[11]); buf[13] = '\0'; hour = atoi(p); + p = &(buf[8]); buf[10] = '\0'; day = atoi(p); + p = &(buf[5]); buf[7] = '\0'; month = atoi(p); + p = &(buf[0]); buf[4] = '\0'; year = atoi(p); + + /* TODO: cache all timezones (just 26 ;-) */ + switch (tzOffset) { + case 0: + if (gmt == nil) { + gmt = [[NSTimeZone timeZoneForSecondsFromGMT:0] retain]; + NSAssert(gmt, @"could not create GMT timezone?!"); + } + attrTZ = gmt; + break; + case 60: + if (gmt01 == nil) { + gmt01 = [[NSTimeZone timeZoneForSecondsFromGMT:3600] retain]; + NSAssert(gmt01, @"could not create GMT+01 timezone?!"); + } + attrTZ = gmt01; + break; + case 120: + if (gmt02 == nil) { + gmt02 = [[NSTimeZone timeZoneForSecondsFromGMT:7200] retain]; + NSAssert(gmt02, @"could not create GMT+02 timezone?!"); + } + attrTZ = gmt02; + break; + + default: { + /* cache the first, "alternative" timezone */ + static int firstTZOffset = 0; // can use 0 since GMT is a separate case + static NSTimeZone *firstTZ = nil; + if (firstTZOffset == 0) { + firstTZOffset = tzOffset; + firstTZ = [[NSTimeZone timeZoneForSecondsFromGMT:(tzOffset*60)] retain]; + } + + attrTZ = (firstTZOffset == tzOffset) + ? firstTZ + : [NSTimeZone timeZoneForSecondsFromGMT:(tzOffset * 60)]; + break; + } + } + + if (NSCalDateClass == Nil) NSCalDateClass = [NSCalendarDate class]; + date = [NSCalDateClass dateWithYear:year month:month day:day + hour:hour minute:min second:sec + timeZone:attrTZ]; + if (date == nil) { + NSLog(@"ERROR(%s): could not construct date from string '%s': " + @"year=%i,month=%i,day=%i,hour=%i,minute=%i,second=%i, tz=%@", + __PRETTY_FUNCTION__, _cstr, + year, month, day, hour, min, sec, attrTZ); + } + return date; +} + +- (id)initWithSQLiteDouble:(double)_value { + return [self initWithTimeIntervalSince1970:_value]; +} +- (id)initWithSQLiteInt:(int)_value { + return [self initWithSQLiteDouble:_value]; +} + +- (id)initWithSQLiteText:(const unsigned char *)_value { + return [self initWithSQLiteData:_value length:strlen(_value)]; +} + +/* generating value */ + +- (NSString *)stringValueForSQLite3Type:(NSString *)_type + attribute:(EOAttribute *)_attribute +{ +#if 0 + NSString *format; +#endif + EOQuotedExpression *expr; + NSTimeZone *serverTimeZone; + NSString *format; + NSString *val; + unsigned len; + unichar c1; + + if ((len = [_type length]) == 0) + c1 = 0; + else + c1 = [_type characterAtIndex:0]; + + if (c1 == 'i' || c1 == 'I') { // INTEGER + char buf[64]; + sprintf(buf, "%d", ((unsigned int)[self timeIntervalSince1970])); + return [NSString stringWithCString:buf]; + } + if (c1 == 'r' || c1 == 'R') { // REAL + char buf[64]; // TODO: check format + sprintf(buf, "%f", [self timeIntervalSince1970]); + return [NSString stringWithCString:buf]; + } + + if ((serverTimeZone = [_attribute serverTimeZone]) == nil ) { + if (DefServerTimezone == nil) { + DefServerTimezone = [[NSTimeZone localTimeZone] retain]; + NSLog(@"Note: SQLite adaptor using timezone '%@' as default", + DefServerTimezone); + } + serverTimeZone = DefServerTimezone; + } + +#if 0 + format = [_attribute calendarFormat]; +#else /* hm, why is that? */ + format = @"%Y-%m-%d %H:%M:%S%z"; +#endif + if (format == nil) + format = SQLITE3_DATETIME_FORMAT; + + [self setTimeZone:serverTimeZone]; + + val = [self descriptionWithCalendarFormat:format]; + expr = [[EOQuotedExpression alloc] initWithExpression:val + quote:@"\'" escape:@"\\'"]; + val = [[expr expressionValueForContext:nil] retain]; + [expr release]; + + return [val autorelease]; +} + +@end /* NSCalendarDate(SQLiteValues) */ diff --git a/sope-gdl1/MySQL4/NSData+SQLiteVal.m b/sope-gdl1/MySQL4/NSData+SQLiteVal.m new file mode 100644 index 00000000..c1f871c7 --- /dev/null +++ b/sope-gdl1/MySQL4/NSData+SQLiteVal.m @@ -0,0 +1,92 @@ +/* + NSData+SQLiteVal.m + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "SQLiteValues.h" +#include "SQLiteChannel.h" +#import +#include "common.h" + +@implementation NSData(SQLiteValues) + +- (id)initWithSQLiteInt:(int)_value { + return [self initWithBytes:&_value length:sizeof(int)]; +} +- (id)initWithSQLiteDouble:(double)_value { + return [self initWithBytes:&_value length:sizeof(double)]; +} +- (id)initWithSQLiteText:(const unsigned char *)_value { + return [self initWithBytes:_value length:strlen(_value)]; +} +- (id)initWithSQLiteData:(const void *)_value length:(int)_length { + return [self initWithBytes:_value length:_length]; +} + +- (NSString *)stringValueForSQLite3Type:(NSString *)_type + attribute:(EOAttribute *)_attribute +{ + // TODO: UNICODE + // TODO: this method looks slow + static NSStringEncoding enc = 0; + NSString *str, *t; + unsigned len; + unichar c1; + + if ((len = [self length]) == 0) + return @""; + + if (enc == 0) { + enc = [NSString defaultCStringEncoding]; + NSLog(@"Note: SQLite adaptor using '%@' encoding for data=>string " + @"conversion.", + [NSString localizedNameOfStringEncoding:enc]); + } + + str = [[NSString alloc] initWithData:self encoding:enc]; + + if (((len = [_type length]) == 0) || (len != 4 && len != 5 && len != 7)) + return [str autorelease]; + + c1 = [_type characterAtIndex:0]; + switch (c1) { + case 'c': case 'C': + case 'v': case 'V': + case 'm': case 'M': + case 't': case 'T': + t = [_type lowercaseString]; + if ([t hasPrefix:@"char"] || + [t hasPrefix:@"varchar"] || + [t hasPrefix:@"money"] || + [t hasPrefix:@"text"]) { + t = [[str stringValueForSQLite3Type:_type + attribute:_attribute] retain]; + [str release]; + return [t autorelease]; + } + } + + return [str autorelease];; +} + +@end /* NSData(SQLiteValues) */ diff --git a/sope-gdl1/MySQL4/NSNumber+SQLiteVal.m b/sope-gdl1/MySQL4/NSNumber+SQLiteVal.m new file mode 100644 index 00000000..92118b23 --- /dev/null +++ b/sope-gdl1/MySQL4/NSNumber+SQLiteVal.m @@ -0,0 +1,103 @@ +/* + SQLiteAdaptor.h + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#import +#include "SQLiteChannel.h" +#include "common.h" + +@implementation NSNumber(SQLiteValues) + +- (id)initWithSQLiteInt:(int)_value { + return [self initWithInt:_value]; +} +- (id)initWithSQLiteDouble:(double)_value { + return [self initWithDouble:_value]; +} +- (id)initWithSQLiteText:(const unsigned char *)_value { + return index(_value, '.') != NULL + ? [self initWithDouble:atof(_value)] + : [self initWithInt:atoi(_value)]; +} + +- (id)initWithSQLiteData:(const void *)_value length:(int)_length { + switch (_length) { + case 1: return [self initWithUnsignedChar:*(char *)_value]; + case 2: return [self initWithShort:*(short *)_value]; + case 4: return [self initWithInt:*(int *)_value]; + case 8: return [self initWithDouble:*(double *)_value]; + } + + [self release]; + return nil; +} + +- (NSString *)stringValueForSQLite3Type:(NSString *)_type + attribute:(EOAttribute *)_attribute +{ + // TODO: can we avoid the lowercaseString? + unsigned len; + unichar c1; + + if ((len = [_type length]) == 0) + return [self stringValue]; + if (len < 4) + return [self stringValue]; + + c1 = [_type characterAtIndex:0]; + switch (c1) { + case 'b': case 'B': + if (![[_type lowercaseString] hasPrefix:@"bool"]) + break; + return [self boolValue] ? @"true" : @"false"; + + case 'm': case 'M': { + if (![[_type lowercaseString] hasPrefix:@"money"]) + break; + return [@"$" stringByAppendingString:[self stringValue]]; + } + + case 'c': case 'C': + case 't': case 'T': + case 'v': case 'V': { + static NSMutableString *ms = nil; // reuse mstring, THREAD + + _type = [_type lowercaseString]; + if (!([_type hasPrefix:@"char"] || + [_type hasPrefix:@"varchar"] || + [_type hasPrefix:@"text"])) + break; + + // TODO: can we get this faster?! + if (ms == nil) ms = [[NSMutableString alloc] initWithCapacity:256]; + [ms setString:@"'"]; + [ms appendString:[self stringValue]]; + [ms appendString:@"'"]; + return [[ms copy] autorelease]; + } + } + return [self stringValue]; +} + +@end /* NSNumber(SQLiteValues) */ diff --git a/sope-gdl1/MySQL4/NSString+SQLite.h b/sope-gdl1/MySQL4/NSString+SQLite.h new file mode 100644 index 00000000..4de73577 --- /dev/null +++ b/sope-gdl1/MySQL4/NSString+SQLite.h @@ -0,0 +1,40 @@ +/* + NSString+SQLite.h + + Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess + + Author: Helge Hess (helge@mdlink.de) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef ___SQLite_NSString_H___ +#define ___SQLite_NSString_H___ + +#import + +@interface NSString(SQLiteMiscStrings) + +- (NSString *)_sqlite3ModelMakeInstanceVarName; +- (NSString *)_sqlite3ModelMakeClassName; +- (NSString *)_sqlite3StringWithCapitalizedFirstChar; +- (NSString *)_sqlite3StripEndSpaces; + +@end + +#endif /* ___SQLite_NSString_H___ */ diff --git a/sope-gdl1/MySQL4/NSString+SQLite.m b/sope-gdl1/MySQL4/NSString+SQLite.m new file mode 100644 index 00000000..3d729b38 --- /dev/null +++ b/sope-gdl1/MySQL4/NSString+SQLite.m @@ -0,0 +1,162 @@ +/* + NSString+SQLite.m + + Copyright (C) 1999 MDlink online service center GmbH and Helge Hess + + Author: Helge Hess (helge@mdlink.de) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +// $Id: NSString+SQLite.m,v 1.1 2004/06/14 14:27:44 helge Exp $ + +#if LIB_FOUNDATION_BOEHM_GC +# include +#endif + +#import +#include "common.h" +#import "NSString+SQLite.h" + +@implementation NSString(SQLiteMiscStrings) + +- (NSString *)_sqlite3ModelMakeInstanceVarName { + if ([self length] == 0) + return @""; + else { + unsigned clen = 0; + char *s = NULL; + int cnt, cnt2; + + clen = [self cStringLength]; + s = malloc(clen + 10); + + [self getCString:s maxLength:clen]; + + for (cnt = cnt2 = 0; cnt < clen; cnt++, cnt2++) { + if ((s[cnt] == '_') && (s[cnt + 1] != '\0')) { + s[cnt2] = toupper(s[cnt + 1]); + cnt++; + } + else if ((s[cnt] == '2') && (s[cnt + 1] != '\0')) { + s[cnt2] = s[cnt]; + cnt++; + cnt2++; + s[cnt2] = toupper(s[cnt]); + } + else + s[cnt2] = tolower(s[cnt]); + } + s[cnt2] = '\0'; + + return [[[NSString alloc] + initWithCStringNoCopy:s length:strlen(s) freeWhenDone:YES] + autorelease]; + } +} + +- (NSString *)_sqlite3ModelMakeClassName { + if ([self length] == 0) + return @""; + else { + unsigned clen = 0; + char *s = NULL; + int cnt, cnt2; + + clen = [self cStringLength]; + s = malloc(clen + 10); + + [self getCString:s maxLength:clen]; + + for (cnt = cnt2 = 0; cnt < clen; cnt++, cnt2++) { + if ((s[cnt] == '_') && (s[cnt + 1] != '\0')) { + s[cnt2] = toupper(s[cnt + 1]); + cnt++; + } + else if ((s[cnt] == '2') && (s[cnt + 1] != '\0')) { + s[cnt2] = s[cnt]; + cnt++; + cnt2++; + s[cnt2] = toupper(s[cnt]); + } + else + s[cnt2] = tolower(s[cnt]); + } + s[cnt2] = '\0'; + + s[0] = toupper(s[0]); + + return [[[NSString alloc] + initWithCStringNoCopy:s length:strlen(s) + freeWhenDone:YES] + autorelease]; + } +} + +- (NSString *)_sqlite3StringWithCapitalizedFirstChar { + NSCharacterSet *upperSet = [NSCharacterSet uppercaseLetterCharacterSet]; + + if ([self length] == 0) + return @""; + else if ([upperSet characterIsMember:[self characterAtIndex:0]]) + return [[self copy] autorelease]; + else { + NSMutableString *str = [NSMutableString stringWithCapacity:[self length]]; + + [str appendString:[[self substringToIndex:1] uppercaseString]]; + [str appendString:[self substringFromIndex:1]]; + + return [[str copy] autorelease]; + } +} + +- (NSString *)_sqlite3StripEndSpaces { + if ([self length] > 0) { + NSCharacterSet *spaceSet = [NSCharacterSet whitespaceCharacterSet]; + NSMutableString *str = [NSMutableString stringWithCapacity:[self length]]; + IMP charAtIndex; + NSRange range; + + charAtIndex = [self methodForSelector:@selector(characterAtIndex:)]; + range.length = 0; + + for (range.location = ([self length] - 1); + range.location >= 0; + range.location++, range.length++) { + unichar c; + + c = (unichar)(int)charAtIndex(self, @selector(characterAtIndex:), + range.location); + if (![spaceSet characterIsMember:c]) + break; + } + + if (range.length > 0) { + [str appendString:self]; + [str deleteCharactersInRange:range]; + return AUTORELEASE([str copy]); + } + } + return AUTORELEASE([self copy]); +} + +@end + +void __link_NSStringSQLite() { + // used to force linking of object file + __link_NSStringSQLite(); +} diff --git a/sope-gdl1/MySQL4/NSString+SQLiteVal.m b/sope-gdl1/MySQL4/NSString+SQLiteVal.m new file mode 100644 index 00000000..8a407f06 --- /dev/null +++ b/sope-gdl1/MySQL4/NSString+SQLiteVal.m @@ -0,0 +1,113 @@ +/* + SQLiteAdaptor.h + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "SQLiteChannel.h" +#include +#import +#include "common.h" + +@implementation NSString(SQLiteValues) + +static Class EOExprClass = Nil; + +- (id)initWithSQLiteInt:(int)_value { + char buf[256]; + sprintf(buf, "%i", _value); + return [self initWithCString:buf]; +} +- (id)initWithSQLiteDouble:(double)_value { + char buf[256]; + sprintf(buf, "%g", _value); + return [self initWithCString:buf]; +} + +- (id)initWithSQLiteText:(const unsigned char *)_value { + return [self initWithUTF8String:_value]; +} + +- (id)initWithSQLiteData:(const void *)_value length:(int)_length { + NSData *d; + + d = [[NSData alloc] initWithBytes:_value length:_length]; + self = [self initWithData:d encoding:NSUTF8StringEncoding]; + [d release]; + return self; +} + +/* generate SQL value */ + +- (NSString *)stringValueForSQLite3Type:(NSString *)_type + attribute:(EOAttribute *)_attribute +{ + // TODO: all this looks slow ... + unsigned len; + unichar c1; + + if ((len = [_type length]) == 0) + return self; + + c1 = [_type characterAtIndex:0]; + switch (c1) { + case 'c': case 'C': // char + case 'v': case 'V': // varchar + case 't': case 'T': { // text + NSString *s; + id expr; + + if (len < 4) + return self; + + _type = [_type lowercaseString]; + + if (!([_type hasPrefix:@"char"] || + [_type hasPrefix:@"varchar"] || + [_type hasPrefix:@"text"])) + break; + + /* TODO: creates too many autoreleased strings :-( */ + + expr = [self stringByReplacingString:@"\\" withString:@"\\\\"]; + + if (EOExprClass == Nil) EOExprClass = [EOQuotedExpression class]; + expr = [[EOExprClass alloc] initWithExpression:expr + quote:@"'" escape:@"\\'"]; + s = [[expr expressionValueForContext:nil] retain]; + [expr release]; + return [s autorelease]; + } + case 'i': case 'I': { // int + unsigned char buf[128]; + sprintf(buf, "%i", [self intValue]); + return [NSString stringWithCString:buf]; + } + default: + NSLog(@"WARNING(%s): return string as is for type %@", + __PRETTY_FUNCTION__, _type); + break; + } + return self; +} + +@end /* NSString(SQLiteValues) */ diff --git a/sope-gdl1/MySQL4/README b/sope-gdl1/MySQL4/README new file mode 100644 index 00000000..96673f5f --- /dev/null +++ b/sope-gdl1/MySQL4/README @@ -0,0 +1,103 @@ +# SQLite3 Adaptor + +Note: this is far from being complete! The adaptor is currently a fork of + the PostgreSQL adaptor. + +TODO +==== +- check EOAttribute+SQLite: + -loadValueClassAndTypeUsingSQLiteType:... +- SQLiteChannel.m: + -primaryFetchAttributes => check field name processing +- rewrite for exception less operation +- implement more methods in SQLiteChannel+Model (hard with SQLite though) + +Basics +====== + +Open a Shell: + + sqlite3 OGo + > insert schema + > select * from date_x; + +Configure the Adaptor (below does not work yet for SQLite3!) + + Defaults write ogo-webui-1.0a LSAdaptor SQLite3 + + Defaults write ogo-webui-1.0a LSConnectionDictionary \ + '{ databaseName = OGo; }' + + Defaults write ogo-webui-1.0a PKeyGeneratorDictionary \ + "{ newKeyExpression=\"select nextval(\\'key_generator\\');\" }" + + SQLiteDebugEnabled + + +Setup gdltest Database +====================== +sqlite3 Test.sqldb +sqlite> CREATE TABLE my_table ( pkey INT PRIMARY KEY ); + + + +Sequential execution +==================== +http://www.hwaci.com/sw/sqlite/c_interface.html + +---snip--- +typedef struct sqlite_vm sqlite_vm; + +int sqlite_compile( + sqlite *db, /* The open database */ + const char *zSql, /* SQL statement to be compiled */ + const char **pzTail, /* OUT: uncompiled tail of zSql */ + sqlite_vm **ppVm, /* OUT: the virtual machine to execute zSql */ + char **pzErrmsg /* OUT: Error message. */ +); + +int sqlite_step( + sqlite_vm *pVm, /* The virtual machine to execute */ + int *pN, /* OUT: Number of columns in result */ + const char ***pazValue, /* OUT: Column data */ + const char ***pazColName /* OUT: Column names and datatypes */ +); + +int sqlite_finalize( + sqlite_vm *pVm, /* The virtual machine to be finalized */ + char **pzErrMsg /* OUT: Error message */ +); +---snap--- + + +Error-Codes +=========== + +---snip--- +#define SQLITE_OK 0 /* Successful result */ +#define SQLITE_ERROR 1 /* SQL error or missing database */ +#define SQLITE_INTERNAL 2 /* An internal logic error in SQLite */ +#define SQLITE_PERM 3 /* Access permission denied */ +#define SQLITE_ABORT 4 /* Callback routine requested an abort */ +#define SQLITE_BUSY 5 /* The database file is locked */ +#define SQLITE_LOCKED 6 /* A table in the database is locked */ +#define SQLITE_NOMEM 7 /* A malloc() failed */ +#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ +#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite_interrupt() */ +#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ +#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ +#define SQLITE_NOTFOUND 12 /* (Internal Only) Table or record not found */ +#define SQLITE_FULL 13 /* Insertion failed because database is full */ +#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ +#define SQLITE_PROTOCOL 15 /* Database lock protocol error */ +#define SQLITE_EMPTY 16 /* (Internal Only) Database table is empty */ +#define SQLITE_SCHEMA 17 /* The database schema changed */ +#define SQLITE_TOOBIG 18 /* Too much data for one row of a table */ +#define SQLITE_CONSTRAINT 19 /* Abort due to contraint violation */ +#define SQLITE_MISMATCH 20 /* Data type mismatch */ +#define SQLITE_MISUSE 21 /* Library used incorrectly */ +#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ +#define SQLITE_AUTH 23 /* Authorization denied */ +#define SQLITE_ROW 100 /* sqlite_step() has another row ready */ +#define SQLITE_DONE 101 /* sqlite_step() has finished executing */ +---snap--- diff --git a/sope-gdl1/MySQL4/SQLiteAdaptor.h b/sope-gdl1/MySQL4/SQLiteAdaptor.h new file mode 100644 index 00000000..583466ca --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteAdaptor.h @@ -0,0 +1,75 @@ +/* + SQLiteAdaptor.h + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef ___SQLite_Adaptor_H___ +#define ___SQLite_Adaptor_H___ + +/* + The SQLite adaptor. + + The connection dictionary of this adaptor understands these keys: + databaseName + + The adaptor is based on libsqlite. +*/ + +#import +#import +#import + +@class NSString, NSMutableDictionary; + +@interface SQLiteAdaptor : EOAdaptor +{ +} + +- (id)initWithName:(NSString *)_name; + +// connection management + +- (NSString *)databaseName; +- (NSString *)newKeyExpression; + +// sequence for primary key generation + +- (NSString *)primaryKeySequenceName; + +// value formatting + +- (id)formatValue:(id)value forAttribute:(EOAttribute *)attribute; + +// attribute typing + +- (BOOL)attributeAllowedInDistinctSelects:(EOAttribute *)_attr; + +// classes used + +- (Class)adaptorContextClass; // SQLiteContext +- (Class)adaptorChannelClass; // SQLiteChannel +- (Class)expressionClass; // SQLiteExpression + +@end + +#endif diff --git a/sope-gdl1/MySQL4/SQLiteAdaptor.m b/sope-gdl1/MySQL4/SQLiteAdaptor.m new file mode 100644 index 00000000..fee187dd --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteAdaptor.m @@ -0,0 +1,169 @@ +/* + SQLiteAdaptor.m + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#import "common.h" +#import "SQLiteAdaptor.h" +#import "SQLiteContext.h" +#import "SQLiteChannel.h" +#import "SQLiteExpression.h" +#import "SQLiteValues.h" + +@implementation SQLiteAdaptor + +- (NSDictionary *)connectionDictionaryForNSURL:(NSURL *)_url { + /* + "Database URLs" + + We use the schema: + SQLite3://localhost/dbpath/foldername + */ + NSMutableDictionary *md; + NSString *p; + + if ((p = [_url path]) == nil) + return nil; + + p = [p stringByDeletingLastPathComponent]; + if ([p length] == 0) p = [_url path]; + + md = [NSMutableDictionary dictionaryWithCapacity:8]; + [md setObject:p forKey:@"databaseName"]; + return md; +} + +- (id)initWithName:(NSString *)_name { + if ((self = [super initWithName:_name])) { + } + return self; +} + +/* NSCopying methods */ + +- (id)copyWithZone:(NSZone *)_zone { + return [self retain]; +} + +// connections + +- (NSString *)serverName { + return @"localhost"; +} +- (NSString *)loginName { + return @"no-login-required"; +} +- (NSString *)loginPassword { + return @"no-pwd-required"; +} +- (NSString *)databaseName { + return [[[[self connectionDictionary] + objectForKey:@"databaseName"] copy] autorelease]; +} + +- (NSString *)port { + return @"no-port-required"; +} +- (NSString *)options { + return [[[[self connectionDictionary] + objectForKey:@"options"] copy] autorelease]; +} + +/* sequence for primary key generation */ + +- (NSString *)primaryKeySequenceName { + NSString *seqName; + + seqName = + [[self pkeyGeneratorDictionary] objectForKey:@"primaryKeySequenceName"]; + return [[seqName copy] autorelease]; +} + +- (NSString *)newKeyExpression { + NSString *newKeyExpr; + + newKeyExpr = + [[self pkeyGeneratorDictionary] objectForKey:@"newKeyExpression"]; + return [[newKeyExpr copy] autorelease]; +} + +// formatting + +- (NSString *)charConvertExpressionForAttributeNamed:(NSString *)_attrName { + return _attrName; +} + +- (id)formatValue:(id)value forAttribute:(EOAttribute *)attribute { + NSString *result; + + result = [value stringValueForSQLite3Type:[attribute externalType] + attribute:attribute]; + + //NSLog(@"formatting value %@ result %@", value, result); + //NSLog(@" value class %@ attr %@ attr type %@", + // [value class], attribute, [attribute externalType]); + + return result; +} + +- (BOOL)attributeAllowedInDistinctSelects:(EOAttribute *)_attr { + return YES; +} + +/* types */ + +- (BOOL)isValidQualifierType:(NSString *)_typeName { + return YES; +} + +/* adaptor info */ + +- (Class)adaptorContextClass { + return [SQLiteContext class]; +} +- (Class)adaptorChannelClass { + return [SQLiteChannel class]; +} + +- (Class)expressionClass { + return [SQLiteExpression class]; +} + +@end /* SQLiteAdaptor */ + +void __linkSQLiteAdaptor(void) { + extern void __link_EOAttributeSQLite(); + extern void __link_NSStringSQLite(); + extern void __link_SQLiteChannelModel(); + extern void __link_SQLiteValues(); + ; + [SQLiteChannel class]; + [SQLiteContext class]; + [SQLiteException class]; + [SQLiteExpression class]; + __link_EOAttributeSQLite(); + __link_NSStringSQLite(); + //__link_SQLiteChannelModel(); + __link_SQLiteValues(); + __linkSQLiteAdaptor(); +} diff --git a/sope-gdl1/MySQL4/SQLiteChannel+Model.h b/sope-gdl1/MySQL4/SQLiteChannel+Model.h new file mode 100644 index 00000000..6d4421ed --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteChannel+Model.h @@ -0,0 +1,41 @@ +/* + SQLiteChannel+Model.h + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef ___SQLite_ModelFetching_H___ +#define ___SQLite_ModelFetching_H___ + +#import "SQLiteChannel.h" + +@class NSArray; +@class EOModel; + +@interface SQLiteChannel(ModelFetching) + +- (EOModel *)describeModelWithTableNames:(NSArray *)_tableNames; +- (NSArray *)describeTableNames; + +@end + +#endif diff --git a/sope-gdl1/MySQL4/SQLiteChannel+Model.m b/sope-gdl1/MySQL4/SQLiteChannel+Model.m new file mode 100644 index 00000000..1932633b --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteChannel+Model.m @@ -0,0 +1,298 @@ +/* + SQLiteChannel+Model.m + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "SQLiteChannel.h" +#include "NSString+SQLite.h" +#include "EOAttribute+SQLite.h" +#include "common.h" + +@interface EORelationship(FixMe) +- (void)addJoin:(id)_join; +@end + +@implementation SQLiteChannel(ModelFetching) + +- (NSArray *)_attributesForTableName:(NSString *)_tableName { + NSMutableArray *attributes; + NSString *sqlExpr; + NSArray *resultDescription; + NSDictionary *row; + + if ([_tableName length] == 0) + return nil; + + attributes = [self->_attributesForTableName objectForKey:_tableName]; + if (attributes == nil) { +#if 1 + // TODO: we would need to parse the SQL field of 'sqlite_master'? + NSLog(@"ERROR(%s): operation not supported on SQLite!", + __PRETTY_FUNCTION__); + return nil; +#else + sqlExpr = [NSString stringWithFormat:sqlExpr, _tableName]; +#endif + + if (![self evaluateExpression:sqlExpr]) { + fprintf(stderr, + "Couldn`t evaluate column-describe '%s' on table '%s'\n", + [sqlExpr cString], [_tableName cString]); + return nil; + } + + resultDescription = [self describeResults]; + attributes = [NSMutableArray arrayWithCapacity:16]; + + while ((row = [self fetchAttributes:resultDescription withZone:NULL])) { + EOAttribute *attribute; + NSString *columnName = nil; + NSString *externalType = nil; + NSString *attrName = nil; + + columnName = [[row objectForKey:@"attname"] stringValue]; + attrName = [columnName _sqlite3ModelMakeInstanceVarName]; + externalType = [[row objectForKey:@"typname"] stringValue]; + + attribute = [[EOAttribute alloc] init]; + [attribute setColumnName:columnName]; + [attribute setName:attrName]; + [attribute setExternalType:externalType]; + [attribute loadValueClassForExternalSQLiteType:externalType]; + [attributes addObject:attribute]; + [attribute release]; + } + [self->_attributesForTableName setObject:attributes forKey:_tableName]; + //NSLog(@"got attrs: %@", attributes); + } + return attributes; +} + +- (NSArray *)_primaryKeysNamesForTableName:(NSString *)_tableName { + //NSArray *pkNameForTableName = nil; + + if ([_tableName length] == 0) + return nil; + + NSLog(@"ERROR(%s): operation not supported on SQLite!", __PRETTY_FUNCTION__); + return nil; +} + +- (NSArray *)_foreignKeysForTableName:(NSString *)_tableName { + return nil; +} + +- (EOModel *)describeModelWithTableNames:(NSArray *)_tableNames { + NSMutableArray *buildRelShips = [NSMutableArray arrayWithCapacity:64]; + EOModel *model = AUTORELEASE([EOModel new]); + int cnt, tc = [_tableNames count]; + + for (cnt = 0; cnt < tc; cnt++) { + NSMutableDictionary *relNamesUsed = + [NSMutableDictionary dictionaryWithCapacity:16]; + NSMutableArray *classProperties = + [NSMutableArray arrayWithCapacity:16]; + NSMutableArray *primaryKeyAttributes = + [NSMutableArray arrayWithCapacity:2]; + NSString *tableName = [_tableNames objectAtIndex:cnt]; + NSArray *attributes = [self _attributesForTableName:tableName]; + NSArray *pkeys = [self _primaryKeysNamesForTableName:tableName]; + NSArray *fkeys = [self _foreignKeysForTableName:tableName]; + EOEntity *entity = [[EOEntity new] autorelease]; + int cnt2; + int ac = [attributes count]; + int fkc = [fkeys count]; + + [entity setName:[tableName _sqlite3ModelMakeClassName]]; + [entity setClassName: + [@"EO" stringByAppendingString:[tableName _sqlite3ModelMakeClassName]]]; + [entity setExternalName:tableName]; + [classProperties addObjectsFromArray:[entity classProperties]]; + [primaryKeyAttributes addObjectsFromArray:[entity primaryKeyAttributes]]; + [model addEntity:entity]; + + for (cnt2 = 0; cnt2 < ac; cnt2++) { + EOAttribute *attribute = [attributes objectAtIndex:cnt2]; + NSString *columnName = [attribute columnName]; + + [entity addAttribute:attribute]; + [classProperties addObject:attribute]; + + if ([pkeys containsObject:columnName]) + [primaryKeyAttributes addObject:attribute]; + } + [entity setClassProperties:classProperties]; + [entity setPrimaryKeyAttributes:primaryKeyAttributes]; + + for (cnt2 = 0; cnt2 < fkc; cnt2++) { + NSDictionary *fkey = [fkeys objectAtIndex:cnt2]; + NSMutableArray *classProperties = + AUTORELEASE([NSMutableArray new]); + NSString *sa = [fkey objectForKey:@"sourceAttr"]; + NSString *da = [fkey objectForKey:@"targetAttr"]; + NSString *dt = [fkey objectForKey:@"targetTable"]; + EORelationship *rel = + [[[EORelationship alloc] init] autorelease]; + EOJoin *join = + [[[EOJoin alloc] init] autorelease]; + NSString *relName = nil; + + if ([pkeys containsObject:sa]) { + relName = [@"to" stringByAppendingString: + [dt _sqlite3ModelMakeClassName]]; + } + else { + relName = [@"to" stringByAppendingString: + [[sa _sqlite3ModelMakeInstanceVarName] + _sqlite3StringWithCapitalizedFirstChar]]; + if ([relName hasSuffix:@"Id"]) { + int cLength = [relName cStringLength]; + + relName = [relName substringToIndex:cLength - 2]; + } + } + if ([relNamesUsed objectForKey:relName]) { + int useCount = [[relNamesUsed objectForKey:relName] intValue]; + + [relNamesUsed setObject:[NSNumber numberWithInt:(useCount++)] + forKey:relName]; + relName = [NSString stringWithFormat:@"%s%d", + [relName cString], useCount]; + } + else + [relNamesUsed setObject:[NSNumber numberWithInt:0] forKey:relName]; + + [rel setName:relName]; + //[rel setDestinationEntity:(EOEntity *)[dt _sqlite3ModelMakeClassName]]; + [rel setToMany:NO]; + + // TODO: EOJoin is removed, fix this ... + [(id)join setSourceAttribute: + (EOAttribute *)[sa _sqlite3ModelMakeInstanceVarName]]; + [(id)join setDestinationAttribute: + (EOAttribute *)[da _sqlite3ModelMakeInstanceVarName]]; + [rel addJoin:join]; + + [entity addRelationship:rel]; + [classProperties addObjectsFromArray:[entity classProperties]]; + [classProperties addObject:rel]; + [entity setClassProperties:classProperties]; + [buildRelShips addObject:rel]; + } + + [entity setAttributesUsedForLocking:[entity attributes]]; + } + + [buildRelShips makeObjectsPerformSelector: + @selector(replaceStringsWithObjects)]; + /* + // make reverse relations + { + int cnt, rc = [buildRelShips count]; + + for (cnt = 0; cnt < rc; cnt++) { + EORelationship *rel = [buildRelShips objectAtIndex:cnt]; + NSMutableArray *classProperties = [NSMutableArray new]; + EORelationship *reverse = [rel reversedRelationShip]; + EOEntity *entity = [rel destinationEntity]; + NSArray *pkeys = [entity primaryKeyAttributes]; + BOOL isToMany = [reverse isToMany]; + EOAttribute *sa = [[[reverse joins] lastObject] sourceAttribute]; + NSString *relName = nil; + + if ([pkeys containsObject:sa] + || isToMany) + relName = [@"to" stringByAppendingString: + [(EOEntity *)[reverse destinationEntity] name]]; + else { + relName = [@"to" stringByAppendingString: + [[[sa name] _sqlite3ModelMakeInstanceVarName] + _sqlite3StringWithCapitalizedFirstChar]]; + if ([relName hasSuffix:@"Id"]) { + int cLength = [relName cStringLength]; + + relName = [relName substringToIndex:cLength - 2]; + } + } + + if ([entity relationshipNamed:relName]) { + int cnt = 1; + NSString *numName; + + numName = [NSString stringWithFormat:@"%s%d", [relName cString], cnt]; + while ([entity relationshipNamed:numName]) { + cnt++; + numName = [NSString stringWithFormat:@"%s%d", [relName cString], cnt]; + } + + relName = numName; + } + + [reverse setName:relName]; + + [entity addRelationship:reverse]; + + [classProperties addObjectsFromArray:[entity classProperties]]; + [classProperties addObject:reverse]; + [entity setClassProperties:classProperties]; + } + } + */ + [model setAdaptorName:@"SQLite"]; + [model setAdaptorClassName:@"SQLiteAdaptor"]; + [model setConnectionDictionary: + [[adaptorContext adaptor] connectionDictionary]]; + + return model; +} + +- (NSArray *)describeTableNames { + NSMutableArray *tableNames = nil; + NSArray *resultDescription = nil; + NSString *attributeName = nil; + NSDictionary *row = nil; + NSString *selectExpression = nil; + + selectExpression = @"SELECT name FROM sqlite_master WHERE type='table'"; + if (![self evaluateExpression:selectExpression]) { + fprintf(stderr, "Could not evaluate table-describe expression '%s'\n", + [selectExpression cString]); + return nil; + } + + resultDescription = [self describeResults]; + attributeName = [(EOAttribute *)[resultDescription objectAtIndex:0] name]; + tableNames = [NSMutableArray arrayWithCapacity:16]; + + while ((row = [self fetchAttributes:resultDescription withZone:NULL])!=nil) + [tableNames addObject:[row objectForKey:attributeName]]; + + return tableNames; +} + +@end /* SQLiteChannel(ModelFetching) */ + +void __link_SQLiteChannelModel() { + // used to force linking of object file + __link_SQLiteChannelModel(); +} diff --git a/sope-gdl1/MySQL4/SQLiteChannel.h b/sope-gdl1/MySQL4/SQLiteChannel.h new file mode 100644 index 00000000..030956fc --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteChannel.h @@ -0,0 +1,94 @@ +/* + SQLiteChannel.h + + Copyright (C) 2003-2005 Helge Hess + + Author: Helge Hess (helge.hess@opengroupware.org) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef ___SQLite_Channel_H___ +#define ___SQLite_Channel_H___ + +#import +#include + +@class NSArray, NSString, NSMutableDictionary; + +@interface SQLiteChannel : EOAdaptorChannel +{ + // connection is valid after an openChannel call + void *_connection; + + // valid during -evaluateExpression: + void *statement; + BOOL hasPendingRow; + BOOL isDone; + + void *results; +#if 0 + int tupleCount; + int fieldCount; + BOOL containsBinaryData; + NSString *cmdStatus; + NSString *cmdTuples; + NSString *oidStatus; + int currentTuple; +#endif + + // turns on/off channel debugging + BOOL isDebuggingEnabled; + + NSMutableDictionary *_attributesForTableName; + NSMutableDictionary *_primaryKeysNamesForTableName; +} + +- (void)setDebugEnabled:(BOOL)_flag; +- (BOOL)isDebugEnabled; + +- (BOOL)isOpen; +- (BOOL)openChannel; +- (void)closeChannel; + +- (NSMutableDictionary *)primaryFetchAttributes:(NSArray *)_attributes + withZone:(NSZone *)_zone; + +- (BOOL)evaluateExpression:(NSString *)_expression; + +// cancelFetch is always called to terminate a fetch +// (even by primaryFetchAttributes) +// it frees all fetch-local variables +- (void)cancelFetch; + +// uses dataFormat type information to create EOAttribute objects +- (NSArray *)describeResults; + +@end + +@interface NSObject(Sybase10ChannelDelegate) + +- (NSArray*)sqlite3Channel:(SQLiteChannel *)channel + willFetchAttributes:(NSArray *)attributes; + +- (BOOL)sqlite3Channel:(SQLiteChannel *)channel + willReturnRow:(NSDictionary *)row; + +@end + +#endif /* ___SQLite_Channel_H___ */ diff --git a/sope-gdl1/MySQL4/SQLiteChannel.m b/sope-gdl1/MySQL4/SQLiteChannel.m new file mode 100644 index 00000000..f380f172 --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteChannel.m @@ -0,0 +1,677 @@ +/* + SQLiteChannel.m + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include +#include +#include +#include "SQLiteChannel.h" +#include "SQLiteAdaptor.h" +#include "SQLiteException.h" +#include "NSString+SQLite.h" +#include "SQLiteValues.h" +#include "EOAttribute+SQLite.h" +#include "common.h" + +#ifndef MIN +# define MIN(x, y) ((x > y) ? y : x) +#endif + +#define MAX_CHAR_BUF 16384 + +@implementation SQLiteChannel + +static EONull *null = nil; + ++ (void)initialize { + if (null == NULL) null = [[EONull null] retain]; +} + +- (id)initWithAdaptorContext:(EOAdaptorContext*)_adaptorContext { + if ((self = [super initWithAdaptorContext:_adaptorContext])) { + [self setDebugEnabled:[[NSUserDefaults standardUserDefaults] + boolForKey:@"SQLiteDebugEnabled"]]; + + self->_attributesForTableName = + [[NSMutableDictionary alloc] initWithCapacity:16]; + self->_primaryKeysNamesForTableName = + [[NSMutableDictionary alloc] initWithCapacity:16]; + } + return self; +} + +- (void)_adaptorWillFinalize:(id)_adaptor { +} + +- (void)dealloc { + if ([self isOpen]) + [self closeChannel]; + [self->_attributesForTableName release]; + [self->_primaryKeysNamesForTableName release]; + [super dealloc]; +} + +/* NSCopying methods */ + +- (id)copyWithZone:(NSZone *)zone { + return [self retain]; +} + +// debugging + +- (void)setDebugEnabled:(BOOL)_flag { + self->isDebuggingEnabled = _flag; +} +- (BOOL)isDebugEnabled { + return self->isDebuggingEnabled; +} + +- (void)receivedMessage:(NSString *)_message { + NSLog(@"%@: message %@.", _message); +} + +/* open/close */ + +static int openConnectionCount = 0; + +- (BOOL)isOpen { + return (self->_connection != NULL) ? YES : NO; +} + +- (int)maxOpenConnectionCount { + static int MaxOpenConnectionCount = -1; + + if (MaxOpenConnectionCount != -1) + return MaxOpenConnectionCount; + + MaxOpenConnectionCount = + [[NSUserDefaults standardUserDefaults] + integerForKey:@"SQLiteMaxOpenConnectionCount"]; + if (MaxOpenConnectionCount == 0) + MaxOpenConnectionCount = 15; + return MaxOpenConnectionCount; +} + +- (BOOL)openChannel { + const unsigned char *cDBName; + SQLiteAdaptor *adaptor; + int rc; + + if (self->_connection) { + NSLog(@"%s: Connection already open !!!", __PRETTY_FUNCTION__); + return NO; + } + + adaptor = (SQLiteAdaptor *)[adaptorContext adaptor]; + + if (![super openChannel]) + return NO; + + if (openConnectionCount > [self maxOpenConnectionCount]) { + [SQLiteCouldNotOpenChannelException + raise:@"NoMoreConnections" + format:@"cannot open a additional connection !"]; + return NO; + } + + cDBName = [[adaptor databaseName] UTF8String]; + + rc = sqlite3_open(cDBName, (void *)&(self->_connection)); + if (rc != SQLITE_OK) { + // could not login .. + // Note: connection *is* set! (might be required to deallocate) + NSLog(@"WARNING: could not open SQLite connection to database '%@': %s", + [adaptor databaseName], sqlite3_errmsg(self->_connection)); + sqlite3_close(self->_connection); + return NO; + } + + if (isDebuggingEnabled) + NSLog(@"SQLite connection established 0x%08X", self->_connection); + +#if 0 + NSLog(@"---------- %s: %@ opens channel count[%d]", __PRETTY_FUNCTION__, + self, openConnectionCount); +#endif + openConnectionCount++; + +#if LIB_FOUNDATION_BOEHM_GC + [GarbageCollector registerForFinalizationObserver:self + selector:@selector(_adaptorWillFinalize:) + object:[[self adaptorContext] adaptor]]; +#endif + + if (isDebuggingEnabled) { + NSLog(@"SQLite channel 0x%08X opened (connection=0x%08X,%s)", + (unsigned)self, self->_connection, cDBName); + } + return YES; +} + +- (void)primaryCloseChannel { + if (self->statement != NULL) { + sqlite3_finalize(self->statement); + self->statement = NULL; + } + + if (self->_connection != NULL) { + sqlite3_close(self->_connection); +#if 0 + NSLog(@"---------- %s: %@ close channel count[%d]", __PRETTY_FUNCTION__, + self, openConnectionCount); +#endif + openConnectionCount--; + + if (isDebuggingEnabled) { + fprintf(stderr, + "SQLite connection dropped 0x%08X (channel=0x%08X)\n", + (unsigned)self->_connection, (unsigned)self); + } + self->_connection = NULL; + } +} + +- (void)closeChannel { + [super closeChannel]; + [self primaryCloseChannel]; +} + +/* fetching rows */ + +- (NSException *)_makeSQLiteStep { + NSString *r; + const char *em; + int rc; + + rc = sqlite3_step(self->statement); +#if 0 + NSLog(@"STEP: %i (row=%i, done=%i, mis=%i)", rc, + SQLITE_ROW, SQLITE_DONE, SQLITE_MISUSE); +#endif + + if (rc == SQLITE_ROW) { + self->hasPendingRow = YES; + self->isDone = NO; + return nil /* no error */; + } + if (rc == SQLITE_DONE) { + self->hasPendingRow = NO; + self->isDone = YES; + return nil /* no error */; + } + + if (rc == SQLITE_ERROR) + r = [NSString stringWithUTF8String:sqlite3_errmsg(self->_connection)]; + else if (rc == SQLITE_MISUSE) + r = @"The SQLite step function was called in an incorrect way"; + else if (rc == SQLITE_BUSY) + r = @"The SQLite is busy."; + else + r = [NSString stringWithFormat:@"Unexpected SQLite error: %i", rc]; + + if ((em = sqlite3_errmsg(self->_connection)) != NULL) + r = [r stringByAppendingFormat:@": %s", em]; + + return [SQLiteException exceptionWithName:@"FetchFailed" + reason:r userInfo:nil]; +} + +- (void)cancelFetch { + if (self->statement != NULL) { + sqlite3_finalize(self->statement); + self->statement = NULL; + } + self->isDone = NO; + self->hasPendingRow = NO; + [super cancelFetch]; +} + +- (NSArray *)describeResults { + // TODO: make exception-less method + int cnt, fieldCount; + NSMutableArray *result = nil; + NSMutableDictionary *usedNames = nil; + NSNumber *yesObj; + + yesObj = [NSNumber numberWithBool:YES]; + + if (![self isFetchInProgress]) { + [SQLiteException raise:@"NoFetchInProgress" + format:@"No fetch in progress (channel=%@)", self]; + } + + /* we need to fetch a row to get the info */ + + if (!self->hasPendingRow) { + NSException *error; + + if ((error = [self _makeSQLiteStep]) != nil) { + [self cancelFetch]; + [error raise]; // raise error, TODO: make exception-less method + return nil; + } + } + if (!self->hasPendingRow) /* no rows available */ + return nil; + + fieldCount = sqlite3_column_count(self->statement); + + /* old code below */ + + result = [[NSMutableArray alloc] initWithCapacity:fieldCount]; + usedNames = [[NSMutableDictionary alloc] initWithCapacity:fieldCount]; + + for (cnt = 0; cnt < fieldCount; cnt++) { + EOAttribute *attribute = nil; + NSString *columnName = nil; + NSString *attrName = nil; + + columnName = [NSString stringWithCString: + sqlite3_column_name(self->statement, cnt)]; + attrName = [columnName _sqlite3ModelMakeInstanceVarName]; + + if ([[usedNames objectForKey:attrName] boolValue]) { + int cnt2 = 0; + char buf[64]; + NSString *newAttrName = nil; + + for (cnt2 = 2; cnt2 < 100; cnt2++) { + NSString *s; + sprintf(buf, "%i", cnt2); + + // TODO: unicode + s = [[NSString alloc] initWithCString:buf]; + newAttrName = [attrName stringByAppendingString:s]; + [s release]; + + if (![[usedNames objectForKey:newAttrName] boolValue]) { + attrName = newAttrName; + break; + } + } + } + [usedNames setObject:yesObj forKey:attrName]; + + attribute = [[EOAttribute alloc] init]; + [attribute setName:attrName]; + [attribute setColumnName:columnName]; + + switch (sqlite3_column_type(self->statement, cnt)) { + case SQLITE_INTEGER: + [attribute setExternalType:@"INTEGER"]; + [attribute setValueClassName:@"NSNumber"]; + [attribute setValueType:@"d"]; + break; + case SQLITE_FLOAT: + [attribute setExternalType:@"REAL"]; + [attribute setValueClassName:@"NSNumber"]; + [attribute setValueType:@"f"]; + break; + case SQLITE_TEXT: + [attribute setExternalType:@"TEXT"]; + [attribute setValueClassName:@"NSString"]; + break; + case SQLITE_BLOB: + [attribute setExternalType:@"BLOB"]; + [attribute setValueClassName:@"NSData"]; + break; + case SQLITE_NULL: + NSLog(@"WARNING(%s): got SQLite NULL type at column %i, can't derive " + @"type information.", + __PRETTY_FUNCTION__, cnt); + [attribute setExternalType:@"NULL"]; + [attribute setValueClassName:@"NSNull"]; + break; + default: + NSLog(@"ERROR(%s): unexpected SQLite type at column %i", + __PRETTY_FUNCTION__, cnt); + break; + } + + [result addObject:attribute]; + [attribute release]; + } + + [usedNames release]; + usedNames = nil; + + return [result autorelease]; +} + +- (BOOL)isColumnNullInCurrentRow:(int)_column { + /* + Note: NULL is SQLite is represented as empty strings ..., don't know + what to do about that? + At least Sybase 10 doesn't support empty strings strings as well + and converts them to a single space. So maybe it is reasonable to + map empty strings to NSNull? + + Or is this column-type SQLITE_NULL? If so, thats rather weird, + since the type query does not take a row. + */ + return NO; +} + +- (NSMutableDictionary *)primaryFetchAttributes:(NSArray *)_attributes + withZone:(NSZone *)_zone +{ + /* + Note: we expect that the attributes match the generated SQL. This is + because auto-generated SQL can contain SQL table prefixes (like + alias.column-name which cannot be detected using the attributes + schema) + */ + // TODO: add a primaryFetchAttributesX method? + NSMutableDictionary *row = nil; + NSException *error; + unsigned attrCount = [_attributes count]; + unsigned cnt; + + if (self->statement == NULL) { + NSLog(@"ERROR: no fetch in progress?"); + [self cancelFetch]; + return nil; + } + + if (!self->hasPendingRow && !self->isDone) { + if ((error = [self _makeSQLiteStep]) != nil) { + [self cancelFetch]; + [error raise]; // raise error, TODO: make exception-less method + return nil; + } + } + if (self->isDone) { /* step was fine, but we are at the end */ + [self cancelFetch]; + return nil; + } + + self->hasPendingRow = NO; /* consume the row */ + + /* build row */ + + row = [NSMutableDictionary dictionaryWithCapacity:attrCount]; + + for (cnt = 0; cnt < attrCount; cnt++) { + EOAttribute *attribute; + NSString *attrName; + id value = nil; + + attribute = [_attributes objectAtIndex:cnt]; + attrName = [attribute name]; + + if ([self isColumnNullInCurrentRow:cnt]) { + value = [null retain]; + } + else { + Class valueClass; + + valueClass = NSClassFromString([attribute valueClassName]); + if (valueClass == Nil) { + NSLog(@"ERROR(%s): %@: got no value class for column:\n" + @" attribute=%@\n type=%@", + __PRETTY_FUNCTION__, self, + attrName, [attribute externalType]); + value = null; + continue; + } + + switch (sqlite3_column_type(self->statement, cnt)) { + case SQLITE_INTEGER: + value = [[valueClass alloc] + initWithSQLiteInt:sqlite3_column_int(self->statement, cnt)]; + break; + case SQLITE_FLOAT: + value = [[valueClass alloc] + initWithSQLiteDouble: + sqlite3_column_double(self->statement, cnt)]; + break; + case SQLITE_TEXT: + value = [[valueClass alloc] + initWithSQLiteText: + sqlite3_column_text(self->statement, cnt)]; + break; + case SQLITE_BLOB: + value = [[valueClass alloc] + initWithSQLiteData: + sqlite3_column_blob(self->statement, cnt) + length:sqlite3_column_bytes(self->statement, cnt)]; + break; + case SQLITE_NULL: + value = [null retain]; + break; + default: + NSLog(@"ERROR(%s): unexpected SQLite type at column %i", + __PRETTY_FUNCTION__, cnt); + continue; + } + + if (value == nil) { + NSLog(@"ERROR(%s): %@: got no value for column:\n" + @" attribute=%@\n valueClass=%@\n type=%@", + __PRETTY_FUNCTION__, self, + attrName, NSStringFromClass(valueClass), + [attribute externalType]); + continue; + } + } + + if (value != nil) { + [row setObject:value forKey:attrName]; + [value release]; + } + } + + return row; +} + +/* sending SQL to server */ + +- (NSException *)evaluateExpressionX:(NSString *)_expression { + NSMutableString *sql; + NSException *error; + BOOL result; + const char *s; + const char *tails = NULL; + int rc; + + *(&result) = YES; + + if (_expression == nil) { + [NSException raise:@"InvalidArgumentException" + format:@"parameter for evaluateExpression: " + @"must not be null (channel=%@)", self]; + } + + sql = [[_expression mutableCopy] autorelease]; + [sql appendString:@";"]; + + /* ask delegate */ + + if (delegateRespondsTo.willEvaluateExpression) { + EODelegateResponse response; + + response = [delegate adaptorChannel:self willEvaluateExpression:sql]; + + if (response == EODelegateRejects) { + return [NSException exceptionWithName:@"EODelegateRejects" + reason:@"delegate rejected insert" + userInfo:nil]; + } + if (response == EODelegateOverrides) + return nil; + } + + /* check some preconditions */ + + if (![self isOpen]) { + return [SQLiteException exceptionWithName:@"ChannelNotOpenException" + reason:@"SQLite connection is not open" + userInfo:nil]; + } + if (self->statement != NULL) { + return [SQLiteException exceptionWithName:@"CommandInProgressException" + reason:@"an evaluation is in progress" + userInfo:nil]; + return NO; + } + + if ([self isFetchInProgress]) { + NSLog(@"WARNING: a fetch is still in progress: %@", self); + [self cancelFetch]; + } + + if (isDebuggingEnabled) + NSLog(@"%@ SQL: %@", self, sql); + + /* reset environment */ + + self->isFetchInProgress = NO; + self->isDone = NO; + self->hasPendingRow = NO; + + s = [sql UTF8String]; + rc = sqlite3_prepare(self->_connection, s, strlen(s), + (void *)&(self->statement), &tails); + + if (rc != SQLITE_OK) { + NSString *r; + + [self cancelFetch]; + // TODO: improve error + + r = [NSString stringWithFormat:@"could not parse SQL statement: %s", + sqlite3_errmsg(self->_connection)]; + return [SQLiteException exceptionWithName:@"ExecutionFailed" + reason:r userInfo:nil]; + } + + /* step to first row */ + + if ([sql hasPrefix:@"SELECT"] || [sql hasPrefix:@"select"]) { + self->isFetchInProgress = YES; + NSAssert(self->statement, @"missing statement"); + } + else { + if ((error = [self _makeSQLiteStep]) != nil) { + [self cancelFetch]; + return error; + } + + self->isFetchInProgress = self->hasPendingRow; + if (!self->isFetchInProgress) { + sqlite3_finalize(self->statement); + self->statement = NULL; + } + } + + /* only on empty results? */ + if (delegateRespondsTo.didEvaluateExpression) + [delegate adaptorChannel:self didEvaluateExpression:sql]; + + return nil /* everything is OK */; +} +- (BOOL)evaluateExpression:(NSString *)_sql { + NSException *e; + NSString *n; + + if ((e = [self evaluateExpressionX:_sql]) == nil) + return YES; + + /* for compatibility with non-X methods, translate some errors to a bool */ + n = [e name]; + if ([n isEqualToString:@"EOEvaluationError"]) + return NO; + if ([n isEqualToString:@"EODelegateRejects"]) + return NO; + + [e raise]; + return NO; +} + +/* description */ + +- (NSString *)description { + NSMutableString *ms; + + ms = [NSMutableString stringWithCapacity:64]; + [ms appendFormat:@"<%@[0x%08X] connection=0x%08X", + NSStringFromClass([self class]), + self, + (unsigned)self->_connection]; + [ms appendString:@">"]; + return ms; +} + +@end /* SQLiteChannel */ + +@implementation SQLiteChannel(PrimaryKeyGeneration) + +- (NSDictionary *)primaryKeyForNewRowWithEntity:(EOEntity *)_entity { + NSArray *pkeys; + SQLiteAdaptor *adaptor; + NSString *seqName, *seq; + NSDictionary *pkey; + + pkeys = [_entity primaryKeyAttributeNames]; + adaptor = (id)[[self adaptorContext] adaptor]; + seqName = [adaptor primaryKeySequenceName]; + pkey = nil; + seq = nil; + + seq = ([seqName length] > 0) + ? [NSString stringWithFormat:@"SELECT NEXTVAL ('%@')", seqName] + : [adaptor newKeyExpression]; + + NS_DURING { + if ([self evaluateExpression:seq]) { + id key = nil; + + NSLog(@"ERROR: new key creation is not implemented in SQLite yet!"); + if ([self isFetchInProgress]) { + NSLog(@"Primary key eval returned results .."); + } + // TODO + NSLog(@"%s: PKEY GEN NOT IMPLEMENTED!", __PRETTY_FUNCTION__); + [self cancelFetch]; + + if (key != nil) { + pkey = [NSDictionary dictionaryWithObject:key + forKey:[pkeys objectAtIndex:0]]; + } + } + } + NS_HANDLER { + pkey = nil; + } + NS_ENDHANDLER; + + return pkey; +} + +@end /* SQLiteChannel(PrimaryKeyGeneration) */ + +void __link_SQLiteChannel() { + // used to force linking of object file + __link_SQLiteChannel(); +} diff --git a/sope-gdl1/MySQL4/SQLiteContext.h b/sope-gdl1/MySQL4/SQLiteContext.h new file mode 100644 index 00000000..31ec3a01 --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteContext.h @@ -0,0 +1,39 @@ +/* + SQLiteContext.h + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef ___SQLite_Context_H___ +#define ___SQLite_Context_H___ + +#import + +@interface SQLiteContext : EOAdaptorContext + +- (BOOL)primaryBeginTransaction; +- (BOOL)primaryCommitTransaction; +- (BOOL)primaryRollbackTransaction; + +@end + +#endif diff --git a/sope-gdl1/MySQL4/SQLiteContext.m b/sope-gdl1/MySQL4/SQLiteContext.m new file mode 100644 index 00000000..31604f3d --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteContext.m @@ -0,0 +1,84 @@ +/* + SQLiteContext.m + + Copyright (C) 2003-2005 SKYRIX Software AG + + Author: Helge Hess (helge.hess@skyrix.com) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include "SQLiteContext.h" +#include "SQLiteChannel.h" +#include "common.h" + +@implementation SQLiteContext + +- (void)channelDidInit:_channel { + if ([channels count] > 0) { + [NSException raise:@"TooManyOpenChannelsException" + format:@"SQLite3 only supports one channel per context"]; + } + [super channelDidInit:_channel]; +} + +- (BOOL)primaryBeginTransaction { + BOOL result; + + result = [[[channels lastObject] + nonretainedObjectValue] + evaluateExpression:@"BEGIN TRANSACTION"]; + + return result; +} + +- (BOOL)primaryCommitTransaction { + BOOL result; + + result = [[[channels lastObject] + nonretainedObjectValue] + evaluateExpression:@"COMMIT TRANSACTION"]; + + return result; +} + +- (BOOL)primaryRollbackTransaction { + BOOL result; + + result = [[[channels lastObject] + nonretainedObjectValue] + evaluateExpression:@"ROLLBACK TRANSACTION"]; + return result; +} + +- (BOOL)canNestTransactions { + return NO; +} + +// NSCopying methods + +- (id)copyWithZone:(NSZone *)zone { + return [self retain]; +} + +@end /* SQLiteContext */ + +void __link_SQLiteContext() { + // used to force linking of object file + __link_SQLiteContext(); +} diff --git a/sope-gdl1/MySQL4/SQLiteException.h b/sope-gdl1/MySQL4/SQLiteException.h new file mode 100644 index 00000000..17fbe915 --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteException.h @@ -0,0 +1,54 @@ +/* + SQLiteException.h + + Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess + + Author: Helge Hess (helge@mdlink.de) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef ___SQLite_Exception_H___ +#define ___SQLite_Exception_H___ + +#import + +@interface SQLiteException : NSException +{ +} + ++ (void)raiseWithFormat:(NSString *)_format, ...; + +@end + +@interface SQLiteCouldNotOpenChannelException : SQLiteException +{ +} +@end + +@interface SQLiteCouldNotConnectException : SQLiteCouldNotOpenChannelException +{ +} +@end + +@interface SQLiteCouldNotBindException : SQLiteException +{ +} +@end + +#endif /* ___SQLite_Exception_H___ */ diff --git a/sope-gdl1/MySQL4/SQLiteException.m b/sope-gdl1/MySQL4/SQLiteException.m new file mode 100644 index 00000000..14ad7368 --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteException.m @@ -0,0 +1,62 @@ +/* + SQLiteException.m + + Copyright (C) 2003-2005 Helge Hess + + Author: Helge Hess (helge.hess@opengroupware.org) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#import +#import "SQLiteException.h" +#include "common.h" + +@implementation SQLiteException + ++ (void)raiseWithFormat:(NSString *)_format, ... { + NSString *tmp = nil; + va_list ap; + + va_start(ap, _format); + tmp = [[NSString allocWithZone:[self zone]] + initWithFormat:_format arguments:ap]; + va_end(ap); + + AUTORELEASE(tmp); + + [[[self alloc] initWithName:NSStringFromClass([self class]) + reason:tmp userInfo:nil] + raise]; +} + +@end + +@implementation SQLiteCouldNotOpenChannelException +@end + +@implementation SQLiteCouldNotConnectException +@end + +@implementation SQLiteCouldNotBindException +@end + +void __link_SQLiteException() { + // used to force linking of object file + __link_SQLiteException(); +} diff --git a/sope-gdl1/MySQL4/SQLiteExpression.h b/sope-gdl1/MySQL4/SQLiteExpression.h new file mode 100644 index 00000000..cd00af8b --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteExpression.h @@ -0,0 +1,55 @@ +/* + SQLiteExpression.h + + Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess + + Author: Helge Hess (helge@mdlink.de) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef ___SQLite3_SQLExpression_H___ +#define ___SQLite3_SQLExpression_H___ + +#include + +@class NSString, NSArray; +@class EOSQLQualifier, EOAdaptorChannel; + +@interface SQLiteExpression : EOSQLExpression + ++ (Class)selectExpressionClass; + +@end + +@interface SQLiteSelectSQLExpression : EOSelectSQLExpression +{ + BOOL lock; +} + +- (id)selectExpressionForAttributes:(NSArray *)attributes + lock:(BOOL)flag + qualifier:(EOSQLQualifier *)qualifier + fetchOrder:(NSArray *)fetchOrder + channel:(EOAdaptorChannel *)channel; + +- (NSString *)fromClause; + +@end + +#endif /* ___SQLite3_SQLExpression_H___ */ diff --git a/sope-gdl1/MySQL4/SQLiteExpression.m b/sope-gdl1/MySQL4/SQLiteExpression.m new file mode 100644 index 00000000..caacb7ac --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteExpression.m @@ -0,0 +1,92 @@ +/* + SQLiteExpression.m + + Copyright (C) 2003-2005 Helge Hess + + Author: Helge Hess (helge.hess@opengroupware.org) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#include +#include +#include "SQLiteExpression.h" + +@implementation SQLiteExpression + ++ (Class)selectExpressionClass { + return [SQLiteSelectSQLExpression class]; +} + +@end /* SQLiteExpression */ + +@implementation SQLiteSelectSQLExpression + +- (id)selectExpressionForAttributes:(NSArray *)attributes + lock:(BOOL)flag + qualifier:(EOSQLQualifier *)qualifier + fetchOrder:(NSArray *)fetchOrder + channel:(EOAdaptorChannel *)channel { + + lock = flag; + [super selectExpressionForAttributes:attributes + lock:flag + qualifier:qualifier + fetchOrder:fetchOrder + channel:channel]; + return self; +} + +- (NSString *)fromClause { + NSMutableString *fromClause; + NSEnumerator *enumerator; + BOOL first = YES; + id key; + + fromClause = [NSMutableString stringWithCString:" "]; + enumerator = [fromListEntities objectEnumerator]; + + // Compute the FROM list from all the aliases found in + // entitiesAndPropertiesAliases dictionary. Note that this dictionary + // contains entities and relationships. The last ones are there for + // flattened attributes over reflexive relationships. + + while((key = [enumerator nextObject])) { + if(first) first = NO; + else [fromClause appendString:@", "]; + + [fromClause appendFormat:@"%@ %@", + [key isKindOfClass:[EORelationship class]] + ? [[key destinationEntity] externalName] // flattened attribute + : [key externalName], // EOEntity + [entitiesAndPropertiesAliases objectForKey:key]]; + +#if 0 + if (lock) [fromClause appendString:@" HOLDLOCK"]; +#endif + } + + return fromClause; +} + +@end /* SQLiteSelectSQLExpression */ + +void __link_SQLiteExpression() { + // used to force linking of object file + __link_SQLiteExpression(); +} diff --git a/sope-gdl1/MySQL4/SQLiteValues.h b/sope-gdl1/MySQL4/SQLiteValues.h new file mode 100644 index 00000000..1372816b --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteValues.h @@ -0,0 +1,83 @@ +/* + SQLiteValues.h + + Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess + + Author: Helge Hess (helge@mdlink.de) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef ___SQLite_Values_H___ +#define ___SQLite_Values_H___ + +#import +#import +#import +#import +#import +#import "SQLiteException.h" + +@class EOAttribute; +@class SQLiteChannel; + +@interface SQLiteDataTypeMappingException : SQLiteException + +- (id)initWithObject:(id)_obj + forAttribute:(EOAttribute *)_attr + andSQLite3Type:(NSString *)_dt + inChannel:(SQLiteChannel *)_channel; + +@end + +@protocol SQLiteValues + +- (NSString *)stringValueForSQLite3Type:(NSString *)_type + attribute:(EOAttribute *)_attribute; + +@end + +@interface NSObject(SQLiteValues) + +- (id)initWithSQLiteInt:(int)_value; +- (id)initWithSQLiteText:(const unsigned char *)_value; +- (id)initWithSQLiteDouble:(double)_value; +- (id)initWithSQLiteData:(const void *)_data length:(int)_length; + +@end + +@interface NSString(SQLiteValues) < SQLiteValues > +@end + +@interface NSNumber(SQLiteValues) < SQLiteValues > +@end + +@interface NSData(SQLiteValues) < SQLiteValues > +@end + +@interface NSCalendarDate(SQLiteValues) < SQLiteValues > +@end + +@interface EONull(SQLiteValues) + +- (NSString *)stringValueForSQLite3Type:(NSString *)_type + attribute:(EOAttribute *)_attribute; + +@end + +#endif /* ___SQLite_Values_H___ */ diff --git a/sope-gdl1/MySQL4/SQLiteValues.m b/sope-gdl1/MySQL4/SQLiteValues.m new file mode 100644 index 00000000..e02bf04c --- /dev/null +++ b/sope-gdl1/MySQL4/SQLiteValues.m @@ -0,0 +1,141 @@ +/* + SQLiteValues.m + + Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess + + Author: Helge Hess (helge@mdlink.de) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#import "SQLiteValues.h" +#import "common.h" + +@implementation SQLiteDataTypeMappingException + +- (id)initWithObject:(id)_obj + forAttribute:(EOAttribute *)_attr + andSQLite3Type:(NSString *)_dt + inChannel:(SQLiteChannel *)_channel; +{ + NSDictionary *ui; + NSString *typeName = nil; + NSString *r; + + typeName = _dt; + + if (typeName == nil) + typeName = [NSString stringWithFormat:@"Oid[%i]", _dt]; + + r = [NSString stringWithFormat: + @"mapping between %@ and " + @"SQLite type %@ is not supported", + [_obj description], + NSStringFromClass([_obj class]), + typeName]; + ui = [NSDictionary dictionaryWithObjectsAndKeys: + _attr, @"attribute", + _channel, @"channel", + _obj, @"object", + nil]; + + return [self initWithName:@"DataTypeMappingNotSupported" reason:r + userInfo:ui]; +} + +@end /* SQLiteDataTypeMappingException */ + +@implementation NSNull(SQLiteValues) + +- (NSString *)stringValueForSQLite3Type:(NSString *)_type + attribute:(EOAttribute *)_attribute +{ + return @"null"; +} + +@end /* NSNull(SQLiteValues) */ + +@implementation NSObject(SQLiteValues) + +- (id)initWithSQLiteInt:(int)_value { + if ([self respondsToSelector:@selector(initWithInt:)]) + return [(NSNumber *)self initWithInt:_value]; + + if ([self respondsToSelector:@selector(initWithDouble:)]) + return [(NSNumber *)self initWithDouble:_value]; + + if ([self respondsToSelector:@selector(initWithString:)]) { + NSString *s; + char buf[256]; + + sprintf(buf, "%i", _value); + s = [[NSString alloc] initWithCString:buf]; + self = [(NSString *)self initWithString:s]; + [s release]; + return self; + } + + [self release]; + return nil; +} + +- (id)initWithSQLiteDouble:(double)_value { + if ([self respondsToSelector:@selector(initWithDouble:)]) + return [(NSNumber *)self initWithDouble:_value]; + + [self release]; + return nil; +} + +- (id)initWithSQLiteText:(const unsigned char *)_value { + if ([self respondsToSelector:@selector(initWithString:)]) { + NSString *s; + + s = [[NSString alloc] initWithUTF8String:_value]; + self = [(NSString *)self initWithString:s]; + [s release]; + return self; + } + + [self release]; + return nil; +} + +- (id)initWithSQLiteData:(const void *)_data length:(int)_length { + if ([self respondsToSelector:@selector(initWithBytes:length:)]) + return [(NSData *)self initWithBytes:_data length:_length]; + + if ([self respondsToSelector:@selector(initWithData:)]) { + NSData *d; + + d = [[NSData alloc] initWithBytes:_data length:_length]; + self = [(NSData *)self initWithData:d]; + [d release]; + return self; + } + + [self release]; + return nil; +} + +@end /* NSObject(SQLiteValues) */ + +void __link_SQLiteValues() { + // used to force linking of object file + __link_SQLiteValues(); +} diff --git a/sope-gdl1/MySQL4/Version b/sope-gdl1/MySQL4/Version new file mode 100644 index 00000000..4010e5dd --- /dev/null +++ b/sope-gdl1/MySQL4/Version @@ -0,0 +1,3 @@ +# Version file + +SUBMINOR_VERSION:=1 diff --git a/sope-gdl1/MySQL4/common.h b/sope-gdl1/MySQL4/common.h new file mode 100644 index 00000000..7a99e005 --- /dev/null +++ b/sope-gdl1/MySQL4/common.h @@ -0,0 +1,40 @@ +/* + common.h + + Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess + + Author: Helge Hess (helge@mdlink.de) + + This file is part of the SQLite3 Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#ifndef ___SQLite3_common_H___ +#define ___SQLite3_common_H___ + +#import + +#include +#include +#include + +#import + +#include +#include + +#endif /* ___SQLite3_common_H___ */ diff --git a/sope-gdl1/MySQL4/condict.plist b/sope-gdl1/MySQL4/condict.plist new file mode 100644 index 00000000..250e134f --- /dev/null +++ b/sope-gdl1/MySQL4/condict.plist @@ -0,0 +1,3 @@ +{ + databaseName = "Test.sqldb"; +} diff --git a/sope-gdl1/MySQL4/fhs.make b/sope-gdl1/MySQL4/fhs.make new file mode 100644 index 00000000..6c5bd960 --- /dev/null +++ b/sope-gdl1/MySQL4/fhs.make @@ -0,0 +1,25 @@ +# postprocessing + +# FHS support (this is a hack and is going to be done by gstep-make!) + +ifneq ($(FHS_INSTALL_ROOT),) + +FHS_LIB_DIR=$(FHS_INSTALL_ROOT)/lib/ +FHS_DB_DIR=$(FHS_LIB_DIR)sope-$(SOPE_MAJOR_VERSION).$(SOPE_MINOR_VERSION)/dbadaptors/ + +fhs-db-dirs :: + $(MKDIRS) $(FHS_DB_DIR) + +move-bundles-to-fhs :: fhs-db-dirs + @echo "moving bundles $(BUNDLE_INSTALL_DIR) to $(FHS_DB_DIR) .." + for i in $(BUNDLE_NAME); do \ + j="$(FHS_DB_DIR)/$${i}$(BUNDLE_EXTENSION)"; \ + if test -d $$j; then rm -r $$j; fi; \ + mv "$(BUNDLE_INSTALL_DIR)/$${i}$(BUNDLE_EXTENSION)" $$j; \ + done + +move-to-fhs :: move-bundles-to-fhs + +after-install :: move-to-fhs + +endif diff --git a/sope-gdl1/MySQL4/gdltest.m b/sope-gdl1/MySQL4/gdltest.m new file mode 100644 index 00000000..35534e1f --- /dev/null +++ b/sope-gdl1/MySQL4/gdltest.m @@ -0,0 +1,220 @@ +/* + gdltest.m + + Copyright (C) 1999-2005 MDlink online service center GmbH and Helge Hess + + Author: Helge Hess (helge@mdlink.de) + + This file is part of the SQLite Adaptor Library + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; see the file COPYING.LIB. + If not, write to the Free Software Foundation, + 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +#import +#import +#include + +static void fetchExprInChannel(NSString *expr, EOAdaptorChannel *ch) { + NSArray *attrs; + NSDictionary *record; + + if (![ch evaluateExpression:expr]) { + NSLog(@"ERROR: failed to evaluate: %@", expr); + return; + } + + attrs = [ch describeResults]; + NSLog(@"results: %@", attrs); + + while ((record = [ch fetchAttributes:attrs withZone:nil]) != nil) + NSLog(@"fetched %@", record); +} + +static void fetchSomePersonRecord(EOEntity *e, EOAdaptorChannel *ch) { + EOSQLQualifier *q; + NSArray *attrs; + + attrs = [e attributes]; + q = [[EOSQLQualifier alloc] + initWithEntity:e + qualifierFormat:@"%A='helge'", @"login"]; + [q autorelease]; + + if ([ch selectAttributes:attrs + describedByQualifier:q + fetchOrder:nil + lock:NO]) { + NSDictionary *record; + + record = [ch fetchAttributes:attrs withZone:nil]; + } + else + NSLog(@"Could not select .."); +} + +static void fetchSomeTeamRecords(EOEntity *e, EOAdaptorChannel *ch) { + EOSQLQualifier *q; + NSArray *attrs; + + q = [e qualifier]; + attrs = [e attributes]; + + if ([ch selectAttributes:attrs describedByQualifier:q fetchOrder:nil + lock:NO]) { + NSDictionary *record; + + while ((record = [ch fetchAttributes:attrs withZone:NULL]) != nil) { + NSLog(@"fetched %@ birthday %@", + [record valueForKey:@"description"], + [record valueForKey:@"companyId"]); + } + } + else + NSLog(@"Could not select team records .."); +} + +static void runtestInOpenChannel(EOAdaptorChannel *ch) { + NSAutoreleasePool *pool = [NSAutoreleasePool new]; + EOEntity *e; + EOSQLQualifier *q; + NSArray *attrs; + EOAdaptorContext *ctx; + EOModel *m; + NSString *expr; + + ctx = [ch adaptorContext]; + m = [[ctx adaptor] model]; + + expr = [[NSUserDefaults standardUserDefaults] stringForKey:@"sql"]; + + NSLog(@"channel is open"); + + if (![ctx beginTransaction]) { + NSLog(@"ERROR: could not begin transaction ..."); + return; + } + + NSLog(@"began tx .."); + + /* do something */ + pool = [[NSAutoreleasePool alloc] init]; +#if 1 + if (expr) fetchExprInChannel(expr, ch); +#endif + + /* fetch some MyEntity records */ + + e = [m entityNamed:@"MyEntity"]; + NSLog(@"entity: %@", e); + if (e == nil) + exit(1); + + q = [e qualifier]; + attrs = [e attributes]; + + // NSLog(@"ATTRS: %@", attrs); + + if ([ch selectAttributes:attrs + describedByQualifier:q + fetchOrder:nil + lock:NO]) { + NSDictionary *record; + + while ((record = [ch fetchAttributes:attrs withZone:nil]) != nil) + NSLog(@"fetched record: %@", record); + } + else + NSLog(@"Could not select .."); + + /* some OGo fetches */ + + if ((e = [m entityNamed:@"Team"]) != nil) + fetchSomeTeamRecords(e, ch); + + if ((e = [m entityNamed:@"Person"]) != nil) + fetchSomePersonRecord(e, ch); + + /* tear down */ + + [pool release]; + + NSLog(@"committing tx .."); + if ([ctx commitTransaction]) + NSLog(@" could commit."); + else + NSLog(@" commit failed."); +} + +static void runtest(void) { + EOModel *m = nil; + EOAdaptor *a; + EOAdaptorContext *ctx; + EOAdaptorChannel *ch; + NSDictionary *conDict; + + NS_DURING { + + conDict = [NSDictionary dictionaryWithContentsOfFile:@"condict.plist"]; + NSLog(@"condict is %@", conDict); + + if ((a = [EOAdaptor adaptorWithName:@"SQLite3"]) == nil) { + NSLog(@"found no SQLite3 adaptor .."); + exit(1); + } + + NSLog(@"got adaptor %@", a); + [a setConnectionDictionary:conDict]; + NSLog(@"got adaptor with condict %@", a); + + ctx = [a createAdaptorContext]; + ch = [ctx createAdaptorChannel]; + +#if 1 + m = AUTORELEASE([[EOModel alloc] initWithContentsOfFile:@"test.eomodel"]); + if (m) { + [a setModel:m]; + [a setConnectionDictionary:conDict]; + } +#endif + + NSLog(@"opening channel .."); + + [ch setDebugEnabled:YES]; + + if ([ch openChannel]) { + runtestInOpenChannel(ch); + + NSLog(@"closing channel .."); + [ch closeChannel]; + } + } + NS_HANDLER { + fprintf(stderr, "exception: %s\n", [[localException description] cString]); + abort(); + } + NS_ENDHANDLER; +} + +int main(int argc, char **argv, char **env) { + NSAutoreleasePool *pool; + + pool = [[NSAutoreleasePool alloc] init]; + [NSProcessInfo initializeWithArguments:argv count:argc environment:env]; + + runtest(); + [pool release]; + return 0; +} diff --git a/sope-gdl1/MySQL4/test.eomodel b/sope-gdl1/MySQL4/test.eomodel new file mode 100644 index 00000000..9079abad --- /dev/null +++ b/sope-gdl1/MySQL4/test.eomodel @@ -0,0 +1,26 @@ +{ + EOModelVersion = 1; + adaptorClassName = SQLiteAdaptor; + adaptorName = SQLite3; + + entities = ( + { /* CREATE TABLE my_table ( pkey INT PRIMARY KEY ); */ + name = MyEntity; + externalName = my_table; + + className = EOGenericRecord; + primaryKeyAttributes = ( pkey ); + attributesUsedForLocking = ( pkey ); + classProperties = ( pkey ); + attributes = ( + { + valueClassName = NSNumber; + columnName = pkey; + name = pkey; + valueType = i; + externalType = INT; // t_id + }, + ); + } + ); +} -- 2.39.5