Пример #1
0
        /// <summary>
        /// Finds column by name in results.
        /// Returns 0-based index, or -1 if not found.
        /// </summary>
        /// <param name="name">Column name in results, as returned by sqlite3_column_name. Case-sensitive.</param>
        public int ColumnIndex(string name)
        {
            int n = ColumnCount;

            if (n > 0 && !name.NE())
            {
                if (AStringUtil.IsAscii(name))
                {
                    for (int i = 0; i < n; i++)
                    {
                        byte *b = SLApi.sqlite3_column_name(_st, i);
                        if (BytePtr_.AsciiEq(b, name))
                        {
                            return(i);
                        }
                    }
                }
                else
                {
                    var bname = AConvert.ToUtf8(name);
                    for (int i = 0; i < n; i++)
                    {
                        byte *b = SLApi.sqlite3_column_name(_st, i);
                        if (BytePtr_.Eq(b, bname))
                        {
                            return(i);
                        }
                    }
                }
            }
            return(-1);
        }
Пример #2
0
        int _B(SLIndexOrName p)
        {
            if (p.name == null)
            {
                return(p.index);
            }
            int r = SLApi.sqlite3_bind_parameter_index(_st, AConvert.ToUtf8(p.name));

            if (r == 0)
            {
                throw new SLException($"Parameter '{p.name}' does not exist in the SQL statement.");
            }
            return(r);
        }
Пример #3
0
        /// <summary>
        /// Opens or creates a database file.
        /// </summary>
        /// <param name="file">
        /// Database file. Can be:
        /// - Full path. Supports environment variables etc, see <see cref="APath.ExpandEnvVar"/>
        /// - ":memory:" - create a private, temporary in-memory database.
        /// - "" - create a private, temporary on-disk database.
        /// - Starts with "file:" - see sqlite3_open_v2 documentation in SQLite website.
        /// </param>
        /// <param name="flags">sqlite3_open_v2 flags, documanted in SQLite website. Default: read-write, create file if does not exist (and parent directory).</param>
        /// <param name="sql">
        /// SQL to execute. For example, one or more ;-separated PRAGMA statements to configure the database connection. Or even "CREATE TABLE IF NOT EXISTS ...".
        /// This function also always executes "PRAGMA foreign_keys=ON;PRAGMA secure_delete=ON;".
        /// </param>
        /// <exception cref="ArgumentException">Not full path.</exception>
        /// <exception cref="SLException">Failed to open database or execute sql.</exception>
        /// <remarks>
        /// Calls sqlite3_open_v2.
        /// <note>If a variable of this class is used by multiple threads, use <c>lock(variable) {  }</c> where need.</note>
        /// </remarks>
        public ASqlite(string file, SLFlags flags = SLFlags.ReadWriteCreate, string sql = null)
        {
            bool isSpec = file != null && (file.Length == 0 || file == ":memory:" || file.Starts("file:"));

            if (!isSpec)
            {
                file = APath.Normalize(file);
                if (flags.Has(SLFlags.SQLITE_OPEN_CREATE) && !AFile.ExistsAsFile(file, true))
                {
                    AFile.CreateDirectoryFor(file);
                }
            }
            var r = SLApi.sqlite3_open_v2(AConvert.ToUtf8(file), ref _db, flags, null);

            if (r != 0)
            {
                Dispose();
                throw new SLException(r, "sqlite3_open " + file);
            }
            Execute("PRAGMA foreign_keys=ON;PRAGMA secure_delete=ON;" + sql);
        }
Пример #4
0
 /// <summary>
 /// Calls sqlite3_exec to execute one or more SQL statements that don't return data.
 /// </summary>
 /// <param name="sql">SQL statement, or several ;-separated statements.</param>
 /// <exception cref="SLException">Failed to execute sql.</exception>
 public void Execute(string sql)
 {
     var   b  = AConvert.ToUtf8(sql);
     byte *es = null;             //gets better error text than sqlite3_errstr; sqlite3_errmsg gets nothing after sqlite3_exec.
     var   r  = SLApi.sqlite3_exec(_db, b, default, default, &es);
Пример #5
0
        /// <summary>
        /// Gets image type from string.
        /// </summary>
        /// <param name="anyFile">When the string is valid but not of any image type, return ShellIcon instead of None.</param>
        /// <param name="s">File path etc. See <see cref="ImageType"/>.</param>
        public static ImageType ImageTypeFromString(bool anyFile, string s)
        {
            var b = AConvert.ToUtf8(s);

            fixed(byte *p = b) return(ImageTypeFromString(true, p, b.Length - 1));
        }